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