File: site_instance_group_browsertest.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (666 lines) | stat: -rw-r--r-- 28,737 bytes parent folder | download | duplicates (4)
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
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "content/browser/process_lock.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/content_navigation_policy.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "content/test/content_browser_test_base.h"
#include "content/test/content_browser_test_utils_internal.h"
#include "net/dns/mock_host_resolver.h"
#include "net/test/embedded_test_server/embedded_test_server.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace content {

// A set of tests for loading data: URL subframes in their own SiteInstance in
// their initiator's SiteInstanceGroup. Parameterized to also run in the legacy
// mode where data: URLs load in their initiator's SiteInstance. See
// https://crbug.com/40176090.
class DataURLSiteInstanceGroupTest
    : public ContentBrowserTestBase,
      public ::testing::WithParamInterface<bool> {
 public:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    IsolateAllSitesForTesting(command_line);

    if (IsSiteInstanceGroupEnabled()) {
      feature_list_.InitAndEnableFeature(
          features::kSiteInstanceGroupsForDataUrls);
    } else {
      feature_list_.InitAndDisableFeature(
          features::kSiteInstanceGroupsForDataUrls);
    }
  }

  static std::string DescribeParams(
      const testing::TestParamInfo<ParamType>& info) {
    return info.param ? "DataSubframeHasOwnSiteInstance"
                      : "DataSubframeInInitiatorSiteInstance";
  }

 protected:
  bool IsSiteInstanceGroupEnabled() const { return GetParam(); }

 private:
  base::test::ScopedFeatureList feature_list_;
};

// A b.com subframe navigates itself to a data: URL. This allows testing that a
// navigation from b.com to a data: URL is a local-to-local frame navigation and
// does not create a proxy, despite being cross-SiteInstance.
IN_PROC_BROWSER_TEST_P(DataURLSiteInstanceGroupTest,
                       SiteInstanceGroupDataURLSubframe) {
  GURL url(embedded_test_server()->GetURL(
      "a.com", "/cross_site_iframe_factory.html?a(b,b)"));
  EXPECT_TRUE(NavigateToURL(shell(), url));

  // Both subframes share a site and therefore SiteInstance.
  EXPECT_EQ(main_frame()
                ->child_at(0)
                ->render_manager()
                ->current_frame_host()
                ->GetSiteInstance(),
            main_frame()
                ->child_at(1)
                ->render_manager()
                ->current_frame_host()
                ->GetSiteInstance());

  // The first B subframe navigates itself to a data: URL, so the data: subframe
  // has initiator b.com.
  GURL data_url("data:text/html,test");
  EXPECT_TRUE(NavigateToURLFromRenderer(main_frame()->child_at(0), data_url));

  SiteInstanceGroup* root_group = main_frame_host()->GetSiteInstance()->group();
  scoped_refptr<SiteInstanceImpl> child0_instance = main_frame()
                                                        ->child_at(0)
                                                        ->render_manager()
                                                        ->current_frame_host()
                                                        ->GetSiteInstance();
  scoped_refptr<SiteInstanceImpl> child1_instance = main_frame()
                                                        ->child_at(1)
                                                        ->render_manager()
                                                        ->current_frame_host()
                                                        ->GetSiteInstance();

  // In both parameterization modes, the root is cross-site from the subframes
  // and should not share a process or SiteInstanceGroup.
  EXPECT_NE(root_group, child0_instance->group());

  if (ShouldCreateSiteInstanceForDataUrls()) {
    // The data: subframe should have its own SiteInstance in the same
    // SiteInstanceGroup as the other B subframe.
    EXPECT_NE(child0_instance, child1_instance);
    EXPECT_EQ(child0_instance->group(), child1_instance->group());
    EXPECT_EQ(
        " Site A ------------ proxies for {B,C}\n"
        "   |--Site B ------- proxies for A\n"
        "   +--Site C ------- proxies for A\n"
        "Where A = http://a.com/\n"
        "      B = data:nonce_B\n"
        "      C = http://b.com/",
        DepictFrameTree(*main_frame()));
  } else {
    // The data: subframe shares a SiteInstance with the other B subframe.
    EXPECT_EQ(child0_instance, child1_instance);
    EXPECT_EQ(
        " Site A ------------ proxies for B\n"
        "   |--Site B ------- proxies for A\n"
        "   +--Site B ------- proxies for A\n"
        "Where A = http://a.com/\n"
        "      B = http://b.com/",
        DepictFrameTree(*main_frame()));
  }
}

