Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: extensions/browser/renderer_startup_helper_unittest.cc

Issue 2766263003: Extensions: Only load incognito-enabled extensions in an incognito renderer. (Closed)
Patch Set: Rebase. Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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/renderer_startup_helper.h" 5 #include "extensions/browser/renderer_startup_helper.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/stl_util.h" 8 #include "base/stl_util.h"
9 #include "components/crx_file/id_util.h"
9 #include "content/public/browser/notification_service.h" 10 #include "content/public/browser/notification_service.h"
10 #include "content/public/browser/notification_types.h" 11 #include "content/public/browser/notification_types.h"
11 #include "content/public/test/mock_render_process_host.h" 12 #include "content/public/test/mock_render_process_host.h"
13 #include "extensions/browser/extension_prefs.h"
12 #include "extensions/browser/extension_registry.h" 14 #include "extensions/browser/extension_registry.h"
13 #include "extensions/browser/extension_registry_factory.h" 15 #include "extensions/browser/extension_registry_factory.h"
16 #include "extensions/browser/extension_util.h"
14 #include "extensions/browser/extensions_test.h" 17 #include "extensions/browser/extensions_test.h"
18 #include "extensions/browser/test_extensions_browser_client.h"
15 #include "extensions/common/extension_builder.h" 19 #include "extensions/common/extension_builder.h"
16 #include "extensions/common/extension_messages.h" 20 #include "extensions/common/extension_messages.h"
17 21
18 namespace extensions { 22 namespace extensions {
19 23
20 class RendererStartupHelperTest : public ExtensionsTest { 24 class RendererStartupHelperTest : public ExtensionsTest {
21 public: 25 public:
22 RendererStartupHelperTest() {} 26 RendererStartupHelperTest() {}
23 ~RendererStartupHelperTest() override {} 27 ~RendererStartupHelperTest() override {}
24 28
25 void SetUp() override { 29 void SetUp() override {
26 ExtensionsTest::SetUp(); 30 ExtensionsTest::SetUp();
27 helper_ = base::MakeUnique<RendererStartupHelper>(browser_context()); 31 helper_ = base::MakeUnique<RendererStartupHelper>(browser_context());
28 registry_ = 32 registry_ =
29 ExtensionRegistryFactory::GetForBrowserContext(browser_context()); 33 ExtensionRegistryFactory::GetForBrowserContext(browser_context());
30 render_process_host_ = 34 render_process_host_ =
31 base::MakeUnique<content::MockRenderProcessHost>(browser_context()); 35 base::MakeUnique<content::MockRenderProcessHost>(browser_context());
36 incognito_render_process_host_ =
37 base::MakeUnique<content::MockRenderProcessHost>(incognito_context());
32 extension_ = CreateExtension("ext_1"); 38 extension_ = CreateExtension("ext_1");
33 } 39 }
34 40
35 void TearDown() override { 41 void TearDown() override {
36 render_process_host_.reset(); 42 render_process_host_.reset();
43 incognito_render_process_host_.reset();
37 helper_.reset(); 44 helper_.reset();
38 ExtensionsTest::TearDown(); 45 ExtensionsTest::TearDown();
39 } 46 }
40 47
41 protected: 48 protected:
42 void SimulateRenderProcessCreated(content::RenderProcessHost* rph) { 49 void SimulateRenderProcessCreated(content::RenderProcessHost* rph) {
43 content::NotificationService::current()->Notify( 50 content::NotificationService::current()->Notify(
44 content::NOTIFICATION_RENDERER_PROCESS_CREATED, 51 content::NOTIFICATION_RENDERER_PROCESS_CREATED,
45 content::Source<content::RenderProcessHost>(rph), 52 content::Source<content::RenderProcessHost>(rph),
46 content::NotificationService::NoDetails()); 53 content::NotificationService::NoDetails());
47 } 54 }
48 55
49 void SimulateRenderProcessTerminated(content::RenderProcessHost* rph) { 56 void SimulateRenderProcessTerminated(content::RenderProcessHost* rph) {
50 content::NotificationService::current()->Notify( 57 content::NotificationService::current()->Notify(
51 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, 58 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED,
52 content::Source<content::RenderProcessHost>(rph), 59 content::Source<content::RenderProcessHost>(rph),
53 content::NotificationService::NoDetails()); 60 content::NotificationService::NoDetails());
54 } 61 }
55 62
56 scoped_refptr<Extension> CreateExtension(const std::string& extension_id) { 63 scoped_refptr<Extension> CreateExtension(const std::string& id_input) {
57 std::unique_ptr<base::DictionaryValue> manifest = 64 std::unique_ptr<base::DictionaryValue> manifest =
58 DictionaryBuilder() 65 DictionaryBuilder()
59 .Set("name", "extension") 66 .Set("name", "extension")
60 .Set("description", "an extension") 67 .Set("description", "an extension")
61 .Set("manifest_version", 2) 68 .Set("manifest_version", 2)
62 .Set("version", "0.1") 69 .Set("version", "0.1")
63 .Build(); 70 .Build();
64 return ExtensionBuilder() 71 return ExtensionBuilder()
65 .SetManifest(std::move(manifest)) 72 .SetManifest(std::move(manifest))
66 .SetID(extension_id) 73 .SetID(crx_file::id_util::GenerateId(id_input))
67 .Build(); 74 .Build();
68 } 75 }
69 76
70 scoped_refptr<Extension> CreateTheme(const std::string& extension_id) { 77 scoped_refptr<Extension> CreateTheme(const std::string& id_input) {
71 std::unique_ptr<base::DictionaryValue> manifest = 78 std::unique_ptr<base::DictionaryValue> manifest =
72 DictionaryBuilder() 79 DictionaryBuilder()
73 .Set("name", "theme") 80 .Set("name", "theme")
74 .Set("description", "a theme") 81 .Set("description", "a theme")
75 .Set("theme", DictionaryBuilder().Build()) 82 .Set("theme", DictionaryBuilder().Build())
76 .Set("manifest_version", 2) 83 .Set("manifest_version", 2)
77 .Set("version", "0.1") 84 .Set("version", "0.1")
78 .Build(); 85 .Build();
79 return ExtensionBuilder() 86 return ExtensionBuilder()
80 .SetManifest(std::move(manifest)) 87 .SetManifest(std::move(manifest))
81 .SetID(extension_id) 88 .SetID(crx_file::id_util::GenerateId(id_input))
82 .Build(); 89 .Build();
83 } 90 }
84 91
85 void AddExtensionToRegistry(scoped_refptr<Extension> extension) { 92 void AddExtensionToRegistry(scoped_refptr<Extension> extension) {
86 registry_->AddEnabled(extension); 93 registry_->AddEnabled(extension);
87 } 94 }
88 95
89 void RemoveExtensionFromRegistry(scoped_refptr<Extension> extension) { 96 void RemoveExtensionFromRegistry(scoped_refptr<Extension> extension) {
90 registry_->RemoveEnabled(extension->id()); 97 registry_->RemoveEnabled(extension->id());
91 } 98 }
(...skipping 16 matching lines...) Expand all
108 bool IsExtensionPendingActivationInProcess(const Extension& extension, 115 bool IsExtensionPendingActivationInProcess(const Extension& extension,
109 content::RenderProcessHost* rph) { 116 content::RenderProcessHost* rph) {
110 return base::ContainsKey(helper_->pending_active_extensions_, rph) && 117 return base::ContainsKey(helper_->pending_active_extensions_, rph) &&
111 base::ContainsKey(helper_->pending_active_extensions_[rph], 118 base::ContainsKey(helper_->pending_active_extensions_[rph],
112 extension.id()); 119 extension.id());
113 } 120 }
114 121
115 std::unique_ptr<RendererStartupHelper> helper_; 122 std::unique_ptr<RendererStartupHelper> helper_;
116 ExtensionRegistry* registry_; // Weak. 123 ExtensionRegistry* registry_; // Weak.
117 std::unique_ptr<content::MockRenderProcessHost> render_process_host_; 124 std::unique_ptr<content::MockRenderProcessHost> render_process_host_;
125 std::unique_ptr<content::MockRenderProcessHost>
126 incognito_render_process_host_;
118 scoped_refptr<Extension> extension_; 127 scoped_refptr<Extension> extension_;
119 128
120 private: 129 private:
121 DISALLOW_COPY_AND_ASSIGN(RendererStartupHelperTest); 130 DISALLOW_COPY_AND_ASSIGN(RendererStartupHelperTest);
122 }; 131 };
123 132
124 // Tests extension loading, unloading and activation and render process creation 133 // Tests extension loading, unloading and activation and render process creation
125 // and termination. 134 // and termination.
126 TEST_F(RendererStartupHelperTest, NormalExtensionLifecycle) { 135 TEST_F(RendererStartupHelperTest, NormalExtensionLifecycle) {
127 // Initialize render process. 136 // Initialize render process.
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 helper_->ActivateExtensionInProcess(*extension, render_process_host_.get()); 245 helper_->ActivateExtensionInProcess(*extension, render_process_host_.get());
237 EXPECT_EQ(0u, sink.message_count()); 246 EXPECT_EQ(0u, sink.message_count());
238 EXPECT_FALSE(IsExtensionPendingActivationInProcess( 247 EXPECT_FALSE(IsExtensionPendingActivationInProcess(
239 *extension, render_process_host_.get())); 248 *extension, render_process_host_.get()));
240 249
241 helper_->OnExtensionUnloaded(*extension); 250 helper_->OnExtensionUnloaded(*extension);
242 EXPECT_EQ(0u, sink.message_count()); 251 EXPECT_EQ(0u, sink.message_count());
243 EXPECT_FALSE(IsExtensionLoaded(*extension)); 252 EXPECT_FALSE(IsExtensionLoaded(*extension));
244 } 253 }
245 254
255 // Tests that only incognito-enabled extensions are loaded in an incognito
256 // context.
257 TEST_F(RendererStartupHelperTest, ExtensionInIncognitoRenderer) {
258 // Initialize the incognito renderer.
259 EXPECT_FALSE(IsProcessInitialized(incognito_render_process_host_.get()));
260 SimulateRenderProcessCreated(incognito_render_process_host_.get());
261 EXPECT_TRUE(IsProcessInitialized(incognito_render_process_host_.get()));
262
263 IPC::TestSink& sink = render_process_host_->sink();
264 IPC::TestSink& incognito_sink = incognito_render_process_host_->sink();
265
266 // Enable the extension. It should not be loaded in the incognito renderer.
267 sink.ClearMessages();
268 incognito_sink.ClearMessages();
269 EXPECT_FALSE(util::IsIncognitoEnabled(extension_->id(), browser_context()));
270 EXPECT_FALSE(IsExtensionLoaded(*extension_));
271 AddExtensionToRegistry(extension_);
272 helper_->OnExtensionLoaded(*extension_);
273 EXPECT_EQ(0u, sink.message_count());
274 EXPECT_EQ(0u, incognito_sink.message_count());
275 EXPECT_TRUE(IsExtensionLoaded(*extension_));
276 EXPECT_FALSE(IsExtensionLoadedInProcess(
277 *extension_, incognito_render_process_host_.get()));
278 EXPECT_FALSE(
279 IsExtensionLoadedInProcess(*extension_, render_process_host_.get()));
280
281 // Initialize the normal renderer. The extension should get loaded in it.
282 sink.ClearMessages();
283 incognito_sink.ClearMessages();
284 EXPECT_FALSE(IsProcessInitialized(render_process_host_.get()));
285 SimulateRenderProcessCreated(render_process_host_.get());
286 EXPECT_TRUE(IsProcessInitialized(render_process_host_.get()));
287 EXPECT_TRUE(
288 IsExtensionLoadedInProcess(*extension_, render_process_host_.get()));
289 EXPECT_LE(1u, sink.message_count());
290 EXPECT_EQ(0u, incognito_sink.message_count());
291
292 // Enable incognito mode for extension. This will reload the extension.
293 sink.ClearMessages();
294 incognito_sink.ClearMessages();
295 ExtensionPrefs::Get(browser_context())
296 ->SetIsIncognitoEnabled(extension_->id(), true);
297 helper_->OnExtensionUnloaded(*extension_);
298 helper_->OnExtensionLoaded(*extension_);
299 EXPECT_TRUE(IsExtensionLoadedInProcess(*extension_,
300 incognito_render_process_host_.get()));
301 EXPECT_TRUE(
302 IsExtensionLoadedInProcess(*extension_, render_process_host_.get()));
303 // The extension would not have been unloaded from the incognito renderer
304 // since it wasn't loaded.
305 ASSERT_EQ(1u, incognito_sink.message_count());
306 EXPECT_EQ(ExtensionMsg_Loaded::ID, incognito_sink.GetMessageAt(0)->type());
307 // The extension would be first unloaded and then loaded from the normal
308 // renderer.
309 ASSERT_EQ(2u, sink.message_count());
310 EXPECT_EQ(ExtensionMsg_Unloaded::ID, sink.GetMessageAt(0)->type());
311 EXPECT_EQ(ExtensionMsg_Loaded::ID, sink.GetMessageAt(1)->type());
312 }
313
314 TEST_F(RendererStartupHelperTest, PlatformAppInIncognitoRenderer) {}
315
246 } // namespace extensions 316 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698