Rename GetIconID to GetIconId
[chromium-blink-merge.git] / chrome / browser / extensions / settings_api_helpers.cc
blobf7a60bff3a2c441cfb026f312240ca65adf1da48
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 {
18 namespace {
20 // Returns which |extension| (if any) is overriding a particular |type| of
21 // setting.
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();
30 ++it) {
31 const SettingsOverrides* settings = SettingsOverrides::Get(it->get());
32 if (settings) {
33 if (type == BUBBLE_TYPE_HOME_PAGE && !settings->homepage)
34 continue;
35 if (type == BUBBLE_TYPE_STARTUP_PAGES && settings->startup_pages.empty())
36 continue;
37 if (type == BUBBLE_TYPE_SEARCH_ENGINE && !settings->search_engine)
38 continue;
40 std::string key;
41 switch (type) {
42 case BUBBLE_TYPE_HOME_PAGE:
43 key = prefs::kHomePage;
44 break;
45 case BUBBLE_TYPE_STARTUP_PAGES:
46 key = prefs::kRestoreOnStartup;
47 break;
48 case BUBBLE_TYPE_SEARCH_ENGINE:
49 key = prefs::kDefaultSearchProviderEnabled;
50 break;
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.
60 return it->get();
64 return NULL;
67 } // namespace
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())
94 return NULL;
95 return ExtensionRegistry::Get(browser_context)->GetExtensionById(
96 extension_id, ExtensionRegistry::ENABLED);
99 } // namespace extensions