// Check that for a main frame data: URL, about:blank frames end up in the data:
// URL's SiteInstance and can be scripted.
IN_PROC_BROWSER_TEST_P(DataURLSiteInstanceGroupTest,
                       MainFrameDataURLWithAboutBlank) {
  // Load a main frame data: URL with an about:blank subframe. The main frame
  // and subframe should be in the same SiteInstance.
  GURL data_url(
      "data:text/html,<body> This page has one about:blank iframe:"
      "<iframe name='frame1' src='about:blank'></iframe> </body>");

  EXPECT_TRUE(NavigateToURL(shell(), data_url));
  EXPECT_EQ(main_frame_host()->GetSiteInstance(),
            main_frame()->child_at(0)->current_frame_host()->GetSiteInstance());
  EXPECT_EQ(
      " Site A\n"
      "   +--Site A\n"
      "Where A = data:nonce_A",
      DepictFrameTree(*main_frame()));

  // The main frame should be able to script its about:blank subframe.
  EXPECT_TRUE(ExecJs(main_frame(), "frames[0].window.name = 'new-name'"));
  EXPECT_EQ(main_frame()->child_at(0)->frame_name(), "new-name");
}

// Check that for a subframe data: URL, about:blank frames end up in the data:
// URL's SiteInstance and can be scripted.
IN_PROC_BROWSER_TEST_P(DataURLSiteInstanceGroupTest,
                       SubframeDataURLWithAboutBlank) {
  // Navigate to a main frame with a cross-site iframe.
  GURL main_url(embedded_test_server()->GetURL(
      "a.com", "/cross_site_iframe_factory.html?a(b)"));
  EXPECT_TRUE(NavigateToURL(shell(), main_url));

  // Navigate the subframe to a data: URL with an about:blank subframe.
  GURL data_url(
      "data:text/html,<body> This page has one about:blank iframe:"
      "<iframe name='frame1' src='about:blank'></iframe> </body>");
  TestNavigationObserver observer(web_contents());
  FrameTreeNode* child = main_frame()->child_at(0);
  EXPECT_TRUE(NavigateToURLFromRenderer(child, data_url));
  EXPECT_TRUE(observer.last_navigation_succeeded());

  // The data: frame should be able to script its about:blank subframe.
  EXPECT_TRUE(ExecJs(child, "frames[0].window.name = 'new-name'"));
  EXPECT_EQ(child->child_at(0)->frame_name(), "new-name");

  if (ShouldCreateSiteInstanceForDataUrls()) {
    EXPECT_EQ(
        " Site A ------------ proxies for {B,C}\n"
        "   +--Site B ------- proxies for A\n"
        "        +--Site B -- proxies for A\n"
        "Where A = http://a.com/\n"
        "      B = data:nonce_B\n"
        "      C = http://b.com/",
        DepictFrameTree(*main_frame()));
  } else {
    EXPECT_EQ(
        " Site A ------------ proxies for B\n"
        "   +--Site B ------- proxies for A\n"
        "        +--Site B -- proxies for A\n"
        "Where A = http://a.com/\n"
        "      B = http://b.com/",
        DepictFrameTree(*main_frame()));
  }
}

