Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / chrome / browser / extensions / chrome_extensions_browser_client.cc
blobb35c4d1b0590d25a88f533d1820c98f46b6a505d
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/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"
53 #else
54 #include "extensions/browser/updater/null_extension_cache.h"
55 #endif
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));
117 #endif
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);
138 net::URLRequestJob*
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(
146 request,
147 network_delegate,
148 directory_path,
149 content_security_policy,
150 send_cors_header);
153 bool ChromeExtensionsBrowserClient::AllowCrossRendererResourceLoad(
154 net::URLRequest* request,
155 bool is_incognito,
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))
161 return allowed;
163 // Couldn't determine if resource is allowed. Block the load.
164 return false;
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)
195 return false;
197 // If we're inside a browser test, then assume prefs are all up-to-date.
198 if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kTestType))
199 return false;
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,
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 base::Version browser_version =
314 base::Version(version_info::GetVersionNumber());
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(
336 content::BrowserContext* browser_context,
337 int embedder_process_id,
338 int view_instance_id) {
339 // Clean up context menus for the WebView.
340 auto menu_manager =
341 MenuManager::Get(Profile::FromBrowserContext(browser_context));
342 menu_manager->RemoveAllContextItems(
343 MenuItem::ExtensionKey("", embedder_process_id, view_instance_id));
346 } // namespace extensions