Adding instrumentation to locate the source of jankiness
[chromium-blink-merge.git] / chrome / browser / extensions / extension_management.h
blob8727fd5f634db563c95629901550ff6096b278f8
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 "base/containers/scoped_ptr_hash_map.h"
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/singleton.h"
12 #include "base/observer_list.h"
13 #include "base/prefs/pref_change_registrar.h"
14 #include "base/values.h"
15 #include "components/keyed_service/content/browser_context_keyed_service_factory.h"
16 #include "components/keyed_service/core/keyed_service.h"
17 #include "extensions/browser/management_policy.h"
18 #include "extensions/common/extension.h"
19 #include "extensions/common/manifest.h"
21 class GURL;
22 class PrefService;
24 namespace content {
25 class BrowserContext;
26 } // namespace content
28 namespace extensions {
30 namespace internal {
32 struct IndividualSettings;
33 struct GlobalSettings;
35 } // namespace internal
37 // Tracks the management policies that affect extensions and provides interfaces
38 // for observing and obtaining the global settings for all extensions, as well
39 // as per-extension settings.
40 class ExtensionManagement : public KeyedService {
41 public:
42 // Observer class for extension management settings changes.
43 class Observer {
44 public:
45 virtual ~Observer() {}
47 // Called when the extension management settings change.
48 virtual void OnExtensionManagementSettingsChanged() = 0;
51 // Installation mode for extensions, default is INSTALLATION_ALLOWED.
52 // * INSTALLATION_ALLOWED: Extension can be installed.
53 // * INSTALLATION_BLOCKED: Extension cannot be installed.
54 // * INSTALLATION_FORCED: Extension will be installed automatically
55 // and cannot be disabled.
56 // * INSTALLATION_RECOMMENDED: Extension will be installed automatically but
57 // can be disabled.
58 enum InstallationMode {
59 INSTALLATION_ALLOWED = 0,
60 INSTALLATION_BLOCKED,
61 INSTALLATION_FORCED,
62 INSTALLATION_RECOMMENDED,
65 explicit ExtensionManagement(PrefService* pref_service);
66 virtual ~ExtensionManagement();
68 void AddObserver(Observer* observer);
69 void RemoveObserver(Observer* observer);
71 // Get the ManagementPolicy::Provider controlled by extension management
72 // policy settings.
73 ManagementPolicy::Provider* GetProvider() const;
75 // Checks if extensions are blacklisted by default, by policy. When true,
76 // this means that even extensions without an ID should be blacklisted (e.g.
77 // from the command line, or when loaded as an unpacked extension).
78 bool BlacklistedByDefault() const;
80 // Returns installation mode for an extension.
81 InstallationMode GetInstallationMode(const ExtensionId& id) const;
83 // Returns the force install list, in format specified by
84 // ExternalPolicyLoader::AddExtension().
85 scoped_ptr<base::DictionaryValue> GetForceInstallList() const;
87 // Like GetForceInstallList(), but returns recommended install list instead.
88 scoped_ptr<base::DictionaryValue> GetRecommendedInstallList() const;
90 // Returns if an extension with id |id| is explicitly allowed by enterprise
91 // policy or not.
92 bool IsInstallationExplicitlyAllowed(const ExtensionId& id) const;
94 // Returns true if an extension download should be allowed to proceed.
95 bool IsOffstoreInstallAllowed(const GURL& url,
96 const GURL& referrer_url) const;
98 // Returns true if an extension with manifest type |manifest_type| is
99 // allowed to be installed.
100 bool IsAllowedManifestType(Manifest::Type manifest_type) const;
102 private:
103 typedef base::ScopedPtrHashMap<ExtensionId, internal::IndividualSettings>
104 SettingsIdMap;
105 friend class ExtensionManagementServiceTest;
107 // Load all extension management preferences from |pref_service|, and
108 // refresh the settings.
109 void Refresh();
111 // Load preference with name |pref_name| and expected type |expected_type|.
112 // If |force_managed| is true, only loading from the managed preference store
113 // is allowed. Returns NULL if the preference is not present, not allowed to
114 // be loaded from or has the wrong type.
115 const base::Value* LoadPreference(const char* pref_name,
116 bool force_managed,
117 base::Value::Type expected_type);
119 void OnExtensionPrefChanged();
120 void NotifyExtensionManagementPrefChanged();
122 // Helper function to read |settings_by_id_| with |id| as key. Returns a
123 // constant reference to default settings if |id| does not exist.
124 const internal::IndividualSettings* ReadById(const ExtensionId& id) const;
126 // Returns a constant reference to |global_settings_|.
127 const internal::GlobalSettings* ReadGlobalSettings() const;
129 // Helper function to access |settings_by_id_| with |id| as key.
130 // Adds a new IndividualSettings entry to |settings_by_id_| if none exists for
131 // |id| yet.
132 internal::IndividualSettings* AccessById(const ExtensionId& id);
134 // A map containing all IndividualSettings applied to an individual extension
135 // identified by extension ID. The extension ID is used as index key of the
136 // map.
137 // TODO(binjin): Add |settings_by_update_url_|, and implement mechanism for
138 // it.
139 SettingsIdMap settings_by_id_;
141 // The default IndividualSettings.
142 // For extension settings applied to an individual extension (identified by
143 // extension ID) or a group of extension (with specified extension update
144 // URL), all unspecified part will take value from |default_settings_|.
145 // For all other extensions, all settings from |default_settings_| will be
146 // enforced.
147 scoped_ptr<internal::IndividualSettings> default_settings_;
149 // Extension settings applicable to all extensions.
150 scoped_ptr<internal::GlobalSettings> global_settings_;
152 PrefService* pref_service_;
154 ObserverList<Observer, true> observer_list_;
155 PrefChangeRegistrar pref_change_registrar_;
156 scoped_ptr<ManagementPolicy::Provider> provider_;
158 DISALLOW_COPY_AND_ASSIGN(ExtensionManagement);
161 class ExtensionManagementFactory : public BrowserContextKeyedServiceFactory {
162 public:
163 static ExtensionManagement* GetForBrowserContext(
164 content::BrowserContext* context);
165 static ExtensionManagementFactory* GetInstance();
167 private:
168 friend struct DefaultSingletonTraits<ExtensionManagementFactory>;
170 ExtensionManagementFactory();
171 virtual ~ExtensionManagementFactory();
173 // BrowserContextKeyedServiceExtensionManagementFactory:
174 virtual KeyedService* BuildServiceInstanceFor(
175 content::BrowserContext* context) const override;
176 virtual content::BrowserContext* GetBrowserContextToUse(
177 content::BrowserContext* context) const override;
178 virtual void RegisterProfilePrefs(
179 user_prefs::PrefRegistrySyncable* registry) override;
181 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementFactory);
184 } // namespace extensions
186 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_H_