File: extension_action_runner_browsertest.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (679 lines) | stat: -rw-r--r-- 28,095 bytes parent folder | download | duplicates (5)
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
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/extension_action_runner.h"

#include <stddef.h>

#include <memory>
#include <utility>
#include <vector>

#include "base/files/file_path.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/test/scoped_feature_list.h"
#include "chrome/browser/extensions/blocked_action_waiter.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/extensions/permissions/active_tab_permission_granter.h"
#include "chrome/browser/extensions/permissions/scripting_permissions_modifier.h"
#include "chrome/browser/extensions/permissions/site_permissions_helper.h"
#include "chrome/browser/extensions/tab_helper.h"
#include "chrome/browser/profiles/profile.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/fenced_frame_test_util.h"
#include "extensions/browser/browsertest_util.h"
#include "extensions/browser/extension_action.h"
#include "extensions/browser/permissions_manager.h"
#include "extensions/buildflags/buildflags.h"
#include "extensions/common/extension_features.h"
#include "extensions/test/extension_test_message_listener.h"
#include "extensions/test/permissions_manager_waiter.h"
#include "extensions/test/test_extension_dir.h"
#include "net/dns/mock_host_resolver.h"
#include "testing/gtest/include/gtest/gtest.h"

static_assert(BUILDFLAG(ENABLE_EXTENSIONS_CORE));

namespace extensions {

namespace {

using UserSiteAccess = PermissionsManager::UserSiteAccess;
using UserSiteSetting = PermissionsManager::UserSiteSetting;

constexpr char kAllHostsScheme[] = "*://*/*";
constexpr char kExplicitHostsScheme[] = "http://127.0.0.1/*";
constexpr char kBackgroundScript[] =
    R"("background": {"scripts": ["script.js"], "persistent": true})";

constexpr char kBackgroundScriptSource[] =
    R"(var listener = function(tabId) {
         chrome.tabs.onUpdated.removeListener(listener);
         chrome.tabs.executeScript(tabId, {
           code: "chrome.test.sendMessage('inject succeeded');"
         });
       };
       chrome.tabs.onUpdated.addListener(listener);
       chrome.test.sendMessage('ready');)";

constexpr char kContentScriptSource[] =
    "chrome.test.sendMessage('inject succeeded');";

constexpr char kInjectSucceeded[] = "inject succeeded";

enum InjectionType { CONTENT_SCRIPT, EXECUTE_SCRIPT };

enum HostType { ALL_HOSTS, EXPLICIT_HOSTS };

enum RequiresConsent { REQUIRES_CONSENT, DOES_NOT_REQUIRE_CONSENT };

enum WithholdPermissions { WITHHOLD_PERMISSIONS, DONT_WITHHOLD_PERMISSIONS };

// Runs all pending tasks in the renderer associated with |web_contents|.
// Returns true on success.
bool RunAllPendingInRenderer(content::WebContents* web_contents) {
  // This is slight hack to achieve a RunPendingInRenderer() method. Since IPCs
  // are sent synchronously, anything started prior to this method will finish
  // before this method returns (as content::ExecJs() is synchronous).
  return content::ExecJs(web_contents, "1 == 1;");
}

// Returns whether the extension injected a script by checking the document
// title. This assumes the use of test extension
// 'extensions/blocked_actions/content_scripts' for this check to work as
// expected.
bool DidInjectScript(content::WebContents& web_contents) {
  return browsertest_util::DidChangeTitle(web_contents,
                                          /*original_title=*/u"OK",
                                          /*changed_title=*/u"success");
}

}  // namespace

using ContextType = extensions::browser_test_util::ContextType;

// TODO(crbug.com/393179880): Port to desktop Android.
class ExtensionActionRunnerBrowserTest : public ExtensionBrowserTest {
 public:
  explicit ExtensionActionRunnerBrowserTest(
      ContextType context_type = ContextType::kNone)
      : ExtensionBrowserTest(context_type) {}

  void TearDownOnMainThread() override;

  // Returns an extension with the given |host_type| and |injection_type|. If
  // one already exists, the existing extension will be returned. Othewrwise,
  // one will be created.
  // This could potentially return NULL if LoadExtension() fails.
  const Extension* CreateExtension(HostType host_type,
                                   InjectionType injection_type,
                                   WithholdPermissions withhold_permissions);

