| 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 "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" |
| 11 #include "extensions/browser/extensions_test_browser_context.h" | 11 #include "content/public/test/test_browser_context.h" |
| 12 #include "extensions/browser/test_extensions_browser_client.h" | 12 #include "extensions/browser/test_extensions_browser_client.h" |
| 13 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 | 14 |
| 15 using content::BrowserContext; | 15 using content::BrowserContext; |
| 16 using content::SiteInstance; | 16 using content::SiteInstance; |
| 17 using content::TestBrowserContext; |
| 17 | 18 |
| 18 namespace extensions { | 19 namespace extensions { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 // An incognito version of a ExtensionsTestBrowserContext. | 23 // An incognito version of a TestBrowserContext. |
| 23 class TestBrowserContextIncognito : public ExtensionsTestBrowserContext { | 24 class TestBrowserContextIncognito : public TestBrowserContext { |
| 24 public: | 25 public: |
| 25 TestBrowserContextIncognito() {} | 26 TestBrowserContextIncognito() {} |
| 26 virtual ~TestBrowserContextIncognito() {} | 27 virtual ~TestBrowserContextIncognito() {} |
| 27 | 28 |
| 28 // TestBrowserContext implementation. | 29 // TestBrowserContext implementation. |
| 29 virtual bool IsOffTheRecord() const OVERRIDE { return true; } | 30 virtual bool IsOffTheRecord() const OVERRIDE { return true; } |
| 30 | 31 |
| 31 private: | 32 private: |
| 32 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito); | 33 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito); |
| 33 }; | 34 }; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 51 // Returns true if the notification |type| is registered for |manager| with | 52 // Returns true if the notification |type| is registered for |manager| with |
| 52 // source |context|. Pass NULL for |context| for all sources. | 53 // source |context|. Pass NULL for |context| for all sources. |
| 53 static bool IsRegistered(ProcessManager* manager, | 54 static bool IsRegistered(ProcessManager* manager, |
| 54 int type, | 55 int type, |
| 55 BrowserContext* context) { | 56 BrowserContext* context) { |
| 56 return manager->registrar_.IsRegistered( | 57 return manager->registrar_.IsRegistered( |
| 57 manager, type, content::Source<BrowserContext>(context)); | 58 manager, type, content::Source<BrowserContext>(context)); |
| 58 } | 59 } |
| 59 | 60 |
| 60 private: | 61 private: |
| 61 ExtensionsTestBrowserContext original_context_; | 62 TestBrowserContext original_context_; |
| 62 TestBrowserContextIncognito incognito_context_; | 63 TestBrowserContextIncognito incognito_context_; |
| 63 TestExtensionsBrowserClient extensions_browser_client_; | 64 TestExtensionsBrowserClient extensions_browser_client_; |
| 64 | 65 |
| 65 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest); | 66 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest); |
| 66 }; | 67 }; |
| 67 | 68 |
| 68 // Test that notification registration works properly. | 69 // Test that notification registration works properly. |
| 69 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) { | 70 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) { |
| 70 // Test for a normal context ProcessManager. | 71 // Test for a normal context ProcessManager. |
| 71 scoped_ptr<ProcessManager> manager1( | 72 scoped_ptr<ProcessManager> manager1( |
| 72 ProcessManager::Create(original_context())); | 73 ProcessManager::Create(original_context())); |
| 73 | 74 |
| 74 EXPECT_EQ(original_context(), manager1->GetBrowserContext()); | 75 EXPECT_EQ(original_context(), manager1->GetBrowserContext()); |
| 75 EXPECT_EQ(0u, manager1->background_hosts().size()); | 76 EXPECT_EQ(0u, manager1->background_hosts().size()); |
| 76 | 77 |
| 77 // It observes other notifications from this context. | 78 // It observes other notifications from this context. |
| 78 EXPECT_TRUE(IsRegistered(manager1.get(), | 79 EXPECT_TRUE(IsRegistered(manager1.get(), |
| 79 chrome::NOTIFICATION_EXTENSIONS_READY, | 80 chrome::NOTIFICATION_EXTENSIONS_READY, |
| 80 original_context())); | 81 original_context())); |
| 81 EXPECT_TRUE(IsRegistered(manager1.get(), | 82 EXPECT_TRUE(IsRegistered(manager1.get(), |
| 83 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, |
| 84 original_context())); |
| 85 EXPECT_TRUE(IsRegistered(manager1.get(), |
| 86 chrome::NOTIFICATION_EXTENSION_UNLOADED_DEPRECATED, |
| 87 original_context())); |
| 88 EXPECT_TRUE(IsRegistered(manager1.get(), |
| 82 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 89 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 83 original_context())); | 90 original_context())); |
| 84 | 91 |
| 85 // Test for an incognito context ProcessManager. | 92 // Test for an incognito context ProcessManager. |
| 86 scoped_ptr<ProcessManager> manager2(ProcessManager::CreateIncognitoForTesting( | 93 scoped_ptr<ProcessManager> manager2(ProcessManager::CreateIncognitoForTesting( |
| 87 incognito_context(), original_context(), manager1.get())); | 94 incognito_context(), original_context(), manager1.get())); |
| 88 | 95 |
| 89 EXPECT_EQ(incognito_context(), manager2->GetBrowserContext()); | 96 EXPECT_EQ(incognito_context(), manager2->GetBrowserContext()); |
| 90 EXPECT_EQ(0u, manager2->background_hosts().size()); | 97 EXPECT_EQ(0u, manager2->background_hosts().size()); |
| 91 | 98 |
| 99 // Some notifications are observed for the original context. |
| 100 EXPECT_TRUE(IsRegistered(manager2.get(), |
| 101 chrome::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, |
| 102 original_context())); |
| 103 |
| 92 // Some notifications are observed for the incognito context. | 104 // Some notifications are observed for the incognito context. |
| 93 EXPECT_TRUE(IsRegistered(manager2.get(), | 105 EXPECT_TRUE(IsRegistered(manager2.get(), |
| 94 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 106 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 95 incognito_context())); | 107 incognito_context())); |
| 96 | 108 |
| 97 // Some notifications are observed for both incognito and original. | 109 // Some notifications are observed for both incognito and original. |
| 98 EXPECT_TRUE(IsRegistered(manager2.get(), | 110 EXPECT_TRUE(IsRegistered(manager2.get(), |
| 99 chrome::NOTIFICATION_PROFILE_DESTROYED, | 111 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 100 original_context())); | 112 original_context())); |
| 101 EXPECT_TRUE(IsRegistered(manager2.get(), | 113 EXPECT_TRUE(IsRegistered(manager2.get(), |
| (...skipping 14 matching lines...) Expand all Loading... |
| 116 // Test that extensions get grouped in the right SiteInstance (and therefore | 128 // Test that extensions get grouped in the right SiteInstance (and therefore |
| 117 // process) based on their URLs. | 129 // process) based on their URLs. |
| 118 TEST_F(ProcessManagerTest, ProcessGrouping) { | 130 TEST_F(ProcessManagerTest, ProcessGrouping) { |
| 119 content::ContentBrowserClient content_browser_client; | 131 content::ContentBrowserClient content_browser_client; |
| 120 content::SetBrowserClientForTesting(&content_browser_client); | 132 content::SetBrowserClientForTesting(&content_browser_client); |
| 121 | 133 |
| 122 // Extensions in different browser contexts should always be different | 134 // Extensions in different browser contexts should always be different |
| 123 // SiteInstances. | 135 // SiteInstances. |
| 124 scoped_ptr<ProcessManager> manager1( | 136 scoped_ptr<ProcessManager> manager1( |
| 125 ProcessManager::Create(original_context())); | 137 ProcessManager::Create(original_context())); |
| 126 | 138 // NOTE: This context is not associated with the TestExtensionsBrowserClient. |
| 127 ExtensionsTestBrowserContext another_context; | 139 // That's OK because we're not testing regular vs. incognito behavior. |
| 140 TestBrowserContext another_context; |
| 128 scoped_ptr<ProcessManager> manager2(ProcessManager::Create(&another_context)); | 141 scoped_ptr<ProcessManager> manager2(ProcessManager::Create(&another_context)); |
| 129 | 142 |
| 130 // Extensions with common origins ("scheme://id/") should be grouped in the | 143 // Extensions with common origins ("scheme://id/") should be grouped in the |
| 131 // same SiteInstance. | 144 // same SiteInstance. |
| 132 GURL ext1_url1("chrome-extension://ext1_id/index.html"); | 145 GURL ext1_url1("chrome-extension://ext1_id/index.html"); |
| 133 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html"); | 146 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html"); |
| 134 GURL ext2_url1("chrome-extension://ext2_id/index.html"); | 147 GURL ext2_url1("chrome-extension://ext2_id/index.html"); |
| 135 | 148 |
| 136 scoped_refptr<SiteInstance> site11 = | 149 scoped_refptr<SiteInstance> site11 = |
| 137 manager1->GetSiteInstanceForURL(ext1_url1); | 150 manager1->GetSiteInstanceForURL(ext1_url1); |
| 138 scoped_refptr<SiteInstance> site12 = | 151 scoped_refptr<SiteInstance> site12 = |
| 139 manager1->GetSiteInstanceForURL(ext1_url2); | 152 manager1->GetSiteInstanceForURL(ext1_url2); |
| 140 EXPECT_EQ(site11, site12); | 153 EXPECT_EQ(site11, site12); |
| 141 | 154 |
| 142 scoped_refptr<SiteInstance> site21 = | 155 scoped_refptr<SiteInstance> site21 = |
| 143 manager1->GetSiteInstanceForURL(ext2_url1); | 156 manager1->GetSiteInstanceForURL(ext2_url1); |
| 144 EXPECT_NE(site11, site21); | 157 EXPECT_NE(site11, site21); |
| 145 | 158 |
| 146 scoped_refptr<SiteInstance> other_profile_site = | 159 scoped_refptr<SiteInstance> other_profile_site = |
| 147 manager2->GetSiteInstanceForURL(ext1_url1); | 160 manager2->GetSiteInstanceForURL(ext1_url1); |
| 148 EXPECT_NE(site11, other_profile_site); | 161 EXPECT_NE(site11, other_profile_site); |
| 149 } | 162 } |
| 150 | 163 |
| 151 } // namespace extensions | 164 } // namespace extensions |
| OLD | NEW |