1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "base/utf_string_conversions.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/extensions/extension_host.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/process_map.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/extension_file_util.h"
#include "chrome/common/string_ordinal.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/browser/renderer_host/render_view_host.h"
#include "content/browser/tab_contents/tab_contents.h"
#include "content/public/browser/notification_service.h"
#include "content/test/test_navigation_observer.h"
#include "net/base/mock_host_resolver.h"
class AppApiTest : public ExtensionApiTest {
protected:
// Gets the base URL for files for a specific test, making sure that it uses
// "localhost" as the hostname, since that is what the extent is declared
// as in the test apps manifests.
GURL GetTestBaseURL(std::string test_directory) {
GURL::Replacements replace_host;
std::string host_str("localhost"); // must stay in scope with replace_host
replace_host.SetHostStr(host_str);
GURL base_url = test_server()->GetURL(
"files/extensions/api_test/" + test_directory + "/");
return base_url.ReplaceComponents(replace_host);
}
};
// Simulates a page calling window.open on an URL, and waits for the navigation.
static void WindowOpenHelper(Browser* browser,
RenderViewHost* opener_host,
const GURL& url,
bool newtab_process_should_equal_opener) {
ui_test_utils::WindowedNotificationObserver observer(
content::NOTIFICATION_LOAD_STOP,
content::NotificationService::AllSources());
ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(
opener_host, L"", L"window.open('" + UTF8ToWide(url.spec()) + L"');"));
// The above window.open call is not user-initiated, it will create
// a popup window instead of a new tab in current window.
// Now the active tab in last active window should be the new tab.
Browser* last_active_browser = BrowserList::GetLastActive();
EXPECT_TRUE(last_active_browser);
TabContents* newtab = last_active_browser->GetSelectedTabContents();
EXPECT_TRUE(newtab);
observer.Wait();
EXPECT_EQ(url, newtab->controller().GetLastCommittedEntry()->url());
if (newtab_process_should_equal_opener)
EXPECT_EQ(opener_host->process(), newtab->render_view_host()->process());
else
EXPECT_NE(opener_host->process(), newtab->render_view_host()->process());
}
// Simulates a page navigating itself to an URL, and waits for the navigation.
static void NavigateTabHelper(TabContents* contents, const GURL& url) {
bool result = false;
ui_test_utils::WindowedNotificationObserver observer(
content::NOTIFICATION_LOAD_STOP,
content::NotificationService::AllSources());
ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
contents->render_view_host(), L"",
L"window.addEventListener('unload', function() {"
L" window.domAutomationController.send(true);"
L"}, false);"
L"window.location = '" + UTF8ToWide(url.spec()) + L"';",
&result));
ASSERT_TRUE(result);
observer.Wait();
EXPECT_EQ(url, contents->controller().GetLastCommittedEntry()->url());
}
IN_PROC_BROWSER_TEST_F(AppApiTest, AppProcess) {
LOG(INFO) << "Start of test.";
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kDisablePopupBlocking);
extensions::ProcessMap* process_map =
browser()->profile()->GetExtensionService()->process_map();
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(test_server()->Start());
ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app_process")));
LOG(INFO) << "Loaded extension.";
// Open two tabs in the app, one outside it.
GURL base_url = GetTestBaseURL("app_process");
// Test both opening a URL in a new tab, and opening a tab and then navigating
// it. Either way, app tabs should be considered extension processes, but
// they have no elevated privileges and thus should not have WebUI bindings.
ui_test_utils::NavigateToURLWithDisposition(
browser(), base_url.Resolve("path1/empty.html"), NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
EXPECT_TRUE(process_map->Contains(
browser()->GetTabContentsAt(1)->render_view_host()->process()->GetID()));
EXPECT_FALSE(browser()->GetTabContentsAt(1)->web_ui());
LOG(INFO) << "Nav 1.";
ui_test_utils::NavigateToURLWithDisposition(
browser(), base_url.Resolve("path2/empty.html"), NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
EXPECT_TRUE(process_map->Contains(
browser()->GetTabContentsAt(2)->render_view_host()->process()->GetID()));
EXPECT_FALSE(browser()->GetTabContentsAt(2)->web_ui());
LOG(INFO) << "Nav 2.";
ui_test_utils::WindowedNotificationObserver tab_added_observer(
content::NOTIFICATION_TAB_ADDED,
content::NotificationService::AllSources());
browser()->NewTab();
tab_added_observer.Wait();
LOG(INFO) << "New tab.";
ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path3/empty.html"));
LOG(INFO) << "Nav 3.";
EXPECT_FALSE(process_map->Contains(
browser()->GetTabContentsAt(3)->render_view_host()->process()->GetID()));
EXPECT_FALSE(browser()->GetTabContentsAt(3)->web_ui());
// We should have opened 3 new extension tabs. Including the original blank
// tab, we now have 4 tabs. Because the app_process app has the background
// permission, all of its instances are in the same process. Thus two tabs
// should be part of the extension app and grouped in the same process.
ASSERT_EQ(4, browser()->tab_count());
RenderViewHost* host = browser()->GetTabContentsAt(1)->render_view_host();
EXPECT_EQ(host->process(),
browser()->GetTabContentsAt(2)->render_view_host()->process());
EXPECT_NE(host->process(),
browser()->GetTabContentsAt(3)->render_view_host()->process());
// Now let's do the same using window.open. The same should happen.
ASSERT_EQ(1u, BrowserList::GetBrowserCount(browser()->profile()));
WindowOpenHelper(browser(), host,
base_url.Resolve("path1/empty.html"), true);
LOG(INFO) << "WindowOpenHelper 1.";
WindowOpenHelper(browser(), host,
base_url.Resolve("path2/empty.html"), true);
LOG(INFO) << "WindowOpenHelper 2.";
// TODO(creis): This should open in a new process (i.e., false for the last
// argument), but we temporarily avoid swapping processes away from an app
// until we're able to support cross-process postMessage calls.
// See crbug.com/59285.
WindowOpenHelper(browser(), host,
base_url.Resolve("path3/empty.html"), true);
LOG(INFO) << "WindowOpenHelper 3.";
// Now let's have these pages navigate, into or out of the extension web
// extent. They should switch processes.
const GURL& app_url(base_url.Resolve("path1/empty.html"));
const GURL& non_app_url(base_url.Resolve("path3/empty.html"));
NavigateTabHelper(browser()->GetTabContentsAt(2), non_app_url);
LOG(INFO) << "NavigateTabHelper 1.";
NavigateTabHelper(browser()->GetTabContentsAt(3), app_url);
LOG(INFO) << "NavigateTabHelper 2.";
// TODO(creis): This should swap out of the app's process (i.e., EXPECT_NE),
// but we temporarily avoid swapping away from an app in case the window
// tries to send a postMessage to the app. See crbug.com/59285.
EXPECT_EQ(host->process(),
browser()->GetTabContentsAt(2)->render_view_host()->process());
EXPECT_EQ(host->process(),
browser()->GetTabContentsAt(3)->render_view_host()->process());
// If one of the popup tabs navigates back to the app, window.opener should
// be valid.
NavigateTabHelper(browser()->GetTabContentsAt(6), app_url);
LOG(INFO) << "NavigateTabHelper 3.";
EXPECT_EQ(host->process(),
browser()->GetTabContentsAt(6)->render_view_host()->process());
bool windowOpenerValid = false;
ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetTabContentsAt(6)->render_view_host(), L"",
L"window.domAutomationController.send(window.opener != null)",
&windowOpenerValid));
ASSERT_TRUE(windowOpenerValid);
LOG(INFO) << "End of test.";
}
// Test that hosted apps without the background permission use a process per app
// instance model, such that separate instances are in separate processes.
IN_PROC_BROWSER_TEST_F(AppApiTest, AppProcessInstances) {
LOG(INFO) << "Start of test.";
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kDisablePopupBlocking);
extensions::ProcessMap* process_map =
browser()->profile()->GetExtensionService()->process_map();
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(test_server()->Start());
ASSERT_TRUE(LoadExtension(
test_data_dir_.AppendASCII("app_process_instances")));
// Open two tabs in the app, one outside it.
GURL base_url = GetTestBaseURL("app_process_instances");
// Test both opening a URL in a new tab, and opening a tab and then navigating
// it. Either way, app tabs should be considered extension processes, but
// they have no elevated privileges and thus should not have WebUI bindings.
ui_test_utils::NavigateToURLWithDisposition(
browser(), base_url.Resolve("path1/empty.html"), NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
LOG(INFO) << "Nav 1.";
EXPECT_TRUE(process_map->Contains(
browser()->GetTabContentsAt(1)->render_view_host()->process()->GetID()));
EXPECT_FALSE(browser()->GetTabContentsAt(1)->web_ui());
ui_test_utils::WindowedNotificationObserver tab_added_observer(
content::NOTIFICATION_TAB_ADDED,
content::NotificationService::AllSources());
browser()->NewTab();
tab_added_observer.Wait();
LOG(INFO) << "New tab.";
ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path2/empty.html"));
LOG(INFO) << "Nav 2.";
EXPECT_TRUE(process_map->Contains(
browser()->GetTabContentsAt(2)->render_view_host()->process()->GetID()));
EXPECT_FALSE(browser()->GetTabContentsAt(2)->web_ui());
// We should have opened 2 new extension tabs. Including the original blank
// tab, we now have 3 tabs. The two app tabs should not be in the same
// process, since they do not have the background permission. (Thus, we want
// to separate them to improve responsiveness.)
ASSERT_EQ(3, browser()->tab_count());
RenderViewHost* host1 = browser()->GetTabContentsAt(1)->render_view_host();
RenderViewHost* host2 = browser()->GetTabContentsAt(2)->render_view_host();
EXPECT_NE(host1->process(), host2->process());
// Opening tabs with window.open should keep the page in the opener's process.
ASSERT_EQ(1u, BrowserList::GetBrowserCount(browser()->profile()));
WindowOpenHelper(browser(), host1,
base_url.Resolve("path1/empty.html"), true);
LOG(INFO) << "WindowOpenHelper 1.";
WindowOpenHelper(browser(), host2,
base_url.Resolve("path2/empty.html"), true);
LOG(INFO) << "End of test.";
}
// Tests that bookmark apps do not use the app process model and are treated
// like normal web pages instead. http://crbug.com/104636.
IN_PROC_BROWSER_TEST_F(AppApiTest, BookmarkAppGetsNormalProcess) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kDisablePopupBlocking);
ExtensionService* service = browser()->profile()->GetExtensionService();
extensions::ProcessMap* process_map = service->process_map();
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(test_server()->Start());
GURL base_url = GetTestBaseURL("app_process");
// Load an app as a bookmark app.
std::string error;
scoped_refptr<const Extension> extension(extension_file_util::LoadExtension(
test_data_dir_.AppendASCII("app_process"),
Extension::LOAD,
Extension::FROM_BOOKMARK,
&error));
service->OnExtensionInstalled(extension, false,
StringOrdinal::CreateInitialOrdinal());
ASSERT_TRUE(extension.get());
ASSERT_TRUE(extension->from_bookmark());
// Test both opening a URL in a new tab, and opening a tab and then navigating
// it. Either way, bookmark app tabs should be considered normal processes
// with no elevated privileges and no WebUI bindings.
ui_test_utils::NavigateToURLWithDisposition(
browser(), base_url.Resolve("path1/empty.html"), NEW_FOREGROUND_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
EXPECT_FALSE(process_map->Contains(
browser()->GetTabContentsAt(1)->render_view_host()->process()->GetID()));
EXPECT_FALSE(browser()->GetTabContentsAt(1)->web_ui());
ui_test_utils::WindowedNotificationObserver tab_added_observer(
content::NOTIFICATION_TAB_ADDED,
content::NotificationService::AllSources());
browser()->NewTab();
tab_added_observer.Wait();
ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path2/empty.html"));
EXPECT_FALSE(process_map->Contains(
browser()->GetTabContentsAt(2)->render_view_host()->process()->GetID()));
EXPECT_FALSE(browser()->GetTabContentsAt(2)->web_ui());
// We should have opened 2 new bookmark app tabs. Including the original blank
// tab, we now have 3 tabs. Because normal pages use the
// process-per-site-instance model, each should be in its own process.
ASSERT_EQ(3, browser()->tab_count());
RenderViewHost* host = browser()->GetTabContentsAt(1)->render_view_host();
EXPECT_NE(host->process(),
browser()->GetTabContentsAt(2)->render_view_host()->process());
// Now let's do the same using window.open. The same should happen.
ASSERT_EQ(1u, BrowserList::GetBrowserCount(browser()->profile()));
WindowOpenHelper(browser(), host,
base_url.Resolve("path1/empty.html"), true);
WindowOpenHelper(browser(), host,
base_url.Resolve("path2/empty.html"), true);
// Now let's have a tab navigate out of and back into the app's web
// extent. Neither navigation should switch processes.
const GURL& app_url(base_url.Resolve("path1/empty.html"));
const GURL& non_app_url(base_url.Resolve("path3/empty.html"));
RenderViewHost* host2 = browser()->GetTabContentsAt(2)->render_view_host();
NavigateTabHelper(browser()->GetTabContentsAt(2), non_app_url);
EXPECT_EQ(host2->process(),
browser()->GetTabContentsAt(2)->render_view_host()->process());
NavigateTabHelper(browser()->GetTabContentsAt(2), app_url);
EXPECT_EQ(host2->process(),
browser()->GetTabContentsAt(2)->render_view_host()->process());
}
// Tests that app process switching works properly in the following scenario:
// 1. navigate to a page1 in the app
// 2. page1 redirects to a page2 outside the app extent (ie, "/server-redirect")
// 3. page2 redirects back to a page in the app
// The final navigation should end up in the app process.
// See http://crbug.com/61757
// This test occasionally timeout on aura. See crbug.com/105957.
#if defined(USE_AURA)
#define MAYBE_AppProcessRedirectBack DISABLED_AppProcessRedirectBack
#else
#define MAYBE_AppProcessRedirectBack AppProcessRedirectBack
#endif
IN_PROC_BROWSER_TEST_F(AppApiTest, MAYBE_AppProcessRedirectBack) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kDisablePopupBlocking);
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(test_server()->Start());
ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app_process")));
// Open two tabs in the app.
GURL base_url = GetTestBaseURL("app_process");
browser()->NewTab();
ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html"));
browser()->NewTab();
// Wait until the second tab finishes its redirect train (3 hops).
// 1. We navigate to redirect.html
// 2. Renderer navigates and finishes, counting as a load stop.
// 3. Renderer issues the meta refresh to navigate to server-redirect.
// 4. Renderer is now in a "provisional load", waiting for navigation to
// complete.
// 5. Browser sees a redirect response from server-redirect to empty.html, and
// transfers that to a new navigation, using RequestTransferURL.
// 6. We navigate to empty.html.
// 7. Renderer is still in a provisional load to server-redirect, so that is
// cancelled, and counts as a load stop
// 8. Renderer navigates to empty.html, and finishes loading, counting as the
// third load stop
ui_test_utils::NavigateToURLBlockUntilNavigationsComplete(
browser(), base_url.Resolve("path1/redirect.html"), 3);
// 3 tabs, including the initial about:blank. The last 2 should be the same
// process.
ASSERT_EQ(3, browser()->tab_count());
EXPECT_EQ("/files/extensions/api_test/app_process/path1/empty.html",
browser()->GetTabContentsAt(2)->controller().
GetLastCommittedEntry()->url().path());
EXPECT_EQ(browser()->GetTabContentsAt(1)->render_view_host()->process(),
browser()->GetTabContentsAt(2)->render_view_host()->process());
}
// Ensure that reloading a URL after installing or uninstalling it as an app
// correctly swaps the process. (http://crbug.com/80621)
// Disabled until we get a correct fix for 80621. See http://crbug.com/102408.
IN_PROC_BROWSER_TEST_F(AppApiTest, DISABLED_ReloadIntoAppProcess) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kDisablePopupBlocking);
extensions::ProcessMap* process_map =
browser()->profile()->GetExtensionService()->process_map();
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(test_server()->Start());
// The app under test acts on URLs whose host is "localhost",
// so the URLs we navigate to must have host "localhost".
GURL base_url = GetTestBaseURL("app_process");
// Load an app URL before loading the app.
ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html"));
TabContents* contents = browser()->GetTabContentsAt(0);
EXPECT_FALSE(process_map->Contains(
contents->render_view_host()->process()->GetID()));
// Load app and navigate to the page.
const Extension* app =
LoadExtension(test_data_dir_.AppendASCII("app_process"));
ASSERT_TRUE(app);
ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html"));
EXPECT_TRUE(process_map->Contains(
contents->render_view_host()->process()->GetID()));
// Disable app and navigate to the page.
DisableExtension(app->id());
ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html"));
EXPECT_FALSE(process_map->Contains(
contents->render_view_host()->process()->GetID()));
// Enable app and reload the page.
EnableExtension(app->id());
ui_test_utils::WindowedNotificationObserver reload_observer(
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(
&browser()->GetSelectedTabContentsWrapper()->tab_contents()->
controller()));
browser()->Reload(CURRENT_TAB);
reload_observer.Wait();
EXPECT_TRUE(process_map->Contains(
contents->render_view_host()->process()->GetID()));
// Disable app and reload the page.
DisableExtension(app->id());
ui_test_utils::WindowedNotificationObserver reload_observer2(
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(
&browser()->GetSelectedTabContentsWrapper()->tab_contents()->
controller()));
browser()->Reload(CURRENT_TAB);
reload_observer2.Wait();
EXPECT_FALSE(process_map->Contains(
contents->render_view_host()->process()->GetID()));
// Enable app and reload via JavaScript.
EnableExtension(app->id());
ui_test_utils::WindowedNotificationObserver js_reload_observer(
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(
&browser()->GetSelectedTabContentsWrapper()->tab_contents()->
controller()));
ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(contents->render_view_host(),
L"", L"location.reload();"));
js_reload_observer.Wait();
EXPECT_TRUE(process_map->Contains(
contents->render_view_host()->process()->GetID()));
// Disable app and reload via JavaScript.
DisableExtension(app->id());
ui_test_utils::WindowedNotificationObserver js_reload_observer2(
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(
&browser()->GetSelectedTabContentsWrapper()->tab_contents()->
controller()));
ASSERT_TRUE(ui_test_utils::ExecuteJavaScript(contents->render_view_host(),
L"", L"location.reload();"));
js_reload_observer2.Wait();
EXPECT_FALSE(process_map->Contains(
contents->render_view_host()->process()->GetID()));
}
// Tests that if we have a non-app process (path3/container.html) that has an
// iframe with a URL in the app's extent (path1/iframe.html), then opening a
// link from that iframe to a new window to a URL in the app's extent (path1/
// empty.html) results in the new window being in an app process. See
// http://crbug.com/89272 for more details.
IN_PROC_BROWSER_TEST_F(AppApiTest, OpenAppFromIframe) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kDisablePopupBlocking);
extensions::ProcessMap* process_map =
browser()->profile()->GetExtensionService()->process_map();
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(test_server()->Start());
GURL base_url = GetTestBaseURL("app_process");
// Load app and start URL (not in the app).
const Extension* app =
LoadExtension(test_data_dir_.AppendASCII("app_process"));
ASSERT_TRUE(app);
ui_test_utils::NavigateToURLWithDisposition(
browser(),
base_url.Resolve("path3/container.html"),
CURRENT_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION |
ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
EXPECT_FALSE(process_map->Contains(
browser()->GetTabContentsAt(0)->render_view_host()->process()->GetID()));
// Wait for popup window to appear.
GURL app_url = base_url.Resolve("path1/empty.html");
Browser* last_active_browser = BrowserList::GetLastActive();
EXPECT_TRUE(last_active_browser);
ASSERT_NE(browser(), last_active_browser);
TabContents* newtab = last_active_browser->GetSelectedTabContents();
EXPECT_TRUE(newtab);
if (!newtab->controller().GetLastCommittedEntry() ||
newtab->controller().GetLastCommittedEntry()->url() != app_url) {
// TODO(gbillock): This still looks racy. Need to make a custom
// observer to intercept new window creation and then look for
// NAV_ENTRY_COMMITTED on the new tab there.
ui_test_utils::WindowedNotificationObserver observer(
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<NavigationController>(&(newtab->controller())));
observer.Wait();
}
// Popup window should be in the app's process.
EXPECT_TRUE(process_map->Contains(
last_active_browser->GetTabContentsAt(0)->render_view_host()->process()->
GetID()));
}
// Tests that if an extension launches an app via chrome.tabs.create with an URL
// that's not in the app's extent but that redirects to it, we still end up with
// an app process. See http://crbug.com/99349 for more details.
IN_PROC_BROWSER_TEST_F(AppApiTest, OpenAppFromExtension) {
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(StartTestServer());
LoadExtension(test_data_dir_.AppendASCII("app_process"));
const Extension* launcher =
LoadExtension(test_data_dir_.AppendASCII("app_launcher"));
// There should be three navigations by the time the app page is loaded.
// 1. The extension launcher page.
// 2. The URL that the extension launches, which redirects.
// 3. The app's URL.
TestNavigationObserver test_navigation_observer(
content::NotificationService::AllSources(),
NULL,
3);
// Load the launcher extension, which should launch the app.
ui_test_utils::NavigateToURLWithDisposition(
browser(),
launcher->GetResourceURL("main.html"),
CURRENT_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
// Wait for app tab to be created and loaded.
test_navigation_observer.WaitForObservation(
base::Bind(&ui_test_utils::RunMessageLoop),
base::Bind(&MessageLoop::Quit,
base::Unretained(MessageLoopForUI::current())));
// App has loaded, and chrome.app.isInstalled should be true.
bool is_installed = false;
ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedTabContents()->render_view_host(), L"",
L"window.domAutomationController.send(chrome.app.isInstalled)",
&is_installed));
ASSERT_TRUE(is_installed);
}
// Tests that if we have an app process (path1/container.html) with a non-app
// iframe (path3/iframe.html), then opening a link from that iframe to a new
// window to a same-origin non-app URL (path3/empty.html) should keep the window
// in the app process.
// This is in contrast to OpenAppFromIframe, since here the popup will not be
// missing special permissions and should be scriptable from the iframe.
// See http://crbug.com/92669 for more details.
IN_PROC_BROWSER_TEST_F(AppApiTest, OpenWebPopupFromWebIframe) {
CommandLine::ForCurrentProcess()->AppendSwitch(
switches::kDisablePopupBlocking);
extensions::ProcessMap* process_map =
browser()->profile()->GetExtensionService()->process_map();
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(test_server()->Start());
GURL base_url = GetTestBaseURL("app_process");
// Load app and start URL (in the app).
const Extension* app =
LoadExtension(test_data_dir_.AppendASCII("app_process"));
ASSERT_TRUE(app);
ui_test_utils::WindowedNotificationObserver observer(
content::NOTIFICATION_LOAD_STOP,
content::NotificationService::AllSources());
ui_test_utils::NavigateToURLWithDisposition(
browser(),
base_url.Resolve("path1/container.html"),
CURRENT_TAB,
ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION |
ui_test_utils::BROWSER_TEST_WAIT_FOR_BROWSER);
content::RenderProcessHost* process =
browser()->GetTabContentsAt(0)->render_view_host()->process();
EXPECT_TRUE(process_map->Contains(process->GetID()));
// Wait for popup window to appear. The new Browser may not have been
// added with SetLastActive, in which case we need to show it first.
// This is necessary for popup windows without a cross-site transition.
if (browser() == BrowserList::GetLastActive()) {
// Grab the second window and show it.
ASSERT_TRUE(BrowserList::size() == 2);
Browser* popup_browser = *(++BrowserList::begin());
popup_browser->window()->Show();
}
Browser* last_active_browser = BrowserList::GetLastActive();
EXPECT_TRUE(last_active_browser);
ASSERT_NE(browser(), last_active_browser);
TabContents* newtab = last_active_browser->GetSelectedTabContents();
EXPECT_TRUE(newtab);
GURL non_app_url = base_url.Resolve("path3/empty.html");
observer.Wait();
// Popup window should be in the app's process.
content::RenderProcessHost* popup_process =
last_active_browser->GetTabContentsAt(0)->render_view_host()->process();
EXPECT_EQ(process, popup_process);
}
IN_PROC_BROWSER_TEST_F(AppApiTest, ReloadAppAfterCrash) {
extensions::ProcessMap* process_map =
browser()->profile()->GetExtensionService()->process_map();
host_resolver()->AddRule("*", "127.0.0.1");
ASSERT_TRUE(test_server()->Start());
ASSERT_TRUE(LoadExtension(test_data_dir_.AppendASCII("app_process")));
GURL base_url = GetTestBaseURL("app_process");
// Load the app, chrome.app.isInstalled should be true.
ui_test_utils::NavigateToURL(browser(), base_url.Resolve("path1/empty.html"));
TabContents* contents = browser()->GetTabContentsAt(0);
EXPECT_TRUE(process_map->Contains(
contents->render_view_host()->process()->GetID()));
bool is_installed = false;
ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
contents->render_view_host(), L"",
L"window.domAutomationController.send(chrome.app.isInstalled)",
&is_installed));
ASSERT_TRUE(is_installed);
// Crash the tab and reload it, chrome.app.isInstalled should still be true.
ui_test_utils::CrashTab(browser()->GetSelectedTabContents());
ui_test_utils::WindowedNotificationObserver observer(
content::NOTIFICATION_LOAD_STOP,
content::Source<NavigationController>(
&browser()->GetSelectedTabContentsWrapper()->tab_contents()->
controller()));
browser()->Reload(CURRENT_TAB);
observer.Wait();
ASSERT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
contents->render_view_host(), L"",
L"window.domAutomationController.send(chrome.app.isInstalled)",
&is_installed));
ASSERT_TRUE(is_installed);
}
|