  void RunActiveScriptsTest(const std::string& name,
                            HostType host_type,
                            InjectionType injection_type,
                            WithholdPermissions withhold_permissions,
                            RequiresConsent requires_consent);

 private:
  std::vector<TestExtensionDir> test_extension_dirs_;
  std::vector<raw_ptr<const Extension, VectorExperimental>> extensions_;
};

void ExtensionActionRunnerBrowserTest::TearDownOnMainThread() {
  test_extension_dirs_.clear();
}

const Extension* ExtensionActionRunnerBrowserTest::CreateExtension(
    HostType host_type,
    InjectionType injection_type,
    WithholdPermissions withhold_permissions) {
  std::string name = base::StringPrintf(
      "%s %s",
      injection_type == CONTENT_SCRIPT ? "content_script" : "execute_script",
      host_type == ALL_HOSTS ? "all_hosts" : "explicit_hosts");

  const char* const permission_scheme =
      host_type == ALL_HOSTS ? kAllHostsScheme : kExplicitHostsScheme;

  std::string permissions =
      base::StringPrintf(R"("permissions": ["tabs", "%s"])", permission_scheme);

  std::string scripts;
  std::string script_source;
  if (injection_type == CONTENT_SCRIPT) {
    scripts = base::StringPrintf(
        R"("content_scripts": [{
              "matches": ["%s"],
              "js": ["script.js"],
              "run_at": "document_end"
           }])",
        permission_scheme);
  } else {
    scripts = kBackgroundScript;
  }

  std::string manifest = base::StringPrintf(
      R"({
           "name": "%s",
           "version": "1.0",
           "manifest_version": 2,
           %s,
           %s
         })",
      name.c_str(), permissions.c_str(), scripts.c_str());

  TestExtensionDir dir;
  dir.WriteManifest(manifest);
#if BUILDFLAG(ENABLE_DESKTOP_ANDROID_EXTENSIONS)
  // TODO(crbug.com/371432155): kBackgroundScriptSource uses chrome.tabs, which
  // isn't supported yet.
  CHECK_EQ(injection_type, CONTENT_SCRIPT);
#endif
  dir.WriteFile(FILE_PATH_LITERAL("script.js"), injection_type == CONTENT_SCRIPT
                                                    ? kContentScriptSource
                                                    : kBackgroundScriptSource);

  const Extension* extension = nullptr;
  if (injection_type == CONTENT_SCRIPT) {
    extension = LoadExtension(dir.UnpackedPath());
  } else {
    ExtensionTestMessageListener listener("ready");
    extension = LoadExtension(dir.UnpackedPath());
    EXPECT_TRUE(listener.WaitUntilSatisfied());
  }

  if (extension) {
    test_extension_dirs_.push_back(std::move(dir));
    extensions_.push_back(extension);

    if (withhold_permissions == WITHHOLD_PERMISSIONS &&
        PermissionsManager::Get(profile())->CanAffectExtension(*extension)) {
      ScriptingPermissionsModifier(profile(), extension)
          .SetWithholdHostPermissions(true);
    }
  }

  // If extension is NULL here, it will be caught later in the test.
  return extension;
}

void ExtensionActionRunnerBrowserTest::RunActiveScriptsTest(
    const std::string& name,
    HostType host_type,
    InjectionType injection_type,
    WithholdPermissions withhold_permissions,
    RequiresConsent requires_consent) {
  ASSERT_TRUE(embedded_test_server()->Start());

  const Extension* extension =
      CreateExtension(host_type, injection_type, withhold_permissions);
  ASSERT_TRUE(extension);

  content::WebContents* web_contents = GetActiveWebContents();
  ASSERT_TRUE(web_contents);

  ExtensionActionRunner* runner =
      ExtensionActionRunner::GetForWebContents(web_contents);
  ASSERT_TRUE(runner);

  ExtensionTestMessageListener inject_success_listener(kInjectSucceeded);
  auto navigate = [this]() {
    // Navigate to an URL (which matches the explicit host specified in the
    // extension content_scripts_explicit_hosts). All extensions should
    // inject the script.
    ASSERT_TRUE(NavigateToURL(
        embedded_test_server()->GetURL("/extensions/test_file.html")));
  };

  if (requires_consent == DOES_NOT_REQUIRE_CONSENT) {
    // If the extension doesn't require explicit consent, it should inject
    // automatically right away.
    navigate();
    EXPECT_FALSE(runner->WantsToRun(extension));
    EXPECT_TRUE(inject_success_listener.WaitUntilSatisfied());
    EXPECT_FALSE(runner->WantsToRun(extension));
    return;
  }

  ASSERT_EQ(REQUIRES_CONSENT, requires_consent);

  BlockedActionWaiter waiter(runner);
  navigate();
  waiter.Wait();
  EXPECT_TRUE(runner->WantsToRun(extension));
  EXPECT_FALSE(inject_success_listener.was_satisfied());

  // Grant permission by clicking on the extension action.
  runner->RunAction(extension, true);

  // Now, the extension should be able to inject the script.
  EXPECT_TRUE(inject_success_listener.WaitUntilSatisfied());

  // The extension should no longer want to run.
  EXPECT_FALSE(runner->WantsToRun(extension));
}

