No dual_mode on Win10+ shortcuts.
[chromium-blink-merge.git] / chrome / browser / extensions / chrome_extensions_browser_client.cc
blob6a49327f91f79c0265379f40721b140aac6a61ba
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_app_sorting.h"
19 #include "chrome/browser/extensions/chrome_component_extension_resource_manager.h"
20 #include "chrome/browser/extensions/chrome_extension_host_delegate.h"
21 #include "chrome/browser/extensions/chrome_extension_web_contents_observer.h"
22 #include "chrome/browser/extensions/chrome_mojo_service_registration.h"
23 #include "chrome/browser/extensions/chrome_process_manager_delegate.h"
24 #include "chrome/browser/extensions/chrome_url_request_util.h"
25 #include "chrome/browser/extensions/error_console/error_console.h"
26 #include "chrome/browser/extensions/event_router_forwarder.h"
27 #include "chrome/browser/extensions/extension_system_factory.h"
28 #include "chrome/browser/extensions/extension_util.h"
29 #include "chrome/browser/extensions/menu_manager.h"
30 #include "chrome/browser/external_protocol/external_protocol_handler.h"
31 #include "chrome/browser/net/chrome_net_log.h"
32 #include "chrome/browser/profiles/profile.h"
33 #include "chrome/browser/profiles/profile_manager.h"
34 #include "chrome/common/chrome_paths.h"
35 #include "chrome/common/chrome_switches.h"
36 #include "chrome/common/chrome_version_info.h"
37 #include "chrome/common/extensions/features/feature_channel.h"
38 #include "chrome/common/pref_names.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"
52 #else
53 #include "extensions/browser/updater/null_extension_cache.h"
54 #endif
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::VersionInfo::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));
116 #endif
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);
137 net::URLRequestJob*
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(
145 request,
146 network_delegate,
147 directory_path,
148 content_security_policy,
149 send_cors_header);
152 bool ChromeExtensionsBrowserClient::AllowCrossRendererResourceLoad(
153 net::URLRequest* request,
154 bool is_incognito,
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))
160 return allowed;
162 // Couldn't determine if resource is allowed. Block the load.
163 return false;
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)
194 return false;
196 // If we're inside a browser test, then assume prefs are all up-to-date.
197 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType))
198 return false;
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 chrome::VersionInfo current_version_info;
209 std::string current_version = current_version_info.Version();
210 pref_service->SetString(pref_names::kLastChromeVersion,
211 current_version);
213 // If there was no version string in prefs, assume we're out of date.
214 if (!last_version.IsValid())
215 return true;
217 return last_version.IsOlderThan(current_version);
220 void ChromeExtensionsBrowserClient::PermitExternalProtocolHandler() {
221 ExternalProtocolHandler::PermitLaunchUrl();
224 scoped_ptr<AppSorting> ChromeExtensionsBrowserClient::CreateAppSorting(
225 content::BrowserContext* context) {
226 return scoped_ptr<AppSorting>(new ChromeAppSorting(context));
229 bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() {
230 return chrome::IsRunningInForcedAppMode();
233 ApiActivityMonitor* ChromeExtensionsBrowserClient::GetApiActivityMonitor(
234 content::BrowserContext* context) {
235 // The ActivityLog monitors and records function calls and events.
236 return ActivityLog::GetInstance(context);
239 ExtensionSystemProvider*
240 ChromeExtensionsBrowserClient::GetExtensionSystemFactory() {
241 return ExtensionSystemFactory::GetInstance();
244 void ChromeExtensionsBrowserClient::RegisterExtensionFunctions(
245 ExtensionFunctionRegistry* registry) const {
246 // Preferences.
247 registry->RegisterFunction<GetPreferenceFunction>();
248 registry->RegisterFunction<SetPreferenceFunction>();
249 registry->RegisterFunction<ClearPreferenceFunction>();
251 // Direct Preference Access for Component Extensions.
252 registry->RegisterFunction<chromedirectsetting::GetDirectSettingFunction>();
253 registry->RegisterFunction<chromedirectsetting::SetDirectSettingFunction>();
254 registry->RegisterFunction<chromedirectsetting::ClearDirectSettingFunction>();
256 // Generated APIs from lower-level modules.
257 api::GeneratedFunctionRegistry::RegisterAll(registry);
259 // Generated APIs from Chrome.
260 api::ChromeGeneratedFunctionRegistry::RegisterAll(registry);
263 void ChromeExtensionsBrowserClient::RegisterMojoServices(
264 content::RenderFrameHost* render_frame_host,
265 const Extension* extension) const {
266 RegisterServicesForFrame(render_frame_host, extension);
267 RegisterChromeServicesForFrame(render_frame_host, extension);
270 scoped_ptr<RuntimeAPIDelegate>
271 ChromeExtensionsBrowserClient::CreateRuntimeAPIDelegate(
272 content::BrowserContext* context) const {
273 return scoped_ptr<RuntimeAPIDelegate>(new ChromeRuntimeAPIDelegate(context));
276 const ComponentExtensionResourceManager*
277 ChromeExtensionsBrowserClient::GetComponentExtensionResourceManager() {
278 return resource_manager_.get();
281 void ChromeExtensionsBrowserClient::BroadcastEventToRenderers(
282 events::HistogramValue histogram_value,
283 const std::string& event_name,
284 scoped_ptr<base::ListValue> args) {
285 g_browser_process->extension_event_router_forwarder()
286 ->BroadcastEventToRenderers(histogram_value, event_name, args.Pass(),
287 GURL());
290 net::NetLog* ChromeExtensionsBrowserClient::GetNetLog() {
291 return g_browser_process->net_log();
294 ExtensionCache* ChromeExtensionsBrowserClient::GetExtensionCache() {
295 if (!extension_cache_.get()) {
296 #if defined(OS_CHROMEOS)
297 extension_cache_.reset(new ExtensionCacheImpl(
298 make_scoped_ptr(new ChromeOSExtensionCacheDelegate())));
299 #else
300 extension_cache_.reset(new NullExtensionCache());
301 #endif
303 return extension_cache_.get();
306 bool ChromeExtensionsBrowserClient::IsBackgroundUpdateAllowed() {
307 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
308 switches::kDisableBackgroundNetworking);
311 bool ChromeExtensionsBrowserClient::IsMinBrowserVersionSupported(
312 const std::string& min_version) {
313 chrome::VersionInfo version_info;
314 base::Version browser_version = base::Version(version_info.Version());
315 Version browser_min_version(min_version);
316 if (browser_version.IsValid() && browser_min_version.IsValid() &&
317 browser_min_version.CompareTo(browser_version) > 0) {
318 return false;
320 return true;
323 ExtensionWebContentsObserver*
324 ChromeExtensionsBrowserClient::GetExtensionWebContentsObserver(
325 content::WebContents* web_contents) {
326 return ChromeExtensionWebContentsObserver::FromWebContents(web_contents);
329 void ChromeExtensionsBrowserClient::ReportError(
330 content::BrowserContext* context,
331 scoped_ptr<ExtensionError> error) {
332 ErrorConsole::Get(context)->ReportError(error.Pass());
335 void ChromeExtensionsBrowserClient::CleanUpWebView(int embedder_process_id,
336 int view_instance_id) {
337 content::BrowserContext* browser_context =
338 content::RenderProcessHost::FromID(embedder_process_id)
339 ->GetBrowserContext();
341 // Clean up context menus for the WebView.
342 auto menu_manager =
343 MenuManager::Get(Profile::FromBrowserContext(browser_context));
344 menu_manager->RemoveAllContextItems(
345 MenuItem::ExtensionKey("", embedder_process_id, view_instance_id));
348 } // namespace extensions