[email protected] | 9fe4204 | 2013-10-29 21:13:33 | [diff] [blame] | 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. |
| 4 | |
| 5 | #include "chrome/browser/extensions/chrome_extensions_browser_client.h" |
| 6 | |
[email protected] | 7f47421 | 2013-11-05 04:26:16 | [diff] [blame] | 7 | #include "base/command_line.h" |
[email protected] | 326e6f0 | 2014-06-20 04:53:37 | [diff] [blame] | 8 | #include "base/path_service.h" |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 9 | #include "base/version.h" |
[email protected] | 45928b68 | 2013-11-28 08:20:27 | [diff] [blame] | 10 | #include "chrome/browser/app_mode/app_mode_utils.h" |
[email protected] | 9fe4204 | 2013-10-29 21:13:33 | [diff] [blame] | 11 | #include "chrome/browser/browser_process.h" |
[email protected] | 13e062e | 2014-08-09 10:21:55 | [diff] [blame] | 12 | #include "chrome/browser/extensions/activity_log/activity_log.h" |
| 13 | #include "chrome/browser/extensions/api/chrome_extensions_api_client.h" |
| 14 | #include "chrome/browser/extensions/api/content_settings/content_settings_service.h" |
yoz | 774dcda2d | 2014-08-27 03:32:58 | [diff] [blame^] | 15 | #include "chrome/browser/extensions/api/generated_api_registration.h" |
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 16 | #include "chrome/browser/extensions/api/preference/chrome_direct_setting.h" |
| 17 | #include "chrome/browser/extensions/api/preference/preference_api.h" |
[email protected] | f0e9ac55 | 2014-05-07 14:26:50 | [diff] [blame] | 18 | #include "chrome/browser/extensions/api/runtime/chrome_runtime_api_delegate.h" |
[email protected] | bf5ee7cc | 2013-11-23 20:48:44 | [diff] [blame] | 19 | #include "chrome/browser/extensions/chrome_app_sorting.h" |
[email protected] | 326e6f0 | 2014-06-20 04:53:37 | [diff] [blame] | 20 | #include "chrome/browser/extensions/chrome_component_extension_resource_manager.h" |
[email protected] | 6c0f179f | 2014-03-19 11:04:41 | [diff] [blame] | 21 | #include "chrome/browser/extensions/chrome_extension_host_delegate.h" |
[email protected] | 13e062e | 2014-08-09 10:21:55 | [diff] [blame] | 22 | #include "chrome/browser/extensions/chrome_process_manager_delegate.h" |
[email protected] | 8172a9d | 2014-01-09 23:18:30 | [diff] [blame] | 23 | #include "chrome/browser/extensions/extension_system_factory.h" |
[email protected] | 6a3d8ce4 | 2014-02-07 00:42:37 | [diff] [blame] | 24 | #include "chrome/browser/extensions/extension_util.h" |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 25 | #include "chrome/browser/extensions/url_request_util.h" |
[email protected] | 0b9de03 | 2014-03-15 05:47:01 | [diff] [blame] | 26 | #include "chrome/browser/external_protocol/external_protocol_handler.h" |
[email protected] | 4d166378 | 2014-07-25 21:44:36 | [diff] [blame] | 27 | #include "chrome/browser/net/chrome_net_log.h" |
[email protected] | 9fe4204 | 2013-10-29 21:13:33 | [diff] [blame] | 28 | #include "chrome/browser/profiles/profile.h" |
[email protected] | 7f47421 | 2013-11-05 04:26:16 | [diff] [blame] | 29 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 326e6f0 | 2014-06-20 04:53:37 | [diff] [blame] | 30 | #include "chrome/common/chrome_paths.h" |
[email protected] | 7f47421 | 2013-11-05 04:26:16 | [diff] [blame] | 31 | #include "chrome/common/chrome_switches.h" |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 32 | #include "chrome/common/chrome_version_info.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 33 | #include "chrome/common/extensions/features/feature_channel.h" |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 34 | #include "chrome/common/pref_names.h" |
yoz | 774dcda2d | 2014-08-27 03:32:58 | [diff] [blame^] | 35 | #include "extensions/browser/api/generated_api_registration.h" |
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 36 | #include "extensions/browser/extension_function_registry.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 37 | #include "extensions/browser/extension_prefs.h" |
[email protected] | 234fc5ff | 2014-01-16 23:32:28 | [diff] [blame] | 38 | #include "extensions/browser/pref_names.h" |
[email protected] | 9fe4204 | 2013-10-29 21:13:33 | [diff] [blame] | 39 | |
[email protected] | d0d1c05 | 2013-12-21 07:20:22 | [diff] [blame] | 40 | #if defined(OS_CHROMEOS) |
| 41 | #include "chromeos/chromeos_switches.h" |
| 42 | #endif |
| 43 | |
[email protected] | 9fe4204 | 2013-10-29 21:13:33 | [diff] [blame] | 44 | namespace extensions { |
| 45 | |
[email protected] | be514c3 | 2014-03-17 23:30:44 | [diff] [blame] | 46 | ChromeExtensionsBrowserClient::ChromeExtensionsBrowserClient() { |
[email protected] | 6b54fda | 2014-07-22 02:13:47 | [diff] [blame] | 47 | process_manager_delegate_.reset(new ChromeProcessManagerDelegate); |
[email protected] | be514c3 | 2014-03-17 23:30:44 | [diff] [blame] | 48 | api_client_.reset(new ChromeExtensionsAPIClient); |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 49 | // Only set if it hasn't already been set (e.g. by a test). |
| 50 | if (GetCurrentChannel() == GetDefaultChannel()) |
| 51 | SetCurrentChannel(chrome::VersionInfo::GetChannel()); |
| 52 | } |
[email protected] | 9fe4204 | 2013-10-29 21:13:33 | [diff] [blame] | 53 | |
| 54 | ChromeExtensionsBrowserClient::~ChromeExtensionsBrowserClient() {} |
| 55 | |
| 56 | bool ChromeExtensionsBrowserClient::IsShuttingDown() { |
| 57 | return g_browser_process->IsShuttingDown(); |
| 58 | } |
| 59 | |
[email protected] | 367d9b17 | 2013-12-03 00:31:02 | [diff] [blame] | 60 | bool ChromeExtensionsBrowserClient::AreExtensionsDisabled( |
| 61 | const CommandLine& command_line, |
| 62 | content::BrowserContext* context) { |
| 63 | Profile* profile = static_cast<Profile*>(context); |
| 64 | return command_line.HasSwitch(switches::kDisableExtensions) || |
| 65 | profile->GetPrefs()->GetBoolean(prefs::kDisableExtensions); |
| 66 | } |
| 67 | |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 68 | bool ChromeExtensionsBrowserClient::IsValidContext( |
| 69 | content::BrowserContext* context) { |
| 70 | Profile* profile = static_cast<Profile*>(context); |
| 71 | return g_browser_process->profile_manager()->IsValidProfile(profile); |
| 72 | } |
| 73 | |
[email protected] | 9fe4204 | 2013-10-29 21:13:33 | [diff] [blame] | 74 | bool ChromeExtensionsBrowserClient::IsSameContext( |
| 75 | content::BrowserContext* first, |
| 76 | content::BrowserContext* second) { |
| 77 | return static_cast<Profile*>(first)->IsSameProfile( |
| 78 | static_cast<Profile*>(second)); |
| 79 | } |
| 80 | |
| 81 | bool ChromeExtensionsBrowserClient::HasOffTheRecordContext( |
| 82 | content::BrowserContext* context) { |
| 83 | return static_cast<Profile*>(context)->HasOffTheRecordProfile(); |
| 84 | } |
| 85 | |
| 86 | content::BrowserContext* ChromeExtensionsBrowserClient::GetOffTheRecordContext( |
| 87 | content::BrowserContext* context) { |
| 88 | return static_cast<Profile*>(context)->GetOffTheRecordProfile(); |
| 89 | } |
| 90 | |
[email protected] | 7f47421 | 2013-11-05 04:26:16 | [diff] [blame] | 91 | content::BrowserContext* ChromeExtensionsBrowserClient::GetOriginalContext( |
| 92 | content::BrowserContext* context) { |
| 93 | return static_cast<Profile*>(context)->GetOriginalProfile(); |
| 94 | } |
| 95 | |
[email protected] | a5fc73e | 2014-02-13 20:26:47 | [diff] [blame] | 96 | bool ChromeExtensionsBrowserClient::IsGuestSession( |
[email protected] | d012be2b | 2014-03-03 17:02:47 | [diff] [blame] | 97 | content::BrowserContext* context) const { |
[email protected] | a5fc73e | 2014-02-13 20:26:47 | [diff] [blame] | 98 | return static_cast<Profile*>(context)->IsGuestSession(); |
| 99 | } |
| 100 | |
[email protected] | 6a3d8ce4 | 2014-02-07 00:42:37 | [diff] [blame] | 101 | bool ChromeExtensionsBrowserClient::IsExtensionIncognitoEnabled( |
| 102 | const std::string& extension_id, |
| 103 | content::BrowserContext* context) const { |
[email protected] | d012be2b | 2014-03-03 17:02:47 | [diff] [blame] | 104 | return IsGuestSession(context) |
| 105 | || util::IsIncognitoEnabled(extension_id, context); |
[email protected] | 6a3d8ce4 | 2014-02-07 00:42:37 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | bool ChromeExtensionsBrowserClient::CanExtensionCrossIncognito( |
| 109 | const extensions::Extension* extension, |
| 110 | content::BrowserContext* context) const { |
[email protected] | d012be2b | 2014-03-03 17:02:47 | [diff] [blame] | 111 | return IsGuestSession(context) |
| 112 | || util::CanCrossIncognito(extension, context); |
[email protected] | 6a3d8ce4 | 2014-02-07 00:42:37 | [diff] [blame] | 113 | } |
| 114 | |
[email protected] | b32f2173 | 2014-04-16 18:25:16 | [diff] [blame] | 115 | bool ChromeExtensionsBrowserClient::IsWebViewRequest( |
| 116 | net::URLRequest* request) const { |
| 117 | return url_request_util::IsWebViewRequest(request); |
| 118 | } |
| 119 | |
[email protected] | 1791e6c9 | 2014-04-11 08:29:01 | [diff] [blame] | 120 | net::URLRequestJob* |
| 121 | ChromeExtensionsBrowserClient::MaybeCreateResourceBundleRequestJob( |
| 122 | net::URLRequest* request, |
| 123 | net::NetworkDelegate* network_delegate, |
| 124 | const base::FilePath& directory_path, |
| 125 | const std::string& content_security_policy, |
| 126 | bool send_cors_header) { |
| 127 | return url_request_util::MaybeCreateURLRequestResourceBundleJob( |
| 128 | request, |
| 129 | network_delegate, |
| 130 | directory_path, |
| 131 | content_security_policy, |
| 132 | send_cors_header); |
| 133 | } |
| 134 | |
| 135 | bool ChromeExtensionsBrowserClient::AllowCrossRendererResourceLoad( |
| 136 | net::URLRequest* request, |
| 137 | bool is_incognito, |
| 138 | const Extension* extension, |
| 139 | InfoMap* extension_info_map) { |
| 140 | return url_request_util::AllowCrossRendererResourceLoad( |
| 141 | request, is_incognito, extension, extension_info_map); |
| 142 | } |
| 143 | |
[email protected] | 367d9b17 | 2013-12-03 00:31:02 | [diff] [blame] | 144 | PrefService* ChromeExtensionsBrowserClient::GetPrefServiceForContext( |
| 145 | content::BrowserContext* context) { |
| 146 | return static_cast<Profile*>(context)->GetPrefs(); |
| 147 | } |
| 148 | |
[email protected] | d31e248 | 2014-04-02 22:52:20 | [diff] [blame] | 149 | void ChromeExtensionsBrowserClient::GetEarlyExtensionPrefsObservers( |
| 150 | content::BrowserContext* context, |
| 151 | std::vector<ExtensionPrefsObserver*>* observers) const { |
[email protected] | d31e248 | 2014-04-02 22:52:20 | [diff] [blame] | 152 | observers->push_back(ContentSettingsService::Get(context)); |
[email protected] | d31e248 | 2014-04-02 22:52:20 | [diff] [blame] | 153 | } |
| 154 | |
[email protected] | 6b54fda | 2014-07-22 02:13:47 | [diff] [blame] | 155 | ProcessManagerDelegate* |
| 156 | ChromeExtensionsBrowserClient::GetProcessManagerDelegate() const { |
[email protected] | 6b54fda | 2014-07-22 02:13:47 | [diff] [blame] | 157 | return process_manager_delegate_.get(); |
[email protected] | 7f47421 | 2013-11-05 04:26:16 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | 6c0f179f | 2014-03-19 11:04:41 | [diff] [blame] | 160 | scoped_ptr<ExtensionHostDelegate> |
| 161 | ChromeExtensionsBrowserClient::CreateExtensionHostDelegate() { |
| 162 | return scoped_ptr<ExtensionHostDelegate>(new ChromeExtensionHostDelegate); |
[email protected] | a36b5560 | 2014-02-08 02:32:32 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 165 | bool ChromeExtensionsBrowserClient::DidVersionUpdate( |
[email protected] | 513b803 | 2013-11-18 07:47:49 | [diff] [blame] | 166 | content::BrowserContext* context) { |
| 167 | Profile* profile = static_cast<Profile*>(context); |
| 168 | |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 169 | // Unit tests may not provide prefs; assume everything is up-to-date. |
[email protected] | 513b803 | 2013-11-18 07:47:49 | [diff] [blame] | 170 | ExtensionPrefs* extension_prefs = ExtensionPrefs::Get(profile); |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 171 | if (!extension_prefs) |
| 172 | return false; |
| 173 | |
| 174 | // If we're inside a browser test, then assume prefs are all up-to-date. |
| 175 | if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType)) |
| 176 | return false; |
| 177 | |
| 178 | PrefService* pref_service = extension_prefs->pref_service(); |
| 179 | base::Version last_version; |
[email protected] | 234fc5ff | 2014-01-16 23:32:28 | [diff] [blame] | 180 | if (pref_service->HasPrefPath(pref_names::kLastChromeVersion)) { |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 181 | std::string last_version_str = |
[email protected] | 234fc5ff | 2014-01-16 23:32:28 | [diff] [blame] | 182 | pref_service->GetString(pref_names::kLastChromeVersion); |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 183 | last_version = base::Version(last_version_str); |
| 184 | } |
| 185 | |
| 186 | chrome::VersionInfo current_version_info; |
| 187 | std::string current_version = current_version_info.Version(); |
[email protected] | 234fc5ff | 2014-01-16 23:32:28 | [diff] [blame] | 188 | pref_service->SetString(pref_names::kLastChromeVersion, |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 189 | current_version); |
| 190 | |
| 191 | // If there was no version string in prefs, assume we're out of date. |
| 192 | if (!last_version.IsValid()) |
| 193 | return true; |
| 194 | |
| 195 | return last_version.IsOlderThan(current_version); |
| 196 | } |
| 197 | |
[email protected] | 86fad30d | 2014-07-29 21:39:27 | [diff] [blame] | 198 | void ChromeExtensionsBrowserClient::PermitExternalProtocolHandler() { |
| 199 | ExternalProtocolHandler::PermitLaunchUrl(); |
| 200 | } |
| 201 | |
[email protected] | bf5ee7cc | 2013-11-23 20:48:44 | [diff] [blame] | 202 | scoped_ptr<AppSorting> ChromeExtensionsBrowserClient::CreateAppSorting() { |
[email protected] | 6c0f179f | 2014-03-19 11:04:41 | [diff] [blame] | 203 | return scoped_ptr<AppSorting>(new ChromeAppSorting()); |
[email protected] | bf5ee7cc | 2013-11-23 20:48:44 | [diff] [blame] | 204 | } |
| 205 | |
[email protected] | 45928b68 | 2013-11-28 08:20:27 | [diff] [blame] | 206 | bool ChromeExtensionsBrowserClient::IsRunningInForcedAppMode() { |
| 207 | return chrome::IsRunningInForcedAppMode(); |
| 208 | } |
| 209 | |
[email protected] | b32260f | 2014-02-06 10:03:41 | [diff] [blame] | 210 | ApiActivityMonitor* ChromeExtensionsBrowserClient::GetApiActivityMonitor( |
| 211 | content::BrowserContext* context) { |
| 212 | // The ActivityLog monitors and records function calls and events. |
| 213 | return ActivityLog::GetInstance(context); |
| 214 | } |
| 215 | |
[email protected] | 7048930 | 2014-02-13 15:15:48 | [diff] [blame] | 216 | ExtensionSystemProvider* |
| 217 | ChromeExtensionsBrowserClient::GetExtensionSystemFactory() { |
| 218 | return ExtensionSystemFactory::GetInstance(); |
[email protected] | 8172a9d | 2014-01-09 23:18:30 | [diff] [blame] | 219 | } |
| 220 | |
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 221 | void ChromeExtensionsBrowserClient::RegisterExtensionFunctions( |
| 222 | ExtensionFunctionRegistry* registry) const { |
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 223 | // Preferences. |
| 224 | registry->RegisterFunction<extensions::GetPreferenceFunction>(); |
| 225 | registry->RegisterFunction<extensions::SetPreferenceFunction>(); |
| 226 | registry->RegisterFunction<extensions::ClearPreferenceFunction>(); |
| 227 | |
| 228 | // Direct Preference Access for Component Extensions. |
| 229 | registry->RegisterFunction< |
| 230 | extensions::chromedirectsetting::GetDirectSettingFunction>(); |
| 231 | registry->RegisterFunction< |
| 232 | extensions::chromedirectsetting::SetDirectSettingFunction>(); |
| 233 | registry->RegisterFunction< |
| 234 | extensions::chromedirectsetting::ClearDirectSettingFunction>(); |
| 235 | |
[email protected] | e3a8ebb9 | 2014-03-31 01:54:38 | [diff] [blame] | 236 | // Generated APIs from lower-level modules. |
[email protected] | 68cb565 | 2014-03-12 06:41:29 | [diff] [blame] | 237 | extensions::core_api::GeneratedFunctionRegistry::RegisterAll(registry); |
[email protected] | e3a8ebb9 | 2014-03-31 01:54:38 | [diff] [blame] | 238 | |
| 239 | // Generated APIs from Chrome. |
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 240 | extensions::api::GeneratedFunctionRegistry::RegisterAll(registry); |
[email protected] | 21c6c43 | 2014-03-05 18:47:31 | [diff] [blame] | 241 | } |
| 242 | |
[email protected] | 326e6f0 | 2014-06-20 04:53:37 | [diff] [blame] | 243 | ComponentExtensionResourceManager* |
| 244 | ChromeExtensionsBrowserClient::GetComponentExtensionResourceManager() { |
| 245 | if (!resource_manager_) |
| 246 | resource_manager_.reset(new ChromeComponentExtensionResourceManager()); |
| 247 | return resource_manager_.get(); |
| 248 | } |
| 249 | |
[email protected] | 4d166378 | 2014-07-25 21:44:36 | [diff] [blame] | 250 | net::NetLog* ChromeExtensionsBrowserClient::GetNetLog() { |
| 251 | return g_browser_process->net_log(); |
| 252 | } |
| 253 | |
[email protected] | f0e9ac55 | 2014-05-07 14:26:50 | [diff] [blame] | 254 | scoped_ptr<extensions::RuntimeAPIDelegate> |
| 255 | ChromeExtensionsBrowserClient::CreateRuntimeAPIDelegate( |
| 256 | content::BrowserContext* context) const { |
| 257 | return scoped_ptr<extensions::RuntimeAPIDelegate>( |
| 258 | new ChromeRuntimeAPIDelegate(context)); |
[email protected] | f0e9ac55 | 2014-05-07 14:26:50 | [diff] [blame] | 259 | } |
| 260 | |
[email protected] | 9fe4204 | 2013-10-29 21:13:33 | [diff] [blame] | 261 | } // namespace extensions |