#if BUILDFLAG(ENABLE_EXTENSIONS)
// TODO(crbug.com/371432155): Port to desktop Android when the chrome.tabs API
// is supported. chrome.tabs is used by the EXECUTE_SCRIPT test extension.
class ExtensionActionRunnerBrowserTestWithContextType
    : public ExtensionActionRunnerBrowserTest,
      public testing::WithParamInterface<ContextType> {
 public:
  ExtensionActionRunnerBrowserTestWithContextType()
      : ExtensionActionRunnerBrowserTest(GetParam()) {}

  ExtensionActionRunnerBrowserTestWithContextType(
      const ExtensionActionRunnerBrowserTestWithContextType&) = delete;
  ExtensionActionRunnerBrowserTestWithContextType& operator=(
      const ExtensionActionRunnerBrowserTestWithContextType&) = delete;
};

INSTANTIATE_TEST_SUITE_P(PersistentBackground,
                         ExtensionActionRunnerBrowserTestWithContextType,
                         ::testing::Values(ContextType::kPersistentBackground));
// These tests use chrome.tabs.executeScript, which is not available in MV3 and
// above. See crbug.com/332328868.
INSTANTIATE_TEST_SUITE_P(ServiceWorker,
                         ExtensionActionRunnerBrowserTestWithContextType,
                         ::testing::Values(ContextType::kServiceWorkerMV2));

// Load up different combinations of extensions, and verify that script
// injection is properly withheld and indicated to the user.
// NOTE: Though these could be parameterized test cases, there's enough
// bits here that just having a helper method is quite a bit more readable.
IN_PROC_BROWSER_TEST_P(
    ExtensionActionRunnerBrowserTestWithContextType,
    ActiveScriptsAreDisplayedAndDelayExecution_ExecuteScripts_AllHosts) {
  RunActiveScriptsTest("execute_scripts_all_hosts", ALL_HOSTS, EXECUTE_SCRIPT,
                       WITHHOLD_PERMISSIONS, REQUIRES_CONSENT);
}
IN_PROC_BROWSER_TEST_P(
    ExtensionActionRunnerBrowserTestWithContextType,
    ActiveScriptsAreDisplayedAndDelayExecution_ExecuteScripts_ExplicitHosts) {
  RunActiveScriptsTest("execute_scripts_explicit_hosts", EXPLICIT_HOSTS,
                       EXECUTE_SCRIPT, WITHHOLD_PERMISSIONS, REQUIRES_CONSENT);
}
#endif  // BUILDFLAG(ENABLE_EXTENSIONS)

IN_PROC_BROWSER_TEST_F(
    ExtensionActionRunnerBrowserTest,
    ActiveScriptsAreDisplayedAndDelayExecution_ContentScripts_AllHosts) {
  RunActiveScriptsTest("content_scripts_all_hosts", ALL_HOSTS, CONTENT_SCRIPT,
                       WITHHOLD_PERMISSIONS, REQUIRES_CONSENT);
}
IN_PROC_BROWSER_TEST_F(
    ExtensionActionRunnerBrowserTest,
    ActiveScriptsAreDisplayedAndDelayExecution_ContentScripts_ExplicitHosts) {
  RunActiveScriptsTest("content_scripts_explicit_hosts", EXPLICIT_HOSTS,
                       CONTENT_SCRIPT, WITHHOLD_PERMISSIONS, REQUIRES_CONSENT);
}