// Test that data: subframes are added to the correct SiteInstanceGroup. In the
// case of A(B_sandbox, B(data_sandbox)), data_sandbox should go in the group of
// its initiator, B. However, because data is sandboxed and B isn't, B's group
// is not the correct group, but B_sandbox's group is.
IN_PROC_BROWSER_TEST_P(DataURLSiteInstanceGroupTest,
                       SandboxedDataFindsExistingSiteInstance) {
  GURL main_url(embedded_test_server()->GetURL("a.com", "/title1.html"));
  EXPECT_TRUE(NavigateToURL(shell(), main_url));

  // Create sandboxed cross-origin child frame.
  GURL b_url(embedded_test_server()->GetURL("b.com", "/title1.html"));
  {
    std::string js_str = base::StringPrintf(
        "var frame = document.createElement('iframe'); "
        "frame.id = 'test_frame_sandboxed'; "
        "frame.sandbox = ''; "
        "frame.src = '%s'; "
        "document.body.appendChild(frame);",
        b_url.spec().c_str());
    EXPECT_TRUE(ExecJs(shell(), js_str));
    ASSERT_TRUE(WaitForLoadStop(web_contents()));
  }
  FrameTreeNode* child0 = main_frame()->child_at(0);
  scoped_refptr<SiteInstanceImpl> child0_instance =
      child0->current_frame_host()->GetSiteInstance();
  EXPECT_TRUE(child0_instance->GetSiteInfo().is_sandboxed());

  // Create a non-sandboxed cross-origin child frame, which has the same site as
  // the sandboxed subframe, but is in a different (non-sandboxed) SiteInstance.
  {
    std::string js_str = base::StringPrintf(
        "var frame = document.createElement('iframe'); "
        "frame.id = 'test_frame_not_sandboxed'; "
        "frame.src = '%s'; "
        "document.body.appendChild(frame);",
        b_url.spec().c_str());
    EXPECT_TRUE(ExecJs(shell(), js_str));
    ASSERT_TRUE(WaitForLoadStop(web_contents()));
  }
  FrameTreeNode* child1 = main_frame()->child_at(1);
  scoped_refptr<SiteInstanceImpl> child1_instance =
      child1->current_frame_host()->GetSiteInstance();
  EXPECT_FALSE(child1_instance->GetSiteInfo().is_sandboxed());
  EXPECT_NE(child0_instance, child1_instance);

  // Add a sandboxed data: URL subframe as a subframe of `child1`. It should get
  // its own SiteInstance.
  {
    GURL data_url("data:text/html,test");
    std::string js_str = base::StringPrintf(
        "var frame = document.createElement('iframe'); "
        "frame.id = 'test_frame_data_sandboxed'; "
        "frame.sandbox = ''; "
        "frame.src = '%s'; "
        "document.body.appendChild(frame);",
        data_url.spec().c_str());
    EXPECT_TRUE(ExecJs(child1, js_str));
    ASSERT_TRUE(WaitForLoadStop(web_contents()));
  }
  FrameTreeNode* data = child1->child_at(0);
  scoped_refptr<SiteInstanceImpl> data_instance =
      data->current_frame_host()->GetSiteInstance();

  // The last committed URL is the data: URL, but because sandboxed data frames
  // are not handled, the data SiteInstance is in the `child0` instance.
  // TODO(crbug.com/40269084): After sandboxed data: subframes are supported,
  // update expectation for `data_instance` to have a separate SiteInstance in
  // the `child0` group with a data: site URL.
  EXPECT_TRUE(data->current_frame_host()->GetLastCommittedURL().SchemeIs(
      url::kDataScheme));
  EXPECT_EQ(data_instance, child0_instance);
  EXPECT_NE(data_instance, child1_instance);
  EXPECT_EQ(data_instance->group(), child0_instance->group());
  EXPECT_NE(data_instance->group(), child1_instance->group());
}

