File: extension_service_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 (151 lines) | stat: -rw-r--r-- 5,443 bytes parent folder | download | duplicates (3)
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
// Copyright 2025 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_service.h"

#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/no_destructor.h"
#include "base/run_loop.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/profiles/profile_test_util.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "components/keyed_service/content/browser_context_keyed_service_shutdown_notifier_factory.h"
#include "components/keyed_service/core/keyed_service_shutdown_notifier.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extensions_browser_client.h"

class Browser;

namespace extensions {

class BrowserContextShutdownNotifierFactory
    : public BrowserContextKeyedServiceShutdownNotifierFactory {
 public:
  static BrowserContextShutdownNotifierFactory* GetInstance() {
    static base::NoDestructor<BrowserContextShutdownNotifierFactory> s_factory;
    return s_factory.get();
  }

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

 private:
  friend class base::NoDestructor<BrowserContextShutdownNotifierFactory>;
  BrowserContextShutdownNotifierFactory()
      : BrowserContextKeyedServiceShutdownNotifierFactory(
            "ProfileShutdownWaiter") {}

  content::BrowserContext* GetBrowserContextToUse(
      content::BrowserContext* context) const override {
    return ExtensionsBrowserClient::Get()->GetContextOwnInstance(context);
  }
};

class ProfileShutdownWaiter {
 public:
  ProfileShutdownWaiter(Profile* profile, base::OnceClosure shutdown_callback)
      : profile_(profile), shutdown_callback_(std::move(shutdown_callback)) {
    SubscribeToProfileShutdownNotifier();
    extension_service_ = ExtensionSystem::Get(profile)->extension_service();
  }

  ~ProfileShutdownWaiter() = default;

  void SubscribeToProfileShutdownNotifier() {
    shutdown_subscription_ =
        BrowserContextShutdownNotifierFactory::GetInstance()
            ->Get(profile_)
            ->Subscribe(base::BindRepeating(&ProfileShutdownWaiter::Shutdown,
                                            base::Unretained(this)));
  }

  void Wait() { run_loop_.Run(); }

  void Shutdown() {
    ASSERT_TRUE(extension_service_);
    ASSERT_TRUE(extension_service_->HasShutDownExecutedForTest());
    run_loop_.Quit();
    profile_ = nullptr;
    extension_service_ = nullptr;
    if (shutdown_callback_) {
      std::move(shutdown_callback_).Run();
    }
  }

  static void EnsureShutdownNotifierFactoryBuilt() {
    BrowserContextShutdownNotifierFactory::GetInstance();
  }

 private:
  base::CallbackListSubscription shutdown_subscription_;
  base::RunLoop run_loop_;
  raw_ptr<Profile> profile_;
  base::OnceClosure shutdown_callback_;
  raw_ptr<ExtensionService> extension_service_;
};

class ExtensionServiceBrowserTest : public ExtensionBrowserTest {
 public:
  ExtensionServiceBrowserTest() {
    ProfileShutdownWaiter::EnsureShutdownNotifierFactoryBuilt();
  }
  ~ExtensionServiceBrowserTest() override = default;
};

// ChromeOS does not support multiple profiles. Hence excluding this test from
// ChromeOS.
#if !BUILDFLAG(IS_CHROMEOS)
// Tests that ExtensionService does not observe host events after
// ExtensionService::Shutdown() has been executed.
IN_PROC_BROWSER_TEST_F(ExtensionServiceBrowserTest,
                       NoHostObservationAfterShutDown) {
  ProfileManager* profile_manager = g_browser_process->profile_manager();
  // Create a new profile
  base::FilePath profile_path =
      profile_manager->user_data_dir().AppendASCII("TestProfile");
  auto* profile =
      &profiles::testing::CreateProfileSync(profile_manager, profile_path);
  ASSERT_TRUE(profile);

  std::unique_ptr<content::WebContents> web_contents;

  base::OnceClosure reset_web_contents =
      base::BindOnce(&std::unique_ptr<content::WebContents>::reset,
                     base::Unretained(&web_contents), nullptr);

  ProfileShutdownWaiter profile_shutdown_waiter(profile,
                                                std::move(reset_web_contents));

  // Create a standalone WebContents for the profile
  web_contents =
      content::WebContents::Create(content::WebContents::CreateParams(profile));
  // Navigate to a URL to ensure render process is created
  ASSERT_TRUE(content::NavigateToURL(web_contents.get(), GURL("about:blank")));
  content::WaitForLoadStop(web_contents.get());

  // Create a browser for the profile
  Browser* new_browser = CreateBrowser(profile);
  ASSERT_TRUE(new_browser);

  // Close the browser window to trigger profile shutdown
  new_browser->window()->Close();

  // Wait for profile to shut down.
  profile_shutdown_waiter.Wait();
}
#endif  // !BUILDFLAG(IS_CHROMEOS)

}  // namespace extensions