| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/extensions/extension_error_reporter.h" | 8 #include "content/public/browser/content_browser_client.h" |
| 9 #include "chrome/test/base/testing_profile.h" | |
| 10 #include "content/public/browser/notification_service.h" | 9 #include "content/public/browser/notification_service.h" |
| 11 #include "content/public/browser/render_process_host.h" | |
| 12 #include "content/public/browser/site_instance.h" | 10 #include "content/public/browser/site_instance.h" |
| 11 #include "content/public/test/test_browser_context.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 #include "testing/platform_test.h" | |
| 15 | 14 |
| 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 // TODO(jamescook): Convert this from TestingProfile to TestBrowserContext and | 21 namespace { |
| 21 // move to extensions/browser. This is dependent on ExtensionPrefs being | |
| 22 // converted and ExtensionSystem being converted or eliminated. | |
| 23 // http://crbug.com/315855 | |
| 24 | 22 |
| 25 // make the test a PlatformTest to setup autorelease pools properly on mac | 23 // An incognito version of a TestBrowserContext. |
| 24 class TestBrowserContextIncognito : public TestBrowserContext { |
| 25 public: |
| 26 TestBrowserContextIncognito() {} |
| 27 virtual ~TestBrowserContextIncognito() {} |
| 28 |
| 29 // TestBrowserContext implementation. |
| 30 virtual bool IsOffTheRecord() const OVERRIDE { return true; } |
| 31 |
| 32 private: |
| 33 DISALLOW_COPY_AND_ASSIGN(TestBrowserContextIncognito); |
| 34 }; |
| 35 |
| 36 } // namespace |
| 37 |
| 26 class ProcessManagerTest : public testing::Test { | 38 class ProcessManagerTest : public testing::Test { |
| 27 public: | 39 public: |
| 28 static void SetUpTestCase() { | 40 ProcessManagerTest() : extensions_browser_client_(&original_context_) { |
| 29 ExtensionErrorReporter::Init(false); // no noisy errors | 41 extensions_browser_client_.SetIncognitoContext(&incognito_context_); |
| 42 ExtensionsBrowserClient::Set(&extensions_browser_client_); |
| 30 } | 43 } |
| 31 | 44 |
| 32 virtual void SetUp() { | 45 virtual ~ProcessManagerTest() { |
| 33 ExtensionErrorReporter::GetInstance()->ClearErrors(); | 46 ExtensionsBrowserClient::Set(NULL); |
| 34 } | 47 } |
| 35 | 48 |
| 49 BrowserContext* original_context() { return &original_context_; } |
| 50 BrowserContext* incognito_context() { return &incognito_context_; } |
| 51 |
| 36 // Returns true if the notification |type| is registered for |manager| with | 52 // Returns true if the notification |type| is registered for |manager| with |
| 37 // source |profile|. Pass NULL for |profile| for all sources. | 53 // source |context|. Pass NULL for |context| for all sources. |
| 38 static bool IsRegistered(ProcessManager* manager, | 54 static bool IsRegistered(ProcessManager* manager, |
| 39 int type, | 55 int type, |
| 40 TestingProfile* profile) { | 56 BrowserContext* context) { |
| 41 return manager->registrar_.IsRegistered( | 57 return manager->registrar_.IsRegistered( |
| 42 manager, type, content::Source<Profile>(profile)); | 58 manager, type, content::Source<BrowserContext>(context)); |
| 43 } | 59 } |
| 60 |
| 61 private: |
| 62 TestBrowserContext original_context_; |
| 63 TestBrowserContextIncognito incognito_context_; |
| 64 TestExtensionsBrowserClient extensions_browser_client_; |
| 65 |
| 66 DISALLOW_COPY_AND_ASSIGN(ProcessManagerTest); |
| 44 }; | 67 }; |
| 45 | 68 |
| 46 // Test that notification registration works properly. | 69 // Test that notification registration works properly. |
| 47 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) { | 70 TEST_F(ProcessManagerTest, ExtensionNotificationRegistration) { |
| 48 // Test for a normal profile. | 71 // Test for a normal context ProcessManager. |
| 49 scoped_ptr<TestingProfile> original_profile(new TestingProfile); | |
| 50 scoped_ptr<ProcessManager> manager1( | 72 scoped_ptr<ProcessManager> manager1( |
| 51 ProcessManager::Create(original_profile.get())); | 73 ProcessManager::Create(original_context())); |
| 52 | 74 |
| 53 EXPECT_EQ(original_profile.get(), manager1->GetBrowserContext()); | 75 EXPECT_EQ(original_context(), manager1->GetBrowserContext()); |
| 54 EXPECT_EQ(0u, manager1->background_hosts().size()); | 76 EXPECT_EQ(0u, manager1->background_hosts().size()); |
| 55 | 77 |
| 56 // It observes other notifications from this profile. | 78 // It observes other notifications from this context. |
| 57 EXPECT_TRUE(IsRegistered(manager1.get(), | 79 EXPECT_TRUE(IsRegistered(manager1.get(), |
| 58 chrome::NOTIFICATION_EXTENSIONS_READY, | 80 chrome::NOTIFICATION_EXTENSIONS_READY, |
| 59 original_profile.get())); | 81 original_context())); |
| 60 EXPECT_TRUE(IsRegistered(manager1.get(), | 82 EXPECT_TRUE(IsRegistered(manager1.get(), |
| 61 chrome::NOTIFICATION_EXTENSION_LOADED, | 83 chrome::NOTIFICATION_EXTENSION_LOADED, |
| 62 original_profile.get())); | 84 original_context())); |
| 63 EXPECT_TRUE(IsRegistered(manager1.get(), | 85 EXPECT_TRUE(IsRegistered(manager1.get(), |
| 64 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 86 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 65 original_profile.get())); | 87 original_context())); |
| 66 EXPECT_TRUE(IsRegistered(manager1.get(), | 88 EXPECT_TRUE(IsRegistered(manager1.get(), |
| 67 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 89 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 68 original_profile.get())); | 90 original_context())); |
| 69 | 91 |
| 70 // Now add an incognito profile associated with the master above. | 92 // Test for an incognito context ProcessManager. |
| 71 TestingProfile::Builder builder; | 93 scoped_ptr<ProcessManager> manager2(ProcessManager::CreateIncognitoForTesting( |
| 72 builder.SetIncognito(); | 94 incognito_context(), original_context(), manager1.get())); |
| 73 scoped_ptr<TestingProfile> incognito_profile = builder.Build(); | |
| 74 incognito_profile->SetOriginalProfile(original_profile.get()); | |
| 75 scoped_ptr<ProcessManager> manager2( | |
| 76 ProcessManager::Create(incognito_profile.get())); | |
| 77 | 95 |
| 78 EXPECT_EQ(incognito_profile.get(), manager2->GetBrowserContext()); | 96 EXPECT_EQ(incognito_context(), manager2->GetBrowserContext()); |
| 79 EXPECT_EQ(0u, manager2->background_hosts().size()); | 97 EXPECT_EQ(0u, manager2->background_hosts().size()); |
| 80 | 98 |
| 81 // Some notifications are observed for the original profile. | 99 // Some notifications are observed for the original context. |
| 82 EXPECT_TRUE(IsRegistered(manager2.get(), | 100 EXPECT_TRUE(IsRegistered(manager2.get(), |
| 83 chrome::NOTIFICATION_EXTENSION_LOADED, | 101 chrome::NOTIFICATION_EXTENSION_LOADED, |
| 84 original_profile.get())); | 102 original_context())); |
| 85 | 103 |
| 86 // Some notifications are observed for the incognito profile. | 104 // Some notifications are observed for the incognito context. |
| 87 EXPECT_TRUE(IsRegistered(manager2.get(), | 105 EXPECT_TRUE(IsRegistered(manager2.get(), |
| 88 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, | 106 chrome::NOTIFICATION_EXTENSION_HOST_DESTROYED, |
| 89 incognito_profile.get())); | 107 incognito_context())); |
| 90 | 108 |
| 91 // Some notifications are observed for both incognito and original. | 109 // Some notifications are observed for both incognito and original. |
| 92 EXPECT_TRUE(IsRegistered(manager2.get(), | 110 EXPECT_TRUE(IsRegistered(manager2.get(), |
| 93 chrome::NOTIFICATION_PROFILE_DESTROYED, | 111 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 94 original_profile.get())); | 112 original_context())); |
| 95 EXPECT_TRUE(IsRegistered(manager2.get(), | 113 EXPECT_TRUE(IsRegistered(manager2.get(), |
| 96 chrome::NOTIFICATION_PROFILE_DESTROYED, | 114 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 97 incognito_profile.get())); | 115 incognito_context())); |
| 98 | 116 |
| 99 // Some are not observed at all. | 117 // Some are not observed at all. |
| 100 EXPECT_FALSE(IsRegistered(manager2.get(), | 118 EXPECT_FALSE(IsRegistered(manager2.get(), |
| 101 chrome::NOTIFICATION_EXTENSIONS_READY, | 119 chrome::NOTIFICATION_EXTENSIONS_READY, |
| 102 original_profile.get())); | 120 original_context())); |
| 103 | 121 |
| 104 // This notification is observed for incognito profiles only. | 122 // This notification is observed for incognito contexts only. |
| 105 EXPECT_TRUE(IsRegistered(manager2.get(), | 123 EXPECT_TRUE(IsRegistered(manager2.get(), |
| 106 chrome::NOTIFICATION_PROFILE_DESTROYED, | 124 chrome::NOTIFICATION_PROFILE_DESTROYED, |
| 107 incognito_profile.get())); | 125 incognito_context())); |
| 108 } | 126 } |
| 109 | 127 |
| 110 // Test that extensions get grouped in the right SiteInstance (and therefore | 128 // Test that extensions get grouped in the right SiteInstance (and therefore |
| 111 // process) based on their URLs. | 129 // process) based on their URLs. |
| 112 TEST_F(ProcessManagerTest, ProcessGrouping) { | 130 TEST_F(ProcessManagerTest, ProcessGrouping) { |
| 113 // Extensions in different profiles should always be different SiteInstances. | 131 content::ContentBrowserClient content_browser_client; |
| 114 // Note: we don't initialize these, since we're not testing that | 132 content::SetBrowserClientForTesting(&content_browser_client); |
| 115 // functionality. This means we can get away with a NULL UserScriptMaster. | |
| 116 TestingProfile profile1; | |
| 117 scoped_ptr<ProcessManager> manager1(ProcessManager::Create(&profile1)); | |
| 118 | 133 |
| 119 TestingProfile profile2; | 134 // Extensions in different browser contexts should always be different |
| 120 scoped_ptr<ProcessManager> manager2(ProcessManager::Create(&profile2)); | 135 // SiteInstances. |
| 136 TestBrowserContext context1; |
| 137 scoped_ptr<ProcessManager> manager1(ProcessManager::Create(&context1)); |
| 138 TestBrowserContext context2; |
| 139 scoped_ptr<ProcessManager> manager2(ProcessManager::Create(&context2)); |
| 121 | 140 |
| 122 // Extensions with common origins ("scheme://id/") should be grouped in the | 141 // Extensions with common origins ("scheme://id/") should be grouped in the |
| 123 // same SiteInstance. | 142 // same SiteInstance. |
| 124 GURL ext1_url1("chrome-extension://ext1_id/index.html"); | 143 GURL ext1_url1("chrome-extension://ext1_id/index.html"); |
| 125 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html"); | 144 GURL ext1_url2("chrome-extension://ext1_id/monkey/monkey.html"); |
| 126 GURL ext2_url1("chrome-extension://ext2_id/index.html"); | 145 GURL ext2_url1("chrome-extension://ext2_id/index.html"); |
| 127 | 146 |
| 128 scoped_refptr<SiteInstance> site11 = | 147 scoped_refptr<SiteInstance> site11 = |
| 129 manager1->GetSiteInstanceForURL(ext1_url1); | 148 manager1->GetSiteInstanceForURL(ext1_url1); |
| 130 scoped_refptr<SiteInstance> site12 = | 149 scoped_refptr<SiteInstance> site12 = |
| 131 manager1->GetSiteInstanceForURL(ext1_url2); | 150 manager1->GetSiteInstanceForURL(ext1_url2); |
| 132 EXPECT_EQ(site11, site12); | 151 EXPECT_EQ(site11, site12); |
| 133 | 152 |
| 134 scoped_refptr<SiteInstance> site21 = | 153 scoped_refptr<SiteInstance> site21 = |
| 135 manager1->GetSiteInstanceForURL(ext2_url1); | 154 manager1->GetSiteInstanceForURL(ext2_url1); |
| 136 EXPECT_NE(site11, site21); | 155 EXPECT_NE(site11, site21); |
| 137 | 156 |
| 138 scoped_refptr<SiteInstance> other_profile_site = | 157 scoped_refptr<SiteInstance> other_profile_site = |
| 139 manager2->GetSiteInstanceForURL(ext1_url1); | 158 manager2->GetSiteInstanceForURL(ext1_url1); |
| 140 EXPECT_NE(site11, other_profile_site); | 159 EXPECT_NE(site11, other_profile_site); |
| 141 } | 160 } |
| 142 | 161 |
| 143 } // namespace extensions | 162 } // namespace extensions |
| OLD | NEW |