blob: 8eae7c1952bd0f141d5e5072f702c31f1e239105 [file] [log] [blame]
[email protected]9a8c4022011-01-25 14:25:331// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]e72e8eb82009-06-18 17:21:512// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]6b75ec32009-08-14 06:37:185#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]e72e8eb82009-06-18 17:21:518
9#include <set>
10#include <string>
11#include <vector>
12
[email protected]696044b2009-12-17 18:30:5313#include "base/linked_ptr.h"
[email protected]5ef47ec2010-01-28 05:58:0514#include "base/time.h"
[email protected]37858e52010-08-26 00:22:0215#include "chrome/browser/prefs/pref_service.h"
[email protected]e72e8eb82009-06-18 17:21:5116#include "chrome/common/extensions/extension.h"
[email protected]e72e8eb82009-06-18 17:21:5117#include "googleurl/src/gurl.h"
18
[email protected]9a8c4022011-01-25 14:25:3319class ExtensionPrefValueMap;
[email protected]f2d1f612010-12-09 15:10:1720
[email protected]e72e8eb82009-06-18 17:21:5121// Class for managing global and per-extension preferences.
[email protected]73c47932010-12-06 18:13:4322//
23// This class distinguishes the following kinds of preferences:
24// - global preferences:
25// internal state for the extension system in general, not associated
26// with an individual extension, such as lastUpdateTime.
27// - per-extension preferences:
28// meta-preferences describing properties of the extension like
29// installation time, whether the extension is enabled, etc.
30// - extension controlled preferences:
31// browser preferences that an extension controls. For example, an
32// extension could use the proxy API to specify the browser's proxy
33// preference. Extension-controlled preferences are stored in
34// PrefValueStore::extension_prefs(), which this class populates and
35// maintains as the underlying extensions change.
[email protected]e72e8eb82009-06-18 17:21:5136class ExtensionPrefs {
37 public:
[email protected]10abd192010-09-30 02:03:4938 // Key name for a preference that keeps track of per-extension settings. This
39 // is a dictionary object read from the Preferences file, keyed off of
40 // extension ids.
41 static const char kExtensionsPref[];
42
[email protected]c6d474f82009-12-16 21:11:0643 typedef std::vector<linked_ptr<ExtensionInfo> > ExtensionsInfo;
44
[email protected]73c47932010-12-06 18:13:4345 // Vector containing identifiers for preferences.
46 typedef std::set<std::string> PrefKeySet;
47
48 // Vector containing identifiers for extensions.
49 typedef std::vector<std::string> ExtensionIdSet;
50
[email protected]10abd192010-09-30 02:03:4951 // This enum is used for the launch type the user wants to use for an
52 // application.
[email protected]9b217652010-10-08 22:04:2353 // Do not remove items or re-order this enum as it is used in preferences
54 // and histograms.
[email protected]10abd192010-09-30 02:03:4955 enum LaunchType {
56 LAUNCH_PINNED,
57 LAUNCH_REGULAR,
[email protected]fa6a9102010-11-22 15:38:5058 LAUNCH_FULLSCREEN,
59 LAUNCH_WINDOW
[email protected]10abd192010-09-30 02:03:4960 };
61
[email protected]9a8c4022011-01-25 14:25:3362 // Does not assume ownership of |prefs| and |incognito_prefs|.
[email protected]f2d1f612010-12-09 15:10:1763 explicit ExtensionPrefs(PrefService* prefs,
64 const FilePath& root_dir,
[email protected]9a8c4022011-01-25 14:25:3365 ExtensionPrefValueMap* extension_pref_value_map);
[email protected]2858bbf2010-10-05 23:46:0266 ~ExtensionPrefs();
[email protected]e72e8eb82009-06-18 17:21:5167
68 // Returns a copy of the Extensions prefs.
69 // TODO(erikkay) Remove this so that external consumers don't need to be
70 // aware of the internal structure of the preferences.
71 DictionaryValue* CopyCurrentExtensions();
72
[email protected]683d0702010-12-06 16:25:5773 // Returns true if the specified extension has an entry in prefs
74 // and its killbit is on.
75 bool IsExtensionKilled(const std::string& id);
[email protected]e72e8eb82009-06-18 17:21:5176
77 // Get the order that toolstrip URLs appear in the shelf.
78 typedef std::vector<GURL> URLList;
79 URLList GetShelfToolstripOrder();
80
[email protected]e81dba32009-06-19 20:19:1381 // Sets the order that toolstrip URLs appear in the shelf.
82 void SetShelfToolstripOrder(const URLList& urls);
83
[email protected]216e0bc02009-12-14 20:51:2984 // Get the order that the browser actions appear in the toolbar.
85 std::vector<std::string> GetToolbarOrder();
86
87 // Set the order that the browser actions appear in the toolbar.
88 void SetToolbarOrder(const std::vector<std::string>& extension_ids);
89
[email protected]e72e8eb82009-06-18 17:21:5190 // Called when an extension is installed, so that prefs get created.
[email protected]9adb9692010-10-29 23:14:0291 void OnExtensionInstalled(const Extension* extension,
[email protected]4416c5a2010-06-26 01:28:5792 Extension::State initial_state,
93 bool initial_incognito_enabled);
[email protected]e72e8eb82009-06-18 17:21:5194
95 // Called when an extension is uninstalled, so that prefs get cleaned up.
[email protected]831aa212010-03-26 13:55:1996 void OnExtensionUninstalled(const std::string& extension_id,
97 const Extension::Location& location,
[email protected]27b985d2009-06-25 17:53:1598 bool external_uninstall);
[email protected]e72e8eb82009-06-18 17:21:5199
[email protected]0c6da502009-08-14 22:32:39100 // Returns the state (enabled/disabled) of the given extension.
[email protected]73c47932010-12-06 18:13:43101 Extension::State GetExtensionState(const std::string& extension_id) const;
[email protected]0c6da502009-08-14 22:32:39102
103 // Called to change the extension's state when it is enabled/disabled.
[email protected]9adb9692010-10-29 23:14:02104 void SetExtensionState(const Extension* extension, Extension::State);
[email protected]0c6da502009-08-14 22:32:39105
[email protected]9a8c4022011-01-25 14:25:33106 // Returns all installed extensions
107 void GetExtensions(ExtensionIdSet* out) const;
[email protected]73c47932010-12-06 18:13:43108
[email protected]f681c782010-11-19 11:19:39109 // Getter and setter for browser action visibility.
110 bool GetBrowserActionVisibility(const Extension* extension);
111 void SetBrowserActionVisibility(const Extension* extension, bool visible);
112
[email protected]e6090e42010-03-23 22:44:08113 // Did the extension ask to escalate its permission during an upgrade?
114 bool DidExtensionEscalatePermissions(const std::string& id);
115
116 // If |did_escalate| is true, the preferences for |extension| will be set to
[email protected]7d845862010-01-04 21:28:57117 // require the install warning when the user tries to enable.
[email protected]9adb9692010-10-29 23:14:02118 void SetDidExtensionEscalatePermissions(const Extension* extension,
[email protected]e6090e42010-03-23 22:44:08119 bool did_escalate);
[email protected]7d845862010-01-04 21:28:57120
[email protected]b6ab96d2009-08-20 18:58:19121 // Returns the version string for the currently installed extension, or
122 // the empty string if not found.
123 std::string GetVersionString(const std::string& extension_id);
124
[email protected]c6d474f82009-12-16 21:11:06125 // Re-writes the extension manifest into the prefs.
126 // Called to change the extension's manifest when it's re-localized.
[email protected]9adb9692010-10-29 23:14:02127 void UpdateManifest(const Extension* extension);
[email protected]b6ab96d2009-08-20 18:58:19128
[email protected]371ed7a2009-08-25 15:22:46129 // Returns extension path based on extension ID, or empty FilePath on error.
130 FilePath GetExtensionPath(const std::string& extension_id);
131
[email protected]a9b00ac2009-06-25 21:03:23132 // Returns base extensions install directory.
133 const FilePath& install_directory() const { return install_directory_; }
134
[email protected]6b75ec32009-08-14 06:37:18135 // Updates the prefs based on the blacklist.
136 void UpdateBlacklist(const std::set<std::string>& blacklist_set);
137
138 // Based on extension id, checks prefs to see if it is blacklisted.
139 bool IsExtensionBlacklisted(const std::string& id);
140
[email protected]306a2bd2010-08-11 14:56:36141 // Is the extension with |extension_id| allowed by policy (checking both
142 // whitelist and blacklist).
143 bool IsExtensionAllowedByPolicy(const std::string& extension_id);
144
[email protected]5ef47ec2010-01-28 05:58:05145 // Returns the last value set via SetLastPingDay. If there isn't such a
146 // pref, the returned Time will return true for is_null().
[email protected]c5804aa12010-03-30 23:44:06147 base::Time LastPingDay(const std::string& extension_id) const;
[email protected]5ef47ec2010-01-28 05:58:05148
149 // The time stored is based on the server's perspective of day start time, not
150 // the client's.
151 void SetLastPingDay(const std::string& extension_id, const base::Time& time);
152
[email protected]8d888c12010-11-30 00:00:25153 // Gets the permissions (|api_permissions|, |host_extent| and |full_access|)
154 // granted to the extension with |extension_id|. |full_access| will be true
155 // if the extension has all effective permissions (like from an NPAPI plugin).
156 // Returns false if the granted permissions haven't been initialized yet.
157 // TODO(jstritar): Refractor the permissions into a class that encapsulates
158 // all granted permissions, can be initialized from preferences or
159 // a manifest file, and can be compared to each other.
160 bool GetGrantedPermissions(const std::string& extension_id,
161 bool* full_access,
162 std::set<std::string>* api_permissions,
163 ExtensionExtent* host_extent);
164
165 // Adds the specified |api_permissions|, |host_extent| and |full_access|
166 // to the granted permissions for extension with |extension_id|.
167 // |full_access| should be set to true if the extension effectively has all
168 // permissions (such as by having an NPAPI plugin).
169 void AddGrantedPermissions(const std::string& extension_id,
170 const bool full_access,
171 const std::set<std::string>& api_permissions,
172 const ExtensionExtent& host_extent);
173
[email protected]aca840c2010-04-06 23:33:04174 // Similar to the 2 above, but for the extensions blacklist.
175 base::Time BlacklistLastPingDay() const;
176 void SetBlacklistLastPingDay(const base::Time& time);
177
[email protected]55a35692010-02-11 23:25:21178 // Returns true if the user enabled this extension to be loaded in incognito
179 // mode.
180 bool IsIncognitoEnabled(const std::string& extension_id);
181 void SetIsIncognitoEnabled(const std::string& extension_id, bool enabled);
[email protected]5ef47ec2010-01-28 05:58:05182
[email protected]05c82182010-06-24 17:49:08183 // Returns true if the user has chosen to allow this extension to inject
184 // scripts into pages with file URLs.
185 bool AllowFileAccess(const std::string& extension_id);
186 void SetAllowFileAccess(const std::string& extension_id, bool allow);
187
[email protected]a3b734b2010-11-30 03:17:11188 // Get the launch type preference. If no preference is set, return
189 // |default_pref_value|.
190 LaunchType GetLaunchType(const std::string& extension_id,
191 LaunchType default_pref_value);
192
[email protected]10abd192010-09-30 02:03:49193 void SetLaunchType(const std::string& extension_id, LaunchType launch_type);
194
[email protected]a3b734b2010-11-30 03:17:11195 // Find the right launch container based on the launch type.
196 // If |extension|'s prefs do not have a launch type set, then
197 // use |default_pref_value|.
198 extension_misc::LaunchContainer GetLaunchContainer(
199 const Extension* extension,
200 LaunchType default_pref_value);
201
202
[email protected]c6d474f82009-12-16 21:11:06203 // Saves ExtensionInfo for each installed extension with the path to the
204 // version directory and the location. Blacklisted extensions won't be saved
205 // and neither will external extensions the user has explicitly uninstalled.
206 // Caller takes ownership of returned structure.
[email protected]e6090e42010-03-23 22:44:08207 ExtensionsInfo* GetInstalledExtensionsInfo();
208
209 // Returns the ExtensionInfo from the prefs for the given extension. If the
210 // extension is not present, NULL is returned.
211 ExtensionInfo* GetInstalledExtensionInfo(const std::string& extension_id);
[email protected]c6d474f82009-12-16 21:11:06212
[email protected]63c64d12010-04-27 21:21:34213 // We've downloaded an updated .crx file for the extension, but are waiting
214 // for idle time to install it.
215 void SetIdleInstallInfo(const std::string& extension_id,
216 const FilePath& crx_path,
217 const std::string& version,
218 const base::Time& fetch_time);
219
220 // Removes any idle install information we have for the given |extension_id|.
221 // Returns true if there was info to remove; false otherwise.
222 bool RemoveIdleInstallInfo(const std::string& extension_id);
223
224 // If we have idle install information for |extension_id|, this puts it into
225 // the out parameters and returns true. Otherwise returns false.
226 bool GetIdleInstallInfo(const std::string& extension_id,
227 FilePath* crx_path,
228 std::string* version,
229 base::Time* fetch_time);
230
231 // Returns the extension id's that have idle install information.
232 std::set<std::string> GetIdleInstallInfoIds();
233
[email protected]63cda0c2010-09-01 04:41:23234 // We allow the web store to set a string containing login information when a
235 // purchase is made, so that when a user logs into sync with a different
236 // account we can recognize the situation. The Get function returns true if
237 // there was previously stored data (placing it in |result|), or false
238 // otherwise. The Set will overwrite any previous login.
239 bool GetWebStoreLogin(std::string* result);
240 void SetWebStoreLogin(const std::string& login);
241
[email protected]ebfd4b92010-10-10 09:17:08242 // Get the application launch index for an extension with |extension_id|. This
243 // determines the order of which the applications appear on the New Tab Page.
244 // A value of 0 generally indicates top left. If the extension has no launch
245 // index a -1 value is returned.
246 int GetAppLaunchIndex(const std::string& extension_id);
247
248 // Sets a specific launch index for an extension with |extension_id|.
249 void SetAppLaunchIndex(const std::string& extension_id, int index);
250
251 // Gets the next available application launch index. This is 1 higher than the
252 // highest current application launch index found.
253 int GetNextAppLaunchIndex();
254
[email protected]48281642011-01-24 03:41:02255 // Sets the order the apps should be displayed in the app launcher.
256 void SetAppLauncherOrder(const std::vector<std::string>& extension_ids);
257
[email protected]a65882c2010-11-12 15:15:09258 // The extension's update URL data. If not empty, the ExtensionUpdater
259 // will append a ap= parameter to the URL when checking if a new version
260 // of the extension is available.
261 void SetUpdateUrlData(const std::string& extension_id,
262 const std::string& data);
263 std::string GetUpdateUrlData(const std::string& extension_id);
264
[email protected]73c47932010-12-06 18:13:43265 // Sets a preference value that is controlled by the extension. In other
266 // words, this is not a pref value *about* the extension but something
267 // global the extension wants to override.
268 void SetExtensionControlledPref(const std::string& extension_id,
269 const std::string& pref_key,
[email protected]9a8c4022011-01-25 14:25:33270 bool incognito,
[email protected]73c47932010-12-06 18:13:43271 Value* value);
272
[email protected]fee8f0222010-03-17 01:13:37273 static void RegisterUserPrefs(PrefService* prefs);
274
[email protected]ba399672010-04-06 15:42:39275 // The underlying PrefService.
276 PrefService* pref_service() const { return prefs_; }
277
[email protected]73c47932010-12-06 18:13:43278 protected:
279 // For unit testing. Enables injecting an artificial clock that is used
280 // to query the current time, when an extension is installed.
281 virtual base::Time GetCurrentTime() const;
282
[email protected]e72e8eb82009-06-18 17:21:51283 private:
[email protected]a9b00ac2009-06-25 21:03:23284 // Converts absolute paths in the pref to paths relative to the
285 // install_directory_.
286 void MakePathsRelative();
287
288 // Converts internal relative paths to be absolute. Used for export to
289 // consumers who expect full paths.
290 void MakePathsAbsolute(DictionaryValue* dict);
291
[email protected]e72e8eb82009-06-18 17:21:51292 // Sets the pref |key| for extension |id| to |value|.
[email protected]4dad9ad82009-11-25 20:47:52293 void UpdateExtensionPref(const std::string& id,
[email protected]e2194742010-08-12 05:54:34294 const std::string& key,
[email protected]e72e8eb82009-06-18 17:21:51295 Value* value);
296
297 // Deletes the pref dictionary for extension |id|.
298 void DeleteExtensionPrefs(const std::string& id);
299
[email protected]7d845862010-01-04 21:28:57300 // Reads a boolean pref from |ext| with key |pref_key|.
[email protected]8d888c12010-11-30 00:00:25301 // Return false if the value is false or |pref_key| does not exist.
[email protected]e2194742010-08-12 05:54:34302 bool ReadBooleanFromPref(DictionaryValue* ext, const std::string& pref_key);
[email protected]7d845862010-01-04 21:28:57303
304 // Reads a boolean pref |pref_key| from extension with id |extension_id|.
305 bool ReadExtensionPrefBoolean(const std::string& extension_id,
[email protected]e2194742010-08-12 05:54:34306 const std::string& pref_key);
[email protected]7d845862010-01-04 21:28:57307
[email protected]10abd192010-09-30 02:03:49308 // Reads an integer pref from |ext| with key |pref_key|.
309 // Return false if the value does not exist.
310 bool ReadIntegerFromPref(DictionaryValue* ext, const std::string& pref_key,
311 int* out_value);
312
313 // Reads an integer pref |pref_key| from extension with id |extension_id|.
314 bool ReadExtensionPrefInteger(const std::string& extension_id,
315 const std::string& pref_key,
316 int* out_value);
317
[email protected]8d888c12010-11-30 00:00:25318 // Reads a list pref |pref_key| from extension with id | extension_id|.
319 bool ReadExtensionPrefList(const std::string& extension_id,
320 const std::string& pref_key,
321 ListValue** out_value);
322
323 // Reads a list pref |pref_key| as a string set from the extension with
324 // id |extension_id|.
325 bool ReadExtensionPrefStringSet(const std::string& extension_id,
326 const std::string& pref_key,
327 std::set<std::string>* result);
328
329 // Adds the |added_values| to the value of |pref_key| for the extension
330 // with id |extension_id| (the new value will be the union of the existing
331 // value and |added_values|).
332 void AddToExtensionPrefStringSet(const std::string& extension_id,
333 const std::string& pref_key,
334 const std::set<std::string>& added_values);
335
[email protected]e72e8eb82009-06-18 17:21:51336 // Ensures and returns a mutable dictionary for extension |id|'s prefs.
337 DictionaryValue* GetOrCreateExtensionPref(const std::string& id);
338
[email protected]0c6da502009-08-14 22:32:39339 // Same as above, but returns NULL if it doesn't exist.
[email protected]c5804aa12010-03-30 23:44:06340 DictionaryValue* GetExtensionPref(const std::string& id) const;
[email protected]0c6da502009-08-14 22:32:39341
[email protected]73c47932010-12-06 18:13:43342 // Returns the dictionary of preferences controlled by the specified extension
[email protected]9a8c4022011-01-25 14:25:33343 // or creates a new one. All entries in the dictionary contain non-expanded
[email protected]73c47932010-12-06 18:13:43344 // paths.
345 DictionaryValue* GetExtensionControlledPrefs(const std::string& id) const;
346
[email protected]10abd192010-09-30 02:03:49347 // Serializes the data and schedules a persistent save via the |PrefService|.
348 // Additionally fires a PREF_CHANGED notification with the top-level
349 // |kExtensionsPref| path set.
350 // TODO(andybons): Switch this to EXTENSION_PREF_CHANGED to be more granular.
351 // TODO(andybons): Use a ScopedPrefUpdate to update observers on changes to
352 // the mutable extension dictionary.
353 void SavePrefsAndNotify();
354
[email protected]6b75ec32009-08-14 06:37:18355 // Checks if kPrefBlacklist is set to true in the DictionaryValue.
356 // Return false if the value is false or kPrefBlacklist does not exist.
357 // This is used to decide if an extension is blacklisted.
358 bool IsBlacklistBitSet(DictionaryValue* ext);
359
[email protected]aca840c2010-04-06 23:33:04360 // Helper methods for the public last ping day functions.
361 base::Time LastPingDayImpl(const DictionaryValue* dictionary) const;
362 void SetLastPingDayImpl(const base::Time& time, DictionaryValue* dictionary);
363
[email protected]73c47932010-12-06 18:13:43364 // Helper method to acquire the installation time of an extension.
[email protected]eb29cc022011-01-10 17:23:07365 // Returns base::Time() if the installation time could not be parsed or
366 // found.
[email protected]73c47932010-12-06 18:13:43367 base::Time GetInstallTime(const std::string& extension_id) const;
368
369 // Fix missing preference entries in the extensions that are were introduced
370 // in a later Chrome version.
371 void FixMissingPrefs(const ExtensionIdSet& extension_ids);
372
373 // Installs the persistent extension preferences into |prefs_|'s extension
374 // pref store.
375 void InitPrefStore();
376
[email protected]9a8c4022011-01-25 14:25:33377 // The pref service specific to this set of extension prefs. Owned by profile.
[email protected]e72e8eb82009-06-18 17:21:51378 PrefService* prefs_;
379
[email protected]a9b00ac2009-06-25 21:03:23380 // Base extensions install directory.
381 FilePath install_directory_;
382
[email protected]9a8c4022011-01-25 14:25:33383 // Weak pointer, owned by Profile.
384 ExtensionPrefValueMap* extension_pref_value_map_;
[email protected]f2d1f612010-12-09 15:10:17385
[email protected]e72e8eb82009-06-18 17:21:51386 // The URLs of all of the toolstrips.
387 URLList shelf_order_;
388
389 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefs);
390};
391
[email protected]6b75ec32009-08-14 06:37:18392#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_