blob: d7ffea9a5b7592821ef626326fbb7161173ae517 [file] [log] [blame]
binjin5f405ef2014-09-03 21:23:161// 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.
4
5#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_
7
dchengc963c7142016-04-08 03:55:228#include <memory>
binjin685ade82014-11-06 09:53:569#include <string>
avi3ec9c0d2016-12-27 22:38:0610#include <unordered_map>
binjine6b58b52014-10-31 01:55:5711#include <vector>
12
binjin5f405ef2014-09-03 21:23:1613#include "base/macros.h"
binjine6b58b52014-10-31 01:55:5714#include "base/memory/ref_counted.h"
binjin1569c9b2014-09-05 13:33:1815#include "base/memory/singleton.h"
16#include "base/observer_list.h"
binjin5f405ef2014-09-03 21:23:1617#include "base/values.h"
binjin1569c9b2014-09-05 13:33:1818#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
19#include "components/keyed_service/core/keyed_service.h"
brettwb1fc1b82016-02-02 00:19:0820#include "components/prefs/pref_change_registrar.h"
binjin1569c9b2014-09-05 13:33:1821#include "extensions/browser/management_policy.h"
rdevlin.cronin0670b562016-07-02 02:05:4322#include "extensions/common/extension_id.h"
binjin5f405ef2014-09-03 21:23:1623#include "extensions/common/manifest.h"
binjin5f405ef2014-09-03 21:23:1624
binjin311ecdf2014-09-12 22:56:5225class GURL;
binjin5f405ef2014-09-03 21:23:1626class PrefService;
27
binjin1569c9b2014-09-05 13:33:1828namespace content {
29class BrowserContext;
30} // namespace content
31
binjin5f405ef2014-09-03 21:23:1632namespace extensions {
33
binjin81d7c552014-10-02 11:47:1234namespace internal {
35
36struct IndividualSettings;
37struct GlobalSettings;
38
39} // namespace internal
40
binjine6b58b52014-10-31 01:55:5741class APIPermissionSet;
rdevlin.cronin0670b562016-07-02 02:05:4342class Extension;
binjine6b58b52014-10-31 01:55:5743class PermissionSet;
44
binjin5f405ef2014-09-03 21:23:1645// Tracks the management policies that affect extensions and provides interfaces
46// for observing and obtaining the global settings for all extensions, as well
47// as per-extension settings.
binjin1569c9b2014-09-05 13:33:1848class ExtensionManagement : public KeyedService {
binjin5f405ef2014-09-03 21:23:1649 public:
binjin1569c9b2014-09-05 13:33:1850 // Observer class for extension management settings changes.
51 class Observer {
52 public:
53 virtual ~Observer() {}
54
binjin81d7c552014-10-02 11:47:1255 // Called when the extension management settings change.
binjin1569c9b2014-09-05 13:33:1856 virtual void OnExtensionManagementSettingsChanged() = 0;
57 };
58
binjin5f405ef2014-09-03 21:23:1659 // Installation mode for extensions, default is INSTALLATION_ALLOWED.
60 // * INSTALLATION_ALLOWED: Extension can be installed.
61 // * INSTALLATION_BLOCKED: Extension cannot be installed.
62 // * INSTALLATION_FORCED: Extension will be installed automatically
63 // and cannot be disabled.
64 // * INSTALLATION_RECOMMENDED: Extension will be installed automatically but
65 // can be disabled.
66 enum InstallationMode {
67 INSTALLATION_ALLOWED = 0,
68 INSTALLATION_BLOCKED,
69 INSTALLATION_FORCED,
70 INSTALLATION_RECOMMENDED,
71 };
72
achuith4607f072017-03-08 11:49:1373 ExtensionManagement(PrefService* pref_service, bool is_signin_profile);
dchengae36a4a2014-10-21 12:36:3674 ~ExtensionManagement() override;
binjin5f405ef2014-09-03 21:23:1675
binjine6b58b52014-10-31 01:55:5776 // KeyedService implementations:
77 void Shutdown() override;
78
binjin1569c9b2014-09-05 13:33:1879 void AddObserver(Observer* observer);
80 void RemoveObserver(Observer* observer);
81
binjine6b58b52014-10-31 01:55:5782 // Get the list of ManagementPolicy::Provider controlled by extension
83 // management policy settings.
lazyboy4aeef202016-09-07 21:28:5984 const std::vector<std::unique_ptr<ManagementPolicy::Provider>>& GetProviders()
85 const;
binjin1569c9b2014-09-05 13:33:1886
87 // Checks if extensions are blacklisted by default, by policy. When true,
88 // this means that even extensions without an ID should be blacklisted (e.g.
89 // from the command line, or when loaded as an unpacked extension).
binjin81d7c552014-10-02 11:47:1290 bool BlacklistedByDefault() const;
91
92 // Returns installation mode for an extension.
binjin685ade82014-11-06 09:53:5693 InstallationMode GetInstallationMode(const Extension* extension) const;
binjin5f405ef2014-09-03 21:23:1694
binjin30301062014-09-08 20:27:3495 // Returns the force install list, in format specified by
96 // ExternalPolicyLoader::AddExtension().
dchengc963c7142016-04-08 03:55:2297 std::unique_ptr<base::DictionaryValue> GetForceInstallList() const;
binjin30301062014-09-08 20:27:3498
binjincccacef2014-10-13 19:00:2099 // Like GetForceInstallList(), but returns recommended install list instead.
dchengc963c7142016-04-08 03:55:22100 std::unique_ptr<base::DictionaryValue> GetRecommendedInstallList() const;
binjincccacef2014-10-13 19:00:20101
binjinc641add2014-10-15 16:20:45102 // Returns if an extension with id |id| is explicitly allowed by enterprise
103 // policy or not.
104 bool IsInstallationExplicitlyAllowed(const ExtensionId& id) const;
binjin30301062014-09-08 20:27:34105
binjin311ecdf2014-09-12 22:56:52106 // Returns true if an extension download should be allowed to proceed.
binjin81d7c552014-10-02 11:47:12107 bool IsOffstoreInstallAllowed(const GURL& url,
108 const GURL& referrer_url) const;
binjin311ecdf2014-09-12 22:56:52109
binjin81d7c552014-10-02 11:47:12110 // Returns true if an extension with manifest type |manifest_type| is
111 // allowed to be installed.
112 bool IsAllowedManifestType(Manifest::Type manifest_type) const;
binjin5f405ef2014-09-03 21:23:16113
binjin685ade82014-11-06 09:53:56114 // Returns the list of blocked API permissions for |extension|.
115 APIPermissionSet GetBlockedAPIPermissions(const Extension* extension) const;
binjine6b58b52014-10-31 01:55:57116
nrpeter40e16382017-04-13 17:34:58117 // Returns the list of hosts blocked by policy for |extension|.
118 const URLPatternSet& GetRuntimeBlockedHosts(const Extension* extension) const;
119
nrpetere33d2a5b2017-04-25 00:12:31120 // Returns the hosts exempted by policy from the RuntimeBlockedHosts for
121 // |extension|.
nrpeter40e16382017-04-13 17:34:58122 const URLPatternSet& GetRuntimeAllowedHosts(const Extension* extension) const;
123
nrpetere33d2a5b2017-04-25 00:12:31124 // Returns the list of hosts blocked by policy for Default scope. This can be
125 // overridden by an invividual scope which is queried via
126 // GetRuntimeBlockedHosts.
127 const URLPatternSet& GetDefaultRuntimeBlockedHosts() const;
128
129 // Returns the hosts exempted by policy from RuntimeBlockedHosts for
130 // the default scope. This can be overridden by an individual scope which is
131 // queries via GetRuntimeAllowedHosts. This should only be used to
132 // initialize a new renderer.
133 const URLPatternSet& GetDefaultRuntimeAllowedHosts() const;
134
135 // Checks if an |extension| has its own runtime_blocked_hosts or
136 // runtime_allowed_hosts defined in the individual scope of the
137 // ExtensionSettings policy.
138 // Returns false if an individual scoped setting isn't defined.
139 bool UsesDefaultRuntimeHostRestrictions(const Extension* extension) const;
140
nrpeter40e16382017-04-13 17:34:58141 // Checks if a URL is on the blocked host permissions list for a specific
142 // extension.
nrpetere33d2a5b2017-04-25 00:12:31143 bool IsRuntimeBlockedHost(const Extension* extension, const GURL& url) const;
nrpeter40e16382017-04-13 17:34:58144
binjin685ade82014-11-06 09:53:56145 // Returns blocked permission set for |extension|.
dchengc963c7142016-04-08 03:55:22146 std::unique_ptr<const PermissionSet> GetBlockedPermissions(
binjin685ade82014-11-06 09:53:56147 const Extension* extension) const;
binjine6b58b52014-10-31 01:55:57148
nrpeter2362e7e2017-05-10 17:21:26149 // If the extension is blocked from install and a custom error message
150 // was defined returns it. Otherwise returns an empty string. The maximum
151 // string length is 1000 characters.
152 const std::string BlockedInstallMessage(const ExtensionId& id) const;
153
binjin685ade82014-11-06 09:53:56154 // Returns true if every permission in |perms| is allowed for |extension|.
155 bool IsPermissionSetAllowed(const Extension* extension,
rdevlin.cronine2d0fd02015-09-24 22:35:49156 const PermissionSet& perms) const;
binjine6b58b52014-10-31 01:55:57157
binjin8e3d0182014-12-04 16:44:28158 // Returns true if |extension| meets the minimum required version set for it.
159 // If there is no such requirement set for it, returns true as well.
160 // If false is returned and |required_version| is not null, the minimum
161 // required version is returned.
162 bool CheckMinimumVersion(const Extension* extension,
163 std::string* required_version) const;
164
binjin5f405ef2014-09-03 21:23:16165 private:
avi3ec9c0d2016-12-27 22:38:06166 using SettingsIdMap =
167 std::unordered_map<ExtensionId,
168 std::unique_ptr<internal::IndividualSettings>>;
169 using SettingsUpdateUrlMap =
170 std::unordered_map<std::string,
171 std::unique_ptr<internal::IndividualSettings>>;
binjin81d7c552014-10-02 11:47:12172 friend class ExtensionManagementServiceTest;
173
binjin1569c9b2014-09-05 13:33:18174 // Load all extension management preferences from |pref_service|, and
175 // refresh the settings.
176 void Refresh();
177
binjin5f405ef2014-09-03 21:23:16178 // Load preference with name |pref_name| and expected type |expected_type|.
179 // If |force_managed| is true, only loading from the managed preference store
180 // is allowed. Returns NULL if the preference is not present, not allowed to
181 // be loaded from or has the wrong type.
182 const base::Value* LoadPreference(const char* pref_name,
183 bool force_managed,
184 base::Value::Type expected_type);
185
binjin1569c9b2014-09-05 13:33:18186 void OnExtensionPrefChanged();
187 void NotifyExtensionManagementPrefChanged();
188
achuith4607f072017-03-08 11:49:13189 // Helper to return an extension install list, in format specified by
190 // ExternalPolicyLoader::AddExtension().
191 std::unique_ptr<base::DictionaryValue> GetInstallListByMode(
192 InstallationMode installation_mode) const;
193
194 // Helper to update |extension_dict| for forced installs.
195 void UpdateForcedExtensions(const base::DictionaryValue* extension_dict);
196
binjin5f405ef2014-09-03 21:23:16197 // Helper function to access |settings_by_id_| with |id| as key.
198 // Adds a new IndividualSettings entry to |settings_by_id_| if none exists for
199 // |id| yet.
binjin81d7c552014-10-02 11:47:12200 internal::IndividualSettings* AccessById(const ExtensionId& id);
binjin5f405ef2014-09-03 21:23:16201
binjin685ade82014-11-06 09:53:56202 // Similar to AccessById(), but access |settings_by_update_url_| instead.
203 internal::IndividualSettings* AccessByUpdateUrl(
204 const std::string& update_url);
205
binjin5f405ef2014-09-03 21:23:16206 // A map containing all IndividualSettings applied to an individual extension
207 // identified by extension ID. The extension ID is used as index key of the
208 // map.
binjin5f405ef2014-09-03 21:23:16209 SettingsIdMap settings_by_id_;
210
binjin685ade82014-11-06 09:53:56211 // Similar to |settings_by_id_|, but contains the settings for a group of
212 // extensions with same update URL. The update url itself is used as index
213 // key for the map.
214 SettingsUpdateUrlMap settings_by_update_url_;
215
binjin5f405ef2014-09-03 21:23:16216 // The default IndividualSettings.
217 // For extension settings applied to an individual extension (identified by
218 // extension ID) or a group of extension (with specified extension update
219 // URL), all unspecified part will take value from |default_settings_|.
220 // For all other extensions, all settings from |default_settings_| will be
221 // enforced.
dchengc963c7142016-04-08 03:55:22222 std::unique_ptr<internal::IndividualSettings> default_settings_;
binjin5f405ef2014-09-03 21:23:16223
224 // Extension settings applicable to all extensions.
dchengc963c7142016-04-08 03:55:22225 std::unique_ptr<internal::GlobalSettings> global_settings_;
binjin5f405ef2014-09-03 21:23:16226
achuith4607f072017-03-08 11:49:13227 PrefService* pref_service_ = nullptr;
228 bool is_signin_profile_ = false;
binjin5f405ef2014-09-03 21:23:16229
brettwd195c952015-06-02 17:31:12230 base::ObserverList<Observer, true> observer_list_;
binjin1569c9b2014-09-05 13:33:18231 PrefChangeRegistrar pref_change_registrar_;
lazyboy4aeef202016-09-07 21:28:59232 std::vector<std::unique_ptr<ManagementPolicy::Provider>> providers_;
binjin1569c9b2014-09-05 13:33:18233
binjin5f405ef2014-09-03 21:23:16234 DISALLOW_COPY_AND_ASSIGN(ExtensionManagement);
235};
236
binjin1569c9b2014-09-05 13:33:18237class ExtensionManagementFactory : public BrowserContextKeyedServiceFactory {
238 public:
239 static ExtensionManagement* GetForBrowserContext(
240 content::BrowserContext* context);
241 static ExtensionManagementFactory* GetInstance();
242
243 private:
olli.raula36aa8be2015-09-10 11:14:22244 friend struct base::DefaultSingletonTraits<ExtensionManagementFactory>;
binjin1569c9b2014-09-05 13:33:18245
246 ExtensionManagementFactory();
dchengae36a4a2014-10-21 12:36:36247 ~ExtensionManagementFactory() override;
binjin1569c9b2014-09-05 13:33:18248
249 // BrowserContextKeyedServiceExtensionManagementFactory:
dchengae36a4a2014-10-21 12:36:36250 KeyedService* BuildServiceInstanceFor(
mostynba15bee12014-10-04 00:40:32251 content::BrowserContext* context) const override;
dchengae36a4a2014-10-21 12:36:36252 content::BrowserContext* GetBrowserContextToUse(
mostynba15bee12014-10-04 00:40:32253 content::BrowserContext* context) const override;
dchengae36a4a2014-10-21 12:36:36254 void RegisterProfilePrefs(
mostynba15bee12014-10-04 00:40:32255 user_prefs::PrefRegistrySyncable* registry) override;
binjin1569c9b2014-09-05 13:33:18256
257 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementFactory);
258};
259
binjin5f405ef2014-09-03 21:23:16260} // namespace extensions
261
262#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_