// When an unsandboxed main frame A opens a data: subframe in a sandboxed
// iframe, e.g. A(data_sandboxed), the data subframe is currently in a sandboxed
// version of A's SiteInstance and SiteInstanceGroup because sandboxed data:
// URL subframes are not supported in SiteInstanceGroup.
// TODO(crbug.com/390452841): Add SiteInstanceGroup support for sandboxed data:
// subframes.
IN_PROC_BROWSER_TEST_P(DataURLSiteInstanceGroupTest,
                       MainFrameWithSandboxedSubframe) {
  GURL url(embedded_test_server()->GetURL("a.com", "/title1.html"));
  EXPECT_TRUE(NavigateToURL(shell(), url));
  {
    // Create a sandboxed data: subframe.
    GURL data_url("data:text/html,test");
    std::string js_str = base::StringPrintf(
        "var frame = document.createElement('iframe'); "
        "frame.id = 'test_frame_sandboxed'; "
        "frame.sandbox = ''; "
        "frame.src = '%s'; "
        "document.body.appendChild(frame);",
        data_url.spec().c_str());
    EXPECT_TRUE(ExecJs(shell(), js_str));
    ASSERT_TRUE(WaitForLoadStop(web_contents()));
  }

  scoped_refptr<SiteInstanceImpl> main_instance =
      main_frame_host()->GetSiteInstance();
  FrameTreeNode* data = main_frame()->child_at(0);
  scoped_refptr<SiteInstanceImpl> data_instance =
      data->current_frame_host()->GetSiteInstance();
  EXPECT_NE(main_instance, data_instance);

  // Sandboxed data: subframes are not currently supported. This means the data:
  // subframe will be in an A_sandbox SiteInstance.
  // TODO(crbug.com/390452841): When sandboxed data: subframes are supported,
  // `data_instance` should be in a SiteInstance with a data:nonce site URL, in
  // a SiteInstanceGroup with nothing else in it.
  EXPECT_TRUE(data->current_frame_host()->GetLastCommittedURL().SchemeIs(
      url::kDataScheme));
  EXPECT_FALSE(data_instance->GetSiteURL().SchemeIs(url::kDataScheme));
  EXPECT_TRUE(data_instance->IsSandboxed());

  RenderProcessHost* main_process = main_instance->GetProcess();
  RenderProcessHost* data_process = data_instance->GetProcess();
  EXPECT_NE(main_process, data_process);

  // The data: subframe's process lock should be a sandboxed version of main
  // frame A's process lock.
  ProcessLock main_process_lock = main_process->GetProcessLock();
  ProcessLock data_process_lock = data_process->GetProcessLock();
  EXPECT_NE(main_process_lock, data_process_lock);
  EXPECT_FALSE(main_process_lock.is_sandboxed());
  EXPECT_TRUE(data_process_lock.is_sandboxed());

  // Compare hosts here, because `main_process_lock` is for the site "a.com" and
  // `data_process_lock` is for the origin "a.com:port", since it's sandboxed.
  EXPECT_EQ(main_process_lock.lock_url().host(),
            data_process_lock.lock_url().host());
}

// Test where a main frame has multiple data: URL subframes. This tests that
// the data: URL subframes each have their own SiteInstance, that the
// SiteInstances are in the same SiteInstanceGroup, and that a SiteInstanceGroup
// can have multiple data: URL SiteInstances.
IN_PROC_BROWSER_TEST_P(DataURLSiteInstanceGroupTest, ParentNavigatesSubframes) {
  GURL url(embedded_test_server()->GetURL("a.com", "/title1.html"));
  EXPECT_TRUE(NavigateToURL(shell(), url));

  std::string data_url1_str("data:text/html,test1");
  std::string data_url2_str("data:text/html,test2");
  {
    std::string js_str = base::StringPrintf(
        "for (let i = 0; i < 2; i++) { "
        "  var frame = document.createElement('iframe'); "
        "  frame.id = 'test_frame'; "
        "  frame.src = 'data:text/html,' + i; "
        "  document.body.appendChild(frame);"
        "}");
    EXPECT_TRUE(ExecJs(shell(), js_str));
    ASSERT_TRUE(WaitForLoadStop(shell()->web_contents()));
  }

  FrameTreeNode* child0 = main_frame()->child_at(0);
  scoped_refptr<SiteInstanceImpl> child0_instance =
      child0->current_frame_host()->GetSiteInstance();
  FrameTreeNode* child1 = main_frame()->child_at(1);
  scoped_refptr<SiteInstanceImpl> child1_instance =
      child1->current_frame_host()->GetSiteInstance();

  if (ShouldCreateSiteInstanceForDataUrls()) {
    EXPECT_NE(child0_instance, child1_instance);
  } else {
    EXPECT_EQ(child0_instance, child1_instance);
  }
  EXPECT_EQ(child0_instance->group(), child1_instance->group());
  EXPECT_EQ(main_frame_host()->GetSiteInstance()->group(),
            child0_instance->group());
}

