| 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()); | |
| 32 } | 30 } |
| 33 | 31 |
| 34 RendererStartupHelper::~RendererStartupHelper() {} | 32 RendererStartupHelper::~RendererStartupHelper() {} |
| 35 | 33 |
| 36 void RendererStartupHelper::Observe( | 34 void RendererStartupHelper::Observe( |
| 37 int type, | 35 int type, |
| 38 const content::NotificationSource& source, | 36 const content::NotificationSource& source, |
| 39 const content::NotificationDetails& details) { | 37 const content::NotificationDetails& details) { |
| 40 switch (type) { | 38 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_CREATED, type); |
| 41 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: | 39 content::RenderProcessHost* process = |
| 42 InitializeProcess( | 40 content::Source<content::RenderProcessHost>(source).ptr(); |
| 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) { | |
| 55 ExtensionsBrowserClient* client = ExtensionsBrowserClient::Get(); | 41 ExtensionsBrowserClient* client = ExtensionsBrowserClient::Get(); |
| 56 if (!client->IsSameContext(browser_context_, process->GetBrowserContext())) | 42 if (!client->IsSameContext(browser_context_, process->GetBrowserContext())) |
| 57 return; | 43 return; |
| 58 | 44 |
| 59 bool activity_logging_enabled = | 45 bool activity_logging_enabled = |
| 60 client->IsActivityLoggingEnabled(process->GetBrowserContext()); | 46 client->IsActivityLoggingEnabled(process->GetBrowserContext()); |
| 61 // We only send the ActivityLoggingEnabled message if it is enabled; otherwise | 47 // We only send the ActivityLoggingEnabled message if it is enabled; otherwise |
| 62 // the default (not enabled) is correct. | 48 // the default (not enabled) is correct. |
| 63 if (activity_logging_enabled) { | 49 if (activity_logging_enabled) { |
| 64 process->Send( | 50 process->Send( |
| (...skipping 28 matching lines...) Expand all Loading... |
| 93 // TODO(kalman): Only include tab specific permissions for extension | 79 // TODO(kalman): Only include tab specific permissions for extension |
| 94 // processes, no other process needs it, so it's mildly wasteful. | 80 // processes, no other process needs it, so it's mildly wasteful. |
| 95 // I am not sure this is possible to know this here, at such a low | 81 // I am not sure this is possible to know this here, at such a low |
| 96 // level of the stack. Perhaps site isolation can help. | 82 // level of the stack. Perhaps site isolation can help. |
| 97 bool include_tab_permissions = true; | 83 bool include_tab_permissions = true; |
| 98 loaded_extensions.push_back( | 84 loaded_extensions.push_back( |
| 99 ExtensionMsg_Loaded_Params(ext.get(), include_tab_permissions)); | 85 ExtensionMsg_Loaded_Params(ext.get(), include_tab_permissions)); |
| 100 } | 86 } |
| 101 } | 87 } |
| 102 process->Send(new ExtensionMsg_Loaded(loaded_extensions)); | 88 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); | |
| 155 } | 89 } |
| 156 | 90 |
| 157 ////////////////////////////////////////////////////////////////////////////// | 91 ////////////////////////////////////////////////////////////////////////////// |
| 158 | 92 |
| 159 // static | 93 // static |
| 160 RendererStartupHelper* RendererStartupHelperFactory::GetForBrowserContext( | 94 RendererStartupHelper* RendererStartupHelperFactory::GetForBrowserContext( |
| 161 BrowserContext* context) { | 95 BrowserContext* context) { |
| 162 return static_cast<RendererStartupHelper*>( | 96 return static_cast<RendererStartupHelper*>( |
| 163 GetInstance()->GetServiceForBrowserContext(context, true)); | 97 GetInstance()->GetServiceForBrowserContext(context, true)); |
| 164 } | 98 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 186 BrowserContext* context) const { | 120 BrowserContext* context) const { |
| 187 // Redirected in incognito. | 121 // Redirected in incognito. |
| 188 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); | 122 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); |
| 189 } | 123 } |
| 190 | 124 |
| 191 bool RendererStartupHelperFactory::ServiceIsCreatedWithBrowserContext() const { | 125 bool RendererStartupHelperFactory::ServiceIsCreatedWithBrowserContext() const { |
| 192 return true; | 126 return true; |
| 193 } | 127 } |
| 194 | 128 |
| 195 } // namespace extensions | 129 } // namespace extensions |
| OLD | NEW |