[email protected] | 2397ce8 | 2014-04-03 16:20:23 | [diff] [blame] | 1 | // Copyright (c) 2014 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/settings_api_helpers.h" |
| 6 | |
| 7 | #include "chrome/browser/extensions/api/preference/preference_api.h" |
| 8 | #include "chrome/common/pref_names.h" |
dbeam | 455f3cc | 2016-11-17 02:08:29 | [diff] [blame] | 9 | #include "chrome/common/url_constants.h" |
abhishek.a21 | 71c61285 | 2015-08-31 10:48:19 | [diff] [blame] | 10 | #include "components/proxy_config/proxy_config_pref_names.h" |
[email protected] | 2d3e710 | 2014-06-21 05:57:17 | [diff] [blame] | 11 | #include "components/search_engines/search_engines_pref_names.h" |
dbeam | 455f3cc | 2016-11-17 02:08:29 | [diff] [blame] | 12 | #include "content/public/browser/browser_url_handler.h" |
[email protected] | 8dc56d0 | 2014-06-07 00:44:23 | [diff] [blame] | 13 | #include "extensions/browser/extension_pref_value_map.h" |
| 14 | #include "extensions/browser/extension_pref_value_map_factory.h" |
[email protected] | 2397ce8 | 2014-04-03 16:20:23 | [diff] [blame] | 15 | #include "extensions/browser/extension_registry.h" |
dbeam | 455f3cc | 2016-11-17 02:08:29 | [diff] [blame] | 16 | #include "extensions/common/constants.h" |
[email protected] | 2397ce8 | 2014-04-03 16:20:23 | [diff] [blame] | 17 | #include "extensions/common/extension_set.h" |
dbeam | 455f3cc | 2016-11-17 02:08:29 | [diff] [blame] | 18 | #include "url/gurl.h" |
[email protected] | 2397ce8 | 2014-04-03 16:20:23 | [diff] [blame] | 19 | |
| 20 | namespace extensions { |
| 21 | |
[email protected] | 8a357df | 2014-06-11 11:09:13 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | // Returns which |extension| (if any) is overriding a particular |type| of |
| 25 | // setting. |
| 26 | const Extension* FindOverridingExtension( |
[email protected] | 2397ce8 | 2014-04-03 16:20:23 | [diff] [blame] | 27 | content::BrowserContext* browser_context, |
[email protected] | 8a357df | 2014-06-11 11:09:13 | [diff] [blame] | 28 | SettingsApiOverrideType type) { |
[email protected] | 8dc56d0 | 2014-06-07 00:44:23 | [diff] [blame] | 29 | const ExtensionSet& extensions = |
| 30 | ExtensionRegistry::Get(browser_context)->enabled_extensions(); |
[email protected] | 2397ce8 | 2014-04-03 16:20:23 | [diff] [blame] | 31 | |
[email protected] | 8dc56d0 | 2014-06-07 00:44:23 | [diff] [blame] | 32 | for (ExtensionSet::const_iterator it = extensions.begin(); |
[email protected] | 2397ce8 | 2014-04-03 16:20:23 | [diff] [blame] | 33 | it != extensions.end(); |
| 34 | ++it) { |
dcheng | c704794 | 2014-08-26 05:05:31 | [diff] [blame] | 35 | const SettingsOverrides* settings = SettingsOverrides::Get(it->get()); |
[email protected] | 2397ce8 | 2014-04-03 16:20:23 | [diff] [blame] | 36 | if (settings) { |
| 37 | if (type == BUBBLE_TYPE_HOME_PAGE && !settings->homepage) |
| 38 | continue; |
| 39 | if (type == BUBBLE_TYPE_STARTUP_PAGES && settings->startup_pages.empty()) |
| 40 | continue; |
| 41 | if (type == BUBBLE_TYPE_SEARCH_ENGINE && !settings->search_engine) |
| 42 | continue; |
| 43 | |
| 44 | std::string key; |
| 45 | switch (type) { |
| 46 | case BUBBLE_TYPE_HOME_PAGE: |
| 47 | key = prefs::kHomePage; |
| 48 | break; |
| 49 | case BUBBLE_TYPE_STARTUP_PAGES: |
| 50 | key = prefs::kRestoreOnStartup; |
| 51 | break; |
| 52 | case BUBBLE_TYPE_SEARCH_ENGINE: |
| 53 | key = prefs::kDefaultSearchProviderEnabled; |
| 54 | break; |
| 55 | } |
| 56 | |
| 57 | // Found an extension overriding the current type, check if primary. |
| 58 | PreferenceAPI* preference_api = PreferenceAPI::Get(browser_context); |
| 59 | if (preference_api && // Expected to be NULL in unit tests. |
| 60 | !preference_api->DoesExtensionControlPref((*it)->id(), key, NULL)) |
| 61 | continue; // Not primary. |
| 62 | |
[email protected] | 8a357df | 2014-06-11 11:09:13 | [diff] [blame] | 63 | // Found the primary extension. |
dcheng | c704794 | 2014-08-26 05:05:31 | [diff] [blame] | 64 | return it->get(); |
[email protected] | 2397ce8 | 2014-04-03 16:20:23 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | |
| 68 | return NULL; |
| 69 | } |
| 70 | |
[email protected] | 8a357df | 2014-06-11 11:09:13 | [diff] [blame] | 71 | } // namespace |
[email protected] | 2397ce8 | 2014-04-03 16:20:23 | [diff] [blame] | 72 | |
[email protected] | 8a357df | 2014-06-11 11:09:13 | [diff] [blame] | 73 | const Extension* GetExtensionOverridingHomepage( |
| 74 | content::BrowserContext* browser_context) { |
| 75 | return FindOverridingExtension(browser_context, BUBBLE_TYPE_HOME_PAGE); |
[email protected] | 2397ce8 | 2014-04-03 16:20:23 | [diff] [blame] | 76 | } |
| 77 | |
dbeam | 455f3cc | 2016-11-17 02:08:29 | [diff] [blame] | 78 | const Extension* GetExtensionOverridingNewTabPage( |
| 79 | content::BrowserContext* browser_context) { |
| 80 | GURL ntp_url(chrome::kChromeUINewTabURL); |
| 81 | bool ignored; |
| 82 | content::BrowserURLHandler::GetInstance()->RewriteURLIfNecessary( |
| 83 | &ntp_url, browser_context, &ignored); |
| 84 | if (ntp_url.SchemeIs(kExtensionScheme)) { |
| 85 | return ExtensionRegistry::Get(browser_context)->GetExtensionById( |
| 86 | ntp_url.host(), ExtensionRegistry::ENABLED); |
| 87 | } |
| 88 | return nullptr; |
| 89 | } |
| 90 | |
[email protected] | 8dc56d0 | 2014-06-07 00:44:23 | [diff] [blame] | 91 | const Extension* GetExtensionOverridingStartupPages( |
[email protected] | 8a357df | 2014-06-11 11:09:13 | [diff] [blame] | 92 | content::BrowserContext* browser_context) { |
| 93 | return FindOverridingExtension(browser_context, BUBBLE_TYPE_STARTUP_PAGES); |
[email protected] | 2397ce8 | 2014-04-03 16:20:23 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 8dc56d0 | 2014-06-07 00:44:23 | [diff] [blame] | 96 | const Extension* GetExtensionOverridingSearchEngine( |
[email protected] | 8a357df | 2014-06-11 11:09:13 | [diff] [blame] | 97 | content::BrowserContext* browser_context) { |
| 98 | return FindOverridingExtension(browser_context, BUBBLE_TYPE_SEARCH_ENGINE); |
[email protected] | 2397ce8 | 2014-04-03 16:20:23 | [diff] [blame] | 99 | } |
| 100 | |
[email protected] | 8dc56d0 | 2014-06-07 00:44:23 | [diff] [blame] | 101 | const Extension* GetExtensionOverridingProxy( |
| 102 | content::BrowserContext* browser_context) { |
| 103 | ExtensionPrefValueMap* extension_prefs_value_map = |
| 104 | ExtensionPrefValueMapFactory::GetForBrowserContext(browser_context); |
| 105 | if (!extension_prefs_value_map) |
| 106 | return NULL; // Can be null during testing. |
| 107 | std::string extension_id = |
abhishek.a21 | 71c61285 | 2015-08-31 10:48:19 | [diff] [blame] | 108 | extension_prefs_value_map->GetExtensionControllingPref( |
| 109 | proxy_config::prefs::kProxy); |
[email protected] | 8dc56d0 | 2014-06-07 00:44:23 | [diff] [blame] | 110 | if (extension_id.empty()) |
| 111 | return NULL; |
| 112 | return ExtensionRegistry::Get(browser_context)->GetExtensionById( |
| 113 | extension_id, ExtensionRegistry::ENABLED); |
| 114 | } |
| 115 | |
[email protected] | 2397ce8 | 2014-04-03 16:20:23 | [diff] [blame] | 116 | } // namespace extensions |