OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/browser/process_manager.h" | 5 #include "extensions/browser/process_manager.h" |
6 | 6 |
7 #include "chrome/browser/chrome_notification_types.h" | 7 #include "chrome/browser/chrome_notification_types.h" |
8 #include "components/keyed_service/content/browser_context_dependency_manager.h" | |
8 #include "content/public/browser/content_browser_client.h" | 9 #include "content/public/browser/content_browser_client.h" |
9 #include "content/public/browser/notification_service.h" | 10 #include "content/public/browser/notification_service.h" |
10 #include "content/public/browser/site_instance.h" | 11 #include "content/public/browser/site_instance.h" |
11 #include "content/public/test/test_browser_context.h" | 12 #include "content/public/test/test_browser_context.h" |
12 #include "extensions/browser/test_extensions_browser_client.h" | 13 #include "extensions/browser/test_extensions_browser_client.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
14 | 15 |
15 using content::BrowserContext; | 16 using content::BrowserContext; |
16 using content::SiteInstance; | 17 using content::SiteInstance; |
17 using content::TestBrowserContext; | 18 using content::TestBrowserContext; |
18 | 19 |
19 namespace extensions { | 20 namespace extensions { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 // An incognito version of a TestBrowserContext. | 24 // An incognito version of a TestBrowserContext. |
24 class TestBrowserContextIncognito : public TestBrowserContext { | 25 class TestBrowserContextIncognito : public TestBrowserContext { |
25 public: | 26 public: |
26 TestBrowserContextIncognito() {} | 27 TestBrowserContextIncognito() {} |
27 virtual ~TestBrowserContextIncognito() {} | 28 virtual ~TestBrowserContextIncognito() {} |
28 | 29 |
29 // TestBrowserContext implementation. | 30 // TestBrowserContext implementation. |
30 virtual bool IsOffTheRecord() const OVERRIDE { return true; } | 31 virtual bool IsOffTheRecord() const OVERRIDE { return true; } |
31 | 32 |
32 private: | 33 private: |
33 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito); | 34 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito); |
34 }; | 35 }; |
35 | 36 |
37 // A trivial ProcessManagerDelegate. | |
38 class TestProcessManagerDelegate : public ProcessManagerDelegate { | |
39 public: | |
40 TestProcessManagerDelegate() | |
41 : is_background_page_allowed_(true), | |
42 defer_creating_startup_background_hosts_(false) {} | |
43 virtual ~TestProcessManagerDelegate() {} | |
44 | |
45 // ProcessManagerDelegate implementation. | |
46 virtual bool IsBackgroundPageAllowed(BrowserContext* context) const OVERRIDE { | |
47 return is_background_page_allowed_; | |
48 } | |
49 virtual bool DeferCreatingStartupBackgroundHosts( | |
50 BrowserContext* context) const OVERRIDE { | |
51 return defer_creating_startup_background_hosts_; | |
52 } | |
53 | |
54 bool is_background_page_allowed_; | |
55 bool defer_creating_startup_background_hosts_; | |
56 }; | |
57 | |
36 } // namespace | 58 } // namespace |
37 | 59 |
38 class ProcessManagerTest : public testing::Test { | 60 class ProcessManagerTest : public testing::Test { |
39 public: | 61 public: |
40 ProcessManagerTest() : extensions_browser_client_(&original_context_) { | 62 ProcessManagerTest() : extensions_browser_client_(&original_context_) { |
41 extensions_browser_client_.SetIncognitoContext(&incognito_context_); | 63 extensions_browser_client_.SetIncognitoContext(&incognito_context_); |
64 extensions_browser_client_.set_process_manager_delegate( | |
65 &process_manager_delegate_); | |
42 ExtensionsBrowserClient::Set(&extensions_browser_client_); | 66 ExtensionsBrowserClient::Set(&extensions_browser_client_); |
43 } | 67 } |
44 | 68 |
45 virtual ~ProcessManagerTest() { | 69 virtual ~ProcessManagerTest() { |
46 ExtensionsBrowserClient::Set(NULL); | 70 ExtensionsBrowserClient::Set(NULL); |
47 } | 71 } |
48 | 72 |
49 BrowserContext* original_context() { return &original_context_; } | 73 BrowserContext* original_context() { return &original_context_; } |
50 BrowserContext* incognito_context() { return &incognito_context_; } | 74 BrowserContext* incognito_context() { return &incognito_context_; } |
51 | 75 |
76 // testing::Test implementation. | |
77 virtual void SetUp() OVERRIDE { | |
78 // Needed for ExtensionRegistry. | |
79 BrowserContextDependencyManager::GetInstance() | |
80 ->CreateBrowserContextServicesForTest(&original_context_); | |
81 BrowserContextDependencyManager::GetInstance() | |
82 ->CreateBrowserContextServicesForTest(&incognito_context_); | |
83 } | |
84 | |
85 virtual void TearDown() OVERRIDE { | |
86 // Needed to clean up ExtensionRegistry. | |
87 BrowserContextDependencyManager::GetInstance() | |
88 ->DestroyBrowserContextServices(&incognito_context_); | |
89 BrowserContextDependencyManager::GetInstance() | |
90 ->DestroyBrowserContextServices(&original_context_); | |
91 } | |
92 | |
52 // Returns true if the notification |type| is registered for |manager| with | 93 // Returns true if the notification |type| is registered for |manager| with |
53 // source |context|. Pass NULL for |context| for all sources. | 94 // source |context|. Pass NULL for |context| for all sources. |
54 static bool IsRegistered(ProcessManager* manager, | 95 static bool IsRegistered(ProcessManager* manager, |
55 int type, | 96 int type, |
56 BrowserContext* context) { | 97 BrowserContext* context) { |
57 return manager->registrar_.IsRegistered( | 98 return manager->registrar_.IsRegistered( |
58 manager, type, content::Source<BrowserContext>(context)); | 99 manager, type, content::Source<BrowserContext>(context)); |
59 } | 100 } |
60 | 101 |
61 private: | 102 protected: |
62 TestBrowserContext original_context_; | 103 TestBrowserContext original_context_; |
63 TestBrowserContextIncognito incognito_context_; | 104 TestBrowserContextIncognito incognito_context_; |
105 TestProcessManagerDelegate process_manager_delegate_; | |
64 TestExtensionsBrowserClient extensions_browser_client_; | 106 TestExtensionsBrowserClient extensions_browser_client_; |
65 | 107 |
66 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest); | 108 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest); |
67 }; | 109 }; |
68 | 110 |
69 // Test that notification registration works properly. | 111 // Test that notification registration works properly. |
70 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) { | 112 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) { |
71 // Test for a normal context ProcessManager. | 113 // Test for a normal context ProcessManager. |
72 scoped_ptr<ProcessManager> manager1( | 114 scoped_ptr<ProcessManager> manager1( |
73 ProcessManager::Create(original_context())); | 115 ProcessManager::Create(original_context())); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
118 EXPECT_FALSE(IsRegistered(manager2.get(), | 160 EXPECT_FALSE(IsRegistered(manager2.get(), |
119 chrome::NOTIFICATION_EXTENSIONS_READY, | 161 chrome::NOTIFICATION_EXTENSIONS_READY, |
120 original_context())); | 162 original_context())); |
121 | 163 |
122 // This notification is observed for incognito contexts only. | 164 // This notification is observed for incognito contexts only. |
123 EXPECT_TRUE(IsRegistered(manager2.get(), | 165 EXPECT_TRUE(IsRegistered(manager2.get(), |
124 chrome::NOTIFICATION_PROFILE_DESTROYED, | 166 chrome::NOTIFICATION_PROFILE_DESTROYED, |
125 incognito_context())); | 167 incognito_context())); |
126 } | 168 } |
127 | 169 |
170 // Test that startup background hosts are created when the extension system | |
171 // becomes ready. | |
172 // | |
173 // NOTE: This test and those that follow do not try to create ExtensionsHosts | |
174 // because ExtensionHost is tightly coupled to WebContents and can't be | |
175 // constructed in unit tests. | |
176 TEST_F(ProcessManagerTest, CreateBackgroundHostsOnExtensionsReady) { | |
177 scoped_ptr<ProcessManager> manager( | |
178 ProcessManager::Create(original_context())); | |
179 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); | |
180 | |
181 // Simulate the extension system becoming ready. | |
182 content::NotificationService::current()->Notify( | |
183 chrome::NOTIFICATION_EXTENSIONS_READY, | |
184 content::Source<BrowserContext>(original_context()), | |
185 content::NotificationService::NoDetails()); | |
186 EXPECT_TRUE(manager->startup_background_hosts_created_for_test()); | |
187 } | |
188 | |
189 // Test that startup background hosts can be created explicitly before the | |
190 // extension system is ready (this is the normal pattern in Chrome). | |
191 TEST_F(ProcessManagerTest, CreateBackgroundHostsExplicitly) { | |
192 scoped_ptr<ProcessManager> manager( | |
193 ProcessManager::Create(original_context())); | |
194 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); | |
195 | |
196 // Embedder explicitly asks for hosts to be created. Chrome does this on | |
197 // normal startup. | |
198 manager->MaybeCreateStartupBackgroundHosts(); | |
199 EXPECT_TRUE(manager->startup_background_hosts_created_for_test()); | |
200 | |
201 // Simulate the extension system becoming ready. Nothing changes. | |
202 content::NotificationService::current()->Notify( | |
203 chrome::NOTIFICATION_EXTENSIONS_READY, | |
204 content::Source<BrowserContext>(original_context()), | |
205 content::NotificationService::NoDetails()); | |
206 EXPECT_TRUE(manager->startup_background_hosts_created_for_test()); | |
Yoyo Zhou
2014/07/15 02:13:22
"Nothing changes" - hosts don't get uncreated, rig
James Cook
2014/07/15 17:02:52
Nothing really, I guess. I was just emulating what
| |
207 } | |
208 | |
209 // Test that the embedder can defer background host creation. Chrome does this | |
210 // when the profile is created asynchronously, which may take a while. | |
211 TEST_F(ProcessManagerTest, CreateBackgroundHostsDeferred) { | |
212 scoped_ptr<ProcessManager> manager( | |
213 ProcessManager::Create(original_context())); | |
214 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); | |
215 | |
216 // Don't create background hosts if the delegate says to defer them. | |
217 process_manager_delegate_.defer_creating_startup_background_hosts_ = true; | |
218 manager->MaybeCreateStartupBackgroundHosts(); | |
219 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); | |
220 | |
221 // The extension system becoming ready still doesn't create the hosts. | |
222 content::NotificationService::current()->Notify( | |
223 chrome::NOTIFICATION_EXTENSIONS_READY, | |
224 content::Source<BrowserContext>(original_context()), | |
225 content::NotificationService::NoDetails()); | |
226 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); | |
227 | |
228 // Once the embedder is ready the background hosts can be created. | |
229 process_manager_delegate_.defer_creating_startup_background_hosts_ = false; | |
230 manager->MaybeCreateStartupBackgroundHosts(); | |
231 EXPECT_TRUE(manager->startup_background_hosts_created_for_test()); | |
232 } | |
233 | |
234 // Test that the embedder can disallow background host creation. | |
235 // Chrome OS does this in guest mode. | |
236 TEST_F(ProcessManagerTest, IsBackgroundHostAllowed) { | |
237 scoped_ptr<ProcessManager> manager( | |
238 ProcessManager::Create(original_context())); | |
239 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); | |
240 | |
241 // Don't create background hosts if the delegate disallows them. | |
242 process_manager_delegate_.is_background_page_allowed_ = false; | |
243 manager->MaybeCreateStartupBackgroundHosts(); | |
244 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); | |
245 | |
246 // The extension system becoming ready still doesn't create the hosts. | |
247 content::NotificationService::current()->Notify( | |
248 chrome::NOTIFICATION_EXTENSIONS_READY, | |
249 content::Source<BrowserContext>(original_context()), | |
250 content::NotificationService::NoDetails()); | |
251 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); | |
252 } | |
253 | |
128 // Test that extensions get grouped in the right SiteInstance (and therefore | 254 // Test that extensions get grouped in the right SiteInstance (and therefore |
129 // process) based on their URLs. | 255 // process) based on their URLs. |
130 TEST_F(ProcessManagerTest, ProcessGrouping) { | 256 TEST_F(ProcessManagerTest, ProcessGrouping) { |
131 content::ContentBrowserClient content_browser_client; | 257 content::ContentBrowserClient content_browser_client; |
132 content::SetBrowserClientForTesting(&content_browser_client); | 258 content::SetBrowserClientForTesting(&content_browser_client); |
133 | 259 |
134 // Extensions in different browser contexts should always be different | 260 // Extensions in different browser contexts should always be different |
135 // SiteInstances. | 261 // SiteInstances. |
136 scoped_ptr<ProcessManager> manager1( | 262 scoped_ptr<ProcessManager> manager1( |
137 ProcessManager::Create(original_context())); | 263 ProcessManager::Create(original_context())); |
(...skipping 17 matching lines...) Expand all Loading... | |
155 scoped_refptr<SiteInstance> site21 = | 281 scoped_refptr<SiteInstance> site21 = |
156 manager1->GetSiteInstanceForURL(ext2_url1); | 282 manager1->GetSiteInstanceForURL(ext2_url1); |
157 EXPECT_NE(site11, site21); | 283 EXPECT_NE(site11, site21); |
158 | 284 |
159 scoped_refptr<SiteInstance> other_profile_site = | 285 scoped_refptr<SiteInstance> other_profile_site = |
160 manager2->GetSiteInstanceForURL(ext1_url1); | 286 manager2->GetSiteInstanceForURL(ext1_url1); |
161 EXPECT_NE(site11, other_profile_site); | 287 EXPECT_NE(site11, other_profile_site); |
162 } | 288 } |
163 | 289 |
164 } // namespace extensions | 290 } // namespace extensions |
OLD | NEW |