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