• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"
9 #include "extensions/browser/extension_pref_value_map.h"
10 #include "extensions/browser/extension_pref_value_map_factory.h"
11 #include "extensions/browser/extension_registry.h"
12 #include "extensions/common/extension_set.h"
13 
14 namespace extensions {
15 
16 namespace {
17 
18 // Returns which |extension| (if any) is overriding a particular |type| of
19 // setting.
FindOverridingExtension(content::BrowserContext * browser_context,SettingsApiOverrideType type)20 const Extension* FindOverridingExtension(
21     content::BrowserContext* browser_context,
22     SettingsApiOverrideType type) {
23   const ExtensionSet& extensions =
24       ExtensionRegistry::Get(browser_context)->enabled_extensions();
25 
26   for (ExtensionSet::const_iterator it = extensions.begin();
27        it != extensions.end();
28        ++it) {
29     const SettingsOverrides* settings = SettingsOverrides::Get(*it);
30     if (settings) {
31       if (type == BUBBLE_TYPE_HOME_PAGE && !settings->homepage)
32         continue;
33       if (type == BUBBLE_TYPE_STARTUP_PAGES && settings->startup_pages.empty())
34         continue;
35       if (type == BUBBLE_TYPE_SEARCH_ENGINE && !settings->search_engine)
36         continue;
37 
38       std::string key;
39       switch (type) {
40         case BUBBLE_TYPE_HOME_PAGE:
41           key = prefs::kHomePage;
42           break;
43         case BUBBLE_TYPE_STARTUP_PAGES:
44           key = prefs::kRestoreOnStartup;
45           break;
46         case BUBBLE_TYPE_SEARCH_ENGINE:
47           key = prefs::kDefaultSearchProviderEnabled;
48           break;
49       }
50 
51       // Found an extension overriding the current type, check if primary.
52       PreferenceAPI* preference_api = PreferenceAPI::Get(browser_context);
53       if (preference_api &&  // Expected to be NULL in unit tests.
54           !preference_api->DoesExtensionControlPref((*it)->id(), key, NULL))
55         continue;  // Not primary.
56 
57       // Found the primary extension.
58       return *it;
59     }
60   }
61 
62   return NULL;
63 }
64 
65 }  // namespace
66 
GetExtensionOverridingHomepage(content::BrowserContext * browser_context)67 const Extension* GetExtensionOverridingHomepage(
68     content::BrowserContext* browser_context) {
69   return FindOverridingExtension(browser_context, BUBBLE_TYPE_HOME_PAGE);
70 }
71 
GetExtensionOverridingStartupPages(content::BrowserContext * browser_context)72 const Extension* GetExtensionOverridingStartupPages(
73     content::BrowserContext* browser_context) {
74   return FindOverridingExtension(browser_context, BUBBLE_TYPE_STARTUP_PAGES);
75 }
76 
GetExtensionOverridingSearchEngine(content::BrowserContext * browser_context)77 const Extension* GetExtensionOverridingSearchEngine(
78     content::BrowserContext* browser_context) {
79   return FindOverridingExtension(browser_context, BUBBLE_TYPE_SEARCH_ENGINE);
80 }
81 
GetExtensionOverridingProxy(content::BrowserContext * browser_context)82 const Extension* GetExtensionOverridingProxy(
83     content::BrowserContext* browser_context) {
84   ExtensionPrefValueMap* extension_prefs_value_map =
85       ExtensionPrefValueMapFactory::GetForBrowserContext(browser_context);
86   if (!extension_prefs_value_map)
87     return NULL;  // Can be null during testing.
88   std::string extension_id =
89       extension_prefs_value_map->GetExtensionControllingPref(prefs::kProxy);
90   if (extension_id.empty())
91     return NULL;
92   return ExtensionRegistry::Get(browser_context)->GetExtensionById(
93       extension_id, ExtensionRegistry::ENABLED);
94 }
95 
96 }  // namespace extensions
97