// Test that removing an extension with pending injections a) removes the
// pending injections for that extension, and b) does not affect pending
// injections for other extensions.
IN_PROC_BROWSER_TEST_F(ExtensionActionRunnerBrowserTest,
                       RemoveExtensionWithPendingInjections) {
  // Load up two extensions, each with content scripts.
  scoped_refptr<const Extension> extension1 =
      CreateExtension(ALL_HOSTS, CONTENT_SCRIPT, WITHHOLD_PERMISSIONS);
  ASSERT_TRUE(extension1);
  scoped_refptr<const Extension> extension2 =
      CreateExtension(ALL_HOSTS, CONTENT_SCRIPT, WITHHOLD_PERMISSIONS);
  ASSERT_TRUE(extension2);

  ASSERT_NE(extension1->id(), extension2->id());

  content::WebContents* web_contents = GetActiveWebContents();
  ASSERT_TRUE(web_contents);
  ExtensionActionRunner* action_runner =
      ExtensionActionRunner::GetForWebContents(web_contents);
  ASSERT_TRUE(action_runner);

  ASSERT_TRUE(embedded_test_server()->Start());
  ASSERT_TRUE(NavigateToURL(
      embedded_test_server()->GetURL("/extensions/test_file.html")));

  // Both extensions should have pending requests.
  EXPECT_TRUE(action_runner->WantsToRun(extension1.get()));
  EXPECT_TRUE(action_runner->WantsToRun(extension2.get()));

  // Unload one of the extensions.
  UnloadExtension(extension2->id());

  EXPECT_TRUE(RunAllPendingInRenderer(web_contents));

  // We should have pending requests for extension1, but not the removed
  // extension2.
  EXPECT_TRUE(action_runner->WantsToRun(extension1.get()));
  EXPECT_FALSE(action_runner->WantsToRun(extension2.get()));

  // We should still be able to run the request for extension1.
  ExtensionTestMessageListener inject_success_listener(kInjectSucceeded);
  inject_success_listener.set_extension_id(extension1->id());
  action_runner->RunAction(extension1.get(), true);
  EXPECT_TRUE(inject_success_listener.WaitUntilSatisfied());
}

// Test that granting the extension all urls permission allows it to run on
// pages, and that the permission update is sent to existing renderers.
IN_PROC_BROWSER_TEST_F(ExtensionActionRunnerBrowserTest,
                       GrantExtensionAllUrlsPermission) {
  // Loadup an extension and navigate.
  const Extension* extension =
      CreateExtension(ALL_HOSTS, CONTENT_SCRIPT, WITHHOLD_PERMISSIONS);
  ASSERT_TRUE(extension);

  content::WebContents* web_contents = GetActiveWebContents();
  ASSERT_TRUE(web_contents);
  ExtensionActionRunner* action_runner =
      ExtensionActionRunner::GetForWebContents(web_contents);
  ASSERT_TRUE(action_runner);

  ExtensionTestMessageListener inject_success_listener(kInjectSucceeded);
  inject_success_listener.set_extension_id(extension->id());

  ASSERT_TRUE(embedded_test_server()->Start());
  GURL url = embedded_test_server()->GetURL("/extensions/test_file.html");
  ASSERT_TRUE(NavigateToURL(url));

  // The extension shouldn't be allowed to run.
  EXPECT_TRUE(action_runner->WantsToRun(extension));
  EXPECT_EQ(1, action_runner->num_page_requests());
  EXPECT_FALSE(inject_success_listener.was_satisfied());

  // Enable the extension to run on all urls.
  ScriptingPermissionsModifier modifier(profile(), extension);
  modifier.SetWithholdHostPermissions(false);
  EXPECT_TRUE(RunAllPendingInRenderer(web_contents));

  // Navigate again - this time, the extension should execute immediately (and
  // should not need to ask the script controller for permission).
  ASSERT_TRUE(NavigateToURL(url));
  EXPECT_FALSE(action_runner->WantsToRun(extension));
  EXPECT_EQ(0, action_runner->num_page_requests());
  EXPECT_TRUE(inject_success_listener.WaitUntilSatisfied());

  // Revoke all urls permissions.
  inject_success_listener.Reset();
  modifier.SetWithholdHostPermissions(true);
  EXPECT_TRUE(RunAllPendingInRenderer(web_contents));

  // Re-navigate; the extension should again need permission to run.
  ASSERT_TRUE(NavigateToURL(url));
  EXPECT_TRUE(action_runner->WantsToRun(extension));
  EXPECT_EQ(1, action_runner->num_page_requests());
  EXPECT_FALSE(inject_success_listener.was_satisfied());
}

