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.
5 #include "chrome/browser/extensions/settings_api_helpers.h"
7 #include "chrome/browser/extensions/api/preference/preference_api.h"
8 #include "chrome/common/pref_names.h"
9 #include "components/proxy_config/proxy_config_pref_names.h"
10 #include "components/search_engines/search_engines_pref_names.h"
11 #include "extensions/browser/extension_pref_value_map.h"
12 #include "extensions/browser/extension_pref_value_map_factory.h"
13 #include "extensions/browser/extension_registry.h"
14 #include "extensions/common/extension_set.h"
16 namespace extensions
{
20 // Returns which |extension| (if any) is overriding a particular |type| of
22 const Extension
* FindOverridingExtension(
23 content::BrowserContext
* browser_context
,
24 SettingsApiOverrideType type
) {
25 const ExtensionSet
& extensions
=
26 ExtensionRegistry::Get(browser_context
)->enabled_extensions();
28 for (ExtensionSet::const_iterator it
= extensions
.begin();
29 it
!= extensions
.end();
31 const SettingsOverrides
* settings
= SettingsOverrides::Get(it
->get());
33 if (type
== BUBBLE_TYPE_HOME_PAGE
&& !settings
->homepage
)
35 if (type
== BUBBLE_TYPE_STARTUP_PAGES
&& settings
->startup_pages
.empty())
37 if (type
== BUBBLE_TYPE_SEARCH_ENGINE
&& !settings
->search_engine
)
42 case BUBBLE_TYPE_HOME_PAGE
:
43 key
= prefs::kHomePage
;
45 case BUBBLE_TYPE_STARTUP_PAGES
:
46 key
= prefs::kRestoreOnStartup
;
48 case BUBBLE_TYPE_SEARCH_ENGINE
:
49 key
= prefs::kDefaultSearchProviderEnabled
;
53 // Found an extension overriding the current type, check if primary.
54 PreferenceAPI
* preference_api
= PreferenceAPI::Get(browser_context
);
55 if (preference_api
&& // Expected to be NULL in unit tests.
56 !preference_api
->DoesExtensionControlPref((*it
)->id(), key
, NULL
))
57 continue; // Not primary.
59 // Found the primary extension.
69 const Extension
* GetExtensionOverridingHomepage(
70 content::BrowserContext
* browser_context
) {
71 return FindOverridingExtension(browser_context
, BUBBLE_TYPE_HOME_PAGE
);
74 const Extension
* GetExtensionOverridingStartupPages(
75 content::BrowserContext
* browser_context
) {
76 return FindOverridingExtension(browser_context
, BUBBLE_TYPE_STARTUP_PAGES
);
79 const Extension
* GetExtensionOverridingSearchEngine(
80 content::BrowserContext
* browser_context
) {
81 return FindOverridingExtension(browser_context
, BUBBLE_TYPE_SEARCH_ENGINE
);
84 const Extension
* GetExtensionOverridingProxy(
85 content::BrowserContext
* browser_context
) {
86 ExtensionPrefValueMap
* extension_prefs_value_map
=
87 ExtensionPrefValueMapFactory::GetForBrowserContext(browser_context
);
88 if (!extension_prefs_value_map
)
89 return NULL
; // Can be null during testing.
90 std::string extension_id
=
91 extension_prefs_value_map
->GetExtensionControllingPref(
92 proxy_config::prefs::kProxy
);
93 if (extension_id
.empty())
95 return ExtensionRegistry::Get(browser_context
)->GetExtensionById(
96 extension_id
, ExtensionRegistry::ENABLED
);
99 } // namespace extensions