1 // Copyright 2013 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 "chrome/browser/extensions/chrome_extensions_browser_client.h"
7 #include "base/command_line.h"
8 #include "base/version.h"
9 #include "chrome/browser/app_mode/app_mode_utils.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/extensions/activity_log/activity_log.h"
12 #include "chrome/browser/extensions/api/chrome_extensions_api_client.h"
13 #include "chrome/browser/extensions/api/content_settings/content_settings_service.h"
14 #include "chrome/browser/extensions/api/generated_api_registration.h"
15 #include "chrome/browser/extensions/api/preference/chrome_direct_setting.h"
16 #include "chrome/browser/extensions/api/preference/preference_api.h"
17 #include "chrome/browser/extensions/api/runtime/chrome_runtime_api_delegate.h"
18 #include "chrome/browser/extensions/chrome_component_extension_resource_manager.h"
19 #include "chrome/browser/extensions/chrome_extension_host_delegate.h"
20 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
21 #include "chrome/browser/extensions/chrome_mojo_service_registration.h"
22 #include "chrome/browser/extensions/chrome_process_manager_delegate.h"
23 #include "chrome/browser/extensions/chrome_url_request_util.h"
24 #include "chrome/browser/extensions/error_console/error_console.h"
25 #include "chrome/browser/extensions/event_router_forwarder.h"
26 #include "chrome/browser/extensions/extension_system_factory.h"
27 #include "chrome/browser/extensions/extension_util.h"
28 #include "chrome/browser/extensions/menu_manager.h"
29 #include "chrome/browser/external_protocol/external_protocol_handler.h"
30 #include "chrome/browser/net/chrome_net_log.h"
31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/browser/profiles/profile_manager.h"
33 #include "chrome/common/channel_info.h"
34 #include "chrome/common/chrome_paths.h"
35 #include "chrome/common/chrome_switches.h"
36 #include "chrome/common/extensions/features/feature_channel.h"
37 #include "chrome/common/pref_names.h"
38 #include "components/version_info/version_info.h"
39 #include "content/public/browser/render_process_host.h"
40 #include "extensions/browser/api/generated_api_registration.h"
41 #include "extensions/browser/extension_function_registry.h"
42 #include "extensions/browser/extension_prefs.h"
43 #include "extensions/browser/mojo/service_registration.h"
44 #include "extensions/browser/pref_names.h"
45 #include "extensions/browser/url_request_util.h"
47 #if defined(OS_CHROMEOS)
48 #include "chrome/browser/chromeos/profiles/profile_helper.h"
49 #include "chrome/browser/extensions/updater/chromeos_extension_cache_delegate.h"
50 #include "chrome/browser/extensions/updater/extension_cache_impl.h"
51 #include "chromeos/chromeos_switches.h"
53 #include "extensions/browser/updater/null_extension_cache.h"
56 namespace extensions
{
58 ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() {
59 process_manager_delegate_
.reset(new ChromeProcessManagerDelegate
);
60 api_client_
.reset(new ChromeExtensionsAPIClient
);
61 // Only set if it hasn't already been set (e.g. by a test).
62 if (GetCurrentChannel() == GetDefaultChannel())
63 SetCurrentChannel(chrome::GetChannel());
64 resource_manager_
.reset(new ChromeComponentExtensionResourceManager());
67 ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {}
69 bool ChromeExtensionsBrowserClient::IsShuttingDown() {
70 return g_browser_process
->IsShuttingDown();
73 bool ChromeExtensionsBrowserClient::AreExtensionsDisabled(
74 const base::CommandLine
& command_line
,
75 content::BrowserContext
* context
) {
76 Profile
* profile
= static_cast<Profile
*>(context
);
77 return command_line
.HasSwitch(switches::kDisableExtensions
) ||
78 profile
->GetPrefs()->GetBoolean(prefs::kDisableExtensions
);
81 bool ChromeExtensionsBrowserClient::IsValidContext(
82 content::BrowserContext
* context
) {
83 Profile
* profile
= static_cast<Profile
*>(context
);
84 return g_browser_process
->profile_manager() &&
85 g_browser_process
->profile_manager()->IsValidProfile(profile
);
88 bool ChromeExtensionsBrowserClient::IsSameContext(
89 content::BrowserContext
* first
,
90 content::BrowserContext
* second
) {
91 return static_cast<Profile
*>(first
)->IsSameProfile(
92 static_cast<Profile
*>(second
));
95 bool ChromeExtensionsBrowserClient::HasOffTheRecordContext(
96 content::BrowserContext
* context
) {
97 return static_cast<Profile
*>(context
)->HasOffTheRecordProfile();
100 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOffTheRecordContext(
101 content::BrowserContext
* context
) {
102 return static_cast<Profile
*>(context
)->GetOffTheRecordProfile();
105 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOriginalContext(
106 content::BrowserContext
* context
) {
107 return static_cast<Profile
*>(context
)->GetOriginalProfile();
110 #if defined(OS_CHROMEOS)
111 std::string
ChromeExtensionsBrowserClient::GetUserIdHashFromContext(
112 content::BrowserContext
* context
) {
113 return chromeos::ProfileHelper::GetUserIdHashFromProfile(
114 static_cast<Profile
*>(context
));
118 bool ChromeExtensionsBrowserClient::IsGuestSession(
119 content::BrowserContext
* context
) const {
120 return static_cast<Profile
*>(context
)->IsGuestSession();
123 bool ChromeExtensionsBrowserClient::IsExtensionIncognitoEnabled(
124 const std::string
& extension_id
,
125 content::BrowserContext
* context
) const {
126 return IsGuestSession(context
)
127 || util::IsIncognitoEnabled(extension_id
, context
);
130 bool ChromeExtensionsBrowserClient::CanExtensionCrossIncognito(
131 const Extension
* extension
,
132 content::BrowserContext
* context
) const {
133 return IsGuestSession(context
)
134 || util::CanCrossIncognito(extension
, context
);
138 ChromeExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob(
139 net::URLRequest
* request
,
140 net::NetworkDelegate
* network_delegate
,
141 const base::FilePath
& directory_path
,
142 const std::string
& content_security_policy
,
143 bool send_cors_header
) {
144 return chrome_url_request_util::MaybeCreateURLRequestResourceBundleJob(
148 content_security_policy
,
152 bool ChromeExtensionsBrowserClient::AllowCrossRendererResourceLoad(
153 net::URLRequest
* request
,
155 const Extension
* extension
,
156 InfoMap
* extension_info_map
) {
157 bool allowed
= false;
158 if (chrome_url_request_util::AllowCrossRendererResourceLoad(
159 request
, is_incognito
, extension
, extension_info_map
, &allowed
))
162 // Couldn't determine if resource is allowed. Block the load.
166 PrefService
* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
167 content::BrowserContext
* context
) {
168 return static_cast<Profile
*>(context
)->GetPrefs();
171 void ChromeExtensionsBrowserClient::GetEarlyExtensionPrefsObservers(
172 content::BrowserContext
* context
,
173 std::vector
<ExtensionPrefsObserver
*>* observers
) const {
174 observers
->push_back(ContentSettingsService::Get(context
));
177 ProcessManagerDelegate
*
178 ChromeExtensionsBrowserClient::GetProcessManagerDelegate() const {
179 return process_manager_delegate_
.get();
182 scoped_ptr
<ExtensionHostDelegate
>
183 ChromeExtensionsBrowserClient::CreateExtensionHostDelegate() {
184 return scoped_ptr
<ExtensionHostDelegate
>(new ChromeExtensionHostDelegate
);
187 bool ChromeExtensionsBrowserClient::DidVersionUpdate(
188 content::BrowserContext
* context
) {
189 Profile
* profile
= static_cast<Profile
*>(context
);
191 // Unit tests may not provide prefs; assume everything is up-to-date.
192 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(profile
);
193 if (!extension_prefs
)
196 // If we're inside a browser test, then assume prefs are all up-to-date.
197 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType
))
200 PrefService
* pref_service
= extension_prefs
->pref_service();
201 base::Version last_version
;
202 if (pref_service
->HasPrefPath(pref_names::kLastChromeVersion
)) {
203 std::string last_version_str
=
204 pref_service
->GetString(pref_names::kLastChromeVersion
);
205 last_version
= base::Version(last_version_str
);
208 std::string current_version
= version_info::GetVersionNumber();
209 pref_service
->SetString(pref_names::kLastChromeVersion
,
212 // If there was no version string in prefs, assume we're out of date.
213 if (!last_version
.IsValid())
216 return last_version
.IsOlderThan(current_version
);
219 void ChromeExtensionsBrowserClient::PermitExternalProtocolHandler() {
220 ExternalProtocolHandler::PermitLaunchUrl();
223 bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() {
224 return chrome::IsRunningInForcedAppMode();
227 ApiActivityMonitor
* ChromeExtensionsBrowserClient::GetApiActivityMonitor(
228 content::BrowserContext
* context
) {
229 // The ActivityLog monitors and records function calls and events.
230 return ActivityLog::GetInstance(context
);
233 ExtensionSystemProvider
*
234 ChromeExtensionsBrowserClient::GetExtensionSystemFactory() {
235 return ExtensionSystemFactory::GetInstance();
238 void ChromeExtensionsBrowserClient::RegisterExtensionFunctions(
239 ExtensionFunctionRegistry
* registry
) const {
241 registry
->RegisterFunction
<GetPreferenceFunction
>();
242 registry
->RegisterFunction
<SetPreferenceFunction
>();
243 registry
->RegisterFunction
<ClearPreferenceFunction
>();
245 // Direct Preference Access for Component Extensions.
246 registry
->RegisterFunction
<chromedirectsetting::GetDirectSettingFunction
>();
247 registry
->RegisterFunction
<chromedirectsetting::SetDirectSettingFunction
>();
248 registry
->RegisterFunction
<chromedirectsetting::ClearDirectSettingFunction
>();
250 // Generated APIs from lower-level modules.
251 api::GeneratedFunctionRegistry::RegisterAll(registry
);
253 // Generated APIs from Chrome.
254 api::ChromeGeneratedFunctionRegistry::RegisterAll(registry
);
257 void ChromeExtensionsBrowserClient::RegisterMojoServices(
258 content::RenderFrameHost
* render_frame_host
,
259 const Extension
* extension
) const {
260 RegisterServicesForFrame(render_frame_host
, extension
);
261 RegisterChromeServicesForFrame(render_frame_host
, extension
);
264 scoped_ptr
<RuntimeAPIDelegate
>
265 ChromeExtensionsBrowserClient::CreateRuntimeAPIDelegate(
266 content::BrowserContext
* context
) const {
267 return scoped_ptr
<RuntimeAPIDelegate
>(new ChromeRuntimeAPIDelegate(context
));
270 const ComponentExtensionResourceManager
*
271 ChromeExtensionsBrowserClient::GetComponentExtensionResourceManager() {
272 return resource_manager_
.get();
275 void ChromeExtensionsBrowserClient::BroadcastEventToRenderers(
276 events::HistogramValue histogram_value
,
277 const std::string
& event_name
,
278 scoped_ptr
<base::ListValue
> args
) {
279 g_browser_process
->extension_event_router_forwarder()
280 ->BroadcastEventToRenderers(histogram_value
, event_name
, args
.Pass(),
284 net::NetLog
* ChromeExtensionsBrowserClient::GetNetLog() {
285 return g_browser_process
->net_log();
288 ExtensionCache
* ChromeExtensionsBrowserClient::GetExtensionCache() {
289 if (!extension_cache_
.get()) {
290 #if defined(OS_CHROMEOS)
291 extension_cache_
.reset(new ExtensionCacheImpl(
292 make_scoped_ptr(new ChromeOSExtensionCacheDelegate())));
294 extension_cache_
.reset(new NullExtensionCache());
297 return extension_cache_
.get();
300 bool ChromeExtensionsBrowserClient::IsBackgroundUpdateAllowed() {
301 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
302 switches::kDisableBackgroundNetworking
);
305 bool ChromeExtensionsBrowserClient::IsMinBrowserVersionSupported(
306 const std::string
& min_version
) {
307 base::Version browser_version
=
308 base::Version(version_info::GetVersionNumber());
309 Version
browser_min_version(min_version
);
310 if (browser_version
.IsValid() && browser_min_version
.IsValid() &&
311 browser_min_version
.CompareTo(browser_version
) > 0) {
317 ExtensionWebContentsObserver
*
318 ChromeExtensionsBrowserClient::GetExtensionWebContentsObserver(
319 content::WebContents
* web_contents
) {
320 return ChromeExtensionWebContentsObserver::FromWebContents(web_contents
);
323 void ChromeExtensionsBrowserClient::ReportError(
324 content::BrowserContext
* context
,
325 scoped_ptr
<ExtensionError
> error
) {
326 ErrorConsole::Get(context
)->ReportError(error
.Pass());
329 void ChromeExtensionsBrowserClient::CleanUpWebView(
330 content::BrowserContext
* browser_context
,
331 int embedder_process_id
,
332 int view_instance_id
) {
333 // Clean up context menus for the WebView.
335 MenuManager::Get(Profile::FromBrowserContext(browser_context
));
336 menu_manager
->RemoveAllContextItems(
337 MenuItem::ExtensionKey("", embedder_process_id
, view_instance_id
));
340 } // namespace extensions