class ExtensionActionRunnerRunActionBubbleBrowserTest
    : public ExtensionActionRunnerBrowserTest,
      public testing::WithParamInterface<bool> {};

INSTANTIATE_TEST_SUITE_P(
    ,
    ExtensionActionRunnerRunActionBubbleBrowserTest,
    testing::Bool(),  // Accept reload bubble.
    [](const testing::TestParamInfo<
        ExtensionActionRunnerRunActionBubbleBrowserTest::ParamType>& info) {
      return info.param ? "AcceptReload" : "DismissReload";
    });

// TODO(crbug.com/40875193): Test an extension that can be granted tab
// permission but without a reload. And also running an action without granting
// tab permission.

// Tests that when running an action and accepting the reload bubble blocked
// actions are run (script injects), but when the user dismissed the bubble
// blocked actions are not run.
IN_PROC_BROWSER_TEST_P(ExtensionActionRunnerRunActionBubbleBrowserTest,
                       RunAction) {
  // Load an extension that wants to run on every page at document start, and
  // load a test page.
  ASSERT_TRUE(embedded_test_server()->Start());
  const Extension* extension = LoadExtension(
      test_data_dir_.AppendASCII("blocked_actions/content_scripts"));
  ASSERT_TRUE(extension);
  ScriptingPermissionsModifier(profile(), extension)
      .SetWithholdHostPermissions(true);

  // Navigate to a page where the extension wants to run.
  const GURL url = embedded_test_server()->GetURL("/simple.html");
  ASSERT_TRUE(NavigateToURL(url));
  content::WebContents* web_contents = GetActiveWebContents();
  EXPECT_TRUE(content::WaitForLoadStop(web_contents));
  content::NavigationController& web_controller = web_contents->GetController();
  const int nav_id = web_controller.GetLastCommittedEntry()->GetUniqueID();

  // The extension should want to run on the page, should not have
  // injected, should have user site access "on click", and page interaction
  // withheld.
  ExtensionActionRunner* runner =
      ExtensionActionRunner::GetForWebContents(web_contents);
  ASSERT_TRUE(runner);
  EXPECT_TRUE(runner->WantsToRun(extension));
  EXPECT_FALSE(DidInjectScript(*web_contents));
  auto* permissions = PermissionsManager::Get(profile());
  EXPECT_EQ(permissions->GetUserSiteAccess(*extension, url),
            UserSiteAccess::kOnClick);
  SitePermissionsHelper permissions_helper(profile());
  EXPECT_EQ(permissions_helper.GetSiteInteraction(*extension, web_contents),
            SitePermissionsHelper::SiteInteraction::kWithheld);

  const bool kAcceptReload = GetParam();
  // Run the action and (accept or dismiss) the reload bubble depending on
  // `kAcceptReload`.
  runner->accept_bubble_for_testing(kAcceptReload);
  runner->RunAction(extension, /*grant_tab_permissions=*/true);

  // Verify extension has granted site interaction (since it's immediately
  // granted when running an action, regardless of page refresh) and its user
  // site access is still on click (since running an action doesn't change the
  // site access the user selected).
  EXPECT_EQ(permissions_helper.GetSiteInteraction(*extension, web_contents),
            SitePermissionsHelper::SiteInteraction::kGranted);
  EXPECT_EQ(permissions->GetUserSiteAccess(*extension, url),
            UserSiteAccess::kOnClick);

  if (kAcceptReload) {
    base::RunLoop().RunUntilIdle();
    ASSERT_TRUE(content::WaitForLoadStop(web_contents));
    // Since we automatically accepted the bubble prompting us, the page should
    // have reloaded, the extension should have injected at document start, and
    // the site access should still be "on click".
    EXPECT_GE(web_controller.GetLastCommittedEntry()->GetUniqueID(), nav_id);
    EXPECT_TRUE(DidInjectScript(*web_contents));
    EXPECT_FALSE(runner->WantsToRun(extension));
  } else {
    // The script should not inject because it needs to run at start and we
    // haven't reloaded the page, and there should be blocked actions to run
    // since we haven't reloaded to run them.
    EXPECT_FALSE(DidInjectScript(*web_contents));
    EXPECT_TRUE(runner->WantsToRun(extension));
  }
}

