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
3 // found in the LICENSE file.
5 #include "extensions/browser/renderer_startup_helper.h"
7 #include "base/values.h"
8 #include "components/keyed_service/content/browser_context_dependency_manager.h"
9 #include "content/public/browser/notification_service.h"
10 #include "content/public/browser/notification_types.h"
11 #include "content/public/browser/render_process_host.h"
12 #include "extensions/browser/extension_function_dispatcher.h"
13 #include "extensions/browser/extension_registry.h"
14 #include "extensions/browser/extensions_browser_client.h"
15 #include "extensions/common/extension_messages.h"
16 #include "extensions/common/extension_set.h"
17 #include "extensions/common/extensions_client.h"
18 #include "ui/base/webui/web_ui_util.h"
20 using content::BrowserContext
;
22 namespace extensions
{
24 RendererStartupHelper::RendererStartupHelper(BrowserContext
* browser_context
)
25 : browser_context_(browser_context
) {
26 DCHECK(browser_context
);
27 registrar_
.Add(this, content::NOTIFICATION_RENDERER_PROCESS_CREATED
,
28 content::NotificationService::AllBrowserContextsAndSources());
31 RendererStartupHelper::~RendererStartupHelper() {}
33 void RendererStartupHelper::Observe(
35 const content::NotificationSource
& source
,
36 const content::NotificationDetails
& details
) {
37 DCHECK_EQ(content::NOTIFICATION_RENDERER_PROCESS_CREATED
, type
);
38 content::RenderProcessHost
* process
=
39 content::Source
<content::RenderProcessHost
>(source
).ptr();
40 if (!ExtensionsBrowserClient::Get()->IsSameContext(
41 browser_context_
, process
->GetBrowserContext()))
44 // Platform apps need to know the system font.
45 // TODO(dbeam): this is not the system font in all cases.
46 process
->Send(new ExtensionMsg_SetSystemFont(webui::GetFontFamily(),
47 webui::GetFontSize()));
49 // Scripting whitelist. This is modified by tests and must be communicated
51 process
->Send(new ExtensionMsg_SetScriptingWhitelist(
52 extensions::ExtensionsClient::Get()->GetScriptingWhitelist()));
55 std::vector
<ExtensionMsg_Loaded_Params
> loaded_extensions
;
56 const ExtensionSet
& extensions
=
57 ExtensionRegistry::Get(browser_context_
)->enabled_extensions();
58 for (const auto& ext
: extensions
) {
59 // Renderers don't need to know about themes.
60 if (!ext
->is_theme()) {
61 // TODO(kalman): Only include tab specific permissions for extension
62 // processes, no other process needs it, so it's mildly wasteful.
63 // I am not sure this is possible to know this here, at such a low
64 // level of the stack. Perhaps site isolation can help.
65 bool include_tab_permissions
= true;
66 loaded_extensions
.push_back(
67 ExtensionMsg_Loaded_Params(ext
.get(), include_tab_permissions
));
70 process
->Send(new ExtensionMsg_Loaded(loaded_extensions
));
73 //////////////////////////////////////////////////////////////////////////////
76 RendererStartupHelper
* RendererStartupHelperFactory::GetForBrowserContext(
77 BrowserContext
* context
) {
78 return static_cast<RendererStartupHelper
*>(
79 GetInstance()->GetServiceForBrowserContext(context
, true));
83 RendererStartupHelperFactory
* RendererStartupHelperFactory::GetInstance() {
84 return Singleton
<RendererStartupHelperFactory
>::get();
87 RendererStartupHelperFactory::RendererStartupHelperFactory()
88 : BrowserContextKeyedServiceFactory(
89 "RendererStartupHelper",
90 BrowserContextDependencyManager::GetInstance()) {
91 // No dependencies on other services.
94 RendererStartupHelperFactory::~RendererStartupHelperFactory() {}
96 KeyedService
* RendererStartupHelperFactory::BuildServiceInstanceFor(
97 content::BrowserContext
* context
) const {
98 return new RendererStartupHelper(context
);
101 BrowserContext
* RendererStartupHelperFactory::GetBrowserContextToUse(
102 BrowserContext
* context
) const {
103 // Redirected in incognito.
104 return ExtensionsBrowserClient::Get()->GetOriginalContext(context
);
107 bool RendererStartupHelperFactory::ServiceIsCreatedWithBrowserContext() const {
111 } // namespace extensions