| 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/renderer_startup_helper.h" | 5 #include "extensions/browser/renderer_startup_helper.h" |
| 6 | 6 |
| 7 #include "base/values.h" | 7 #include "base/values.h" |
| 8 #include "components/keyed_service/content/browser_context_dependency_manager.h" | 8 #include "components/keyed_service/content/browser_context_dependency_manager.h" |
| 9 #include "content/public/browser/notification_service.h" | 9 #include "content/public/browser/notification_service.h" |
| 10 #include "content/public/browser/notification_types.h" | 10 #include "content/public/browser/notification_types.h" |
| 11 #include "content/public/browser/render_process_host.h" | 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "extensions/browser/extension_function_dispatcher.h" | 12 #include "extensions/browser/extension_function_dispatcher.h" |
| 13 #include "extensions/browser/extension_registry.h" | 13 #include "extensions/browser/extension_registry.h" |
| 14 #include "extensions/browser/extensions_browser_client.h" | 14 #include "extensions/browser/extensions_browser_client.h" |
| 15 #include "extensions/browser/guest_view/web_view/web_view_guest.h" | 15 #include "extensions/browser/guest_view/web_view/web_view_guest.h" |
| 16 #include "extensions/common/extension_messages.h" | 16 #include "extensions/common/extension_messages.h" |
| 17 #include "extensions/common/extension_set.h" | 17 #include "extensions/common/extension_set.h" |
| 18 #include "extensions/common/extensions_client.h" | 18 #include "extensions/common/extensions_client.h" |
| 19 #include "ui/base/webui/web_ui_util.h" | 19 #include "ui/base/webui/web_ui_util.h" |
| 20 | 20 |
| 21 using content::BrowserContext; | 21 using content::BrowserContext; |
| 22 | 22 |
| 23 namespace extensions { | 23 namespace extensions { |
| 24 | 24 |
| 25 RendererStartupHelper::RendererStartupHelper(BrowserContext* browser_context) | 25 RendererStartupHelper::RendererStartupHelper(BrowserContext* browser_context) |
| 26 : browser_context_(browser_context) { | 26 : browser_context_(browser_context) { |
| 27 DCHECK(browser_context); | 27 DCHECK(browser_context); |
| 28 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, | 28 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED, |
| 29 content::NotificationService::AllBrowserContextsAndSources()); | 29 content::NotificationService::AllBrowserContextsAndSources()); |
| 30 registrar_.Add(this, content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| 31 content::NotificationService::AllBrowserContextsAndSources()); |
| 30 } | 32 } |
| 31 | 33 |
| 32 RendererStartupHelper::~RendererStartupHelper() {} | 34 RendererStartupHelper::~RendererStartupHelper() {} |
| 33 | 35 |
| 34 void RendererStartupHelper::Observe( | 36 void RendererStartupHelper::Observe( |
| 35 int type, | 37 int type, |
| 36 const content::NotificationSource& source, | 38 const content::NotificationSource& source, |
| 37 const content::NotificationDetails& details) { | 39 const content::NotificationDetails& details) { |
| 38 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_CREATED, type); | 40 switch (type) { |
| 39 content::RenderProcessHost* process = | 41 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: |
| 40 content::Source<content::RenderProcessHost>(source).ptr(); | 42 InitializeProcess( |
| 43 content::Source<content::RenderProcessHost>(source).ptr()); |
| 44 break; |
| 45 case content::NOTIFICATION_RENDERER_PROCESS_TERMINATED: |
| 46 UntrackProcess(content::Source<content::RenderProcessHost>(source).ptr()); |
| 47 break; |
| 48 default: |
| 49 NOTREACHED() << "Unexpected notification: " << type; |
| 50 } |
| 51 } |
| 52 |
| 53 void RendererStartupHelper::InitializeProcess( |
| 54 content::RenderProcessHost* process) { |
| 41 ExtensionsBrowserClient* client = ExtensionsBrowserClient::Get(); | 55 ExtensionsBrowserClient* client = ExtensionsBrowserClient::Get(); |
| 42 if (!client->IsSameContext(browser_context_, process->GetBrowserContext())) | 56 if (!client->IsSameContext(browser_context_, process->GetBrowserContext())) |
| 43 return; | 57 return; |
| 44 | 58 |
| 45 bool activity_logging_enabled = | 59 bool activity_logging_enabled = |
| 46 client->IsActivityLoggingEnabled(process->GetBrowserContext()); | 60 client->IsActivityLoggingEnabled(process->GetBrowserContext()); |
| 47 // We only send the ActivityLoggingEnabled message if it is enabled; otherwise | 61 // We only send the ActivityLoggingEnabled message if it is enabled; otherwise |
| 48 // the default (not enabled) is correct. | 62 // the default (not enabled) is correct. |
| 49 if (activity_logging_enabled) { | 63 if (activity_logging_enabled) { |
| 50 process->Send( | 64 process->Send( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 79 // TODO(kalman): Only include tab specific permissions for extension | 93 // TODO(kalman): Only include tab specific permissions for extension |
| 80 // processes, no other process needs it, so it's mildly wasteful. | 94 // processes, no other process needs it, so it's mildly wasteful. |
| 81 // I am not sure this is possible to know this here, at such a low | 95 // I am not sure this is possible to know this here, at such a low |
| 82 // level of the stack. Perhaps site isolation can help. | 96 // level of the stack. Perhaps site isolation can help. |
| 83 bool include_tab_permissions = true; | 97 bool include_tab_permissions = true; |
| 84 loaded_extensions.push_back( | 98 loaded_extensions.push_back( |
| 85 ExtensionMsg_Loaded_Params(ext.get(), include_tab_permissions)); | 99 ExtensionMsg_Loaded_Params(ext.get(), include_tab_permissions)); |
| 86 } | 100 } |
| 87 } | 101 } |
| 88 process->Send(new ExtensionMsg_Loaded(loaded_extensions)); | 102 process->Send(new ExtensionMsg_Loaded(loaded_extensions)); |
| 103 auto iter = pending_active_extensions_.find(process); |
| 104 if (iter != pending_active_extensions_.end()) { |
| 105 for (const ExtensionId& id : iter->second) { |
| 106 DCHECK(extensions.Contains(id)); |
| 107 process->Send(new ExtensionMsg_ActivateExtension(id)); |
| 108 } |
| 109 } |
| 110 |
| 111 initialized_processes_.insert(process); |
| 112 } |
| 113 |
| 114 void RendererStartupHelper::UntrackProcess( |
| 115 content::RenderProcessHost* process) { |
| 116 if (!ExtensionsBrowserClient::Get()->IsSameContext( |
| 117 browser_context_, process->GetBrowserContext())) { |
| 118 return; |
| 119 } |
| 120 |
| 121 initialized_processes_.erase(process); |
| 122 pending_active_extensions_.erase(process); |
| 123 } |
| 124 |
| 125 void RendererStartupHelper::ActivateExtensionInProcess( |
| 126 const ExtensionId& id, |
| 127 content::RenderProcessHost* process) { |
| 128 if (initialized_processes_.count(process)) |
| 129 process->Send(new ExtensionMsg_ActivateExtension(id)); |
| 130 else |
| 131 pending_active_extensions_[process].insert(id); |
| 132 } |
| 133 |
| 134 void RendererStartupHelper::OnExtensionLoaded(const Extension& extension) { |
| 135 // Renderers don't need to know about themes. |
| 136 if (extension.is_theme()) |
| 137 return; |
| 138 |
| 139 // We don't need to include tab permisisons here, since the extension |
| 140 // was just loaded. |
| 141 // Uninitialized renderers will be informed of the extension load during the |
| 142 // first batch of messages. |
| 143 std::vector<ExtensionMsg_Loaded_Params> params( |
| 144 1, |
| 145 ExtensionMsg_Loaded_Params(&extension, false /* no tab permissions */)); |
| 146 for (content::RenderProcessHost* process : initialized_processes_) |
| 147 process->Send(new ExtensionMsg_Loaded(params)); |
| 148 } |
| 149 |
| 150 void RendererStartupHelper::OnExtensionUnloaded(const ExtensionId& id) { |
| 151 for (content::RenderProcessHost* process : initialized_processes_) |
| 152 process->Send(new ExtensionMsg_Unloaded(id)); |
| 153 for (auto& process_extensions_pair : pending_active_extensions_) |
| 154 process_extensions_pair.second.erase(id); |
| 89 } | 155 } |
| 90 | 156 |
| 91 ////////////////////////////////////////////////////////////////////////////// | 157 ////////////////////////////////////////////////////////////////////////////// |
| 92 | 158 |
| 93 // static | 159 // static |
| 94 RendererStartupHelper* RendererStartupHelperFactory::GetForBrowserContext( | 160 RendererStartupHelper* RendererStartupHelperFactory::GetForBrowserContext( |
| 95 BrowserContext* context) { | 161 BrowserContext* context) { |
| 96 return static_cast<RendererStartupHelper*>( | 162 return static_cast<RendererStartupHelper*>( |
| 97 GetInstance()->GetServiceForBrowserContext(context, true)); | 163 GetInstance()->GetServiceForBrowserContext(context, true)); |
| 98 } | 164 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 120 BrowserContext* context) const { | 186 BrowserContext* context) const { |
| 121 // Redirected in incognito. | 187 // Redirected in incognito. |
| 122 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); | 188 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); |
| 123 } | 189 } |
| 124 | 190 |
| 125 bool RendererStartupHelperFactory::ServiceIsCreatedWithBrowserContext() const { | 191 bool RendererStartupHelperFactory::ServiceIsCreatedWithBrowserContext() const { |
| 126 return true; | 192 return true; |
| 127 } | 193 } |
| 128 | 194 |
| 129 } // namespace extensions | 195 } // namespace extensions |
| OLD | NEW |