// The same as DataURLSiteInstanceGroupTest, but with kSitePerProcess disabled.
class DataURLSiteInstanceGroupTestWithoutSiteIsolation
    : public DataURLSiteInstanceGroupTest {
 public:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    command_line->RemoveSwitch(switches::kSitePerProcess);
    command_line->AppendSwitch(switches::kDisableSiteIsolation);
  }
};

// Ensure that when SiteIsolation is disabled, a data: subframe in an isolated
// main frame can create and navigate a subframe successfully. E.g.
// A_isolated(data(B)). This is a regression test for crbug.com/379385125.
IN_PROC_BROWSER_TEST_P(DataURLSiteInstanceGroupTestWithoutSiteIsolation,
                       DataSubframeOpensSubframeAndNavigates) {
  // Explicitly isolate a.com since SiteIsolation is disabled.
  IsolateOriginsForTesting(embedded_test_server(), web_contents(), {"a.com"});

  GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
  EXPECT_TRUE(NavigateToURL(shell(), url_a));

  // Add a data: URL subframe.
  {
    TestNavigationObserver observer(web_contents());
    GURL data_url("data:text/html,test");
    std::string js_str = base::StringPrintf(
        "var frame = document.createElement('iframe'); "
        "frame.id = 'data_frame'; "
        "frame.src = '%s'; "
        "document.body.appendChild(frame);",
        data_url.spec().c_str());
    EXPECT_TRUE(ExecJs(shell(), js_str));
    observer.Wait();
    EXPECT_TRUE(observer.last_navigation_succeeded());
    ASSERT_TRUE(WaitForLoadStop(web_contents()));
    EXPECT_EQ(data_url, observer.last_navigation_url());
  }
  scoped_refptr<SiteInstanceImpl> a_instance =
      main_frame_host()->GetSiteInstance();

  FrameTreeNode* data_frame = main_frame()->child_at(0);
  scoped_refptr<SiteInstanceImpl> data_instance =
      data_frame->current_frame_host()->GetSiteInstance();

  if (ShouldCreateSiteInstanceForDataUrls()) {
    EXPECT_NE(a_instance, data_instance);
  } else {
    EXPECT_EQ(a_instance, data_instance);
  }
  EXPECT_EQ(a_instance->group(), data_instance->group());

  // Add a subframe of the data: URL, and navigate it to b.com.
  GURL url_b(embedded_test_server()->GetURL("b.com", "/title1.html"));
  {
    TestNavigationObserver observer(web_contents());
    std::string js_str = base::StringPrintf(
        "var frame = document.createElement('iframe'); "
        "frame.id = 'child_frame_of_data'; "
        "frame.src = '%s'; "
        "document.body.appendChild(frame);",
        url_b.spec().c_str());
    EXPECT_TRUE(ExecJs(data_frame, js_str));
    observer.Wait();
    EXPECT_TRUE(observer.last_navigation_succeeded());
    EXPECT_EQ(url_b, observer.last_navigation_url());
  }

  FrameTreeNode* b_frame = data_frame->child_at(0);
  scoped_refptr<SiteInstanceImpl> b_instance =
      b_frame->current_frame_host()->GetSiteInstance();
  EXPECT_NE(data_instance, b_instance);
  EXPECT_NE(data_instance->group(), b_instance->group());
  EXPECT_NE(data_instance->GetProcess(), b_instance->GetProcess());
}

