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/search_engines/search_engines_pref_names.h"
10 #include "extensions/browser/extension_pref_value_map.h"
11 #include "extensions/browser/extension_pref_value_map_factory.h"
12 #include "extensions/browser/extension_registry.h"
13 #include "extensions/common/extension_set.h"
15 namespace extensions
{
19 // Returns which |extension| (if any) is overriding a particular |type| of
21 const Extension
* FindOverridingExtension(
22 content::BrowserContext
* browser_context
,
23 SettingsApiOverrideType type
) {
24 const ExtensionSet
& extensions
=
25 ExtensionRegistry::Get(browser_context
)->enabled_extensions();
27 for (ExtensionSet::const_iterator it
= extensions
.begin();
28 it
!= extensions
.end();
30 const SettingsOverrides
* settings
= SettingsOverrides::Get(it
->get());
32 if (type
== BUBBLE_TYPE_HOME_PAGE
&& !settings
->homepage
)
34 if (type
== BUBBLE_TYPE_STARTUP_PAGES
&& settings
->startup_pages
.empty())
36 if (type
== BUBBLE_TYPE_SEARCH_ENGINE
&& !settings
->search_engine
)
41 case BUBBLE_TYPE_HOME_PAGE
:
42 key
= prefs::kHomePage
;
44 case BUBBLE_TYPE_STARTUP_PAGES
:
45 key
= prefs::kRestoreOnStartup
;
47 case BUBBLE_TYPE_SEARCH_ENGINE
:
48 key
= prefs::kDefaultSearchProviderEnabled
;
52 // Found an extension overriding the current type, check if primary.
53 PreferenceAPI
* preference_api
= PreferenceAPI::Get(browser_context
);
54 if (preference_api
&& // Expected to be NULL in unit tests.
55 !preference_api
->DoesExtensionControlPref((*it
)->id(), key
, NULL
))
56 continue; // Not primary.
58 // Found the primary extension.
68 const Extension
* GetExtensionOverridingHomepage(
69 content::BrowserContext
* browser_context
) {
70 return FindOverridingExtension(browser_context
, BUBBLE_TYPE_HOME_PAGE
);
73 const Extension
* GetExtensionOverridingStartupPages(
74 content::BrowserContext
* browser_context
) {
75 return FindOverridingExtension(browser_context
, BUBBLE_TYPE_STARTUP_PAGES
);
78 const Extension
* GetExtensionOverridingSearchEngine(
79 content::BrowserContext
* browser_context
) {
80 return FindOverridingExtension(browser_context
, BUBBLE_TYPE_SEARCH_ENGINE
);
83 const Extension
* GetExtensionOverridingProxy(
84 content::BrowserContext
* browser_context
) {
85 ExtensionPrefValueMap
* extension_prefs_value_map
=
86 ExtensionPrefValueMapFactory::GetForBrowserContext(browser_context
);
87 if (!extension_prefs_value_map
)
88 return NULL
; // Can be null during testing.
89 std::string extension_id
=
90 extension_prefs_value_map
->GetExtensionControllingPref(prefs::kProxy
);
91 if (extension_id
.empty())
93 return ExtensionRegistry::Get(browser_context
)->GetExtensionById(
94 extension_id
, ExtensionRegistry::ENABLED
);
97 } // namespace extensions