Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "extensions/browser/renderer_startup_helper.h" | |
| 6 | |
| 7 #include "base/memory/ptr_util.h" | |
| 8 #include "base/stl_util.h" | |
| 9 #include "content/public/browser/notification_service.h" | |
| 10 #include "content/public/browser/notification_types.h" | |
| 11 #include "content/public/test/mock_render_process_host.h" | |
| 12 #include "extensions/browser/extension_registry.h" | |
| 13 #include "extensions/browser/extension_registry_factory.h" | |
| 14 #include "extensions/browser/extensions_test.h" | |
| 15 #include "extensions/common/extension_builder.h" | |
| 16 #include "extensions/common/extension_messages.h" | |
| 17 | |
| 18 namespace extensions { | |
| 19 namespace test { | |
|
Devlin
2017/03/27 14:47:58
We don't frequently use namespace test {} in exten
karandeepb
2017/04/04 00:49:42
Removed. Reason was that it couldn't be in an anon
| |
| 20 | |
| 21 class RendererStartupHelperTest : public ExtensionsTest { | |
| 22 public: | |
| 23 RendererStartupHelperTest() {} | |
| 24 ~RendererStartupHelperTest() override {} | |
| 25 | |
| 26 void SetUp() override { | |
| 27 ExtensionsTest::SetUp(); | |
| 28 helper_ = base::MakeUnique<RendererStartupHelper>(browser_context()); | |
| 29 registry_ = | |
| 30 ExtensionRegistryFactory::GetForBrowserContext(browser_context()); | |
| 31 render_process_host_ = | |
| 32 base::MakeUnique<content::MockRenderProcessHost>(browser_context()); | |
| 33 extension_ = CreateExtension("ext_1"); | |
| 34 } | |
| 35 | |
| 36 void TearDown() override { | |
| 37 render_process_host_.reset(); | |
| 38 helper_.reset(); | |
| 39 ExtensionsTest::TearDown(); | |
| 40 } | |
| 41 | |
| 42 protected: | |
| 43 void SimulateRenderProcessCreated(content::RenderProcessHost* rph) { | |
| 44 content::NotificationService::current()->Notify( | |
| 45 content::NOTIFICATION_RENDERER_PROCESS_CREATED, | |
| 46 content::Source<content::RenderProcessHost>(rph), | |
| 47 content::NotificationService::NoDetails()); | |
| 48 } | |
| 49 | |
| 50 void SimulateRenderProcessTerminated(content::RenderProcessHost* rph) { | |
| 51 content::NotificationService::current()->Notify( | |
| 52 content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, | |
| 53 content::Source<content::RenderProcessHost>(rph), | |
| 54 content::NotificationService::NoDetails()); | |
| 55 } | |
| 56 | |
| 57 scoped_refptr<Extension> CreateExtension(const std::string& extension_id) { | |
| 58 std::unique_ptr<base::DictionaryValue> manifest = | |
| 59 DictionaryBuilder() | |
| 60 .Set("name", "extension") | |
| 61 .Set("description", "an extension") | |
| 62 .Set("manifest_version", 2) | |
| 63 .Set("version", "0.1") | |
| 64 .Build(); | |
| 65 return ExtensionBuilder() | |
| 66 .SetManifest(std::move(manifest)) | |
| 67 .SetID(extension_id) | |
| 68 .Build(); | |
| 69 } | |
| 70 | |
| 71 scoped_refptr<Extension> CreateTheme(const std::string& extension_id) { | |
| 72 std::unique_ptr<base::DictionaryValue> manifest = | |
| 73 DictionaryBuilder() | |
| 74 .Set("name", "theme") | |
| 75 .Set("description", "a theme") | |
| 76 .Set("theme", DictionaryBuilder().Build()) | |
| 77 .Set("manifest_version", 2) | |
| 78 .Set("version", "0.1") | |
| 79 .Build(); | |
| 80 return ExtensionBuilder() | |
| 81 .SetManifest(std::move(manifest)) | |
| 82 .SetID(extension_id) | |
| 83 .Build(); | |
| 84 } | |
| 85 | |
| 86 void AddExtensionToRegistry(scoped_refptr<Extension> extension) { | |
| 87 registry_->AddEnabled(extension); | |
| 88 } | |
| 89 | |
| 90 void RemoveExtensionFromRegistry(scoped_refptr<Extension> extension) { | |
| 91 registry_->RemoveEnabled(extension->id()); | |
| 92 } | |
| 93 | |
| 94 bool IsProcessInitialized(content::RenderProcessHost* rph) { | |
| 95 return base::ContainsKey(helper_->loaded_extensions_, rph); | |
| 96 } | |
| 97 | |
| 98 bool IsExtensionLoadedInProcess(const Extension& extension, | |
| 99 content::RenderProcessHost* rph) { | |
| 100 return IsProcessInitialized(rph) && | |
| 101 base::ContainsKey(helper_->loaded_extensions_[rph], extension.id()); | |
| 102 } | |
| 103 | |
| 104 bool IsExtensionPendingActivationInProcess(const Extension& extension, | |
| 105 content::RenderProcessHost* rph) { | |
| 106 return base::ContainsKey(helper_->pending_active_extensions_, rph) && | |
| 107 base::ContainsKey(helper_->pending_active_extensions_[rph], | |
| 108 extension.id()); | |
| 109 } | |
| 110 | |
| 111 std::unique_ptr<RendererStartupHelper> helper_; | |
| 112 ExtensionRegistry* registry_; // Weak. | |
| 113 std::unique_ptr<content::MockRenderProcessHost> render_process_host_; | |
| 114 scoped_refptr<Extension> extension_; | |
| 115 | |
| 116 private: | |
| 117 DISALLOW_COPY_AND_ASSIGN(RendererStartupHelperTest); | |
| 118 }; | |
| 119 | |
| 120 // Tests extension loading, unloading and activation and render process creation | |
| 121 // and termination. | |
| 122 TEST_F(RendererStartupHelperTest, NormalExtensionLifecycle) { | |
| 123 // Initialize render process. | |
| 124 EXPECT_FALSE(IsProcessInitialized(render_process_host_.get())); | |
| 125 SimulateRenderProcessCreated(render_process_host_.get()); | |
| 126 EXPECT_TRUE(IsProcessInitialized(render_process_host_.get())); | |
| 127 | |
| 128 IPC::TestSink& sink = render_process_host_->sink(); | |
| 129 | |
| 130 // Enable extension. | |
| 131 sink.ClearMessages(); | |
| 132 EXPECT_FALSE( | |
| 133 IsExtensionLoadedInProcess(*extension_, render_process_host_.get())); | |
| 134 AddExtensionToRegistry(extension_); | |
| 135 helper_->OnExtensionLoaded(*extension_); | |
| 136 EXPECT_TRUE( | |
| 137 IsExtensionLoadedInProcess(*extension_, render_process_host_.get())); | |
| 138 EXPECT_FALSE(IsExtensionPendingActivationInProcess( | |
| 139 *extension_, render_process_host_.get())); | |
| 140 EXPECT_EQ(1u, sink.message_count()); | |
|
Devlin
2017/03/27 14:47:58
The checks for count may as well be ASSERTs since
karandeepb
2017/04/04 00:49:42
Done.
| |
| 141 EXPECT_EQ(ExtensionMsg_Loaded::ID, sink.GetMessageAt(0)->type()); | |
| 142 | |
| 143 // Activate extension. | |
| 144 sink.ClearMessages(); | |
| 145 helper_->ActivateExtensionInProcess(*extension_, render_process_host_.get()); | |
| 146 EXPECT_FALSE(IsExtensionPendingActivationInProcess( | |
| 147 *extension_, render_process_host_.get())); | |
| 148 EXPECT_EQ(1u, sink.message_count()); | |
| 149 EXPECT_EQ(ExtensionMsg_ActivateExtension::ID, sink.GetMessageAt(0)->type()); | |
| 150 | |
| 151 // Disable extension. | |
| 152 sink.ClearMessages(); | |
| 153 RemoveExtensionFromRegistry(extension_); | |
| 154 helper_->OnExtensionUnloaded(*extension_); | |
| 155 EXPECT_FALSE( | |
| 156 IsExtensionLoadedInProcess(*extension_, render_process_host_.get())); | |
| 157 EXPECT_EQ(1u, sink.message_count()); | |
| 158 EXPECT_EQ(ExtensionMsg_Unloaded::ID, sink.GetMessageAt(0)->type()); | |
| 159 | |
| 160 // Extension enabled again. | |
| 161 sink.ClearMessages(); | |
| 162 AddExtensionToRegistry(extension_); | |
| 163 helper_->OnExtensionLoaded(*extension_); | |
| 164 EXPECT_TRUE( | |
| 165 IsExtensionLoadedInProcess(*extension_, render_process_host_.get())); | |
| 166 EXPECT_FALSE(IsExtensionPendingActivationInProcess( | |
| 167 *extension_, render_process_host_.get())); | |
| 168 EXPECT_EQ(1u, sink.message_count()); | |
| 169 EXPECT_EQ(ExtensionMsg_Loaded::ID, sink.GetMessageAt(0)->type()); | |
| 170 | |
| 171 // Render Process terminated. | |
| 172 SimulateRenderProcessTerminated(render_process_host_.get()); | |
| 173 EXPECT_FALSE(IsProcessInitialized(render_process_host_.get())); | |
| 174 EXPECT_FALSE( | |
| 175 IsExtensionLoadedInProcess(*extension_, render_process_host_.get())); | |
| 176 EXPECT_FALSE(IsExtensionPendingActivationInProcess( | |
| 177 *extension_, render_process_host_.get())); | |
| 178 } | |
| 179 | |
| 180 // Test loading an extension again in a render process is a no-op. | |
| 181 TEST_F(RendererStartupHelperTest, LoadTwice) { | |
| 182 // Initialize render process. | |
| 183 EXPECT_FALSE(IsProcessInitialized(render_process_host_.get())); | |
| 184 SimulateRenderProcessCreated(render_process_host_.get()); | |
| 185 EXPECT_TRUE(IsProcessInitialized(render_process_host_.get())); | |
| 186 | |
| 187 IPC::TestSink& sink = render_process_host_->sink(); | |
| 188 | |
| 189 // Enable and load extension. | |
| 190 sink.ClearMessages(); | |
| 191 EXPECT_FALSE( | |
| 192 IsExtensionLoadedInProcess(*extension_, render_process_host_.get())); | |
| 193 AddExtensionToRegistry(extension_); | |
| 194 helper_->OnExtensionLoaded(*extension_); | |
| 195 EXPECT_TRUE( | |
| 196 IsExtensionLoadedInProcess(*extension_, render_process_host_.get())); | |
| 197 EXPECT_EQ(1u, sink.message_count()); | |
| 198 EXPECT_EQ(ExtensionMsg_Loaded::ID, sink.GetMessageAt(0)->type()); | |
| 199 | |
| 200 // Loading extension again is a no-op. | |
| 201 sink.ClearMessages(); | |
| 202 helper_->OnExtensionLoaded(*extension_); | |
| 203 EXPECT_EQ(0u, sink.message_count()); | |
| 204 } | |
| 205 | |
| 206 // Test unloading an extension in a render process in which it isn't loaded is a | |
| 207 // no-op. | |
| 208 TEST_F(RendererStartupHelperTest, UnloadBeforeLoad) { | |
|
Devlin
2017/03/27 14:47:58
As mentioned in the other comment, I'd prefer that
karandeepb
2017/03/27 20:54:11
So one of the reasons of doing this was that with
Devlin
2017/03/28 23:10:08
Doing this reordering would actually be non-trivia
karandeepb
2017/04/04 00:49:42
Done.
| |
| 209 // Initialize render process. | |
| 210 EXPECT_FALSE(IsProcessInitialized(render_process_host_.get())); | |
| 211 SimulateRenderProcessCreated(render_process_host_.get()); | |
| 212 EXPECT_TRUE(IsProcessInitialized(render_process_host_.get())); | |
| 213 | |
| 214 IPC::TestSink& sink = render_process_host_->sink(); | |
| 215 | |
| 216 // Unloading should be a no-op since the extension isn't loaded yet. | |
| 217 sink.ClearMessages(); | |
| 218 EXPECT_FALSE( | |
| 219 IsExtensionLoadedInProcess(*extension_, render_process_host_.get())); | |
| 220 helper_->OnExtensionUnloaded(*extension_); | |
| 221 EXPECT_EQ(0u, sink.message_count()); | |
| 222 } | |
| 223 | |
| 224 // Test activating an extension in an initialized process in which it isn't | |
| 225 // loaded is a no-op. | |
| 226 TEST_F(RendererStartupHelperTest, ActivatedBeforeLoad) { | |
| 227 // Initialize render process. | |
| 228 EXPECT_FALSE(IsProcessInitialized(render_process_host_.get())); | |
| 229 SimulateRenderProcessCreated(render_process_host_.get()); | |
| 230 EXPECT_TRUE(IsProcessInitialized(render_process_host_.get())); | |
| 231 | |
| 232 IPC::TestSink& sink = render_process_host_->sink(); | |
| 233 | |
| 234 // Activating the extension should be a no-op, since it isn't loaded in the | |
| 235 // render process. | |
| 236 sink.ClearMessages(); | |
| 237 EXPECT_FALSE( | |
| 238 IsExtensionLoadedInProcess(*extension_, render_process_host_.get())); | |
| 239 helper_->ActivateExtensionInProcess(*extension_, render_process_host_.get()); | |
| 240 EXPECT_EQ(0u, sink.message_count()); | |
| 241 EXPECT_FALSE( | |
| 242 IsExtensionLoadedInProcess(*extension_, render_process_host_.get())); | |
| 243 EXPECT_FALSE(IsExtensionPendingActivationInProcess( | |
| 244 *extension_, render_process_host_.get())); | |
| 245 } | |
| 246 | |
| 247 // Tests that activating an extension in an uninitialized render process works | |
| 248 // fine. | |
| 249 TEST_F(RendererStartupHelperTest, EnabledBeforeProcessInitialized) { | |
| 250 EXPECT_FALSE(IsProcessInitialized(render_process_host_.get())); | |
| 251 IPC::TestSink& sink = render_process_host_->sink(); | |
| 252 | |
| 253 // Enable extension. The render process isn't initialized yet, so the | |
| 254 // extension should be added to the list of extensions awaiting activation. | |
| 255 sink.ClearMessages(); | |
| 256 AddExtensionToRegistry(extension_); | |
| 257 helper_->OnExtensionLoaded(*extension_); | |
| 258 helper_->ActivateExtensionInProcess(*extension_, render_process_host_.get()); | |
| 259 EXPECT_EQ(0u, sink.message_count()); | |
| 260 EXPECT_FALSE( | |
| 261 IsExtensionLoadedInProcess(*extension_, render_process_host_.get())); | |
| 262 EXPECT_TRUE(IsExtensionPendingActivationInProcess( | |
| 263 *extension_, render_process_host_.get())); | |
| 264 | |
| 265 // Initialize the render process. | |
| 266 SimulateRenderProcessCreated(render_process_host_.get()); | |
| 267 // The renderer would have been sent both the loading and activation IPC | |
| 268 // messages. | |
| 269 EXPECT_LE(2u, sink.message_count()); | |
| 270 EXPECT_TRUE(IsProcessInitialized(render_process_host_.get())); | |
| 271 EXPECT_TRUE( | |
| 272 IsExtensionLoadedInProcess(*extension_, render_process_host_.get())); | |
| 273 EXPECT_FALSE(IsExtensionPendingActivationInProcess( | |
| 274 *extension_, render_process_host_.get())); | |
| 275 } | |
| 276 | |
| 277 // Tests that themes aren't loaded in a renderer process. | |
| 278 TEST_F(RendererStartupHelperTest, LoadTheme) { | |
| 279 // Initialize render process. | |
| 280 EXPECT_FALSE(IsProcessInitialized(render_process_host_.get())); | |
| 281 SimulateRenderProcessCreated(render_process_host_.get()); | |
| 282 EXPECT_TRUE(IsProcessInitialized(render_process_host_.get())); | |
| 283 | |
| 284 scoped_refptr<Extension> extension(CreateTheme("theme")); | |
| 285 ASSERT_TRUE(extension->is_theme()); | |
| 286 | |
| 287 IPC::TestSink& sink = render_process_host_->sink(); | |
| 288 | |
| 289 // Enable the theme. Verify it can't be loaded and activated in the renderer. | |
| 290 sink.ClearMessages(); | |
| 291 EXPECT_FALSE( | |
| 292 IsExtensionLoadedInProcess(*extension, render_process_host_.get())); | |
| 293 AddExtensionToRegistry(extension_); | |
| 294 helper_->OnExtensionLoaded(*extension); | |
| 295 EXPECT_EQ(0u, sink.message_count()); | |
| 296 EXPECT_FALSE( | |
| 297 IsExtensionLoadedInProcess(*extension, render_process_host_.get())); | |
| 298 | |
| 299 helper_->ActivateExtensionInProcess(*extension, render_process_host_.get()); | |
| 300 EXPECT_EQ(0u, sink.message_count()); | |
| 301 EXPECT_FALSE(IsExtensionPendingActivationInProcess( | |
| 302 *extension, render_process_host_.get())); | |
| 303 } | |
| 304 | |
| 305 } // namespace test | |
| 306 } // namespace extensions | |
| OLD | NEW |