// Tests that the blocked actions of an extension are run (e.g. scripts
// injected) when calling this method.
IN_PROC_BROWSER_TEST_F(ExtensionActionRunnerBrowserTest, RunBlockedActions) {
  // Load an extension that wants to run on every page at document idle, and
  // load a test page.
  ASSERT_TRUE(embedded_test_server()->Start());
  const Extension* extension = LoadExtension(
      test_data_dir_.AppendASCII("blocked_actions/content_script_at_idle"));
  ASSERT_TRUE(extension);
  ScriptingPermissionsModifier(profile(), extension)
      .SetWithholdHostPermissions(true);

  // Navigate to a page where the extension wants to run.
  const GURL url = embedded_test_server()->GetURL("/simple.html");
  ASSERT_TRUE(NavigateToURL(url));
  content::WebContents* web_contents = GetActiveWebContents();
  EXPECT_TRUE(content::WaitForLoadStop(web_contents));

  // The extension should want to run on the page at first.
  ExtensionActionRunner* runner =
      ExtensionActionRunner::GetForWebContents(web_contents);
  ASSERT_TRUE(runner);
  ASSERT_TRUE(runner->WantsToRun(extension));
  ExtensionTestMessageListener script_injection_listener("injection succeeded");

  // Confirm that running blocked actions clears out any blocked actions for the
  // extension.
  runner->RunBlockedActions(extension);
  SitePermissionsHelper permissions_helper(profile());
  EXPECT_EQ(permissions_helper.GetSiteInteraction(*extension, web_contents),
            SitePermissionsHelper::SiteInteraction::kGranted);
  EXPECT_FALSE(runner->WantsToRun(extension));
  EXPECT_TRUE(script_injection_listener.WaitUntilSatisfied());
  EXPECT_TRUE(DidInjectScript(*web_contents));
}

// If we don't withhold permissions, extensions should execute normally.
IN_PROC_BROWSER_TEST_F(ExtensionActionRunnerBrowserTest,
                       ScriptsExecuteWhenNoPermissionsWithheld_ContentScripts) {
  RunActiveScriptsTest("content_scripts_all_hosts", ALL_HOSTS, CONTENT_SCRIPT,
                       DONT_WITHHOLD_PERMISSIONS, DOES_NOT_REQUIRE_CONSENT);
}

#if BUILDFLAG(ENABLE_EXTENSIONS)
// TODO(crbug.com/371432155): Requires chrome.tabs API.
IN_PROC_BROWSER_TEST_F(ExtensionActionRunnerBrowserTest,
                       ScriptsExecuteWhenNoPermissionsWithheld_ExecuteScripts) {
  RunActiveScriptsTest("execute_scripts_all_hosts", ALL_HOSTS, EXECUTE_SCRIPT,
                       DONT_WITHHOLD_PERMISSIONS, DOES_NOT_REQUIRE_CONSENT);
}
#endif

class ExtensionActionRunnerFencedFrameBrowserTest
    : public ExtensionActionRunnerBrowserTest {
 public:
  ExtensionActionRunnerFencedFrameBrowserTest() = default;
  ~ExtensionActionRunnerFencedFrameBrowserTest() override = default;

  ExtensionActionRunnerFencedFrameBrowserTest(
      const ExtensionActionRunnerFencedFrameBrowserTest&) = delete;
  ExtensionActionRunnerFencedFrameBrowserTest& operator=(
      const ExtensionActionRunnerFencedFrameBrowserTest&) = delete;

  void SetUpOnMainThread() override {
    host_resolver()->AddRule("*", "127.0.0.1");
    ASSERT_TRUE(embedded_test_server()->Start());
    ExtensionActionRunnerBrowserTest::SetUpOnMainThread();
  }

 protected:
  content::test::FencedFrameTestHelper fenced_frame_helper_;
};

