blob: cb6ef0cd8d40bb8ee1085387e14278f8ceecf7d6 [file] [log] [blame]
[email protected]b9f6ba32014-03-10 18:34:081// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "extensions/browser/process_manager.h"
6
avic9cec102015-12-23 00:39:267#include "base/macros.h"
[email protected]b9f6ba32014-03-10 18:34:088#include "content/public/browser/content_browser_client.h"
9#include "content/public/browser/notification_service.h"
10#include "content/public/browser/site_instance.h"
[email protected]39ca20512014-07-24 12:35:5211#include "content/public/common/content_client.h"
[email protected]49e7741e2014-07-21 01:57:4212#include "content/public/test/test_browser_context.h"
[email protected]6b54fda2014-07-22 02:13:4713#include "extensions/browser/extension_registry.h"
[email protected]a80de9112014-07-31 03:15:0814#include "extensions/browser/extensions_test.h"
[email protected]adf5a102014-07-31 12:44:0615#include "extensions/browser/notification_types.h"
[email protected]6b54fda2014-07-22 02:13:4716#include "extensions/browser/process_manager_delegate.h"
[email protected]b9f6ba32014-03-10 18:34:0817#include "extensions/browser/test_extensions_browser_client.h"
[email protected]b9f6ba32014-03-10 18:34:0818
19using content::BrowserContext;
20using content::SiteInstance;
[email protected]49e7741e2014-07-21 01:57:4221using content::TestBrowserContext;
[email protected]b9f6ba32014-03-10 18:34:0822
23namespace extensions {
24
25namespace {
26
[email protected]6b54fda2014-07-22 02:13:4727// A trivial ProcessManagerDelegate.
28class TestProcessManagerDelegate : public ProcessManagerDelegate {
29 public:
30 TestProcessManagerDelegate()
31 : is_background_page_allowed_(true),
32 defer_creating_startup_background_hosts_(false) {}
dcheng9168b2f2014-10-21 12:38:2433 ~TestProcessManagerDelegate() override {}
[email protected]6b54fda2014-07-22 02:13:4734
35 // ProcessManagerDelegate implementation.
achuithd3da4f02017-03-23 20:05:2936 bool AreBackgroundPagesAllowedForContext(
37 BrowserContext* context) const override {
38 return is_background_page_allowed_;
39 }
40 bool IsExtensionBackgroundPageAllowed(
41 BrowserContext* context,
42 const Extension& extension) const override {
[email protected]6b54fda2014-07-22 02:13:4743 return is_background_page_allowed_;
44 }
dcheng9168b2f2014-10-21 12:38:2445 bool DeferCreatingStartupBackgroundHosts(
mostynb0eac4e1b2014-10-03 16:32:1946 BrowserContext* context) const override {
[email protected]6b54fda2014-07-22 02:13:4747 return defer_creating_startup_background_hosts_;
48 }
49
50 bool is_background_page_allowed_;
51 bool defer_creating_startup_background_hosts_;
52};
53
[email protected]b9f6ba32014-03-10 18:34:0854} // namespace
55
[email protected]a80de9112014-07-31 03:15:0856class ProcessManagerTest : public ExtensionsTest {
[email protected]b9f6ba32014-03-10 18:34:0857 public:
karandeepbfb19fb92017-04-12 00:23:2058 ProcessManagerTest() {}
59
60 ~ProcessManagerTest() override {}
61
62 void SetUp() override {
63 ExtensionsTest::SetUp();
64 extension_registry_ =
Jeremy Roman16529d0e2017-08-24 18:13:4765 std::make_unique<ExtensionRegistry>(browser_context());
[email protected]a80de9112014-07-31 03:15:0866 extensions_browser_client()->set_process_manager_delegate(
[email protected]6b54fda2014-07-22 02:13:4767 &process_manager_delegate_);
[email protected]b9f6ba32014-03-10 18:34:0868 }
69
[email protected]a80de9112014-07-31 03:15:0870 // Use original_context() to make it clear it is a non-incognito context.
71 BrowserContext* original_context() { return browser_context(); }
karandeepbfb19fb92017-04-12 00:23:2072 ExtensionRegistry* extension_registry() { return extension_registry_.get(); }
[email protected]6b54fda2014-07-22 02:13:4773 TestProcessManagerDelegate* process_manager_delegate() {
74 return &process_manager_delegate_;
75 }
[email protected]b9f6ba32014-03-10 18:34:0876
77 // Returns true if the notification |type| is registered for |manager| with
78 // source |context|. Pass NULL for |context| for all sources.
79 static bool IsRegistered(ProcessManager* manager,
80 int type,
81 BrowserContext* context) {
82 return manager->registrar_.IsRegistered(
83 manager, type, content::Source<BrowserContext>(context));
84 }
85
[email protected]46a19f62014-07-17 17:21:4586 private:
karandeepbfb19fb92017-04-12 00:23:2087 std::unique_ptr<ExtensionRegistry>
88 extension_registry_; // Shared between BrowserContexts.
[email protected]6b54fda2014-07-22 02:13:4789 TestProcessManagerDelegate process_manager_delegate_;
[email protected]b9f6ba32014-03-10 18:34:0890
91 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest);
92};
93
94// Test that notification registration works properly.
95TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) {
96 // Test for a normal context ProcessManager.
dchengf5d241082016-04-21 03:43:1197 std::unique_ptr<ProcessManager> manager1(ProcessManager::CreateForTesting(
[email protected]6b54fda2014-07-22 02:13:4798 original_context(), extension_registry()));
[email protected]b9f6ba32014-03-10 18:34:0899
rdevlin.cronin6ae04a012015-04-03 20:19:40100 EXPECT_EQ(original_context(), manager1->browser_context());
[email protected]b9f6ba32014-03-10 18:34:08101 EXPECT_EQ(0u, manager1->background_hosts().size());
102
103 // It observes other notifications from this context.
104 EXPECT_TRUE(IsRegistered(manager1.get(),
[email protected]adf5a102014-07-31 12:44:06105 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
[email protected]b9f6ba32014-03-10 18:34:08106 original_context()));
107
108 // Test for an incognito context ProcessManager.
dchengf5d241082016-04-21 03:43:11109 std::unique_ptr<ProcessManager> manager2(
110 ProcessManager::CreateIncognitoForTesting(
111 incognito_context(), original_context(), extension_registry()));
[email protected]b9f6ba32014-03-10 18:34:08112
rdevlin.cronin6ae04a012015-04-03 20:19:40113 EXPECT_EQ(incognito_context(), manager2->browser_context());
[email protected]b9f6ba32014-03-10 18:34:08114 EXPECT_EQ(0u, manager2->background_hosts().size());
115
Evan Stade1ba771c2019-12-05 22:55:19116 // Notifications are observed for the incognito context.
[email protected]b9f6ba32014-03-10 18:34:08117 EXPECT_TRUE(IsRegistered(manager2.get(),
[email protected]adf5a102014-07-31 12:44:06118 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED,
[email protected]b9f6ba32014-03-10 18:34:08119 incognito_context()));
[email protected]b9f6ba32014-03-10 18:34:08120}
121
[email protected]6b54fda2014-07-22 02:13:47122// Test that startup background hosts are created when the extension system
123// becomes ready.
124//
125// NOTE: This test and those that follow do not try to create ExtensionsHosts
126// because ExtensionHost is tightly coupled to WebContents and can't be
127// constructed in unit tests.
128TEST_F(ProcessManagerTest, CreateBackgroundHostsOnExtensionsReady) {
dchengf5d241082016-04-21 03:43:11129 std::unique_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
[email protected]6b54fda2014-07-22 02:13:47130 original_context(), extension_registry()));
131 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
132
133 // Simulate the extension system becoming ready.
Evan Stade1ba771c2019-12-05 22:55:19134 extension_system()->SetReady();
135 base::RunLoop().RunUntilIdle();
[email protected]6b54fda2014-07-22 02:13:47136 EXPECT_TRUE(manager->startup_background_hosts_created_for_test());
137}
138
139// Test that the embedder can defer background host creation. Chrome does this
140// when the profile is created asynchronously, which may take a while.
141TEST_F(ProcessManagerTest, CreateBackgroundHostsDeferred) {
dchengf5d241082016-04-21 03:43:11142 std::unique_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
[email protected]6b54fda2014-07-22 02:13:47143 original_context(), extension_registry()));
144 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
145
146 // Don't create background hosts if the delegate says to defer them.
147 process_manager_delegate()->defer_creating_startup_background_hosts_ = true;
148 manager->MaybeCreateStartupBackgroundHosts();
149 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
150
151 // The extension system becoming ready still doesn't create the hosts.
Evan Stade1ba771c2019-12-05 22:55:19152 extension_system()->SetReady();
153 base::RunLoop().RunUntilIdle();
[email protected]6b54fda2014-07-22 02:13:47154 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
155
156 // Once the embedder is ready the background hosts can be created.
157 process_manager_delegate()->defer_creating_startup_background_hosts_ = false;
158 manager->MaybeCreateStartupBackgroundHosts();
159 EXPECT_TRUE(manager->startup_background_hosts_created_for_test());
160}
161
162// Test that the embedder can disallow background host creation.
163// Chrome OS does this in guest mode.
164TEST_F(ProcessManagerTest, IsBackgroundHostAllowed) {
dchengf5d241082016-04-21 03:43:11165 std::unique_ptr<ProcessManager> manager(ProcessManager::CreateForTesting(
[email protected]6b54fda2014-07-22 02:13:47166 original_context(), extension_registry()));
167 ASSERT_FALSE(manager->startup_background_hosts_created_for_test());
168
169 // Don't create background hosts if the delegate disallows them.
170 process_manager_delegate()->is_background_page_allowed_ = false;
171 manager->MaybeCreateStartupBackgroundHosts();
172 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
173
174 // The extension system becoming ready still doesn't create the hosts.
Evan Stade1ba771c2019-12-05 22:55:19175 extension_system()->SetReady();
176 base::RunLoop().RunUntilIdle();
[email protected]6b54fda2014-07-22 02:13:47177 EXPECT_FALSE(manager->startup_background_hosts_created_for_test());
178}
179
[email protected]b9f6ba32014-03-10 18:34:08180// Test that extensions get grouped in the right SiteInstance (and therefore
181// process) based on their URLs.
182TEST_F(ProcessManagerTest, ProcessGrouping) {
[email protected]b9f6ba32014-03-10 18:34:08183 // Extensions in different browser contexts should always be different
184 // SiteInstances.
dchengf5d241082016-04-21 03:43:11185 std::unique_ptr<ProcessManager> manager1(ProcessManager::CreateForTesting(
[email protected]6b54fda2014-07-22 02:13:47186 original_context(), extension_registry()));
[email protected]49e7741e2014-07-21 01:57:42187 // NOTE: This context is not associated with the TestExtensionsBrowserClient.
188 // That's OK because we're not testing regular vs. incognito behavior.
189 TestBrowserContext another_context;
[email protected]6b54fda2014-07-22 02:13:47190 ExtensionRegistry another_registry(&another_context);
dchengf5d241082016-04-21 03:43:11191 std::unique_ptr<ProcessManager> manager2(
[email protected]6b54fda2014-07-22 02:13:47192 ProcessManager::CreateForTesting(&another_context, &another_registry));
[email protected]b9f6ba32014-03-10 18:34:08193
194 // Extensions with common origins ("scheme://id/") should be grouped in the
195 // same SiteInstance.
196 GURL ext1_url1("chrome-extension://ext1_id/index.html");
197 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html");
198 GURL ext2_url1("chrome-extension://ext2_id/index.html");
199
200 scoped_refptr<SiteInstance> site11 =
201 manager1->GetSiteInstanceForURL(ext1_url1);
202 scoped_refptr<SiteInstance> site12 =
203 manager1->GetSiteInstanceForURL(ext1_url2);
204 EXPECT_EQ(site11, site12);
205
206 scoped_refptr<SiteInstance> site21 =
207 manager1->GetSiteInstanceForURL(ext2_url1);
208 EXPECT_NE(site11, site21);
209
210 scoped_refptr<SiteInstance> other_profile_site =
211 manager2->GetSiteInstanceForURL(ext1_url1);
212 EXPECT_NE(site11, other_profile_site);
Evan Stade1ba771c2019-12-05 22:55:19213
214 BrowserContextDependencyManager::GetInstance()->DestroyBrowserContextServices(
215 &another_context);
[email protected]b9f6ba32014-03-10 18:34:08216}
217
218} // namespace extensions