Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 <map> |
|
Joao da Silva
2014/09/25 14:09:41
Not used
binjin
2014/09/25 14:17:23
Done.
| |
| 9 #include <string> | |
| 10 #include <vector> | |
| 11 | 9 |
| 10 #include "base/containers/scoped_ptr_hash_map.h" | |
| 12 #include "base/macros.h" | 11 #include "base/macros.h" |
| 13 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
| 15 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
| 16 #include "base/prefs/pref_change_registrar.h" | 15 #include "base/prefs/pref_change_registrar.h" |
| 17 #include "base/values.h" | 16 #include "base/values.h" |
| 18 #include "components/keyed_service/content/browser_context_keyed_service_factory .h" | 17 #include "components/keyed_service/content/browser_context_keyed_service_factory .h" |
| 19 #include "components/keyed_service/core/keyed_service.h" | 18 #include "components/keyed_service/core/keyed_service.h" |
| 20 #include "extensions/browser/management_policy.h" | 19 #include "extensions/browser/management_policy.h" |
| 21 #include "extensions/common/extension.h" | 20 #include "extensions/common/extension.h" |
| 22 #include "extensions/common/manifest.h" | 21 #include "extensions/common/manifest.h" |
| 23 #include "extensions/common/url_pattern_set.h" | |
| 24 | 22 |
| 25 class GURL; | 23 class GURL; |
| 26 class PrefService; | 24 class PrefService; |
| 27 | 25 |
| 28 namespace content { | 26 namespace content { |
| 29 class BrowserContext; | 27 class BrowserContext; |
| 30 } // namespace content | 28 } // namespace content |
| 31 | 29 |
| 32 namespace extensions { | 30 namespace extensions { |
| 33 | 31 |
| 32 namespace internal { | |
| 33 | |
| 34 struct IndividualSettings; | |
| 35 struct GlobalSettings; | |
| 36 | |
| 37 } // namespace internal | |
| 38 | |
| 34 // Tracks the management policies that affect extensions and provides interfaces | 39 // Tracks the management policies that affect extensions and provides interfaces |
| 35 // for observing and obtaining the global settings for all extensions, as well | 40 // for observing and obtaining the global settings for all extensions, as well |
| 36 // as per-extension settings. | 41 // as per-extension settings. |
| 37 class ExtensionManagement : public KeyedService { | 42 class ExtensionManagement : public KeyedService { |
| 38 public: | 43 public: |
| 39 // Observer class for extension management settings changes. | 44 // Observer class for extension management settings changes. |
| 40 class Observer { | 45 class Observer { |
| 41 public: | 46 public: |
| 42 virtual ~Observer() {} | 47 virtual ~Observer() {} |
| 43 | 48 |
| 44 // Will be called when an extension management preference changes. | 49 // Called when the extension management settings change. |
| 45 virtual void OnExtensionManagementSettingsChanged() = 0; | 50 virtual void OnExtensionManagementSettingsChanged() = 0; |
| 46 }; | 51 }; |
| 47 | 52 |
| 48 // Installation mode for extensions, default is INSTALLATION_ALLOWED. | 53 // Installation mode for extensions, default is INSTALLATION_ALLOWED. |
| 49 // * INSTALLATION_ALLOWED: Extension can be installed. | 54 // * INSTALLATION_ALLOWED: Extension can be installed. |
| 50 // * INSTALLATION_BLOCKED: Extension cannot be installed. | 55 // * INSTALLATION_BLOCKED: Extension cannot be installed. |
| 51 // * INSTALLATION_FORCED: Extension will be installed automatically | 56 // * INSTALLATION_FORCED: Extension will be installed automatically |
| 52 // and cannot be disabled. | 57 // and cannot be disabled. |
| 53 // * INSTALLATION_RECOMMENDED: Extension will be installed automatically but | 58 // * INSTALLATION_RECOMMENDED: Extension will be installed automatically but |
| 54 // can be disabled. | 59 // can be disabled. |
| 55 enum InstallationMode { | 60 enum InstallationMode { |
| 56 INSTALLATION_ALLOWED = 0, | 61 INSTALLATION_ALLOWED = 0, |
| 57 INSTALLATION_BLOCKED, | 62 INSTALLATION_BLOCKED, |
| 58 INSTALLATION_FORCED, | 63 INSTALLATION_FORCED, |
| 59 INSTALLATION_RECOMMENDED, | 64 INSTALLATION_RECOMMENDED, |
| 60 }; | 65 }; |
| 61 | 66 |
| 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); | 67 explicit ExtensionManagement(PrefService* pref_service); |
| 105 virtual ~ExtensionManagement(); | 68 virtual ~ExtensionManagement(); |
| 106 | 69 |
| 107 void AddObserver(Observer* observer); | 70 void AddObserver(Observer* observer); |
| 108 void RemoveObserver(Observer* observer); | 71 void RemoveObserver(Observer* observer); |
| 109 | 72 |
| 110 // Get the ManagementPolicy::Provider controlled by extension management | 73 // Get the ManagementPolicy::Provider controlled by extension management |
| 111 // policy settings. | 74 // policy settings. |
| 112 ManagementPolicy::Provider* GetProvider(); | 75 ManagementPolicy::Provider* GetProvider() const; |
| 113 | 76 |
| 114 // Checks if extensions are blacklisted by default, by policy. When true, | 77 // 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. | 78 // 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). | 79 // from the command line, or when loaded as an unpacked extension). |
| 117 bool BlacklistedByDefault(); | 80 bool BlacklistedByDefault() const; |
| 81 | |
| 82 // Returns installation mode for an extension. | |
| 83 InstallationMode GetInstallationMode(const ExtensionId& id) const; | |
| 118 | 84 |
| 119 // Returns the force install list, in format specified by | 85 // Returns the force install list, in format specified by |
| 120 // ExternalPolicyLoader::AddExtension(). | 86 // ExternalPolicyLoader::AddExtension(). |
| 121 scoped_ptr<base::DictionaryValue> GetForceInstallList() const; | 87 scoped_ptr<base::DictionaryValue> GetForceInstallList() const; |
| 122 | 88 |
| 123 // Returns if an extension with id |id| is allowed to install or not. | 89 // Returns if an extension with id |id| is allowed to install or not. |
| 124 bool IsInstallationAllowed(const ExtensionId& id) const; | 90 bool IsInstallationAllowed(const ExtensionId& id) const; |
| 125 | 91 |
| 126 // Returns true if an extension download should be allowed to proceed. | 92 // Returns true if an extension download should be allowed to proceed. |
| 127 bool IsOffstoreInstallAllowed(const GURL& url, const GURL& referrer_url); | 93 bool IsOffstoreInstallAllowed(const GURL& url, |
| 94 const GURL& referrer_url) const; | |
| 128 | 95 |
| 129 // Helper function to read |settings_by_id_| with |id| as key. Returns a | 96 // Returns true if an extension with manifest type |manifest_type| is |
| 130 // constant reference to default settings if |id| does not exist. | 97 // allowed to be installed. |
| 131 const IndividualSettings& ReadById(const ExtensionId& id) const; | 98 bool IsAllowedManifestType(Manifest::Type manifest_type) const; |
| 132 | |
| 133 // Returns a constant reference to |global_settings_|. | |
| 134 const GlobalSettings& ReadGlobalSettings() const; | |
| 135 | 99 |
| 136 private: | 100 private: |
| 101 typedef base::ScopedPtrHashMap<ExtensionId, internal::IndividualSettings> | |
| 102 SettingsIdMap; | |
| 103 friend class ExtensionManagementServiceTest; | |
| 104 | |
| 137 // Load all extension management preferences from |pref_service|, and | 105 // Load all extension management preferences from |pref_service|, and |
| 138 // refresh the settings. | 106 // refresh the settings. |
| 139 void Refresh(); | 107 void Refresh(); |
| 140 | 108 |
| 141 // Load preference with name |pref_name| and expected type |expected_type|. | 109 // Load preference with name |pref_name| and expected type |expected_type|. |
| 142 // If |force_managed| is true, only loading from the managed preference store | 110 // 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 | 111 // is allowed. Returns NULL if the preference is not present, not allowed to |
| 144 // be loaded from or has the wrong type. | 112 // be loaded from or has the wrong type. |
| 145 const base::Value* LoadPreference(const char* pref_name, | 113 const base::Value* LoadPreference(const char* pref_name, |
| 146 bool force_managed, | 114 bool force_managed, |
| 147 base::Value::Type expected_type); | 115 base::Value::Type expected_type); |
| 148 | 116 |
| 149 void OnExtensionPrefChanged(); | 117 void OnExtensionPrefChanged(); |
| 150 void NotifyExtensionManagementPrefChanged(); | 118 void NotifyExtensionManagementPrefChanged(); |
| 151 | 119 |
| 120 // Helper function to read |settings_by_id_| with |id| as key. Returns a | |
| 121 // constant reference to default settings if |id| does not exist. | |
| 122 const internal::IndividualSettings* ReadById(const ExtensionId& id) const; | |
| 123 | |
| 124 // Returns a constant reference to |global_settings_|. | |
| 125 const internal::GlobalSettings* ReadGlobalSettings() const; | |
| 126 | |
| 152 // Helper function to access |settings_by_id_| with |id| as key. | 127 // 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 | 128 // Adds a new IndividualSettings entry to |settings_by_id_| if none exists for |
| 154 // |id| yet. | 129 // |id| yet. |
| 155 IndividualSettings* AccessById(const ExtensionId& id); | 130 internal::IndividualSettings* AccessById(const ExtensionId& id); |
| 156 | 131 |
| 157 // A map containing all IndividualSettings applied to an individual extension | 132 // 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 | 133 // identified by extension ID. The extension ID is used as index key of the |
| 159 // map. | 134 // map. |
| 160 // TODO(binjin): Add |settings_by_update_url_|, and implement mechanism for | 135 // TODO(binjin): Add |settings_by_update_url_|, and implement mechanism for |
| 161 // it. | 136 // it. |
| 162 SettingsIdMap settings_by_id_; | 137 SettingsIdMap settings_by_id_; |
| 163 | 138 |
| 164 // The default IndividualSettings. | 139 // The default IndividualSettings. |
| 165 // For extension settings applied to an individual extension (identified by | 140 // For extension settings applied to an individual extension (identified by |
| 166 // extension ID) or a group of extension (with specified extension update | 141 // extension ID) or a group of extension (with specified extension update |
| 167 // URL), all unspecified part will take value from |default_settings_|. | 142 // URL), all unspecified part will take value from |default_settings_|. |
| 168 // For all other extensions, all settings from |default_settings_| will be | 143 // For all other extensions, all settings from |default_settings_| will be |
| 169 // enforced. | 144 // enforced. |
| 170 IndividualSettings default_settings_; | 145 scoped_ptr<internal::IndividualSettings> default_settings_; |
| 171 | 146 |
| 172 // Extension settings applicable to all extensions. | 147 // Extension settings applicable to all extensions. |
| 173 GlobalSettings global_settings_; | 148 scoped_ptr<internal::GlobalSettings> global_settings_; |
| 174 | 149 |
| 175 PrefService* pref_service_; | 150 PrefService* pref_service_; |
| 176 | 151 |
| 177 ObserverList<Observer, true> observer_list_; | 152 ObserverList<Observer, true> observer_list_; |
| 178 PrefChangeRegistrar pref_change_registrar_; | 153 PrefChangeRegistrar pref_change_registrar_; |
| 179 scoped_ptr<ManagementPolicy::Provider> provider_; | 154 scoped_ptr<ManagementPolicy::Provider> provider_; |
| 180 | 155 |
| 181 DISALLOW_COPY_AND_ASSIGN(ExtensionManagement); | 156 DISALLOW_COPY_AND_ASSIGN(ExtensionManagement); |
| 182 }; | 157 }; |
| 183 | 158 |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 200 content::BrowserContext* context) const OVERRIDE; | 175 content::BrowserContext* context) const OVERRIDE; |
| 201 virtual void RegisterProfilePrefs( | 176 virtual void RegisterProfilePrefs( |
| 202 user_prefs::PrefRegistrySyncable* registry) OVERRIDE; | 177 user_prefs::PrefRegistrySyncable* registry) OVERRIDE; |
| 203 | 178 |
| 204 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementFactory); | 179 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementFactory); |
| 205 }; | 180 }; |
| 206 | 181 |
| 207 } // namespace extensions | 182 } // namespace extensions |
| 208 | 183 |
| 209 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_ | 184 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_ |
| OLD | NEW |