Add ENABLE_MEDIA_ROUTER define to builds other than Android and iOS.
[chromium-blink-merge.git] / chrome / browser / extensions / chrome_extensions_browser_client.cc
blob1e838eaa4a4f78a9e122c3d02b8e488229c86237
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_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/event_router_forwarder.h"
25 #include "chrome/browser/extensions/extension_system_factory.h"
26 #include "chrome/browser/extensions/extension_util.h"
27 #include "chrome/browser/external_protocol/external_protocol_handler.h"
28 #include "chrome/browser/net/chrome_net_log.h"
29 #include "chrome/browser/profiles/profile.h"
30 #include "chrome/browser/profiles/profile_manager.h"
31 #include "chrome/common/chrome_paths.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/chrome_version_info.h"
34 #include "chrome/common/extensions/features/feature_channel.h"
35 #include "chrome/common/pref_names.h"
36 #include "extensions/browser/api/generated_api_registration.h"
37 #include "extensions/browser/extension_function_registry.h"
38 #include "extensions/browser/extension_prefs.h"
39 #include "extensions/browser/mojo/service_registration.h"
40 #include "extensions/browser/pref_names.h"
41 #include "extensions/browser/url_request_util.h"
43 #if defined(OS_CHROMEOS)
44 #include "chrome/browser/chromeos/profiles/profile_helper.h"
45 #include "chrome/browser/extensions/updater/chromeos_extension_cache_delegate.h"
46 #include "chrome/browser/extensions/updater/extension_cache_impl.h"
47 #include "chromeos/chromeos_switches.h"
48 #else
49 #include "extensions/browser/updater/null_extension_cache.h"
50 #endif
52 namespace extensions {
54 ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() {
55 process_manager_delegate_.reset(new ChromeProcessManagerDelegate);
56 api_client_.reset(new ChromeExtensionsAPIClient);
57 // Only set if it hasn't already been set (e.g. by a test).
58 if (GetCurrentChannel() == GetDefaultChannel())
59 SetCurrentChannel(chrome::VersionInfo::GetChannel());
60 resource_manager_.reset(new ChromeComponentExtensionResourceManager());
63 ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {}
65 bool ChromeExtensionsBrowserClient::IsShuttingDown() {
66 return g_browser_process->IsShuttingDown();
69 bool ChromeExtensionsBrowserClient::AreExtensionsDisabled(
70 const base::CommandLine& command_line,
71 content::BrowserContext* context) {
72 Profile* profile = static_cast<Profile*>(context);
73 return command_line.HasSwitch(switches::kDisableExtensions) ||
74 profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions);
77 bool ChromeExtensionsBrowserClient::IsValidContext(
78 content::BrowserContext* context) {
79 Profile* profile = static_cast<Profile*>(context);
80 return g_browser_process->profile_manager() &&
81 g_browser_process->profile_manager()->IsValidProfile(profile);
84 bool ChromeExtensionsBrowserClient::IsSameContext(
85 content::BrowserContext* first,
86 content::BrowserContext* second) {
87 return static_cast<Profile*>(first)->IsSameProfile(
88 static_cast<Profile*>(second));
91 bool ChromeExtensionsBrowserClient::HasOffTheRecordContext(
92 content::BrowserContext* context) {
93 return static_cast<Profile*>(context)->HasOffTheRecordProfile();
96 content::BrowserContext* ChromeExtensionsBrowserClient::GetOffTheRecordContext(
97 content::BrowserContext* context) {
98 return static_cast<Profile*>(context)->GetOffTheRecordProfile();
101 content::BrowserContext* ChromeExtensionsBrowserClient::GetOriginalContext(
102 content::BrowserContext* context) {
103 return static_cast<Profile*>(context)->GetOriginalProfile();
106 #if defined(OS_CHROMEOS)
107 std::string ChromeExtensionsBrowserClient::GetUserIdHashFromContext(
108 content::BrowserContext* context) {
109 return chromeos::ProfileHelper::GetUserIdHashFromProfile(
110 static_cast<Profile*>(context));
112 #endif
114 bool ChromeExtensionsBrowserClient::IsGuestSession(
115 content::BrowserContext* context) const {
116 return static_cast<Profile*>(context)->IsGuestSession();
119 bool ChromeExtensionsBrowserClient::IsExtensionIncognitoEnabled(
120 const std::string& extension_id,
121 content::BrowserContext* context) const {
122 return IsGuestSession(context)
123 || util::IsIncognitoEnabled(extension_id, context);
126 bool ChromeExtensionsBrowserClient::CanExtensionCrossIncognito(
127 const extensions::Extension* extension,
128 content::BrowserContext* context) const {
129 return IsGuestSession(context)
130 || util::CanCrossIncognito(extension, context);
133 net::URLRequestJob*
134 ChromeExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob(
135 net::URLRequest* request,
136 net::NetworkDelegate* network_delegate,
137 const base::FilePath& directory_path,
138 const std::string& content_security_policy,
139 bool send_cors_header) {
140 return chrome_url_request_util::MaybeCreateURLRequestResourceBundleJob(
141 request,
142 network_delegate,
143 directory_path,
144 content_security_policy,
145 send_cors_header);
148 bool ChromeExtensionsBrowserClient::AllowCrossRendererResourceLoad(
149 net::URLRequest* request,
150 bool is_incognito,
151 const Extension* extension,
152 InfoMap* extension_info_map) {
153 bool allowed = false;
154 if (chrome_url_request_util::AllowCrossRendererResourceLoad(
155 request, is_incognito, extension, extension_info_map, &allowed))
156 return allowed;
158 // Couldn't determine if resource is allowed. Block the load.
159 return false;
162 PrefService* ChromeExtensionsBrowserClient::GetPrefServiceForContext(
163 content::BrowserContext* context) {
164 return static_cast<Profile*>(context)->GetPrefs();
167 void ChromeExtensionsBrowserClient::GetEarlyExtensionPrefsObservers(
168 content::BrowserContext* context,
169 std::vector<ExtensionPrefsObserver*>* observers) const {
170 observers->push_back(ContentSettingsService::Get(context));
173 ProcessManagerDelegate*
174 ChromeExtensionsBrowserClient::GetProcessManagerDelegate() const {
175 return process_manager_delegate_.get();
178 scoped_ptr<ExtensionHostDelegate>
179 ChromeExtensionsBrowserClient::CreateExtensionHostDelegate() {
180 return scoped_ptr<ExtensionHostDelegate>(new ChromeExtensionHostDelegate);
183 bool ChromeExtensionsBrowserClient::DidVersionUpdate(
184 content::BrowserContext* context) {
185 Profile* profile = static_cast<Profile*>(context);
187 // Unit tests may not provide prefs; assume everything is up-to-date.
188 ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile);
189 if (!extension_prefs)
190 return false;
192 // If we're inside a browser test, then assume prefs are all up-to-date.
193 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType))
194 return false;
196 PrefService* pref_service = extension_prefs->pref_service();
197 base::Version last_version;
198 if (pref_service->HasPrefPath(pref_names::kLastChromeVersion)) {
199 std::string last_version_str =
200 pref_service->GetString(pref_names::kLastChromeVersion);
201 last_version = base::Version(last_version_str);
204 chrome::VersionInfo current_version_info;
205 std::string current_version = current_version_info.Version();
206 pref_service->SetString(pref_names::kLastChromeVersion,
207 current_version);
209 // If there was no version string in prefs, assume we're out of date.
210 if (!last_version.IsValid())
211 return true;
213 return last_version.IsOlderThan(current_version);
216 void ChromeExtensionsBrowserClient::PermitExternalProtocolHandler() {
217 ExternalProtocolHandler::PermitLaunchUrl();
220 scoped_ptr<AppSorting> ChromeExtensionsBrowserClient::CreateAppSorting() {
221 return scoped_ptr<AppSorting>(new ChromeAppSorting());
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 {
241 // Preferences.
242 registry->RegisterFunction<extensions::GetPreferenceFunction>();
243 registry->RegisterFunction<extensions::SetPreferenceFunction>();
244 registry->RegisterFunction<extensions::ClearPreferenceFunction>();
246 // Direct Preference Access for Component Extensions.
247 registry->RegisterFunction<
248 extensions::chromedirectsetting::GetDirectSettingFunction>();
249 registry->RegisterFunction<
250 extensions::chromedirectsetting::SetDirectSettingFunction>();
251 registry->RegisterFunction<
252 extensions::chromedirectsetting::ClearDirectSettingFunction>();
254 // Generated APIs from lower-level modules.
255 extensions::core_api::GeneratedFunctionRegistry::RegisterAll(registry);
257 // Generated APIs from Chrome.
258 extensions::api::GeneratedFunctionRegistry::RegisterAll(registry);
261 void ChromeExtensionsBrowserClient::RegisterMojoServices(
262 content::RenderFrameHost* render_frame_host,
263 const Extension* extension) const {
264 RegisterServicesForFrame(render_frame_host, extension);
265 RegisterChromeServicesForFrame(render_frame_host, extension);
268 scoped_ptr<extensions::RuntimeAPIDelegate>
269 ChromeExtensionsBrowserClient::CreateRuntimeAPIDelegate(
270 content::BrowserContext* context) const {
271 return scoped_ptr<extensions::RuntimeAPIDelegate>(
272 new ChromeRuntimeAPIDelegate(context));
275 const ComponentExtensionResourceManager*
276 ChromeExtensionsBrowserClient::GetComponentExtensionResourceManager() {
277 return resource_manager_.get();
280 void ChromeExtensionsBrowserClient::BroadcastEventToRenderers(
281 const std::string& event_name,
282 scoped_ptr<base::ListValue> args) {
283 g_browser_process->extension_event_router_forwarder()
284 ->BroadcastEventToRenderers(event_name, args.Pass(), GURL());
287 net::NetLog* ChromeExtensionsBrowserClient::GetNetLog() {
288 return g_browser_process->net_log();
291 ExtensionCache* ChromeExtensionsBrowserClient::GetExtensionCache() {
292 if (!extension_cache_.get()) {
293 #if defined(OS_CHROMEOS)
294 extension_cache_.reset(new ExtensionCacheImpl(
295 make_scoped_ptr(new ChromeOSExtensionCacheDelegate())));
296 #else
297 extension_cache_.reset(new NullExtensionCache());
298 #endif
300 return extension_cache_.get();
303 bool ChromeExtensionsBrowserClient::IsBackgroundUpdateAllowed() {
304 return !base::CommandLine::ForCurrentProcess()->HasSwitch(
305 switches::kDisableBackgroundNetworking);
308 bool ChromeExtensionsBrowserClient::IsMinBrowserVersionSupported(
309 const std::string& min_version) {
310 chrome::VersionInfo version_info;
311 base::Version browser_version = base::Version(version_info.Version());
312 Version browser_min_version(min_version);
313 if (browser_version.IsValid() && browser_min_version.IsValid() &&
314 browser_min_version.CompareTo(browser_version) > 0) {
315 return false;
317 return true;
320 } // namespace extensions