blob: cc5a601b6cb98678e6e142df69ccdc169f91cf49 [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
binjin685ade82014-11-06 09:53:568#include <string>
binjine6b58b52014-10-31 01:55:579#include <vector>
10
binjin81d7c552014-10-02 11:47:1211#include "base/containers/scoped_ptr_hash_map.h"
binjin5f405ef2014-09-03 21:23:1612#include "base/macros.h"
binjine6b58b52014-10-31 01:55:5713#include "base/memory/ref_counted.h"
binjin1569c9b2014-09-05 13:33:1814#include "base/memory/scoped_ptr.h"
binjine6b58b52014-10-31 01:55:5715#include "base/memory/scoped_vector.h"
binjin1569c9b2014-09-05 13:33:1816#include "base/memory/singleton.h"
17#include "base/observer_list.h"
18#include "base/prefs/pref_change_registrar.h"
binjin5f405ef2014-09-03 21:23:1619#include "base/values.h"
binjin1569c9b2014-09-05 13:33:1820#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
21#include "components/keyed_service/core/keyed_service.h"
22#include "extensions/browser/management_policy.h"
binjin5f405ef2014-09-03 21:23:1623#include "extensions/common/extension.h"
24#include "extensions/common/manifest.h"
binjin5f405ef2014-09-03 21:23:1625
binjin311ecdf2014-09-12 22:56:5226class GURL;
binjin5f405ef2014-09-03 21:23:1627class PrefService;
28
binjin1569c9b2014-09-05 13:33:1829namespace content {
30class BrowserContext;
31} // namespace content
32
binjin5f405ef2014-09-03 21:23:1633namespace extensions {
34
binjin81d7c552014-10-02 11:47:1235namespace internal {
36
37struct IndividualSettings;
38struct GlobalSettings;
39
40} // namespace internal
41
binjine6b58b52014-10-31 01:55:5742class APIPermissionSet;
43class 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
binjin5f405ef2014-09-03 21:23:1673 explicit ExtensionManagement(PrefService* pref_service);
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.
84 std::vector<ManagementPolicy::Provider*> GetProviders() const;
binjin1569c9b2014-09-05 13:33:1885
86 // Checks if extensions are blacklisted by default, by policy. When true,
87 // this means that even extensions without an ID should be blacklisted (e.g.
88 // from the command line, or when loaded as an unpacked extension).
binjin81d7c552014-10-02 11:47:1289 bool BlacklistedByDefault() const;
90
91 // Returns installation mode for an extension.
binjin685ade82014-11-06 09:53:5692 InstallationMode GetInstallationMode(const Extension* extension) const;
binjin5f405ef2014-09-03 21:23:1693
binjin30301062014-09-08 20:27:3494 // Returns the force install list, in format specified by
95 // ExternalPolicyLoader::AddExtension().
96 scoped_ptr<base::DictionaryValue> GetForceInstallList() const;
97
binjincccacef2014-10-13 19:00:2098 // Like GetForceInstallList(), but returns recommended install list instead.
99 scoped_ptr<base::DictionaryValue> GetRecommendedInstallList() const;
100
binjinc641add2014-10-15 16:20:45101 // Returns if an extension with id |id| is explicitly allowed by enterprise
102 // policy or not.
103 bool IsInstallationExplicitlyAllowed(const ExtensionId& id) const;
binjin30301062014-09-08 20:27:34104
binjin311ecdf2014-09-12 22:56:52105 // Returns true if an extension download should be allowed to proceed.
binjin81d7c552014-10-02 11:47:12106 bool IsOffstoreInstallAllowed(const GURL& url,
107 const GURL& referrer_url) const;
binjin311ecdf2014-09-12 22:56:52108
binjin81d7c552014-10-02 11:47:12109 // Returns true if an extension with manifest type |manifest_type| is
110 // allowed to be installed.
111 bool IsAllowedManifestType(Manifest::Type manifest_type) const;
binjin5f405ef2014-09-03 21:23:16112
binjin685ade82014-11-06 09:53:56113 // Returns the list of blocked API permissions for |extension|.
114 APIPermissionSet GetBlockedAPIPermissions(const Extension* extension) const;
binjine6b58b52014-10-31 01:55:57115
binjin685ade82014-11-06 09:53:56116 // Returns blocked permission set for |extension|.
binjine6b58b52014-10-31 01:55:57117 scoped_refptr<const PermissionSet> GetBlockedPermissions(
binjin685ade82014-11-06 09:53:56118 const Extension* extension) const;
binjine6b58b52014-10-31 01:55:57119
binjin685ade82014-11-06 09:53:56120 // Returns true if every permission in |perms| is allowed for |extension|.
121 bool IsPermissionSetAllowed(const Extension* extension,
binjine6b58b52014-10-31 01:55:57122 scoped_refptr<const PermissionSet> perms) const;
123
binjin8e3d0182014-12-04 16:44:28124 // Returns true if |extension| meets the minimum required version set for it.
125 // If there is no such requirement set for it, returns true as well.
126 // If false is returned and |required_version| is not null, the minimum
127 // required version is returned.
128 bool CheckMinimumVersion(const Extension* extension,
129 std::string* required_version) const;
130
binjin5f405ef2014-09-03 21:23:16131 private:
binjin81d7c552014-10-02 11:47:12132 typedef base::ScopedPtrHashMap<ExtensionId, internal::IndividualSettings>
133 SettingsIdMap;
binjin685ade82014-11-06 09:53:56134 typedef base::ScopedPtrHashMap<std::string, internal::IndividualSettings>
135 SettingsUpdateUrlMap;
binjin81d7c552014-10-02 11:47:12136 friend class ExtensionManagementServiceTest;
137
binjin1569c9b2014-09-05 13:33:18138 // Load all extension management preferences from |pref_service|, and
139 // refresh the settings.
140 void Refresh();
141
binjin5f405ef2014-09-03 21:23:16142 // Load preference with name |pref_name| and expected type |expected_type|.
143 // If |force_managed| is true, only loading from the managed preference store
144 // is allowed. Returns NULL if the preference is not present, not allowed to
145 // be loaded from or has the wrong type.
146 const base::Value* LoadPreference(const char* pref_name,
147 bool force_managed,
148 base::Value::Type expected_type);
149
binjin1569c9b2014-09-05 13:33:18150 void OnExtensionPrefChanged();
151 void NotifyExtensionManagementPrefChanged();
152
binjin5f405ef2014-09-03 21:23:16153 // Helper function to access |settings_by_id_| with |id| as key.
154 // Adds a new IndividualSettings entry to |settings_by_id_| if none exists for
155 // |id| yet.
binjin81d7c552014-10-02 11:47:12156 internal::IndividualSettings* AccessById(const ExtensionId& id);
binjin5f405ef2014-09-03 21:23:16157
binjin685ade82014-11-06 09:53:56158 // Similar to AccessById(), but access |settings_by_update_url_| instead.
159 internal::IndividualSettings* AccessByUpdateUrl(
160 const std::string& update_url);
161
binjin5f405ef2014-09-03 21:23:16162 // A map containing all IndividualSettings applied to an individual extension
163 // identified by extension ID. The extension ID is used as index key of the
164 // map.
binjin5f405ef2014-09-03 21:23:16165 SettingsIdMap settings_by_id_;
166
binjin685ade82014-11-06 09:53:56167 // Similar to |settings_by_id_|, but contains the settings for a group of
168 // extensions with same update URL. The update url itself is used as index
169 // key for the map.
170 SettingsUpdateUrlMap settings_by_update_url_;
171
binjin5f405ef2014-09-03 21:23:16172 // The default IndividualSettings.
173 // For extension settings applied to an individual extension (identified by
174 // extension ID) or a group of extension (with specified extension update
175 // URL), all unspecified part will take value from |default_settings_|.
176 // For all other extensions, all settings from |default_settings_| will be
177 // enforced.
binjin81d7c552014-10-02 11:47:12178 scoped_ptr<internal::IndividualSettings> default_settings_;
binjin5f405ef2014-09-03 21:23:16179
180 // Extension settings applicable to all extensions.
binjin81d7c552014-10-02 11:47:12181 scoped_ptr<internal::GlobalSettings> global_settings_;
binjin5f405ef2014-09-03 21:23:16182
183 PrefService* pref_service_;
184
binjin1569c9b2014-09-05 13:33:18185 ObserverList<Observer, true> observer_list_;
186 PrefChangeRegistrar pref_change_registrar_;
binjine6b58b52014-10-31 01:55:57187 ScopedVector<ManagementPolicy::Provider> providers_;
binjin1569c9b2014-09-05 13:33:18188
binjin5f405ef2014-09-03 21:23:16189 DISALLOW_COPY_AND_ASSIGN(ExtensionManagement);
190};
191
binjin1569c9b2014-09-05 13:33:18192class ExtensionManagementFactory : public BrowserContextKeyedServiceFactory {
193 public:
194 static ExtensionManagement* GetForBrowserContext(
195 content::BrowserContext* context);
196 static ExtensionManagementFactory* GetInstance();
197
198 private:
199 friend struct DefaultSingletonTraits<ExtensionManagementFactory>;
200
201 ExtensionManagementFactory();
dchengae36a4a2014-10-21 12:36:36202 ~ExtensionManagementFactory() override;
binjin1569c9b2014-09-05 13:33:18203
204 // BrowserContextKeyedServiceExtensionManagementFactory:
dchengae36a4a2014-10-21 12:36:36205 KeyedService* BuildServiceInstanceFor(
mostynba15bee12014-10-04 00:40:32206 content::BrowserContext* context) const override;
dchengae36a4a2014-10-21 12:36:36207 content::BrowserContext* GetBrowserContextToUse(
mostynba15bee12014-10-04 00:40:32208 content::BrowserContext* context) const override;
dchengae36a4a2014-10-21 12:36:36209 void RegisterProfilePrefs(
mostynba15bee12014-10-04 00:40:32210 user_prefs::PrefRegistrySyncable* registry) override;
binjin1569c9b2014-09-05 13:33:18211
212 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementFactory);
213};
214
binjin5f405ef2014-09-03 21:23:16215} // namespace extensions
216
217#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_