extension_prefs.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
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
8#include <set>
9#include <string>
10#include <vector>
11
12#include "base/linked_ptr.h"
13#include "base/time.h"
14#include "chrome/browser/pref_service.h"
15#include "chrome/common/extensions/extension.h"
16#include "googleurl/src/gurl.h"
17
18// Class for managing global and per-extension preferences.
19// This class is instantiated by ExtensionsService, so it should be accessed
20// from there.
21class ExtensionPrefs {
22 public:
23  typedef std::vector<linked_ptr<ExtensionInfo> > ExtensionsInfo;
24
25  explicit ExtensionPrefs(PrefService* prefs, const FilePath& root_dir_);
26
27  // Returns a copy of the Extensions prefs.
28  // TODO(erikkay) Remove this so that external consumers don't need to be
29  // aware of the internal structure of the preferences.
30  DictionaryValue* CopyCurrentExtensions();
31
32  // Populate |killed_ids| with extension ids that have been killed.
33  void GetKilledExtensionIds(std::set<std::string>* killed_ids);
34
35  // Get the order that toolstrip URLs appear in the shelf.
36  typedef std::vector<GURL> URLList;
37  URLList GetShelfToolstripOrder();
38
39  // Sets the order that toolstrip URLs appear in the shelf.
40  void SetShelfToolstripOrder(const URLList& urls);
41
42  // Get the order that the browser actions appear in the toolbar.
43  std::vector<std::string> GetToolbarOrder();
44
45  // Set the order that the browser actions appear in the toolbar.
46  void SetToolbarOrder(const std::vector<std::string>& extension_ids);
47
48  // Called when an extension is installed, so that prefs get created.
49  void OnExtensionInstalled(Extension* extension,
50                            Extension::State initial_state,
51                            bool initial_incognito_enabled);
52
53  // Called when an extension is uninstalled, so that prefs get cleaned up.
54  void OnExtensionUninstalled(const std::string& extension_id,
55                              const Extension::Location& location,
56                              bool external_uninstall);
57
58  // Returns the state (enabled/disabled) of the given extension.
59  Extension::State GetExtensionState(const std::string& extension_id);
60
61  // Called to change the extension's state when it is enabled/disabled.
62  void SetExtensionState(Extension* extension, Extension::State);
63
64  // Did the extension ask to escalate its permission during an upgrade?
65  bool DidExtensionEscalatePermissions(const std::string& id);
66
67  // If |did_escalate| is true, the preferences for |extension| will be set to
68  // require the install warning when the user tries to enable.
69  void SetDidExtensionEscalatePermissions(Extension* extension,
70                                          bool did_escalate);
71
72  // Returns the version string for the currently installed extension, or
73  // the empty string if not found.
74  std::string GetVersionString(const std::string& extension_id);
75
76  // Re-writes the extension manifest into the prefs.
77  // Called to change the extension's manifest when it's re-localized.
78  void UpdateManifest(Extension* extension);
79
80  // Returns extension path based on extension ID, or empty FilePath on error.
81  FilePath GetExtensionPath(const std::string& extension_id);
82
83  // Returns base extensions install directory.
84  const FilePath& install_directory() const { return install_directory_; }
85
86  // Updates the prefs based on the blacklist.
87  void UpdateBlacklist(const std::set<std::string>& blacklist_set);
88
89  // Based on extension id, checks prefs to see if it is blacklisted.
90  bool IsExtensionBlacklisted(const std::string& id);
91
92  // Returns the last value set via SetLastPingDay. If there isn't such a
93  // pref, the returned Time will return true for is_null().
94  base::Time LastPingDay(const std::string& extension_id) const;
95
96  // The time stored is based on the server's perspective of day start time, not
97  // the client's.
98  void SetLastPingDay(const std::string& extension_id, const base::Time& time);
99
100  // Similar to the 2 above, but for the extensions blacklist.
101  base::Time BlacklistLastPingDay() const;
102  void SetBlacklistLastPingDay(const base::Time& time);
103
104  // Returns true if the user enabled this extension to be loaded in incognito
105  // mode.
106  bool IsIncognitoEnabled(const std::string& extension_id);
107  void SetIsIncognitoEnabled(const std::string& extension_id, bool enabled);
108
109  // Returns true if the user has chosen to allow this extension to inject
110  // scripts into pages with file URLs.
111  bool AllowFileAccess(const std::string& extension_id);
112  void SetAllowFileAccess(const std::string& extension_id, bool allow);
113
114  // Saves ExtensionInfo for each installed extension with the path to the
115  // version directory and the location. Blacklisted extensions won't be saved
116  // and neither will external extensions the user has explicitly uninstalled.
117  // Caller takes ownership of returned structure.
118  ExtensionsInfo* GetInstalledExtensionsInfo();
119
120  // Returns the ExtensionInfo from the prefs for the given extension. If the
121  // extension is not present, NULL is returned.
122  ExtensionInfo* GetInstalledExtensionInfo(const std::string& extension_id);
123
124  // We've downloaded an updated .crx file for the extension, but are waiting
125  // for idle time to install it.
126  void SetIdleInstallInfo(const std::string& extension_id,
127                          const FilePath& crx_path,
128                          const std::string& version,
129                          const base::Time& fetch_time);
130
131  // Removes any idle install information we have for the given |extension_id|.
132  // Returns true if there was info to remove; false otherwise.
133  bool RemoveIdleInstallInfo(const std::string& extension_id);
134
135  // If we have idle install information for |extension_id|, this puts it into
136  // the out parameters and returns true. Otherwise returns false.
137  bool GetIdleInstallInfo(const std::string& extension_id,
138                          FilePath* crx_path,
139                          std::string* version,
140                          base::Time* fetch_time);
141
142  // Returns the extension id's that have idle install information.
143  std::set<std::string> GetIdleInstallInfoIds();
144
145  static void RegisterUserPrefs(PrefService* prefs);
146
147  // The underlying PrefService.
148  PrefService* pref_service() const { return prefs_; }
149
150 private:
151
152  // Converts absolute paths in the pref to paths relative to the
153  // install_directory_.
154  void MakePathsRelative();
155
156  // Converts internal relative paths to be absolute. Used for export to
157  // consumers who expect full paths.
158  void MakePathsAbsolute(DictionaryValue* dict);
159
160  // Sets the pref |key| for extension |id| to |value|.
161  void UpdateExtensionPref(const std::string& id,
162                           const std::wstring& key,
163                           Value* value);
164
165  // Deletes the pref dictionary for extension |id|.
166  void DeleteExtensionPrefs(const std::string& id);
167
168  // Reads a boolean pref from |ext| with key |pref_key|.
169  // Return false if the value is false or kPrefBlacklist does not exist.
170  bool ReadBooleanFromPref(DictionaryValue* ext, const std::wstring& pref_key);
171
172  // Reads a boolean pref |pref_key| from extension with id |extension_id|.
173  bool ReadExtensionPrefBoolean(const std::string& extension_id,
174                                const std::wstring& pref_key);
175
176  // Ensures and returns a mutable dictionary for extension |id|'s prefs.
177  DictionaryValue* GetOrCreateExtensionPref(const std::string& id);
178
179  // Same as above, but returns NULL if it doesn't exist.
180  DictionaryValue* GetExtensionPref(const std::string& id) const;
181
182  // Checks if kPrefBlacklist is set to true in the DictionaryValue.
183  // Return false if the value is false or kPrefBlacklist does not exist.
184  // This is used to decide if an extension is blacklisted.
185  bool IsBlacklistBitSet(DictionaryValue* ext);
186
187  // Helper methods for the public last ping day functions.
188  base::Time LastPingDayImpl(const DictionaryValue* dictionary) const;
189  void SetLastPingDayImpl(const base::Time& time, DictionaryValue* dictionary);
190
191  // The pref service specific to this set of extension prefs.
192  PrefService* prefs_;
193
194  // Base extensions install directory.
195  FilePath install_directory_;
196
197  // The URLs of all of the toolstrips.
198  URLList shelf_order_;
199
200  DISALLOW_COPY_AND_ASSIGN(ExtensionPrefs);
201};
202
203#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREFS_H_
204