Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: chrome/browser/extensions/extension_management.h

Issue 602803002: Refactor ExtensionManagement (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ext-fix
Patch Set: fix addressing #7 Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_management.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_ 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_ 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_
7 7
8 #include <map> 8 #include "base/containers/scoped_ptr_hash_map.h"
9 #include <string>
10 #include <vector>
11
12 #include "base/macros.h" 9 #include "base/macros.h"
13 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/singleton.h" 11 #include "base/memory/singleton.h"
15 #include "base/observer_list.h" 12 #include "base/observer_list.h"
16 #include "base/prefs/pref_change_registrar.h" 13 #include "base/prefs/pref_change_registrar.h"
17 #include "base/values.h" 14 #include "base/values.h"
18 #include "components/keyed_service/content/browser_context_keyed_service_factory .h" 15 #include "components/keyed_service/content/browser_context_keyed_service_factory .h"
19 #include "components/keyed_service/core/keyed_service.h" 16 #include "components/keyed_service/core/keyed_service.h"
20 #include "extensions/browser/management_policy.h" 17 #include "extensions/browser/management_policy.h"
21 #include "extensions/common/extension.h" 18 #include "extensions/common/extension.h"
22 #include "extensions/common/manifest.h" 19 #include "extensions/common/manifest.h"
23 #include "extensions/common/url_pattern_set.h"
24 20
25 class GURL; 21 class GURL;
26 class PrefService; 22 class PrefService;
27 23
28 namespace content { 24 namespace content {
29 class BrowserContext; 25 class BrowserContext;
30 } // namespace content 26 } // namespace content
31 27
32 namespace extensions { 28 namespace extensions {
33 29
30 namespace internal {
31
32 struct IndividualSettings;
33 struct GlobalSettings;
34
35 } // namespace internal
36
34 // Tracks the management policies that affect extensions and provides interfaces 37 // Tracks the management policies that affect extensions and provides interfaces
35 // for observing and obtaining the global settings for all extensions, as well 38 // for observing and obtaining the global settings for all extensions, as well
36 // as per-extension settings. 39 // as per-extension settings.
37 class ExtensionManagement : public KeyedService { 40 class ExtensionManagement : public KeyedService {
38 public: 41 public:
39 // Observer class for extension management settings changes. 42 // Observer class for extension management settings changes.
40 class Observer { 43 class Observer {
41 public: 44 public:
42 virtual ~Observer() {} 45 virtual ~Observer() {}
43 46
44 // Will be called when an extension management preference changes. 47 // Called when the extension management settings change.
45 virtual void OnExtensionManagementSettingsChanged() = 0; 48 virtual void OnExtensionManagementSettingsChanged() = 0;
46 }; 49 };
47 50
48 // Installation mode for extensions, default is INSTALLATION_ALLOWED. 51 // Installation mode for extensions, default is INSTALLATION_ALLOWED.
49 // * INSTALLATION_ALLOWED: Extension can be installed. 52 // * INSTALLATION_ALLOWED: Extension can be installed.
50 // * INSTALLATION_BLOCKED: Extension cannot be installed. 53 // * INSTALLATION_BLOCKED: Extension cannot be installed.
51 // * INSTALLATION_FORCED: Extension will be installed automatically 54 // * INSTALLATION_FORCED: Extension will be installed automatically
52 // and cannot be disabled. 55 // and cannot be disabled.
53 // * INSTALLATION_RECOMMENDED: Extension will be installed automatically but 56 // * INSTALLATION_RECOMMENDED: Extension will be installed automatically but
54 // can be disabled. 57 // can be disabled.
55 enum InstallationMode { 58 enum InstallationMode {
56 INSTALLATION_ALLOWED = 0, 59 INSTALLATION_ALLOWED = 0,
57 INSTALLATION_BLOCKED, 60 INSTALLATION_BLOCKED,
58 INSTALLATION_FORCED, 61 INSTALLATION_FORCED,
59 INSTALLATION_RECOMMENDED, 62 INSTALLATION_RECOMMENDED,
60 }; 63 };
61 64
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 IndividualSettings();
68 ~IndividualSettings();
69
70 void Reset();
71
72 // Extension installation mode. Setting this to INSTALLATION_FORCED or
73 // INSTALLATION_RECOMMENDED will enable extension auto-loading (only
74 // applicable to single extension), and in this case the |update_url| must
75 // be specified, containing the update URL for this extension.
76 // Note that |update_url| will be ignored for INSTALLATION_ALLOWED and
77 // INSTALLATION_BLOCKED installation mode.
78 // These settings will override the default settings, and unspecified
79 // settings will take value from default settings.
80 InstallationMode installation_mode;
81 std::string update_url;
82 };
83
84 // Global extension management settings, applicable to all extensions.
85 struct GlobalSettings {
86 GlobalSettings();
87 ~GlobalSettings();
88
89 void Reset();
90
91 // Settings specifying which URLs are allowed to install extensions, will be
92 // enforced only if |has_restricted_install_sources| is set to true.
93 URLPatternSet install_sources;
94 bool has_restricted_install_sources;
95
96 // Settings specifying all allowed app/extension types, will be enforced
97 // only of |has_restricted_allowed_types| is set to true.
98 std::vector<Manifest::Type> allowed_types;
99 bool has_restricted_allowed_types;
100 };
101
102 typedef std::map<ExtensionId, IndividualSettings> SettingsIdMap;
103
104 explicit ExtensionManagement(PrefService* pref_service); 65 explicit ExtensionManagement(PrefService* pref_service);
105 virtual ~ExtensionManagement(); 66 virtual ~ExtensionManagement();
106 67
107 void AddObserver(Observer* observer); 68 void AddObserver(Observer* observer);
108 void RemoveObserver(Observer* observer); 69 void RemoveObserver(Observer* observer);
109 70
110 // Get the ManagementPolicy::Provider controlled by extension management 71 // Get the ManagementPolicy::Provider controlled by extension management
111 // policy settings. 72 // policy settings.
112 ManagementPolicy::Provider* GetProvider(); 73 ManagementPolicy::Provider* GetProvider() const;
113 74
114 // Checks if extensions are blacklisted by default, by policy. When true, 75 // Checks if extensions are blacklisted by default, by policy. When true,
115 // this means that even extensions without an ID should be blacklisted (e.g. 76 // this means that even extensions without an ID should be blacklisted (e.g.
116 // from the command line, or when loaded as an unpacked extension). 77 // from the command line, or when loaded as an unpacked extension).
117 bool BlacklistedByDefault(); 78 bool BlacklistedByDefault() const;
79
80 // Returns installation mode for an extension.
81 InstallationMode GetInstallationMode(const ExtensionId& id) const;
118 82
119 // Returns the force install list, in format specified by 83 // Returns the force install list, in format specified by
120 // ExternalPolicyLoader::AddExtension(). 84 // ExternalPolicyLoader::AddExtension().
121 scoped_ptr<base::DictionaryValue> GetForceInstallList() const; 85 scoped_ptr<base::DictionaryValue> GetForceInstallList() const;
122 86
123 // Returns if an extension with id |id| is allowed to install or not. 87 // Returns if an extension with id |id| is allowed to install or not.
124 bool IsInstallationAllowed(const ExtensionId& id) const; 88 bool IsInstallationAllowed(const ExtensionId& id) const;
125 89
126 // Returns true if an extension download should be allowed to proceed. 90 // Returns true if an extension download should be allowed to proceed.
127 bool IsOffstoreInstallAllowed(const GURL& url, const GURL& referrer_url); 91 bool IsOffstoreInstallAllowed(const GURL& url,
92 const GURL& referrer_url) const;
128 93
129 // Helper function to read |settings_by_id_| with |id| as key. Returns a 94 // Returns true if an extension with manifest type |manifest_type| is
130 // constant reference to default settings if |id| does not exist. 95 // allowed to be installed.
131 const IndividualSettings& ReadById(const ExtensionId& id) const; 96 bool IsAllowedManifestType(Manifest::Type manifest_type) const;
132
133 // Returns a constant reference to |global_settings_|.
134 const GlobalSettings& ReadGlobalSettings() const;
135 97
136 private: 98 private:
99 typedef base::ScopedPtrHashMap<ExtensionId, internal::IndividualSettings>
100 SettingsIdMap;
101 friend class ExtensionManagementServiceTest;
102
137 // Load all extension management preferences from |pref_service|, and 103 // Load all extension management preferences from |pref_service|, and
138 // refresh the settings. 104 // refresh the settings.
139 void Refresh(); 105 void Refresh();
140 106
141 // Load preference with name |pref_name| and expected type |expected_type|. 107 // Load preference with name |pref_name| and expected type |expected_type|.
142 // If |force_managed| is true, only loading from the managed preference store 108 // If |force_managed| is true, only loading from the managed preference store
143 // is allowed. Returns NULL if the preference is not present, not allowed to 109 // is allowed. Returns NULL if the preference is not present, not allowed to
144 // be loaded from or has the wrong type. 110 // be loaded from or has the wrong type.
145 const base::Value* LoadPreference(const char* pref_name, 111 const base::Value* LoadPreference(const char* pref_name,
146 bool force_managed, 112 bool force_managed,
147 base::Value::Type expected_type); 113 base::Value::Type expected_type);
148 114
149 void OnExtensionPrefChanged(); 115 void OnExtensionPrefChanged();
150 void NotifyExtensionManagementPrefChanged(); 116 void NotifyExtensionManagementPrefChanged();
151 117
118 // Helper function to read |settings_by_id_| with |id| as key. Returns a
119 // constant reference to default settings if |id| does not exist.
120 const internal::IndividualSettings* ReadById(const ExtensionId& id) const;
121
122 // Returns a constant reference to |global_settings_|.
123 const internal::GlobalSettings* ReadGlobalSettings() const;
124
152 // Helper function to access |settings_by_id_| with |id| as key. 125 // Helper function to access |settings_by_id_| with |id| as key.
153 // Adds a new IndividualSettings entry to |settings_by_id_| if none exists for 126 // Adds a new IndividualSettings entry to |settings_by_id_| if none exists for
154 // |id| yet. 127 // |id| yet.
155 IndividualSettings* AccessById(const ExtensionId& id); 128 internal::IndividualSettings* AccessById(const ExtensionId& id);
156 129
157 // A map containing all IndividualSettings applied to an individual extension 130 // A map containing all IndividualSettings applied to an individual extension
158 // identified by extension ID. The extension ID is used as index key of the 131 // identified by extension ID. The extension ID is used as index key of the
159 // map. 132 // map.
160 // TODO(binjin): Add |settings_by_update_url_|, and implement mechanism for 133 // TODO(binjin): Add |settings_by_update_url_|, and implement mechanism for
161 // it. 134 // it.
162 SettingsIdMap settings_by_id_; 135 SettingsIdMap settings_by_id_;
163 136
164 // The default IndividualSettings. 137 // The default IndividualSettings.
165 // For extension settings applied to an individual extension (identified by 138 // For extension settings applied to an individual extension (identified by
166 // extension ID) or a group of extension (with specified extension update 139 // extension ID) or a group of extension (with specified extension update
167 // URL), all unspecified part will take value from |default_settings_|. 140 // URL), all unspecified part will take value from |default_settings_|.
168 // For all other extensions, all settings from |default_settings_| will be 141 // For all other extensions, all settings from |default_settings_| will be
169 // enforced. 142 // enforced.
170 IndividualSettings default_settings_; 143 scoped_ptr<internal::IndividualSettings> default_settings_;
171 144
172 // Extension settings applicable to all extensions. 145 // Extension settings applicable to all extensions.
173 GlobalSettings global_settings_; 146 scoped_ptr<internal::GlobalSettings> global_settings_;
174 147
175 PrefService* pref_service_; 148 PrefService* pref_service_;
176 149
177 ObserverList<Observer, true> observer_list_; 150 ObserverList<Observer, true> observer_list_;
178 PrefChangeRegistrar pref_change_registrar_; 151 PrefChangeRegistrar pref_change_registrar_;
179 scoped_ptr<ManagementPolicy::Provider> provider_; 152 scoped_ptr<ManagementPolicy::Provider> provider_;
180 153
181 DISALLOW_COPY_AND_ASSIGN(ExtensionManagement); 154 DISALLOW_COPY_AND_ASSIGN(ExtensionManagement);
182 }; 155 };
183 156
(...skipping 16 matching lines...) Expand all
200 content::BrowserContext* context) const OVERRIDE; 173 content::BrowserContext* context) const OVERRIDE;
201 virtual void RegisterProfilePrefs( 174 virtual void RegisterProfilePrefs(
202 user_prefs::PrefRegistrySyncable* registry) OVERRIDE; 175 user_prefs::PrefRegistrySyncable* registry) OVERRIDE;
203 176
204 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementFactory); 177 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementFactory);
205 }; 178 };
206 179
207 } // namespace extensions 180 } // namespace extensions
208 181
209 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_ 182 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/extension_management.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698