extension_prefs.h revision 731df977c0511bca2206b5f333555b1205ff1f43
1// Copyright (c) 2010 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_PREFS_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_
7#pragma once
8
9#include <set>
10#include <string>
11#include <vector>
12
13#include "base/linked_ptr.h"
14#include "base/time.h"
15#include "chrome/browser/prefs/pref_service.h"
16#include "chrome/common/extensions/extension.h"
17#include "googleurl/src/gurl.h"
18
19// Class for managing global and per-extension preferences.
20// This class is instantiated by ExtensionsService, so it should be accessed
21// from there.
22class ExtensionPrefs {
23 public:
24  // Key name for a preference that keeps track of per-extension settings. This
25  // is a dictionary object read from the Preferences file, keyed off of
26  // extension ids.
27  static const char kExtensionsPref[];
28
29  typedef std::vector<linked_ptr<ExtensionInfo> > ExtensionsInfo;
30
31  // This enum is used for the launch type the user wants to use for an
32  // application.
33  // Do not remove items or re-order this enum as it is used in preferences
34  // and histograms.
35  enum LaunchType {
36    LAUNCH_PINNED,
37    LAUNCH_REGULAR,
38    LAUNCH_FULLSCREEN
39  };
40
41  explicit ExtensionPrefs(PrefService* prefs, const FilePath& root_dir_);
42  ~ExtensionPrefs();
43
44  // Returns a copy of the Extensions prefs.
45  // TODO(erikkay) Remove this so that external consumers don't need to be
46  // aware of the internal structure of the preferences.
47  DictionaryValue* CopyCurrentExtensions();
48
49  // Populate |killed_ids| with extension ids that have been killed.
50  void GetKilledExtensionIds(std::set<std::string>* killed_ids);
51
52  // Get the order that toolstrip URLs appear in the shelf.
53  typedef std::vector<GURL> URLList;
54  URLList GetShelfToolstripOrder();
55
56  // Sets the order that toolstrip URLs appear in the shelf.
57  void SetShelfToolstripOrder(const URLList& urls);
58
59  // Get the order that the browser actions appear in the toolbar.
60  std::vector<std::string> GetToolbarOrder();
61
62  // Set the order that the browser actions appear in the toolbar.
63  void SetToolbarOrder(const std::vector<std::string>& extension_ids);
64
65  // Called when an extension is installed, so that prefs get created.
66  void OnExtensionInstalled(Extension* extension,
67                            Extension::State initial_state,
68                            bool initial_incognito_enabled);
69
70  // Called when an extension is uninstalled, so that prefs get cleaned up.
71  void OnExtensionUninstalled(const std::string& extension_id,
72                              const Extension::Location& location,
73                              bool external_uninstall);
74
75  // Returns the state (enabled/disabled) of the given extension.
76  Extension::State GetExtensionState(const std::string& extension_id);
77
78  // Called to change the extension's state when it is enabled/disabled.
79  void SetExtensionState(Extension* extension, Extension::State);
80
81  // Did the extension ask to escalate its permission during an upgrade?
82  bool DidExtensionEscalatePermissions(const std::string& id);
83
84  // If |did_escalate| is true, the preferences for |extension| will be set to
85  // require the install warning when the user tries to enable.
86  void SetDidExtensionEscalatePermissions(Extension* extension,
87                                          bool did_escalate);
88
89  // Returns the version string for the currently installed extension, or
90  // the empty string if not found.
91  std::string GetVersionString(const std::string& extension_id);
92
93  // Re-writes the extension manifest into the prefs.
94  // Called to change the extension's manifest when it's re-localized.
95  void UpdateManifest(Extension* extension);
96
97  // Returns extension path based on extension ID, or empty FilePath on error.
98  FilePath GetExtensionPath(const std::string& extension_id);
99
100  // Returns base extensions install directory.
101  const FilePath& install_directory() const { return install_directory_; }
102
103  // Updates the prefs based on the blacklist.
104  void UpdateBlacklist(const std::set<std::string>& blacklist_set);
105
106  // Based on extension id, checks prefs to see if it is blacklisted.
107  bool IsExtensionBlacklisted(const std::string& id);
108
109  // Is the extension with |extension_id| allowed by policy (checking both
110  // whitelist and blacklist).
111  bool IsExtensionAllowedByPolicy(const std::string& extension_id);
112
113  // Returns the last value set via SetLastPingDay. If there isn't such a
114  // pref, the returned Time will return true for is_null().
115  base::Time LastPingDay(const std::string& extension_id) const;
116
117  // The time stored is based on the server's perspective of day start time, not
118  // the client's.
119  void SetLastPingDay(const std::string& extension_id, const base::Time& time);
120
121  // Similar to the 2 above, but for the extensions blacklist.
122  base::Time BlacklistLastPingDay() const;
123  void SetBlacklistLastPingDay(const base::Time& time);
124
125  // Returns true if the user enabled this extension to be loaded in incognito
126  // mode.
127  bool IsIncognitoEnabled(const std::string& extension_id);
128  void SetIsIncognitoEnabled(const std::string& extension_id, bool enabled);
129
130  // Returns true if the user has chosen to allow this extension to inject
131  // scripts into pages with file URLs.
132  bool AllowFileAccess(const std::string& extension_id);
133  void SetAllowFileAccess(const std::string& extension_id, bool allow);
134
135  ExtensionPrefs::LaunchType GetLaunchType(const std::string& extension_id);
136  void SetLaunchType(const std::string& extension_id, LaunchType launch_type);
137
138  // Saves ExtensionInfo for each installed extension with the path to the
139  // version directory and the location. Blacklisted extensions won't be saved
140  // and neither will external extensions the user has explicitly uninstalled.
141  // Caller takes ownership of returned structure.
142  ExtensionsInfo* GetInstalledExtensionsInfo();
143
144  // Returns the ExtensionInfo from the prefs for the given extension. If the
145  // extension is not present, NULL is returned.
146  ExtensionInfo* GetInstalledExtensionInfo(const std::string& extension_id);
147
148  // We've downloaded an updated .crx file for the extension, but are waiting
149  // for idle time to install it.
150  void SetIdleInstallInfo(const std::string& extension_id,
151                          const FilePath& crx_path,
152                          const std::string& version,
153                          const base::Time& fetch_time);
154
155  // Removes any idle install information we have for the given |extension_id|.
156  // Returns true if there was info to remove; false otherwise.
157  bool RemoveIdleInstallInfo(const std::string& extension_id);
158
159  // If we have idle install information for |extension_id|, this puts it into
160  // the out parameters and returns true. Otherwise returns false.
161  bool GetIdleInstallInfo(const std::string& extension_id,
162                          FilePath* crx_path,
163                          std::string* version,
164                          base::Time* fetch_time);
165
166  // Returns the extension id's that have idle install information.
167  std::set<std::string> GetIdleInstallInfoIds();
168
169  // We allow the web store to set a string containing login information when a
170  // purchase is made, so that when a user logs into sync with a different
171  // account we can recognize the situation. The Get function returns true if
172  // there was previously stored data (placing it in |result|), or false
173  // otherwise. The Set will overwrite any previous login.
174  bool GetWebStoreLogin(std::string* result);
175  void SetWebStoreLogin(const std::string& login);
176
177  // Get the application launch index for an extension with |extension_id|. This
178  // determines the order of which the applications appear on the New Tab Page.
179  // A value of 0 generally indicates top left. If the extension has no launch
180  // index a -1 value is returned.
181  int GetAppLaunchIndex(const std::string& extension_id);
182
183  // Sets a specific launch index for an extension with |extension_id|.
184  void SetAppLaunchIndex(const std::string& extension_id, int index);
185
186  // Gets the next available application launch index. This is 1 higher than the
187  // highest current application launch index found.
188  int GetNextAppLaunchIndex();
189
190  static void RegisterUserPrefs(PrefService* prefs);
191
192  // The underlying PrefService.
193  PrefService* pref_service() const { return prefs_; }
194
195 private:
196  // Converts absolute paths in the pref to paths relative to the
197  // install_directory_.
198  void MakePathsRelative();
199
200  // Converts internal relative paths to be absolute. Used for export to
201  // consumers who expect full paths.
202  void MakePathsAbsolute(DictionaryValue* dict);
203
204  // Sets the pref |key| for extension |id| to |value|.
205  void UpdateExtensionPref(const std::string& id,
206                           const std::string& key,
207                           Value* value);
208
209  // Deletes the pref dictionary for extension |id|.
210  void DeleteExtensionPrefs(const std::string& id);
211
212  // Reads a boolean pref from |ext| with key |pref_key|.
213  // Return false if the value is false or kPrefBlacklist does not exist.
214  bool ReadBooleanFromPref(DictionaryValue* ext, const std::string& pref_key);
215
216  // Reads a boolean pref |pref_key| from extension with id |extension_id|.
217  bool ReadExtensionPrefBoolean(const std::string& extension_id,
218                                const std::string& pref_key);
219
220  // Reads an integer pref from |ext| with key |pref_key|.
221  // Return false if the value does not exist.
222  bool ReadIntegerFromPref(DictionaryValue* ext, const std::string& pref_key,
223                           int* out_value);
224
225  // Reads an integer pref |pref_key| from extension with id |extension_id|.
226  bool ReadExtensionPrefInteger(const std::string& extension_id,
227                                const std::string& pref_key,
228                                int* out_value);
229
230  // Ensures and returns a mutable dictionary for extension |id|'s prefs.
231  DictionaryValue* GetOrCreateExtensionPref(const std::string& id);
232
233  // Same as above, but returns NULL if it doesn't exist.
234  DictionaryValue* GetExtensionPref(const std::string& id) const;
235
236  // Serializes the data and schedules a persistent save via the |PrefService|.
237  // Additionally fires a PREF_CHANGED notification with the top-level
238  // |kExtensionsPref| path set.
239  // TODO(andybons): Switch this to EXTENSION_PREF_CHANGED to be more granular.
240  // TODO(andybons): Use a ScopedPrefUpdate to update observers on changes to
241  // the mutable extension dictionary.
242  void SavePrefsAndNotify();
243
244  // Checks if kPrefBlacklist is set to true in the DictionaryValue.
245  // Return false if the value is false or kPrefBlacklist does not exist.
246  // This is used to decide if an extension is blacklisted.
247  bool IsBlacklistBitSet(DictionaryValue* ext);
248
249  // Helper methods for the public last ping day functions.
250  base::Time LastPingDayImpl(const DictionaryValue* dictionary) const;
251  void SetLastPingDayImpl(const base::Time& time, DictionaryValue* dictionary);
252
253  // The pref service specific to this set of extension prefs.
254  PrefService* prefs_;
255
256  // Base extensions install directory.
257  FilePath install_directory_;
258
259  // The URLs of all of the toolstrips.
260  URLList shelf_order_;
261
262  DISALLOW_COPY_AND_ASSIGN(ExtensionPrefs);
263};
264
265#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_
266