// Ensure that when default SiteInstances and SiteInstanceGroups for data: URLs
// are enabled, data: URL subframes are in a separate SiteInstance, but share a
// process with the default SiteInstance.
IN_PROC_BROWSER_TEST_P(DataURLSiteInstanceGroupTestWithoutSiteIsolation,
                       DataSubframeInDefaultSiteInstance) {
  GURL url_a(embedded_test_server()->GetURL("a.com", "/title1.html"));
  EXPECT_TRUE(NavigateToURL(shell(), url_a));

  // Add a data: URL subframe.
  {
    TestNavigationObserver observer(web_contents());
    GURL data_url("data:text/html,test");
    std::string js_str = base::StringPrintf(
        "var frame = document.createElement('iframe'); "
        "frame.id = 'data_frame'; "
        "frame.src = '%s'; "
        "document.body.appendChild(frame);",
        data_url.spec().c_str());
    EXPECT_TRUE(ExecJs(shell(), js_str));
    observer.Wait();
    EXPECT_TRUE(observer.last_navigation_succeeded());
    ASSERT_TRUE(WaitForLoadStop(web_contents()));
    EXPECT_EQ(data_url, observer.last_navigation_url());
  }

  scoped_refptr<SiteInstanceImpl> a_instance =
      main_frame_host()->GetSiteInstance();
  if (ShouldUseDefaultSiteInstanceGroup()) {
    EXPECT_EQ(a_instance->group(),
              a_instance->DefaultSiteInstanceGroupForBrowsingInstance());
  } else {
    EXPECT_TRUE(a_instance->IsDefaultSiteInstance());
  }

  FrameTreeNode* data_frame = main_frame()->child_at(0);
  scoped_refptr<SiteInstanceImpl> data_instance =
      data_frame->current_frame_host()->GetSiteInstance();

  if (ShouldCreateSiteInstanceForDataUrls()) {
    EXPECT_NE(a_instance, data_instance);
    EXPECT_FALSE(data_instance->IsDefaultSiteInstance());
  } else {
    EXPECT_EQ(a_instance, data_instance);
  }

  // In both cases, A and data: should share a group. If the feature is enabled,
  // data: should have its own SiteInstance, but should still be in the default
  // group as it does not need further isolating. If the feature is disabled,
  // both should be in the default SiteInstance.
  EXPECT_EQ(a_instance->group(), data_instance->group());
}

// Tests for the default SiteInstanceGroup behaviour. This is parameterized to
// also run in the legacy mode which puts unisolated sites in the default
// SiteInstance.
class DefaultSiteInstanceGroupTest
    : public ContentBrowserTestBase,
      public ::testing::WithParamInterface<bool> {
 public:
  void SetUpCommandLine(base::CommandLine* command_line) override {
    // This feature only takes effect when there is no full site isolation.
    command_line->RemoveSwitch(switches::kSitePerProcess);
    command_line->AppendSwitch(switches::kDisableSiteIsolation);

    if (IsDefaultSiteInstanceGroupEnabled()) {
      feature_list_.InitAndEnableFeature(features::kDefaultSiteInstanceGroups);
    } else {
      feature_list_.InitAndDisableFeature(features::kDefaultSiteInstanceGroups);
    }
  }

  static std::string DescribeParams(
      const testing::TestParamInfo<ParamType>& info) {
    return info.param ? "UseDefaultSiteInstanceGroups"
                      : "UseDefaultSiteInstances";
  }

 protected:
  bool IsDefaultSiteInstanceGroupEnabled() const { return GetParam(); }

 private:
  base::test::ScopedFeatureList feature_list_;
};