// Tests that a fenced frame doesn't clear active extensions.
IN_PROC_BROWSER_TEST_F(ExtensionActionRunnerFencedFrameBrowserTest,
                       FencedFrameDoesNotClearActiveExtensions) {
  // Set a situation that |granted_extensions_| of ActiveTabPermissionGranter is
  // not empty to test a fenced frame doesn't clear active extensions.
  const Extension* extension = LoadExtension(
      test_data_dir_.AppendASCII("blocked_actions/content_scripts"));
  ASSERT_TRUE(extension);
  ScriptingPermissionsModifier(profile(), extension)
      .SetWithholdHostPermissions(true);

  GURL initial_url = embedded_test_server()->GetURL("a.com", "/simple.html");
  ASSERT_TRUE(NavigateToURL(initial_url));
  content::WebContents* web_contents = GetActiveWebContents();

  ExtensionActionRunner* runner =
      ExtensionActionRunner::GetForWebContents(web_contents);
  ASSERT_TRUE(runner);

  runner->accept_bubble_for_testing(true);

  content::NavigationEntry* entry =
      web_contents->GetController().GetLastCommittedEntry();
  ASSERT_TRUE(entry);
  const int first_nav_id = entry->GetUniqueID();

  runner->RunAction(extension, true);
  base::RunLoop().RunUntilIdle();
  EXPECT_TRUE(content::WaitForLoadStop(web_contents));
  entry = web_contents->GetController().GetLastCommittedEntry();
  ASSERT_TRUE(entry);
  EXPECT_GE(entry->GetUniqueID(), first_nav_id);
  EXPECT_TRUE(DidInjectScript(*web_contents));
  EXPECT_FALSE(runner->WantsToRun(extension));

  ActiveTabPermissionGranter* active_tab_granter =
      ActiveTabPermissionGranter::FromWebContents(web_contents);
  ASSERT_TRUE(active_tab_granter);
  EXPECT_EQ(active_tab_granter->granted_extensions_.size(), 1U);

  // The origin of |url| and |fenced_frame_url| should be different because
  // ActiveTabPermissionGranter::DidFinishNavigation is only able to clear
  // active extensions when the origins are different.
  GURL fenced_frame_url =
      embedded_test_server()->GetURL("b.com", "/fenced_frames/title1.html");
  // Create a fenced frame and load the test url. Active extensions should not
  // be cleared by the fenced frame navigation.
  content::RenderFrameHost* fenced_frame_host =
      fenced_frame_helper_.CreateFencedFrame(
          web_contents->GetPrimaryMainFrame(), fenced_frame_url);
  ASSERT_TRUE(fenced_frame_host);
  EXPECT_EQ(active_tab_granter->granted_extensions_.size(), 1U);

  // Active extensions should be cleared after navigating a test url on the
  // primary main frame.
  GURL test_url = embedded_test_server()->GetURL("c.com", "/simple.html");
  ASSERT_TRUE(NavigateToURL(test_url));
  EXPECT_EQ(active_tab_granter->granted_extensions_.size(), 0U);
}

IN_PROC_BROWSER_TEST_F(ExtensionActionRunnerFencedFrameBrowserTest,
                       DoNotResetExtensionActionRunner) {
  // Loadup an extension and navigate to test that a fenced frame doesn't reset
  // ExtensionActionRunner's member variables.
  const Extension* extension =
      CreateExtension(ALL_HOSTS, CONTENT_SCRIPT, WITHHOLD_PERMISSIONS);
  ASSERT_TRUE(extension);

  content::WebContents* web_contents = GetActiveWebContents();
  ASSERT_TRUE(web_contents);
  ExtensionActionRunner* action_runner =
      ExtensionActionRunner::GetForWebContents(web_contents);
  ASSERT_TRUE(action_runner);

  ExtensionTestMessageListener inject_success_listener(kInjectSucceeded);
  inject_success_listener.set_extension_id(extension->id());

  GURL url = embedded_test_server()->GetURL("/extensions/test_file.html");
  ASSERT_TRUE(NavigateToURL(url));

  ScriptingPermissionsModifier modifier(profile(), extension);
  modifier.SetWithholdHostPermissions(false);
  EXPECT_TRUE(RunAllPendingInRenderer(web_contents));

  // Create a fenced frame and navigate the fenced frame url.
  GURL fenced_frame_url =
      embedded_test_server()->GetURL("/fenced_frames/title1.html");
  content::RenderFrameHost* fenced_frame_host =
      fenced_frame_helper_.CreateFencedFrame(
          web_contents->GetPrimaryMainFrame(), fenced_frame_url);
  ASSERT_TRUE(fenced_frame_host);
  // Fenced frame doesn't clear pending script injection requests and the
  // scripts.
  EXPECT_EQ(1, action_runner->num_page_requests());
  EXPECT_EQ(1U, action_runner->pending_scripts_.size());

  // Navigate again on the primary main frame. Pending script injection requests
  // and scripts should be cleared.
  ASSERT_TRUE(NavigateToURL(url));
  EXPECT_EQ(0, action_runner->num_page_requests());
  EXPECT_EQ(0U, action_runner->pending_scripts_.size());
}

}  // namespace extensions