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/browser/task_management/web_contents_tags.h"
34 #include "chrome/common/channel_info.h"
35 #include "chrome/common/chrome_paths.h"
36 #include "chrome/common/chrome_switches.h"
37 #include "chrome/common/extensions/features/feature_channel.h"
38 #include "chrome/common/pref_names.h"
39 #include "components/version_info/version_info.h"
40 #include "content/public/browser/render_process_host.h"
41 #include "extensions/browser/api/generated_api_registration.h"
42 #include "extensions/browser/extension_function_registry.h"
43 #include "extensions/browser/extension_prefs.h"
44 #include "extensions/browser/mojo/service_registration.h"
45 #include "extensions/browser/pref_names.h"
46 #include "extensions/browser/url_request_util.h"
48 #if defined(OS_CHROMEOS)
49 #include "chrome/browser/chromeos/profiles/profile_helper.h"
50 #include "chrome/browser/extensions/updater/chromeos_extension_cache_delegate.h"
51 #include "chrome/browser/extensions/updater/extension_cache_impl.h"
52 #include "chromeos/chromeos_switches.h"
54 #include "extensions/browser/updater/null_extension_cache.h"
57 namespace extensions
{
59 ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() {
60 process_manager_delegate_
.reset(new ChromeProcessManagerDelegate
);
61 api_client_
.reset(new ChromeExtensionsAPIClient
);
62 // Only set if it hasn't already been set (e.g. by a test).
63 if (GetCurrentChannel() == GetDefaultChannel())
64 SetCurrentChannel(chrome::GetChannel());
65 resource_manager_
.reset(new ChromeComponentExtensionResourceManager());
68 ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {}
70 bool ChromeExtensionsBrowserClient::IsShuttingDown() {
71 return g_browser_process
->IsShuttingDown();
74 bool ChromeExtensionsBrowserClient::AreExtensionsDisabled(
75 const base::CommandLine
& command_line
,
76 content::BrowserContext
* context
) {
77 Profile
* profile
= static_cast<Profile
*>(context
);
78 return command_line
.HasSwitch(switches::kDisableExtensions
) ||
79 profile
->GetPrefs()->GetBoolean(prefs::kDisableExtensions
);
82 bool ChromeExtensionsBrowserClient::IsValidContext(
83 content::BrowserContext
* context
) {
84 Profile
* profile
= static_cast<Profile
*>(context
);
85 return g_browser_process
->profile_manager() &&
86 g_browser_process
->profile_manager()->IsValidProfile(profile
);
89 bool ChromeExtensionsBrowserClient::IsSameContext(
90 content::BrowserContext
* first
,
91 content::BrowserContext
* second
) {
92 return static_cast<Profile
*>(first
)->IsSameProfile(
93 static_cast<Profile
*>(second
));
96 bool ChromeExtensionsBrowserClient::HasOffTheRecordContext(
97 content::BrowserContext
* context
) {
98 return static_cast<Profile
*>(context
)->HasOffTheRecordProfile();
101 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOffTheRecordContext(
102 content::BrowserContext
* context
) {
103 return static_cast<Profile
*>(context
)->GetOffTheRecordProfile();
106 content::BrowserContext
* ChromeExtensionsBrowserClient::GetOriginalContext(
107 content::BrowserContext
* context
) {
108 return static_cast<Profile
*>(context
)->GetOriginalProfile();
111 #if defined(OS_CHROMEOS)
112 std::string
ChromeExtensionsBrowserClient::GetUserIdHashFromContext(
113 content::BrowserContext
* context
) {
114 return chromeos::ProfileHelper::GetUserIdHashFromProfile(
115 static_cast<Profile
*>(context
));
119 bool ChromeExtensionsBrowserClient::IsGuestSession(
120 content::BrowserContext
* context
) const {
121 return static_cast<Profile
*>(context
)->IsGuestSession();
124 bool ChromeExtensionsBrowserClient::IsExtensionIncognitoEnabled(
125 const std::string
& extension_id
,
126 content::BrowserContext
* context
) const {
127 return IsGuestSession(context
)
128 || util::IsIncognitoEnabled(extension_id
, context
);
131 bool ChromeExtensionsBrowserClient::CanExtensionCrossIncognito(
132 const Extension
* extension
,
133 content::BrowserContext
* context
) const {
134 return IsGuestSession(context
)
135 || util::CanCrossIncognito(extension
, context
);
139 ChromeExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob(
140 net::URLRequest
* request
,
141 net::NetworkDelegate
* network_delegate
,
142 const base::FilePath
& directory_path
,
143 const std::string
& content_security_policy
,
144 bool send_cors_header
) {
145 return chrome_url_request_util::MaybeCreateURLRequestResourceBundleJob(
149 content_security_policy
,
153 bool ChromeExtensionsBrowserClient::AllowCrossRendererResourceLoad(
154 net::URLRequest
* request
,
156 const Extension
* extension
,
157 InfoMap
* extension_info_map
) {
158 bool allowed
= false;
159 if (chrome_url_request_util::AllowCrossRendererResourceLoad(
160 request
, is_incognito
, extension
, extension_info_map
, &allowed
))
163 // Couldn't determine if resource is allowed. Block the load.
167 PrefService
* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
168 content::BrowserContext
* context
) {
169 return static_cast<Profile
*>(context
)->GetPrefs();
172 void ChromeExtensionsBrowserClient::GetEarlyExtensionPrefsObservers(
173 content::BrowserContext
* context
,
174 std::vector
<ExtensionPrefsObserver
*>* observers
) const {
175 observers
->push_back(ContentSettingsService::Get(context
));
178 ProcessManagerDelegate
*
179 ChromeExtensionsBrowserClient::GetProcessManagerDelegate() const {
180 return process_manager_delegate_
.get();
183 scoped_ptr
<ExtensionHostDelegate
>
184 ChromeExtensionsBrowserClient::CreateExtensionHostDelegate() {
185 return scoped_ptr
<ExtensionHostDelegate
>(new ChromeExtensionHostDelegate
);
188 bool ChromeExtensionsBrowserClient::DidVersionUpdate(
189 content::BrowserContext
* context
) {
190 Profile
* profile
= static_cast<Profile
*>(context
);
192 // Unit tests may not provide prefs; assume everything is up-to-date.
193 ExtensionPrefs
* extension_prefs
= ExtensionPrefs::Get(profile
);
194 if (!extension_prefs
)
197 // If we're inside a browser test, then assume prefs are all up-to-date.
198 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType
))
201 PrefService
* pref_service
= extension_prefs
->pref_service();
202 base::Version last_version
;
203 if (pref_service
->HasPrefPath(pref_names::kLastChromeVersion
)) {
204 std::string last_version_str
=
205 pref_service
->GetString(pref_names::kLastChromeVersion
);
206 last_version
= base::Version(last_version_str
);
209 std::string current_version
= version_info::GetVersionNumber();
210 pref_service
->SetString(pref_names::kLastChromeVersion
,
213 // If there was no version string in prefs, assume we're out of date.
214 if (!last_version
.IsValid())
217 return last_version
.IsOlderThan(current_version
);
220 void ChromeExtensionsBrowserClient::PermitExternalProtocolHandler() {
221 ExternalProtocolHandler::PermitLaunchUrl();
224 bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() {
225 return chrome::IsRunningInForcedAppMode();
228 ApiActivityMonitor
* ChromeExtensionsBrowserClient::GetApiActivityMonitor(
229 content::BrowserContext
* context
) {
230 // The ActivityLog monitors and records function calls and events.
231 return ActivityLog::GetInstance(context
);
234 ExtensionSystemProvider
*
235 ChromeExtensionsBrowserClient::GetExtensionSystemFactory() {
236 return ExtensionSystemFactory::GetInstance();
239 void ChromeExtensionsBrowserClient::RegisterExtensionFunctions(
240 ExtensionFunctionRegistry
* registry
) const {
242 registry
->RegisterFunction
<GetPreferenceFunction
>();
243 registry
->RegisterFunction
<SetPreferenceFunction
>();
244 registry
->RegisterFunction
<ClearPreferenceFunction
>();
246 // Direct Preference Access for Component Extensions.
247 registry
->RegisterFunction
<chromedirectsetting::GetDirectSettingFunction
>();
248 registry
->RegisterFunction
<chromedirectsetting::SetDirectSettingFunction
>();
249 registry
->RegisterFunction
<chromedirectsetting::ClearDirectSettingFunction
>();
251 // Generated APIs from lower-level modules.
252 api::GeneratedFunctionRegistry::RegisterAll(registry
);
254 // Generated APIs from Chrome.
255 api::ChromeGeneratedFunctionRegistry::RegisterAll(registry
);
258 void ChromeExtensionsBrowserClient::RegisterMojoServices(
259 content::RenderFrameHost
* render_frame_host
,
260 const Extension
* extension
) const {
261 RegisterServicesForFrame(render_frame_host
, extension
);
262 RegisterChromeServicesForFrame(render_frame_host
, extension
);
265 scoped_ptr
<RuntimeAPIDelegate
>
266 ChromeExtensionsBrowserClient::CreateRuntimeAPIDelegate(
267 content::BrowserContext
* context
) const {
268 return scoped_ptr
<RuntimeAPIDelegate
>(new ChromeRuntimeAPIDelegate(context
));
271 const ComponentExtensionResourceManager
*
272 ChromeExtensionsBrowserClient::GetComponentExtensionResourceManager() {
273 return resource_manager_
.get();
276 void ChromeExtensionsBrowserClient::BroadcastEventToRenderers(
277 events::HistogramValue histogram_value
,
278 const std::string
& event_name
,
279 scoped_ptr
<base::ListValue
> args
) {
280 g_browser_process
->extension_event_router_forwarder()
281 ->BroadcastEventToRenderers(histogram_value
, event_name
, args
.Pass(),
285 net::NetLog
* ChromeExtensionsBrowserClient::GetNetLog() {
286 return g_browser_process
->net_log();
289 ExtensionCache
* ChromeExtensionsBrowserClient::GetExtensionCache() {
290 if (!extension_cache_
.get()) {
291 #if defined(OS_CHROMEOS)
292 extension_cache_
.reset(new ExtensionCacheImpl(
293 make_scoped_ptr(new ChromeOSExtensionCacheDelegate())));
295 extension_cache_
.reset(new NullExtensionCache());
298 return extension_cache_
.get();
301 bool ChromeExtensionsBrowserClient::IsBackgroundUpdateAllowed() {
302 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
303 switches::kDisableBackgroundNetworking
);
306 bool ChromeExtensionsBrowserClient::IsMinBrowserVersionSupported(
307 const std::string
& min_version
) {
308 base::Version browser_version
=
309 base::Version(version_info::GetVersionNumber());
310 Version
browser_min_version(min_version
);
311 if (browser_version
.IsValid() && browser_min_version
.IsValid() &&
312 browser_min_version
.CompareTo(browser_version
) > 0) {
318 ExtensionWebContentsObserver
*
319 ChromeExtensionsBrowserClient::GetExtensionWebContentsObserver(
320 content::WebContents
* web_contents
) {
321 return ChromeExtensionWebContentsObserver::FromWebContents(web_contents
);
324 void ChromeExtensionsBrowserClient::ReportError(
325 content::BrowserContext
* context
,
326 scoped_ptr
<ExtensionError
> error
) {
327 ErrorConsole::Get(context
)->ReportError(error
.Pass());
330 void ChromeExtensionsBrowserClient::CleanUpWebView(
331 content::BrowserContext
* browser_context
,
332 int embedder_process_id
,
333 int view_instance_id
) {
334 // Clean up context menus for the WebView.
336 MenuManager::Get(Profile::FromBrowserContext(browser_context
));
337 menu_manager
->RemoveAllContextItems(
338 MenuItem::ExtensionKey("", embedder_process_id
, view_instance_id
));
341 void ChromeExtensionsBrowserClient::AttachExtensionTaskManagerTag(
342 content::WebContents
* web_contents
,
343 ViewType view_type
) {
345 case VIEW_TYPE_APP_WINDOW
:
346 case VIEW_TYPE_EXTENSION_BACKGROUND_PAGE
:
347 case VIEW_TYPE_EXTENSION_DIALOG
:
348 case VIEW_TYPE_EXTENSION_POPUP
:
349 case VIEW_TYPE_LAUNCHER_PAGE
:
350 case VIEW_TYPE_VIRTUAL_KEYBOARD
:
351 // These are the only types that are tracked by the ExtensionTag.
352 task_management::WebContentsTags::CreateForExtension(web_contents
,
356 case VIEW_TYPE_BACKGROUND_CONTENTS
:
357 case VIEW_TYPE_PANEL
:
358 case VIEW_TYPE_TAB_CONTENTS
:
359 // Those types are tracked by other tags:
360 // BACKGROUND_CONTENTS --> task_management::BackgroundContentsTag.
361 // PANEL --> task_management::PanelTag.
362 // TAB_CONTENTS --> task_management::TabContentsTag.
363 // These tags are created and attached to the web_contents in other
364 // locations, and they must be ignored here.
367 case VIEW_TYPE_INVALID
:
373 } // namespace extensions