// Basic use case, where multiple unisolated sites are put in the default
// SiteInstanceGroup, but have their own SiteInstances.
IN_PROC_BROWSER_TEST_P(DefaultSiteInstanceGroupTest,
                       DefaultSiteInstanceGroupProperties) {
  // Navigate to a non-isolated site.
  GURL url_c(embedded_test_server()->GetURL("c.com", "/title1.html"));
  EXPECT_TRUE(NavigateToURL(shell(), url_c));

  scoped_refptr<SiteInstanceImpl> c_instance =
      main_frame_host()->GetSiteInstance();
  EXPECT_FALSE(c_instance->RequiresDedicatedProcess());
  EXPECT_TRUE(c_instance->GetProcess()->GetProcessLock().allows_any_site());
  if (ShouldUseDefaultSiteInstanceGroup()) {
    EXPECT_EQ(c_instance->group(),
              c_instance->DefaultSiteInstanceGroupForBrowsingInstance());
  } else {
    EXPECT_TRUE(c_instance->IsDefaultSiteInstance());
  }

  // Navigate to a site with a cross-site iframe, both of which are not
  // isolated.
  GURL url(embedded_test_server()->GetURL(
      "a.com", "/cross_site_iframe_factory.html?a(b)"));
  EXPECT_TRUE(NavigateToURL(shell(), url));

  scoped_refptr<SiteInstanceImpl> a_instance =
      main_frame_host()->GetSiteInstance();
  scoped_refptr<SiteInstanceImpl> b_instance = main_frame()
                                                   ->child_at(0)
                                                   ->render_manager()
                                                   ->current_frame_host()
                                                   ->GetSiteInstance();
  EXPECT_TRUE(a_instance->GetProcess()->GetProcessLock().allows_any_site());
  EXPECT_FALSE(a_instance->RequiresDedicatedProcess());
  EXPECT_FALSE(b_instance->RequiresDedicatedProcess());
  if (ShouldUseDefaultSiteInstanceGroup()) {
    // A, B and C should all have their own SiteInstances, but all share the
    // default SiteInstanceGroup.
    EXPECT_NE(a_instance, b_instance);
    EXPECT_NE(a_instance, c_instance);
    EXPECT_NE(b_instance, c_instance);
    EXPECT_EQ(a_instance->group(),
              a_instance->DefaultSiteInstanceGroupForBrowsingInstance());
    EXPECT_EQ(a_instance->group(), b_instance->group());
  } else {
    EXPECT_TRUE(a_instance->IsDefaultSiteInstance());
    EXPECT_EQ(a_instance, b_instance);
  }

  // Navigate to an isolated site that does not use the default
  // SiteInstance/Group.
  IsolateOriginsForTesting(embedded_test_server(), web_contents(), {"d.com"});
  GURL url_d(embedded_test_server()->GetURL("d.com", "/title1.html"));
  EXPECT_TRUE(NavigateToURL(shell(), url_d));

  scoped_refptr<SiteInstanceImpl> d_instance =
      main_frame_host()->GetSiteInstance();
  EXPECT_TRUE(d_instance->RequiresDedicatedProcess());
  if (ShouldUseDefaultSiteInstanceGroup()) {
    EXPECT_NE(d_instance->group(),
              d_instance->DefaultSiteInstanceGroupForBrowsingInstance());
    EXPECT_FALSE(d_instance->DefaultSiteInstanceGroupForBrowsingInstance());
  } else {
    EXPECT_FALSE(d_instance->IsDefaultSiteInstance());
  }
}

