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 "base/macros.h" | 7 #include "base/macros.h" |
8 #include "content/public/browser/content_browser_client.h" | 8 #include "content/public/browser/content_browser_client.h" |
9 #include "content/public/browser/notification_service.h" | 9 #include "content/public/browser/notification_service.h" |
10 #include "content/public/browser/site_instance.h" | 10 #include "content/public/browser/site_instance.h" |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 // Returns true if the notification |type| is registered for |manager| with | 83 // Returns true if the notification |type| is registered for |manager| with |
84 // source |context|. Pass NULL for |context| for all sources. | 84 // source |context|. Pass NULL for |context| for all sources. |
85 static bool IsRegistered(ProcessManager* manager, | 85 static bool IsRegistered(ProcessManager* manager, |
86 int type, | 86 int type, |
87 BrowserContext* context) { | 87 BrowserContext* context) { |
88 return manager->registrar_.IsRegistered( | 88 return manager->registrar_.IsRegistered( |
89 manager, type, content::Source<BrowserContext>(context)); | 89 manager, type, content::Source<BrowserContext>(context)); |
90 } | 90 } |
91 | 91 |
92 private: | 92 private: |
93 scoped_ptr<content::NotificationService> notification_service_; | 93 std::unique_ptr<content::NotificationService> notification_service_; |
94 TestBrowserContextIncognito incognito_context_; | 94 TestBrowserContextIncognito incognito_context_; |
95 ExtensionRegistry extension_registry_; // Shared between BrowserContexts. | 95 ExtensionRegistry extension_registry_; // Shared between BrowserContexts. |
96 TestProcessManagerDelegate process_manager_delegate_; | 96 TestProcessManagerDelegate process_manager_delegate_; |
97 | 97 |
98 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest); | 98 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest); |
99 }; | 99 }; |
100 | 100 |
101 // Test that notification registration works properly. | 101 // Test that notification registration works properly. |
102 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) { | 102 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) { |
103 // Test for a normal context ProcessManager. | 103 // Test for a normal context ProcessManager. |
104 scoped_ptr<ProcessManager> manager1(ProcessManager::CreateForTesting( | 104 std::unique_ptr<ProcessManager> manager1(ProcessManager::CreateForTesting( |
105 original_context(), extension_registry())); | 105 original_context(), extension_registry())); |
106 | 106 |
107 EXPECT_EQ(original_context(), manager1->browser_context()); | 107 EXPECT_EQ(original_context(), manager1->browser_context()); |
108 EXPECT_EQ(0u, manager1->background_hosts().size()); | 108 EXPECT_EQ(0u, manager1->background_hosts().size()); |
109 | 109 |
110 // It observes other notifications from this context. | 110 // It observes other notifications from this context. |
111 EXPECT_TRUE(IsRegistered(manager1.get(), | 111 EXPECT_TRUE(IsRegistered(manager1.get(), |
112 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, | 112 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, |
113 original_context())); | 113 original_context())); |
114 EXPECT_TRUE(IsRegistered(manager1.get(), | 114 EXPECT_TRUE(IsRegistered(manager1.get(), |
115 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 115 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
116 original_context())); | 116 original_context())); |
117 | 117 |
118 // Test for an incognito context ProcessManager. | 118 // Test for an incognito context ProcessManager. |
119 scoped_ptr<ProcessManager> manager2( | 119 std::unique_ptr<ProcessManager> manager2( |
120 ProcessManager::CreateIncognitoForTesting(incognito_context(), | 120 ProcessManager::CreateIncognitoForTesting( |
121 original_context(), | 121 incognito_context(), original_context(), extension_registry())); |
122 extension_registry())); | |
123 | 122 |
124 EXPECT_EQ(incognito_context(), manager2->browser_context()); | 123 EXPECT_EQ(incognito_context(), manager2->browser_context()); |
125 EXPECT_EQ(0u, manager2->background_hosts().size()); | 124 EXPECT_EQ(0u, manager2->background_hosts().size()); |
126 | 125 |
127 // Some notifications are observed for the incognito context. | 126 // Some notifications are observed for the incognito context. |
128 EXPECT_TRUE(IsRegistered(manager2.get(), | 127 EXPECT_TRUE(IsRegistered(manager2.get(), |
129 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 128 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
130 incognito_context())); | 129 incognito_context())); |
131 | 130 |
132 // Some are not observed at all. | 131 // Some are not observed at all. |
133 EXPECT_FALSE( | 132 EXPECT_FALSE( |
134 IsRegistered(manager2.get(), | 133 IsRegistered(manager2.get(), |
135 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, | 134 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, |
136 original_context())); | 135 original_context())); |
137 } | 136 } |
138 | 137 |
139 // Test that startup background hosts are created when the extension system | 138 // Test that startup background hosts are created when the extension system |
140 // becomes ready. | 139 // becomes ready. |
141 // | 140 // |
142 // NOTE: This test and those that follow do not try to create ExtensionsHosts | 141 // NOTE: This test and those that follow do not try to create ExtensionsHosts |
143 // because ExtensionHost is tightly coupled to WebContents and can't be | 142 // because ExtensionHost is tightly coupled to WebContents and can't be |
144 // constructed in unit tests. | 143 // constructed in unit tests. |
145 TEST_F(ProcessManagerTest, CreateBackgroundHostsOnExtensionsReady) { | 144 TEST_F(ProcessManagerTest, CreateBackgroundHostsOnExtensionsReady) { |
146 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting( | 145 std::unique_ptr<ProcessManager> manager(ProcessManager::CreateForTesting( |
147 original_context(), extension_registry())); | 146 original_context(), extension_registry())); |
148 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); | 147 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); |
149 | 148 |
150 // Simulate the extension system becoming ready. | 149 // Simulate the extension system becoming ready. |
151 content::NotificationService::current()->Notify( | 150 content::NotificationService::current()->Notify( |
152 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, | 151 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, |
153 content::Source<BrowserContext>(original_context()), | 152 content::Source<BrowserContext>(original_context()), |
154 content::NotificationService::NoDetails()); | 153 content::NotificationService::NoDetails()); |
155 EXPECT_TRUE(manager->startup_background_hosts_created_for_test()); | 154 EXPECT_TRUE(manager->startup_background_hosts_created_for_test()); |
156 } | 155 } |
157 | 156 |
158 // Test that startup background hosts can be created explicitly before the | 157 // Test that startup background hosts can be created explicitly before the |
159 // extension system is ready (this is the normal pattern in Chrome). | 158 // extension system is ready (this is the normal pattern in Chrome). |
160 TEST_F(ProcessManagerTest, CreateBackgroundHostsExplicitly) { | 159 TEST_F(ProcessManagerTest, CreateBackgroundHostsExplicitly) { |
161 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting( | 160 std::unique_ptr<ProcessManager> manager(ProcessManager::CreateForTesting( |
162 original_context(), extension_registry())); | 161 original_context(), extension_registry())); |
163 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); | 162 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); |
164 | 163 |
165 // Embedder explicitly asks for hosts to be created. Chrome does this on | 164 // Embedder explicitly asks for hosts to be created. Chrome does this on |
166 // normal startup. | 165 // normal startup. |
167 manager->MaybeCreateStartupBackgroundHosts(); | 166 manager->MaybeCreateStartupBackgroundHosts(); |
168 EXPECT_TRUE(manager->startup_background_hosts_created_for_test()); | 167 EXPECT_TRUE(manager->startup_background_hosts_created_for_test()); |
169 } | 168 } |
170 | 169 |
171 // Test that the embedder can defer background host creation. Chrome does this | 170 // Test that the embedder can defer background host creation. Chrome does this |
172 // when the profile is created asynchronously, which may take a while. | 171 // when the profile is created asynchronously, which may take a while. |
173 TEST_F(ProcessManagerTest, CreateBackgroundHostsDeferred) { | 172 TEST_F(ProcessManagerTest, CreateBackgroundHostsDeferred) { |
174 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting( | 173 std::unique_ptr<ProcessManager> manager(ProcessManager::CreateForTesting( |
175 original_context(), extension_registry())); | 174 original_context(), extension_registry())); |
176 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); | 175 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); |
177 | 176 |
178 // Don't create background hosts if the delegate says to defer them. | 177 // Don't create background hosts if the delegate says to defer them. |
179 process_manager_delegate()->defer_creating_startup_background_hosts_ = true; | 178 process_manager_delegate()->defer_creating_startup_background_hosts_ = true; |
180 manager->MaybeCreateStartupBackgroundHosts(); | 179 manager->MaybeCreateStartupBackgroundHosts(); |
181 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); | 180 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); |
182 | 181 |
183 // The extension system becoming ready still doesn't create the hosts. | 182 // The extension system becoming ready still doesn't create the hosts. |
184 content::NotificationService::current()->Notify( | 183 content::NotificationService::current()->Notify( |
185 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, | 184 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, |
186 content::Source<BrowserContext>(original_context()), | 185 content::Source<BrowserContext>(original_context()), |
187 content::NotificationService::NoDetails()); | 186 content::NotificationService::NoDetails()); |
188 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); | 187 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); |
189 | 188 |
190 // Once the embedder is ready the background hosts can be created. | 189 // Once the embedder is ready the background hosts can be created. |
191 process_manager_delegate()->defer_creating_startup_background_hosts_ = false; | 190 process_manager_delegate()->defer_creating_startup_background_hosts_ = false; |
192 manager->MaybeCreateStartupBackgroundHosts(); | 191 manager->MaybeCreateStartupBackgroundHosts(); |
193 EXPECT_TRUE(manager->startup_background_hosts_created_for_test()); | 192 EXPECT_TRUE(manager->startup_background_hosts_created_for_test()); |
194 } | 193 } |
195 | 194 |
196 // Test that the embedder can disallow background host creation. | 195 // Test that the embedder can disallow background host creation. |
197 // Chrome OS does this in guest mode. | 196 // Chrome OS does this in guest mode. |
198 TEST_F(ProcessManagerTest, IsBackgroundHostAllowed) { | 197 TEST_F(ProcessManagerTest, IsBackgroundHostAllowed) { |
199 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting( | 198 std::unique_ptr<ProcessManager> manager(ProcessManager::CreateForTesting( |
200 original_context(), extension_registry())); | 199 original_context(), extension_registry())); |
201 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); | 200 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); |
202 | 201 |
203 // Don't create background hosts if the delegate disallows them. | 202 // Don't create background hosts if the delegate disallows them. |
204 process_manager_delegate()->is_background_page_allowed_ = false; | 203 process_manager_delegate()->is_background_page_allowed_ = false; |
205 manager->MaybeCreateStartupBackgroundHosts(); | 204 manager->MaybeCreateStartupBackgroundHosts(); |
206 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); | 205 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); |
207 | 206 |
208 // The extension system becoming ready still doesn't create the hosts. | 207 // The extension system becoming ready still doesn't create the hosts. |
209 content::NotificationService::current()->Notify( | 208 content::NotificationService::current()->Notify( |
210 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, | 209 extensions::NOTIFICATION_EXTENSIONS_READY_DEPRECATED, |
211 content::Source<BrowserContext>(original_context()), | 210 content::Source<BrowserContext>(original_context()), |
212 content::NotificationService::NoDetails()); | 211 content::NotificationService::NoDetails()); |
213 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); | 212 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); |
214 } | 213 } |
215 | 214 |
216 // Test that extensions get grouped in the right SiteInstance (and therefore | 215 // Test that extensions get grouped in the right SiteInstance (and therefore |
217 // process) based on their URLs. | 216 // process) based on their URLs. |
218 TEST_F(ProcessManagerTest, ProcessGrouping) { | 217 TEST_F(ProcessManagerTest, ProcessGrouping) { |
219 // Extensions in different browser contexts should always be different | 218 // Extensions in different browser contexts should always be different |
220 // SiteInstances. | 219 // SiteInstances. |
221 scoped_ptr<ProcessManager> manager1(ProcessManager::CreateForTesting( | 220 std::unique_ptr<ProcessManager> manager1(ProcessManager::CreateForTesting( |
222 original_context(), extension_registry())); | 221 original_context(), extension_registry())); |
223 // NOTE: This context is not associated with the TestExtensionsBrowserClient. | 222 // NOTE: This context is not associated with the TestExtensionsBrowserClient. |
224 // That's OK because we're not testing regular vs. incognito behavior. | 223 // That's OK because we're not testing regular vs. incognito behavior. |
225 TestBrowserContext another_context; | 224 TestBrowserContext another_context; |
226 ExtensionRegistry another_registry(&another_context); | 225 ExtensionRegistry another_registry(&another_context); |
227 scoped_ptr<ProcessManager> manager2( | 226 std::unique_ptr<ProcessManager> manager2( |
228 ProcessManager::CreateForTesting(&another_context, &another_registry)); | 227 ProcessManager::CreateForTesting(&another_context, &another_registry)); |
229 | 228 |
230 // Extensions with common origins ("scheme://id/") should be grouped in the | 229 // Extensions with common origins ("scheme://id/") should be grouped in the |
231 // same SiteInstance. | 230 // same SiteInstance. |
232 GURL ext1_url1("chrome-extension://ext1_id/index.html"); | 231 GURL ext1_url1("chrome-extension://ext1_id/index.html"); |
233 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html"); | 232 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html"); |
234 GURL ext2_url1("chrome-extension://ext2_id/index.html"); | 233 GURL ext2_url1("chrome-extension://ext2_id/index.html"); |
235 | 234 |
236 scoped_refptr<SiteInstance> site11 = | 235 scoped_refptr<SiteInstance> site11 = |
237 manager1->GetSiteInstanceForURL(ext1_url1); | 236 manager1->GetSiteInstanceForURL(ext1_url1); |
238 scoped_refptr<SiteInstance> site12 = | 237 scoped_refptr<SiteInstance> site12 = |
239 manager1->GetSiteInstanceForURL(ext1_url2); | 238 manager1->GetSiteInstanceForURL(ext1_url2); |
240 EXPECT_EQ(site11, site12); | 239 EXPECT_EQ(site11, site12); |
241 | 240 |
242 scoped_refptr<SiteInstance> site21 = | 241 scoped_refptr<SiteInstance> site21 = |
243 manager1->GetSiteInstanceForURL(ext2_url1); | 242 manager1->GetSiteInstanceForURL(ext2_url1); |
244 EXPECT_NE(site11, site21); | 243 EXPECT_NE(site11, site21); |
245 | 244 |
246 scoped_refptr<SiteInstance> other_profile_site = | 245 scoped_refptr<SiteInstance> other_profile_site = |
247 manager2->GetSiteInstanceForURL(ext1_url1); | 246 manager2->GetSiteInstanceForURL(ext1_url1); |
248 EXPECT_NE(site11, other_profile_site); | 247 EXPECT_NE(site11, other_profile_site); |
249 } | 248 } |
250 | 249 |
251 } // namespace extensions | 250 } // namespace extensions |
OLD | NEW |