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
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
|
// Copyright (c) 2012 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/file_util.h"
#include "base/memory/ref_counted.h"
#include "base/path_service.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/browser/renderer_host/render_view_host_impl.h"
#include "content/browser/site_instance_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "content/public/browser/notification_types.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host_observer.h"
#include "content/public/common/url_constants.h"
#include "net/base/net_util.h"
#include "net/test/test_server.h"
using content::RenderViewHost;
using content::SiteInstance;
class RenderViewHostManagerTest : public InProcessBrowserTest {
public:
RenderViewHostManagerTest() {
EnableDOMAutomation();
}
static bool GetFilePathWithHostAndPortReplacement(
const std::string& original_file_path,
const net::HostPortPair& host_port_pair,
std::string* replacement_path) {
std::vector<net::TestServer::StringPair> replacement_text;
replacement_text.push_back(
make_pair("REPLACE_WITH_HOST_AND_PORT", host_port_pair.ToString()));
return net::TestServer::GetFilePathWithReplacements(
original_file_path, replacement_text, replacement_path);
}
};
// Web pages should not have script access to the swapped out page.
IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, NoScriptAccessAfterSwapOut) {
// Start two servers with different sites.
ASSERT_TRUE(test_server()->Start());
net::TestServer https_server(
net::TestServer::TYPE_HTTPS,
net::TestServer::kLocalhost,
FilePath(FILE_PATH_LITERAL("chrome/test/data")));
ASSERT_TRUE(https_server.Start());
// Load a page with links that open in a new window.
std::string replacement_path;
ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
"files/click-noreferrer-links.html",
https_server.host_port_pair(),
&replacement_path));
ui_test_utils::NavigateToURL(browser(),
test_server()->GetURL(replacement_path));
// Get the original SiteInstance for later comparison.
scoped_refptr<SiteInstance> orig_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_TRUE(orig_site_instance != NULL);
// Open a same-site link in a new tab.
ui_test_utils::WindowedTabAddedNotificationObserver new_tab_observer((
content::Source<content::WebContentsDelegate>(browser())));
bool success = false;
EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
L"window.domAutomationController.send(clickSameSiteTargetedLink());",
&success));
EXPECT_TRUE(success);
new_tab_observer.Wait();
// Opens in new tab.
EXPECT_EQ(2, browser()->tab_count());
EXPECT_EQ(1, browser()->active_index());
// Wait for the navigation in the new tab to finish, if it hasn't.
ui_test_utils::WaitForLoadStop(browser()->GetSelectedWebContents());
EXPECT_EQ("/files/navigate_opener.html",
browser()->GetSelectedWebContents()->GetURL().path());
EXPECT_EQ(1, browser()->active_index());
// Should have the same SiteInstance.
scoped_refptr<SiteInstance> blank_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_EQ(orig_site_instance, blank_site_instance);
// We should have access to the opened tab's location.
browser()->ActivateTabAt(0, true);
success = false;
EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
L"window.domAutomationController.send(testScriptAccessToWindow());",
&success));
EXPECT_TRUE(success);
// Now navigate the new tab to a different site.
browser()->ActivateTabAt(1, true);
ui_test_utils::NavigateToURL(browser(),
https_server.GetURL("files/title1.html"));
scoped_refptr<SiteInstance> new_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_NE(orig_site_instance, new_site_instance);
// We should no longer have script access to the opened tab's location.
browser()->ActivateTabAt(0, true);
success = false;
EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
L"window.domAutomationController.send(testScriptAccessToWindow());",
&success));
EXPECT_FALSE(success);
}
// Test for crbug.com/24447. Following a cross-site link with rel=noreferrer
// and target=_blank should create a new SiteInstance.
IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest,
SwapProcessWithRelNoreferrerAndTargetBlank) {
// Start two servers with different sites.
ASSERT_TRUE(test_server()->Start());
net::TestServer https_server(
net::TestServer::TYPE_HTTPS,
net::TestServer::kLocalhost,
FilePath(FILE_PATH_LITERAL("chrome/test/data")));
ASSERT_TRUE(https_server.Start());
// Load a page with links that open in a new window.
std::string replacement_path;
ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
"files/click-noreferrer-links.html",
https_server.host_port_pair(),
&replacement_path));
ui_test_utils::NavigateToURL(browser(),
test_server()->GetURL(replacement_path));
// Get the original SiteInstance for later comparison.
scoped_refptr<SiteInstance> orig_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_TRUE(orig_site_instance != NULL);
// Test clicking a rel=noreferrer + target=blank link.
bool success = false;
EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
L"window.domAutomationController.send(clickNoRefTargetBlankLink());",
&success));
EXPECT_TRUE(success);
// Wait for the tab to open.
if (browser()->tab_count() < 2)
ui_test_utils::WaitForNewTab(browser());
// Opens in new tab.
EXPECT_EQ(2, browser()->tab_count());
EXPECT_EQ(1, browser()->active_index());
EXPECT_EQ("/files/title2.html",
browser()->GetSelectedWebContents()->GetURL().path());
// Wait for the cross-site transition in the new tab to finish.
ui_test_utils::WaitForLoadStop(browser()->GetSelectedWebContents());
WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
browser()->GetSelectedWebContents());
EXPECT_FALSE(web_contents->GetRenderManagerForTesting()->
pending_render_view_host());
// Should have a new SiteInstance.
scoped_refptr<SiteInstance> noref_blank_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_NE(orig_site_instance, noref_blank_site_instance);
}
// As of crbug.com/69267, we create a new BrowsingInstance (and SiteInstance)
// for rel=noreferrer links in new windows, even to same site pages and named
// targets.
IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest,
SwapProcessWithSameSiteRelNoreferrer) {
// Start two servers with different sites.
ASSERT_TRUE(test_server()->Start());
net::TestServer https_server(
net::TestServer::TYPE_HTTPS,
net::TestServer::kLocalhost,
FilePath(FILE_PATH_LITERAL("chrome/test/data")));
ASSERT_TRUE(https_server.Start());
// Load a page with links that open in a new window.
std::string replacement_path;
ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
"files/click-noreferrer-links.html",
https_server.host_port_pair(),
&replacement_path));
ui_test_utils::NavigateToURL(browser(),
test_server()->GetURL(replacement_path));
// Get the original SiteInstance for later comparison.
scoped_refptr<SiteInstance> orig_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_TRUE(orig_site_instance != NULL);
// Test clicking a same-site rel=noreferrer + target=foo link.
bool success = false;
EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
L"window.domAutomationController.send(clickSameSiteNoRefTargetedLink());",
&success));
EXPECT_TRUE(success);
// Wait for the tab to open.
if (browser()->tab_count() < 2)
ui_test_utils::WaitForNewTab(browser());
// Opens in new tab.
EXPECT_EQ(2, browser()->tab_count());
EXPECT_EQ(1, browser()->active_index());
EXPECT_EQ("/files/title2.html",
browser()->GetSelectedWebContents()->GetURL().path());
// Wait for the cross-site transition in the new tab to finish.
ui_test_utils::WaitForLoadStop(browser()->GetSelectedWebContents());
WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
browser()->GetSelectedWebContents());
EXPECT_FALSE(web_contents->GetRenderManagerForTesting()->
pending_render_view_host());
// Should have a new SiteInstance (in a new BrowsingInstance).
scoped_refptr<SiteInstance> noref_blank_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_NE(orig_site_instance, noref_blank_site_instance);
}
// Test for crbug.com/24447. Following a cross-site link with just
// target=_blank should not create a new SiteInstance.
IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest,
DontSwapProcessWithOnlyTargetBlank) {
// Start two servers with different sites.
ASSERT_TRUE(test_server()->Start());
net::TestServer https_server(
net::TestServer::TYPE_HTTPS,
net::TestServer::kLocalhost,
FilePath(FILE_PATH_LITERAL("chrome/test/data")));
ASSERT_TRUE(https_server.Start());
// Load a page with links that open in a new window.
std::string replacement_path;
ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
"files/click-noreferrer-links.html",
https_server.host_port_pair(),
&replacement_path));
ui_test_utils::NavigateToURL(browser(),
test_server()->GetURL(replacement_path));
// Get the original SiteInstance for later comparison.
scoped_refptr<SiteInstance> orig_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_TRUE(orig_site_instance != NULL);
// Test clicking a target=blank link.
bool success = false;
EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
L"window.domAutomationController.send(clickTargetBlankLink());",
&success));
EXPECT_TRUE(success);
// Wait for the tab to open.
if (browser()->tab_count() < 2)
ui_test_utils::WaitForNewTab(browser());
// Opens in new tab.
EXPECT_EQ(2, browser()->tab_count());
EXPECT_EQ(1, browser()->active_index());
// Wait for the cross-site transition in the new tab to finish.
ui_test_utils::WaitForLoadStop(browser()->GetSelectedWebContents());
EXPECT_EQ("/files/title2.html",
browser()->GetSelectedWebContents()->GetURL().path());
// Should have the same SiteInstance.
scoped_refptr<SiteInstance> blank_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_EQ(orig_site_instance, blank_site_instance);
}
// Test for crbug.com/24447. Following a cross-site link with rel=noreferrer
// and no target=_blank should not create a new SiteInstance.
IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest,
DontSwapProcessWithOnlyRelNoreferrer) {
// Start two servers with different sites.
ASSERT_TRUE(test_server()->Start());
net::TestServer https_server(
net::TestServer::TYPE_HTTPS,
net::TestServer::kLocalhost,
FilePath(FILE_PATH_LITERAL("chrome/test/data")));
ASSERT_TRUE(https_server.Start());
// Load a page with links that open in a new window.
std::string replacement_path;
ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
"files/click-noreferrer-links.html",
https_server.host_port_pair(),
&replacement_path));
ui_test_utils::NavigateToURL(browser(),
test_server()->GetURL(replacement_path));
// Get the original SiteInstance for later comparison.
scoped_refptr<SiteInstance> orig_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_TRUE(orig_site_instance != NULL);
// Test clicking a rel=noreferrer link.
bool success = false;
EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
L"window.domAutomationController.send(clickNoRefLink());",
&success));
EXPECT_TRUE(success);
// Wait for the cross-site transition in the current tab to finish.
ui_test_utils::WaitForLoadStop(browser()->GetSelectedWebContents());
// Opens in same tab.
EXPECT_EQ(1, browser()->tab_count());
EXPECT_EQ(0, browser()->active_index());
EXPECT_EQ("/files/title2.html",
browser()->GetSelectedWebContents()->GetURL().path());
// Should have the same SiteInstance.
scoped_refptr<SiteInstance> noref_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_EQ(orig_site_instance, noref_site_instance);
}
// Test for crbug.com/116192. Targeted links should still work after the
// named target window has swapped processes.
IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest,
AllowTargetedNavigationsAfterSwap) {
// Start two servers with different sites.
ASSERT_TRUE(test_server()->Start());
net::TestServer https_server(
net::TestServer::TYPE_HTTPS,
net::TestServer::kLocalhost,
FilePath(FILE_PATH_LITERAL("chrome/test/data")));
ASSERT_TRUE(https_server.Start());
// Load a page with links that open in a new window.
std::string replacement_path;
ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
"files/click-noreferrer-links.html",
https_server.host_port_pair(),
&replacement_path));
ui_test_utils::NavigateToURL(browser(),
test_server()->GetURL(replacement_path));
// Get the original SiteInstance for later comparison.
scoped_refptr<SiteInstance> orig_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_TRUE(orig_site_instance != NULL);
// Test clicking a target=foo link.
ui_test_utils::WindowedTabAddedNotificationObserver new_tab_observer((
content::Source<content::WebContentsDelegate>(browser())));
bool success = false;
EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
L"window.domAutomationController.send(clickSameSiteTargetedLink());",
&success));
EXPECT_TRUE(success);
new_tab_observer.Wait();
// Opens in new tab.
EXPECT_EQ(2, browser()->tab_count());
EXPECT_EQ(1, browser()->active_index());
// Wait for the navigation in the new tab to finish, if it hasn't.
ui_test_utils::WaitForLoadStop(browser()->GetSelectedWebContents());
EXPECT_EQ("/files/navigate_opener.html",
browser()->GetSelectedWebContents()->GetURL().path());
EXPECT_EQ(1, browser()->active_index());
// Should have the same SiteInstance.
scoped_refptr<SiteInstance> blank_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_EQ(orig_site_instance, blank_site_instance);
// Now navigate the new tab to a different site.
content::WebContents* new_contents = browser()->GetSelectedWebContents();
ui_test_utils::NavigateToURL(browser(),
https_server.GetURL("files/title1.html"));
scoped_refptr<SiteInstance> new_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_NE(orig_site_instance, new_site_instance);
// Clicking the original link in the first tab should cause us to swap back.
browser()->ActivateTabAt(0, true);
ui_test_utils::WindowedNotificationObserver navigation_observer(
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&new_contents->GetController()));
EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
L"window.domAutomationController.send(clickSameSiteTargetedLink());",
&success));
EXPECT_TRUE(success);
navigation_observer.Wait();
// Should have swapped back and shown the new tab again.
EXPECT_EQ(1, browser()->active_index());
scoped_refptr<SiteInstance> revisit_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_EQ(orig_site_instance, revisit_site_instance);
// If it navigates away to another process, the original window should
// still be able to close it (using a cross-process close message).
ui_test_utils::NavigateToURL(browser(),
https_server.GetURL("files/title1.html"));
EXPECT_EQ(new_site_instance,
browser()->GetSelectedWebContents()->GetSiteInstance());
browser()->ActivateTabAt(0, true);
ui_test_utils::WindowedNotificationObserver close_observer(
content::NOTIFICATION_WEB_CONTENTS_DESTROYED,
content::Source<content::WebContents>(new_contents));
EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
L"window.domAutomationController.send(testCloseWindow());",
&success));
EXPECT_TRUE(success);
close_observer.Wait();
}
// Test for crbug.com/116192. Navigations to a window's opener should
// still work after a process swap.
IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest,
AllowTargetedNavigationsInOpenerAfterSwap) {
// Start two servers with different sites.
ASSERT_TRUE(test_server()->Start());
net::TestServer https_server(
net::TestServer::TYPE_HTTPS,
net::TestServer::kLocalhost,
FilePath(FILE_PATH_LITERAL("chrome/test/data")));
ASSERT_TRUE(https_server.Start());
// Load a page with links that open in a new window.
std::string replacement_path;
ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
"files/click-noreferrer-links.html",
https_server.host_port_pair(),
&replacement_path));
ui_test_utils::NavigateToURL(browser(),
test_server()->GetURL(replacement_path));
// Get the original tab and SiteInstance for later comparison.
content::WebContents* orig_contents = browser()->GetSelectedWebContents();
scoped_refptr<SiteInstance> orig_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_TRUE(orig_site_instance != NULL);
// Test clicking a target=foo link.
ui_test_utils::WindowedTabAddedNotificationObserver new_tab_observer((
content::Source<content::WebContentsDelegate>(browser())));
bool success = false;
EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
L"window.domAutomationController.send(clickSameSiteTargetedLink());",
&success));
EXPECT_TRUE(success);
new_tab_observer.Wait();
// Opens in new tab.
EXPECT_EQ(2, browser()->tab_count());
EXPECT_EQ(1, browser()->active_index());
// Wait for the navigation in the new tab to finish, if it hasn't.
ui_test_utils::WaitForLoadStop(browser()->GetSelectedWebContents());
EXPECT_EQ("/files/navigate_opener.html",
browser()->GetSelectedWebContents()->GetURL().path());
EXPECT_EQ(1, browser()->active_index());
// Should have the same SiteInstance.
scoped_refptr<SiteInstance> blank_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_EQ(orig_site_instance, blank_site_instance);
// Now navigate the original (opener) tab to a different site.
browser()->ActivateTabAt(0, true);
ui_test_utils::NavigateToURL(browser(),
https_server.GetURL("files/title1.html"));
scoped_refptr<SiteInstance> new_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_NE(orig_site_instance, new_site_instance);
// The opened tab should be able to navigate the opener back to its process.
browser()->ActivateTabAt(1, true);
ui_test_utils::WindowedNotificationObserver navigation_observer(
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&orig_contents->GetController()));
EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
L"window.domAutomationController.send(navigateOpener());",
&success));
EXPECT_TRUE(success);
navigation_observer.Wait();
// Active tab should not have changed.
EXPECT_EQ(1, browser()->active_index());
// Should have swapped back into this process.
browser()->ActivateTabAt(0, true);
scoped_refptr<SiteInstance> revisit_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_EQ(orig_site_instance, revisit_site_instance);
}
// Test that opening a new window in the same SiteInstance and then navigating
// both windows to a different SiteInstance allows the first process to exit.
// See http://crbug.com/126333.
IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest,
ProcessExitWithSwappedOutViews) {
// Start two servers with different sites.
ASSERT_TRUE(test_server()->Start());
net::TestServer https_server(
net::TestServer::TYPE_HTTPS,
net::TestServer::kLocalhost,
FilePath(FILE_PATH_LITERAL("chrome/test/data")));
ASSERT_TRUE(https_server.Start());
// Load a page with links that open in a new window.
std::string replacement_path;
ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
"files/click-noreferrer-links.html",
https_server.host_port_pair(),
&replacement_path));
ui_test_utils::NavigateToURL(browser(),
test_server()->GetURL(replacement_path));
// Get the original SiteInstance for later comparison.
scoped_refptr<SiteInstance> orig_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_TRUE(orig_site_instance != NULL);
// Test clicking a target=foo link.
ui_test_utils::WindowedTabAddedNotificationObserver new_tab_observer((
content::Source<content::WebContentsDelegate>(browser())));
bool success = false;
EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
L"window.domAutomationController.send(clickSameSiteTargetedLink());",
&success));
EXPECT_TRUE(success);
new_tab_observer.Wait();
// Opens in new tab.
EXPECT_EQ(2, browser()->tab_count());
EXPECT_EQ(1, browser()->active_index());
// Wait for the navigation in the new tab to finish, if it hasn't.
ui_test_utils::WaitForLoadStop(browser()->GetSelectedWebContents());
EXPECT_EQ("/files/navigate_opener.html",
browser()->GetSelectedWebContents()->GetURL().path());
EXPECT_EQ(1, browser()->active_index());
// Should have the same SiteInstance.
scoped_refptr<SiteInstance> opened_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_EQ(orig_site_instance, opened_site_instance);
// Now navigate the opened tab to a different site.
ui_test_utils::NavigateToURL(browser(),
https_server.GetURL("files/title1.html"));
scoped_refptr<SiteInstance> new_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_NE(orig_site_instance, new_site_instance);
// The original process should still be alive, since it is still used in the
// first tab.
content::RenderProcessHost* orig_process = orig_site_instance->GetProcess();
EXPECT_TRUE(orig_process->HasConnection());
// Navigate the first tab to a different site as well. The original process
// should exit, since all of its views are now swapped out.
browser()->ActivateTabAt(0, true);
ui_test_utils::WindowedNotificationObserver exit_observer(
content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
content::Source<content::RenderProcessHost>(orig_process));
ui_test_utils::NavigateToURL(browser(),
https_server.GetURL("files/title1.html"));
exit_observer.Wait();
scoped_refptr<SiteInstance> new_site_instance2(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_EQ(new_site_instance, new_site_instance2);
}
// Test for crbug.com/76666. A cross-site navigation that fails with a 204
// error should not make us ignore future renderer-initiated navigations.
IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, ClickLinkAfter204Error) {
// Start two servers with different sites.
ASSERT_TRUE(test_server()->Start());
net::TestServer https_server(
net::TestServer::TYPE_HTTPS,
net::TestServer::kLocalhost,
FilePath(FILE_PATH_LITERAL("chrome/test/data")));
ASSERT_TRUE(https_server.Start());
// Load a page with links that open in a new window.
// The links will point to the HTTPS server.
std::string replacement_path;
ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
"files/click-noreferrer-links.html",
https_server.host_port_pair(),
&replacement_path));
ui_test_utils::NavigateToURL(browser(),
test_server()->GetURL(replacement_path));
// Get the original SiteInstance for later comparison.
scoped_refptr<SiteInstance> orig_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_TRUE(orig_site_instance != NULL);
// Load a cross-site page that fails with a 204 error.
ui_test_utils::NavigateToURL(browser(), https_server.GetURL("nocontent"));
// We should still be looking at the normal page.
scoped_refptr<SiteInstance> post_nav_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_EQ(orig_site_instance, post_nav_site_instance);
EXPECT_EQ("/files/click-noreferrer-links.html",
browser()->GetSelectedWebContents()->GetURL().path());
// Renderer-initiated navigations should work.
bool success = false;
EXPECT_TRUE(ui_test_utils::ExecuteJavaScriptAndExtractBool(
browser()->GetSelectedWebContents()->GetRenderViewHost(), L"",
L"window.domAutomationController.send(clickNoRefLink());",
&success));
EXPECT_TRUE(success);
// Wait for the cross-site transition in the current tab to finish.
ui_test_utils::WaitForLoadStop(browser()->GetSelectedWebContents());
// Opens in same tab.
EXPECT_EQ(1, browser()->tab_count());
EXPECT_EQ(0, browser()->active_index());
EXPECT_EQ("/files/title2.html",
browser()->GetSelectedWebContents()->GetURL().path());
// Should have the same SiteInstance.
scoped_refptr<SiteInstance> noref_site_instance(
browser()->GetSelectedWebContents()->GetSiteInstance());
EXPECT_EQ(orig_site_instance, noref_site_instance);
}
// Test for http://crbug.com/93427. Ensure that cross-site navigations
// do not cause back/forward navigations to be considered stale by the
// renderer.
IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, BackForwardNotStale) {
// Start two servers with different sites.
ASSERT_TRUE(test_server()->Start());
net::TestServer https_server(
net::TestServer::TYPE_HTTPS,
net::TestServer::kLocalhost,
FilePath(FILE_PATH_LITERAL("chrome/test/data")));
ASSERT_TRUE(https_server.Start());
// Visit a page on first site.
std::string replacement_path_a1;
ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
"files/title1.html",
test_server()->host_port_pair(),
&replacement_path_a1));
ui_test_utils::NavigateToURL(browser(),
test_server()->GetURL(replacement_path_a1));
// Visit three pages on second site.
std::string replacement_path_b1;
ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
"files/title1.html",
https_server.host_port_pair(),
&replacement_path_b1));
ui_test_utils::NavigateToURL(browser(),
https_server.GetURL(replacement_path_b1));
std::string replacement_path_b2;
ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
"files/title2.html",
https_server.host_port_pair(),
&replacement_path_b2));
ui_test_utils::NavigateToURL(browser(),
https_server.GetURL(replacement_path_b2));
std::string replacement_path_b3;
ASSERT_TRUE(GetFilePathWithHostAndPortReplacement(
"files/title3.html",
https_server.host_port_pair(),
&replacement_path_b3));
ui_test_utils::NavigateToURL(browser(),
https_server.GetURL(replacement_path_b3));
// History is now [blank, A1, B1, B2, *B3].
content::WebContents* contents = browser()->GetSelectedWebContents();
EXPECT_EQ(5, contents->GetController().GetEntryCount());
// Open another tab in same process to keep this process alive.
ui_test_utils::NavigateToURLWithDisposition(
browser(), https_server.GetURL(replacement_path_b1),
NEW_BACKGROUND_TAB, ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
// Go back three times to first site.
{
ui_test_utils::WindowedNotificationObserver back_nav_load_observer(
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&contents->GetController()));
browser()->GoBack(CURRENT_TAB);
back_nav_load_observer.Wait();
}
{
ui_test_utils::WindowedNotificationObserver back_nav_load_observer(
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&contents->GetController()));
browser()->GoBack(CURRENT_TAB);
back_nav_load_observer.Wait();
}
{
ui_test_utils::WindowedNotificationObserver back_nav_load_observer(
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&contents->GetController()));
browser()->GoBack(CURRENT_TAB);
back_nav_load_observer.Wait();
}
// Now go forward twice to B2. Shouldn't be left spinning.
{
ui_test_utils::WindowedNotificationObserver forward_nav_load_observer(
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&contents->GetController()));
browser()->GoForward(CURRENT_TAB);
forward_nav_load_observer.Wait();
}
{
ui_test_utils::WindowedNotificationObserver forward_nav_load_observer(
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&contents->GetController()));
browser()->GoForward(CURRENT_TAB);
forward_nav_load_observer.Wait();
}
// Go back twice to first site.
{
ui_test_utils::WindowedNotificationObserver back_nav_load_observer(
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&contents->GetController()));
browser()->GoBack(CURRENT_TAB);
back_nav_load_observer.Wait();
}
{
ui_test_utils::WindowedNotificationObserver back_nav_load_observer(
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&contents->GetController()));
browser()->GoBack(CURRENT_TAB);
back_nav_load_observer.Wait();
}
// Now go forward directly to B3. Shouldn't be left spinning.
{
ui_test_utils::WindowedNotificationObserver forward_nav_load_observer(
content::NOTIFICATION_NAV_ENTRY_COMMITTED,
content::Source<content::NavigationController>(
&contents->GetController()));
contents->GetController().GoToIndex(4);
forward_nav_load_observer.Wait();
}
}
// This class holds onto RenderViewHostObservers for as long as their observed
// RenderViewHosts are alive. This allows us to confirm that all hosts have
// properly been shutdown.
class RenderViewHostObserverArray {
public:
~RenderViewHostObserverArray() {
// In case some would be left in there with a dead pointer to us.
for (std::list<RVHObserver*>::iterator iter = observers_.begin();
iter != observers_.end(); ++iter) {
(*iter)->ClearParent();
}
}
void AddObserverToRVH(RenderViewHost* rvh) {
observers_.push_back(new RVHObserver(this, rvh));
}
size_t GetNumObservers() const {
return observers_.size();
}
private:
friend class RVHObserver;
class RVHObserver : public content::RenderViewHostObserver {
public:
RVHObserver(RenderViewHostObserverArray* parent, RenderViewHost* rvh)
: content::RenderViewHostObserver(rvh),
parent_(parent) {
}
virtual void RenderViewHostDestroyed(RenderViewHost* rvh) OVERRIDE {
if (parent_)
parent_->RemoveObserver(this);
content::RenderViewHostObserver::RenderViewHostDestroyed(rvh);
};
void ClearParent() {
parent_ = NULL;
}
private:
RenderViewHostObserverArray* parent_;
};
void RemoveObserver(RVHObserver* observer) {
observers_.remove(observer);
}
std::list<RVHObserver*> observers_;
};
// Test for crbug.com/90867. Make sure we don't leak render view hosts since
// they may cause crashes or memory corruptions when trying to call dead
// delegate_.
IN_PROC_BROWSER_TEST_F(RenderViewHostManagerTest, LeakingRenderViewHosts) {
// Start two servers with different sites.
ASSERT_TRUE(test_server()->Start());
net::TestServer https_server(net::TestServer::TYPE_HTTPS,
net::TestServer::kLocalhost,
FilePath(FILE_PATH_LITERAL("chrome/test/data")));
ASSERT_TRUE(https_server.Start());
// Create a new tab so that we can close the one we navigate and still have
// a running browser.
AddBlankTabAndShow(browser());
// Load a random page and then navigate to view-source: of it.
// This used to cause two RVH instances for the same SiteInstance, which
// was a problem. This is no longer the case.
GURL navigated_url(test_server()->GetURL("files/title2.html"));
ui_test_utils::NavigateToURL(browser(), navigated_url);
SiteInstance* site_instance1 = browser()->GetSelectedWebContents()->
GetRenderViewHost()->GetSiteInstance();
// Observe the newly created render_view_host to make sure it will not leak.
RenderViewHostObserverArray rvh_observers;
rvh_observers.AddObserverToRVH(browser()->GetSelectedWebContents()->
GetRenderViewHost());
GURL view_source_url(chrome::kViewSourceScheme + std::string(":") +
navigated_url.spec());
ui_test_utils::NavigateToURL(browser(), view_source_url);
rvh_observers.AddObserverToRVH(browser()->GetSelectedWebContents()->
GetRenderViewHost());
SiteInstance* site_instance2 = browser()->GetSelectedWebContents()->
GetRenderViewHost()->GetSiteInstance();
// Ensure that view-source navigations force a new SiteInstance.
EXPECT_NE(site_instance1, site_instance2);
// Now navigate to a different instance so that we swap out again.
ui_test_utils::NavigateToURL(browser(),
https_server.GetURL("files/title2.html"));
rvh_observers.AddObserverToRVH(browser()->GetSelectedWebContents()->
GetRenderViewHost());
// This used to leak a render view host.
browser()->CloseTabContents(browser()->GetSelectedWebContents());
EXPECT_EQ(0U, rvh_observers.GetNumObservers());
}
|