IN_PROC_BROWSER_TEST_P(DefaultSiteInstanceGroupTest,
                       ProcessHasAllowsAnySiteLock) {
  // Test starts with a tab that has an unassigned SiteInstance. The associated
  // SiteInstance should not yet have a site set. The process is locked with an
  // allow-any-site lock.
  scoped_refptr<SiteInstanceImpl> start_instance =
      main_frame_host()->GetSiteInstance();
  EXPECT_FALSE(start_instance->HasSite());
  EXPECT_TRUE(start_instance->HasProcess());
  RenderProcessHost* start_process = start_instance->GetProcess();
  EXPECT_FALSE(start_process->GetProcessLock().is_locked_to_site());
  EXPECT_FALSE(start_process->GetProcessLock().is_invalid());
  EXPECT_EQ(start_process->GetProcessLock().lock_url(), GURL());
  if (ShouldUseDefaultSiteInstanceGroup()) {
    EXPECT_NE(start_instance->group(),
              start_instance->DefaultSiteInstanceGroupForBrowsingInstance());
  } else {
    EXPECT_FALSE(start_instance->IsDefaultSiteInstance());
  }

  // Do a browser-initiated navigation to about:blank. Because it's an
  // about:blank navigation without an initiator, it should stay in the previous
  // SiteInstance which hasn't had a site set yet, and not 'use up' a
  // SiteInstance.
  EXPECT_TRUE(NavigateToURL(shell(), GURL("about:blank")));
  scoped_refptr<SiteInstanceImpl> blank_instance =
      main_frame_host()->GetSiteInstance();
  EXPECT_FALSE(start_instance->HasSite());
  RenderProcessHost* blank_process = blank_instance->GetProcess();
  EXPECT_EQ(start_process, blank_process);
  EXPECT_EQ(start_instance, blank_instance);
  EXPECT_FALSE(blank_process->GetProcessLock().is_locked_to_site());
  EXPECT_FALSE(blank_process->GetProcessLock().is_invalid());
  EXPECT_EQ(blank_process->GetProcessLock().lock_url().spec(), "");
  if (ShouldUseDefaultSiteInstanceGroup()) {
    EXPECT_NE(start_instance->group(),
              start_instance->DefaultSiteInstanceGroupForBrowsingInstance());
  } else {
    EXPECT_FALSE(start_instance->IsDefaultSiteInstance());
  }

  // Now navigate to a 'real' site. Since the previous SiteInstance still did
  // not have a site set, that will now be set as foo.com. With default
  // SiteInstance/Group, all navigations should remain in the same process.
  GURL foo_url(embedded_test_server()->GetURL("foo.com", "/title1.html"));
  EXPECT_TRUE(NavigateToURL(shell(), foo_url));
  scoped_refptr<SiteInstanceImpl> foo_instance =
      main_frame_host()->GetSiteInstance();
  EXPECT_TRUE(foo_instance->HasSite());
  RenderProcessHost* foo_process = foo_instance->GetProcess();
  EXPECT_TRUE(foo_process->GetProcessLock().allows_any_site());
  EXPECT_FALSE(blank_process->GetProcessLock().is_invalid());
  EXPECT_EQ(foo_process->GetProcessLock().lock_url().spec(), "");
  EXPECT_EQ(foo_instance, start_instance);
  if (ShouldUseDefaultSiteInstanceGroup()) {
    EXPECT_EQ(foo_instance->group(),
              foo_instance->DefaultSiteInstanceGroupForBrowsingInstance());
  } else {
    EXPECT_TRUE(foo_instance->IsDefaultSiteInstance());
  }
}

INSTANTIATE_TEST_SUITE_P(All,
                         DataURLSiteInstanceGroupTest,
                         ::testing::Bool(),
                         &DataURLSiteInstanceGroupTest::DescribeParams);
INSTANTIATE_TEST_SUITE_P(All,
                         DataURLSiteInstanceGroupTestWithoutSiteIsolation,
                         ::testing::Bool(),
                         &DataURLSiteInstanceGroupTest::DescribeParams);
INSTANTIATE_TEST_SUITE_P(All,
                         DefaultSiteInstanceGroupTest,
                         ::testing::Bool(),
                         &DefaultSiteInstanceGroupTest::DescribeParams);
}  // namespace content