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" |
| 13 #include "extensions/browser/extension_registry.h" |
| 14 #include "extensions/browser/process_manager_delegate.h" |
12 #include "extensions/browser/test_extensions_browser_client.h" | 15 #include "extensions/browser/test_extensions_browser_client.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
14 | 17 |
15 using content::BrowserContext; | 18 using content::BrowserContext; |
16 using content::SiteInstance; | 19 using content::SiteInstance; |
17 using content::TestBrowserContext; | 20 using content::TestBrowserContext; |
18 | 21 |
19 namespace extensions { | 22 namespace extensions { |
20 | 23 |
21 namespace { | 24 namespace { |
22 | 25 |
23 // An incognito version of a TestBrowserContext. | 26 // An incognito version of a TestBrowserContext. |
24 class TestBrowserContextIncognito : public TestBrowserContext { | 27 class TestBrowserContextIncognito : public TestBrowserContext { |
25 public: | 28 public: |
26 TestBrowserContextIncognito() {} | 29 TestBrowserContextIncognito() {} |
27 virtual ~TestBrowserContextIncognito() {} | 30 virtual ~TestBrowserContextIncognito() {} |
28 | 31 |
29 // TestBrowserContext implementation. | 32 // TestBrowserContext implementation. |
30 virtual bool IsOffTheRecord() const OVERRIDE { return true; } | 33 virtual bool IsOffTheRecord() const OVERRIDE { return true; } |
31 | 34 |
32 private: | 35 private: |
33 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito); | 36 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito); |
34 }; | 37 }; |
35 | 38 |
| 39 // A trivial ProcessManagerDelegate. |
| 40 class TestProcessManagerDelegate : public ProcessManagerDelegate { |
| 41 public: |
| 42 TestProcessManagerDelegate() |
| 43 : is_background_page_allowed_(true), |
| 44 defer_creating_startup_background_hosts_(false) {} |
| 45 virtual ~TestProcessManagerDelegate() {} |
| 46 |
| 47 // ProcessManagerDelegate implementation. |
| 48 virtual bool IsBackgroundPageAllowed(BrowserContext* context) const OVERRIDE { |
| 49 return is_background_page_allowed_; |
| 50 } |
| 51 virtual bool DeferCreatingStartupBackgroundHosts( |
| 52 BrowserContext* context) const OVERRIDE { |
| 53 return defer_creating_startup_background_hosts_; |
| 54 } |
| 55 |
| 56 bool is_background_page_allowed_; |
| 57 bool defer_creating_startup_background_hosts_; |
| 58 }; |
| 59 |
36 } // namespace | 60 } // namespace |
37 | 61 |
38 class ProcessManagerTest : public testing::Test { | 62 class ProcessManagerTest : public testing::Test { |
39 public: | 63 public: |
40 ProcessManagerTest() : extensions_browser_client_(&original_context_) { | 64 ProcessManagerTest() |
| 65 : extension_registry_(&original_context_), |
| 66 extensions_browser_client_(&original_context_) { |
41 extensions_browser_client_.SetIncognitoContext(&incognito_context_); | 67 extensions_browser_client_.SetIncognitoContext(&incognito_context_); |
| 68 extensions_browser_client_.set_process_manager_delegate( |
| 69 &process_manager_delegate_); |
42 ExtensionsBrowserClient::Set(&extensions_browser_client_); | 70 ExtensionsBrowserClient::Set(&extensions_browser_client_); |
43 } | 71 } |
44 | 72 |
45 virtual ~ProcessManagerTest() { | 73 virtual ~ProcessManagerTest() { |
46 ExtensionsBrowserClient::Set(NULL); | 74 ExtensionsBrowserClient::Set(NULL); |
47 } | 75 } |
48 | 76 |
49 BrowserContext* original_context() { return &original_context_; } | 77 BrowserContext* original_context() { return &original_context_; } |
50 BrowserContext* incognito_context() { return &incognito_context_; } | 78 BrowserContext* incognito_context() { return &incognito_context_; } |
| 79 ExtensionRegistry* extension_registry() { return &extension_registry_; } |
| 80 TestProcessManagerDelegate* process_manager_delegate() { |
| 81 return &process_manager_delegate_; |
| 82 } |
51 | 83 |
52 // Returns true if the notification |type| is registered for |manager| with | 84 // Returns true if the notification |type| is registered for |manager| with |
53 // source |context|. Pass NULL for |context| for all sources. | 85 // source |context|. Pass NULL for |context| for all sources. |
54 static bool IsRegistered(ProcessManager* manager, | 86 static bool IsRegistered(ProcessManager* manager, |
55 int type, | 87 int type, |
56 BrowserContext* context) { | 88 BrowserContext* context) { |
57 return manager->registrar_.IsRegistered( | 89 return manager->registrar_.IsRegistered( |
58 manager, type, content::Source<BrowserContext>(context)); | 90 manager, type, content::Source<BrowserContext>(context)); |
59 } | 91 } |
60 | 92 |
61 private: | 93 private: |
62 TestBrowserContext original_context_; | 94 TestBrowserContext original_context_; |
63 TestBrowserContextIncognito incognito_context_; | 95 TestBrowserContextIncognito incognito_context_; |
| 96 ExtensionRegistry extension_registry_; // Shared between BrowserContexts. |
| 97 TestProcessManagerDelegate process_manager_delegate_; |
64 TestExtensionsBrowserClient extensions_browser_client_; | 98 TestExtensionsBrowserClient extensions_browser_client_; |
65 | 99 |
66 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest); | 100 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest); |
67 }; | 101 }; |
68 | 102 |
69 // Test that notification registration works properly. | 103 // Test that notification registration works properly. |
70 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) { | 104 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) { |
71 // Test for a normal context ProcessManager. | 105 // Test for a normal context ProcessManager. |
72 scoped_ptr<ProcessManager> manager1( | 106 scoped_ptr<ProcessManager> manager1(ProcessManager::CreateForTesting( |
73 ProcessManager::Create(original_context())); | 107 original_context(), extension_registry())); |
74 | 108 |
75 EXPECT_EQ(original_context(), manager1->GetBrowserContext()); | 109 EXPECT_EQ(original_context(), manager1->GetBrowserContext()); |
76 EXPECT_EQ(0u, manager1->background_hosts().size()); | 110 EXPECT_EQ(0u, manager1->background_hosts().size()); |
77 | 111 |
78 // It observes other notifications from this context. | 112 // It observes other notifications from this context. |
79 EXPECT_TRUE(IsRegistered(manager1.get(), | 113 EXPECT_TRUE(IsRegistered(manager1.get(), |
80 chrome::NOTIFICATION_EXTENSIONS_READY, | 114 chrome::NOTIFICATION_EXTENSIONS_READY, |
81 original_context())); | 115 original_context())); |
82 EXPECT_TRUE(IsRegistered(manager1.get(), | 116 EXPECT_TRUE(IsRegistered(manager1.get(), |
83 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 117 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, |
84 original_context())); | 118 original_context())); |
85 EXPECT_TRUE(IsRegistered(manager1.get(), | 119 EXPECT_TRUE(IsRegistered(manager1.get(), |
86 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, | 120 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
87 original_context())); | 121 original_context())); |
88 EXPECT_TRUE(IsRegistered(manager1.get(), | 122 EXPECT_TRUE(IsRegistered(manager1.get(), |
89 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 123 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
90 original_context())); | 124 original_context())); |
91 | 125 |
92 // Test for an incognito context ProcessManager. | 126 // Test for an incognito context ProcessManager. |
93 scoped_ptr<ProcessManager> manager2(ProcessManager::CreateIncognitoForTesting( | 127 scoped_ptr<ProcessManager> manager2( |
94 incognito_context(), original_context(), manager1.get())); | 128 ProcessManager::CreateIncognitoForTesting(incognito_context(), |
| 129 original_context(), |
| 130 manager1.get(), |
| 131 extension_registry())); |
95 | 132 |
96 EXPECT_EQ(incognito_context(), manager2->GetBrowserContext()); | 133 EXPECT_EQ(incognito_context(), manager2->GetBrowserContext()); |
97 EXPECT_EQ(0u, manager2->background_hosts().size()); | 134 EXPECT_EQ(0u, manager2->background_hosts().size()); |
98 | 135 |
99 // Some notifications are observed for the original context. | 136 // Some notifications are observed for the original context. |
100 EXPECT_TRUE(IsRegistered(manager2.get(), | 137 EXPECT_TRUE(IsRegistered(manager2.get(), |
101 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 138 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, |
102 original_context())); | 139 original_context())); |
103 | 140 |
104 // Some notifications are observed for the incognito context. | 141 // Some notifications are observed for the incognito context. |
(...skipping 13 matching lines...) Expand all Loading... |
118 EXPECT_FALSE(IsRegistered(manager2.get(), | 155 EXPECT_FALSE(IsRegistered(manager2.get(), |
119 chrome::NOTIFICATION_EXTENSIONS_READY, | 156 chrome::NOTIFICATION_EXTENSIONS_READY, |
120 original_context())); | 157 original_context())); |
121 | 158 |
122 // This notification is observed for incognito contexts only. | 159 // This notification is observed for incognito contexts only. |
123 EXPECT_TRUE(IsRegistered(manager2.get(), | 160 EXPECT_TRUE(IsRegistered(manager2.get(), |
124 chrome::NOTIFICATION_PROFILE_DESTROYED, | 161 chrome::NOTIFICATION_PROFILE_DESTROYED, |
125 incognito_context())); | 162 incognito_context())); |
126 } | 163 } |
127 | 164 |
| 165 // Test that startup background hosts are created when the extension system |
| 166 // becomes ready. |
| 167 // |
| 168 // NOTE: This test and those that follow do not try to create ExtensionsHosts |
| 169 // because ExtensionHost is tightly coupled to WebContents and can't be |
| 170 // constructed in unit tests. |
| 171 TEST_F(ProcessManagerTest, CreateBackgroundHostsOnExtensionsReady) { |
| 172 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting( |
| 173 original_context(), extension_registry())); |
| 174 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); |
| 175 |
| 176 // Simulate the extension system becoming ready. |
| 177 content::NotificationService::current()->Notify( |
| 178 chrome::NOTIFICATION_EXTENSIONS_READY, |
| 179 content::Source<BrowserContext>(original_context()), |
| 180 content::NotificationService::NoDetails()); |
| 181 EXPECT_TRUE(manager->startup_background_hosts_created_for_test()); |
| 182 } |
| 183 |
| 184 // Test that startup background hosts can be created explicitly before the |
| 185 // extension system is ready (this is the normal pattern in Chrome). |
| 186 TEST_F(ProcessManagerTest, CreateBackgroundHostsExplicitly) { |
| 187 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting( |
| 188 original_context(), extension_registry())); |
| 189 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); |
| 190 |
| 191 // Embedder explicitly asks for hosts to be created. Chrome does this on |
| 192 // normal startup. |
| 193 manager->MaybeCreateStartupBackgroundHosts(); |
| 194 EXPECT_TRUE(manager->startup_background_hosts_created_for_test()); |
| 195 } |
| 196 |
| 197 // Test that the embedder can defer background host creation. Chrome does this |
| 198 // when the profile is created asynchronously, which may take a while. |
| 199 TEST_F(ProcessManagerTest, CreateBackgroundHostsDeferred) { |
| 200 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting( |
| 201 original_context(), extension_registry())); |
| 202 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); |
| 203 |
| 204 // Don't create background hosts if the delegate says to defer them. |
| 205 process_manager_delegate()->defer_creating_startup_background_hosts_ = true; |
| 206 manager->MaybeCreateStartupBackgroundHosts(); |
| 207 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); |
| 208 |
| 209 // The extension system becoming ready still doesn't create the hosts. |
| 210 content::NotificationService::current()->Notify( |
| 211 chrome::NOTIFICATION_EXTENSIONS_READY, |
| 212 content::Source<BrowserContext>(original_context()), |
| 213 content::NotificationService::NoDetails()); |
| 214 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); |
| 215 |
| 216 // Once the embedder is ready the background hosts can be created. |
| 217 process_manager_delegate()->defer_creating_startup_background_hosts_ = false; |
| 218 manager->MaybeCreateStartupBackgroundHosts(); |
| 219 EXPECT_TRUE(manager->startup_background_hosts_created_for_test()); |
| 220 } |
| 221 |
| 222 // Test that the embedder can disallow background host creation. |
| 223 // Chrome OS does this in guest mode. |
| 224 TEST_F(ProcessManagerTest, IsBackgroundHostAllowed) { |
| 225 scoped_ptr<ProcessManager> manager(ProcessManager::CreateForTesting( |
| 226 original_context(), extension_registry())); |
| 227 ASSERT_FALSE(manager->startup_background_hosts_created_for_test()); |
| 228 |
| 229 // Don't create background hosts if the delegate disallows them. |
| 230 process_manager_delegate()->is_background_page_allowed_ = false; |
| 231 manager->MaybeCreateStartupBackgroundHosts(); |
| 232 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); |
| 233 |
| 234 // The extension system becoming ready still doesn't create the hosts. |
| 235 content::NotificationService::current()->Notify( |
| 236 chrome::NOTIFICATION_EXTENSIONS_READY, |
| 237 content::Source<BrowserContext>(original_context()), |
| 238 content::NotificationService::NoDetails()); |
| 239 EXPECT_FALSE(manager->startup_background_hosts_created_for_test()); |
| 240 } |
| 241 |
128 // Test that extensions get grouped in the right SiteInstance (and therefore | 242 // Test that extensions get grouped in the right SiteInstance (and therefore |
129 // process) based on their URLs. | 243 // process) based on their URLs. |
130 TEST_F(ProcessManagerTest, ProcessGrouping) { | 244 TEST_F(ProcessManagerTest, ProcessGrouping) { |
131 content::ContentBrowserClient content_browser_client; | 245 content::ContentBrowserClient content_browser_client; |
132 content::SetBrowserClientForTesting(&content_browser_client); | 246 content::SetBrowserClientForTesting(&content_browser_client); |
133 | 247 |
134 // Extensions in different browser contexts should always be different | 248 // Extensions in different browser contexts should always be different |
135 // SiteInstances. | 249 // SiteInstances. |
136 scoped_ptr<ProcessManager> manager1( | 250 scoped_ptr<ProcessManager> manager1(ProcessManager::CreateForTesting( |
137 ProcessManager::Create(original_context())); | 251 original_context(), extension_registry())); |
138 // NOTE: This context is not associated with the TestExtensionsBrowserClient. | 252 // NOTE: This context is not associated with the TestExtensionsBrowserClient. |
139 // That's OK because we're not testing regular vs. incognito behavior. | 253 // That's OK because we're not testing regular vs. incognito behavior. |
140 TestBrowserContext another_context; | 254 TestBrowserContext another_context; |
141 scoped_ptr<ProcessManager> manager2(ProcessManager::Create(&another_context)); | 255 ExtensionRegistry another_registry(&another_context); |
| 256 scoped_ptr<ProcessManager> manager2( |
| 257 ProcessManager::CreateForTesting(&another_context, &another_registry)); |
142 | 258 |
143 // Extensions with common origins ("scheme://id/") should be grouped in the | 259 // Extensions with common origins ("scheme://id/") should be grouped in the |
144 // same SiteInstance. | 260 // same SiteInstance. |
145 GURL ext1_url1("chrome-extension://ext1_id/index.html"); | 261 GURL ext1_url1("chrome-extension://ext1_id/index.html"); |
146 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html"); | 262 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html"); |
147 GURL ext2_url1("chrome-extension://ext2_id/index.html"); | 263 GURL ext2_url1("chrome-extension://ext2_id/index.html"); |
148 | 264 |
149 scoped_refptr<SiteInstance> site11 = | 265 scoped_refptr<SiteInstance> site11 = |
150 manager1->GetSiteInstanceForURL(ext1_url1); | 266 manager1->GetSiteInstanceForURL(ext1_url1); |
151 scoped_refptr<SiteInstance> site12 = | 267 scoped_refptr<SiteInstance> site12 = |
152 manager1->GetSiteInstanceForURL(ext1_url2); | 268 manager1->GetSiteInstanceForURL(ext1_url2); |
153 EXPECT_EQ(site11, site12); | 269 EXPECT_EQ(site11, site12); |
154 | 270 |
155 scoped_refptr<SiteInstance> site21 = | 271 scoped_refptr<SiteInstance> site21 = |
156 manager1->GetSiteInstanceForURL(ext2_url1); | 272 manager1->GetSiteInstanceForURL(ext2_url1); |
157 EXPECT_NE(site11, site21); | 273 EXPECT_NE(site11, site21); |
158 | 274 |
159 scoped_refptr<SiteInstance> other_profile_site = | 275 scoped_refptr<SiteInstance> other_profile_site = |
160 manager2->GetSiteInstanceForURL(ext1_url1); | 276 manager2->GetSiteInstanceForURL(ext1_url1); |
161 EXPECT_NE(site11, other_profile_site); | 277 EXPECT_NE(site11, other_profile_site); |
162 } | 278 } |
163 | 279 |
164 } // namespace extensions | 280 } // namespace extensions |
OLD | NEW |