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. 4 5 #include "extensions/browser/renderer_startup_helper.h" 6 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" 19 20 using content::BrowserContext; 21 22 namespace extensions { 23 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()); 29 } 30 31 RendererStartupHelper::~RendererStartupHelper() {} 32 33 void RendererStartupHelper::Observe( 34 int type, 35 const content::NotificationSource& source, 36 const content::NotificationDetails& details) { 37 switch (type) { 38 case content::NOTIFICATION_RENDERER_PROCESS_CREATED: { 39 content::RenderProcessHost* process = 40 content::Source<content::RenderProcessHost>(source).ptr(); 41 if (!ExtensionsBrowserClient::Get()->IsSameContext( 42 browser_context_, process->GetBrowserContext())) 43 break; 44 45 // Platform apps need to know the system font. 46 scoped_ptr<base::DictionaryValue> fonts(new base::DictionaryValue); 47 webui::SetFontAndTextDirection(fonts.get()); 48 std::string font_family, font_size; 49 fonts->GetString("fontfamily", &font_family); 50 fonts->GetString("fontsize", &font_size); 51 process->Send(new ExtensionMsg_SetSystemFont( 52 font_family, font_size)); 53 54 // Valid extension function names, used to setup bindings in renderer. 55 std::vector<std::string> function_names; 56 ExtensionFunctionDispatcher::GetAllFunctionNames(&function_names); 57 process->Send(new ExtensionMsg_SetFunctionNames(function_names)); 58 59 // Scripting whitelist. This is modified by tests and must be communicated 60 // to renderers. 61 process->Send(new ExtensionMsg_SetScriptingWhitelist( 62 extensions::ExtensionsClient::Get()->GetScriptingWhitelist())); 63 64 // Loaded extensions. 65 std::vector<ExtensionMsg_Loaded_Params> loaded_extensions; 66 const ExtensionSet& extensions = 67 ExtensionRegistry::Get(browser_context_)->enabled_extensions(); 68 for (ExtensionSet::const_iterator iter = extensions.begin(); 69 iter != extensions.end(); ++iter) { 70 // Renderers don't need to know about themes. 71 if (!(*iter)->is_theme()) 72 loaded_extensions.push_back(ExtensionMsg_Loaded_Params(iter->get())); 73 } 74 process->Send(new ExtensionMsg_Loaded(loaded_extensions)); 75 break; 76 } 77 default: 78 NOTREACHED(); 79 break; 80 } 81 } 82 83 ////////////////////////////////////////////////////////////////////////////// 84 85 // static 86 RendererStartupHelper* RendererStartupHelperFactory::GetForBrowserContext( 87 BrowserContext* context) { 88 return static_cast<RendererStartupHelper*>( 89 GetInstance()->GetServiceForBrowserContext(context, true)); 90 } 91 92 // static 93 RendererStartupHelperFactory* RendererStartupHelperFactory::GetInstance() { 94 return Singleton<RendererStartupHelperFactory>::get(); 95 } 96 97 RendererStartupHelperFactory::RendererStartupHelperFactory() 98 : BrowserContextKeyedServiceFactory( 99 "RendererStartupHelper", 100 BrowserContextDependencyManager::GetInstance()) { 101 // No dependencies on other services. 102 } 103 104 RendererStartupHelperFactory::~RendererStartupHelperFactory() {} 105 106 KeyedService* RendererStartupHelperFactory::BuildServiceInstanceFor( 107 content::BrowserContext* context) const { 108 return new RendererStartupHelper(context); 109 } 110 111 BrowserContext* RendererStartupHelperFactory::GetBrowserContextToUse( 112 BrowserContext* context) const { 113 // Redirected in incognito. 114 return ExtensionsBrowserClient::Get()->GetOriginalContext(context); 115 } 116 117 bool RendererStartupHelperFactory::ServiceIsCreatedWithBrowserContext() const { 118 return true; 119 } 120 121 } // namespace extensions 122