Deprecate direct use of legacy extension management preference
[chromium-blink-merge.git] / chrome / browser / extensions / extension_management.h
blob1bfe39b4d8e68137b09356b53425d2b9c8291330
1 // Copyright 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 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/singleton.h"
15 #include "base/observer_list.h"
16 #include "base/prefs/pref_change_registrar.h"
17 #include "base/values.h"
18 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
19 #include "components/keyed_service/core/keyed_service.h"
20 #include "extensions/browser/management_policy.h"
21 #include "extensions/common/extension.h"
22 #include "extensions/common/manifest.h"
23 #include "extensions/common/url_pattern_set.h"
25 class GURL;
26 class PrefService;
28 namespace content {
29 class BrowserContext;
30 } // namespace content
32 namespace extensions {
34 // Tracks the management policies that affect extensions and provides interfaces
35 // for observing and obtaining the global settings for all extensions, as well
36 // as per-extension settings.
37 class ExtensionManagement : public KeyedService {
38 public:
39 // Observer class for extension management settings changes.
40 class Observer {
41 public:
42 virtual ~Observer() {}
44 // Will be called when an extension management preference changes.
45 virtual void OnExtensionManagementSettingsChanged() = 0;
48 // Installation mode for extensions, default is INSTALLATION_ALLOWED.
49 // * INSTALLATION_ALLOWED: Extension can be installed.
50 // * INSTALLATION_BLOCKED: Extension cannot be installed.
51 // * INSTALLATION_FORCED: Extension will be installed automatically
52 // and cannot be disabled.
53 // * INSTALLATION_RECOMMENDED: Extension will be installed automatically but
54 // can be disabled.
55 enum InstallationMode {
56 INSTALLATION_ALLOWED = 0,
57 INSTALLATION_BLOCKED,
58 INSTALLATION_FORCED,
59 INSTALLATION_RECOMMENDED,
62 // Class to hold extension management settings for one or a group of
63 // extensions. Settings can be applied to an individual extension identified
64 // by an ID, a group of extensions with specific |update_url| or all
65 // extensions at once.
66 struct IndividualSettings {
67 void Reset();
69 // Extension installation mode. Setting this to INSTALLATION_FORCED or
70 // INSTALLATION_RECOMMENDED will enable extension auto-loading (only
71 // applicable to single extension), and in this case the |update_url| must
72 // be specified, containing the update URL for this extension.
73 // Note that |update_url| will be ignored for INSTALLATION_ALLOWED and
74 // INSTALLATION_BLOCKED installation mode.
75 InstallationMode installation_mode;
76 std::string update_url;
79 // Global extension management settings, applicable to all extensions.
80 struct GlobalSettings {
81 GlobalSettings();
82 ~GlobalSettings();
84 void Reset();
86 // Settings specifying which URLs are allowed to install extensions, will be
87 // enforced only if |has_restricted_install_sources| is set to true.
88 URLPatternSet install_sources;
89 bool has_restricted_install_sources;
91 // Settings specifying all allowed app/extension types, will be enforced
92 // only of |has_restricted_allowed_types| is set to true.
93 std::vector<Manifest::Type> allowed_types;
94 bool has_restricted_allowed_types;
97 typedef std::map<ExtensionId, IndividualSettings> SettingsIdMap;
99 explicit ExtensionManagement(PrefService* pref_service);
100 virtual ~ExtensionManagement();
102 void AddObserver(Observer* observer);
103 void RemoveObserver(Observer* observer);
105 // Get the ManagementPolicy::Provider controlled by extension management
106 // policy settings.
107 ManagementPolicy::Provider* GetProvider();
109 // Checks if extensions are blacklisted by default, by policy. When true,
110 // this means that even extensions without an ID should be blacklisted (e.g.
111 // from the command line, or when loaded as an unpacked extension).
112 bool BlacklistedByDefault();
114 // Returns the force install list, in format specified by
115 // ExternalPolicyLoader::AddExtension().
116 scoped_ptr<base::DictionaryValue> GetForceInstallList() const;
118 // Returns if an extension with id |id| is allowed to install or not.
119 bool IsInstallationAllowed(const ExtensionId& id) const;
121 // Returns true if an extension download should be allowed to proceed.
122 bool IsOffstoreInstallAllowed(const GURL& url, const GURL& referrer_url);
124 // Helper function to read |settings_by_id_| with |id| as key. Returns a
125 // constant reference to default settings if |id| does not exist.
126 const IndividualSettings& ReadById(const ExtensionId& id) const;
128 // Returns a constant reference to |global_settings_|.
129 const GlobalSettings& ReadGlobalSettings() const;
131 private:
132 // Load all extension management preferences from |pref_service|, and
133 // refresh the settings.
134 void Refresh();
136 // Load preference with name |pref_name| and expected type |expected_type|.
137 // If |force_managed| is true, only loading from the managed preference store
138 // is allowed. Returns NULL if the preference is not present, not allowed to
139 // be loaded from or has the wrong type.
140 const base::Value* LoadPreference(const char* pref_name,
141 bool force_managed,
142 base::Value::Type expected_type);
144 void OnExtensionPrefChanged();
145 void NotifyExtensionManagementPrefChanged();
147 // Helper function to access |settings_by_id_| with |id| as key.
148 // Adds a new IndividualSettings entry to |settings_by_id_| if none exists for
149 // |id| yet.
150 IndividualSettings* AccessById(const ExtensionId& id);
152 // A map containing all IndividualSettings applied to an individual extension
153 // identified by extension ID. The extension ID is used as index key of the
154 // map.
155 // TODO(binjin): Add |settings_by_update_url_|, and implement mechanism for
156 // it.
157 SettingsIdMap settings_by_id_;
159 // The default IndividualSettings.
160 // For extension settings applied to an individual extension (identified by
161 // extension ID) or a group of extension (with specified extension update
162 // URL), all unspecified part will take value from |default_settings_|.
163 // For all other extensions, all settings from |default_settings_| will be
164 // enforced.
165 IndividualSettings default_settings_;
167 // Extension settings applicable to all extensions.
168 GlobalSettings global_settings_;
170 PrefService* pref_service_;
172 ObserverList<Observer, true> observer_list_;
173 PrefChangeRegistrar pref_change_registrar_;
174 scoped_ptr<ManagementPolicy::Provider> provider_;
176 DISALLOW_COPY_AND_ASSIGN(ExtensionManagement);
179 class ExtensionManagementFactory : public BrowserContextKeyedServiceFactory {
180 public:
181 static ExtensionManagement* GetForBrowserContext(
182 content::BrowserContext* context);
183 static ExtensionManagementFactory* GetInstance();
185 private:
186 friend struct DefaultSingletonTraits<ExtensionManagementFactory>;
188 ExtensionManagementFactory();
189 virtual ~ExtensionManagementFactory();
191 // BrowserContextKeyedServiceExtensionManagementFactory:
192 virtual KeyedService* BuildServiceInstanceFor(
193 content::BrowserContext* context) const OVERRIDE;
194 virtual content::BrowserContext* GetBrowserContextToUse(
195 content::BrowserContext* context) const OVERRIDE;
197 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementFactory);
200 } // namespace extensions
202 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_