extension_prefs.h 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949
  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.
  4. #ifndef EXTENSIONS_BROWSER_EXTENSION_PREFS_H_
  5. #define EXTENSIONS_BROWSER_EXTENSION_PREFS_H_
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include <vector>
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/observer_list.h"
  12. #include "base/strings/string_piece_forward.h"
  13. #include "base/time/time.h"
  14. #include "base/values.h"
  15. #include "components/keyed_service/core/keyed_service.h"
  16. #include "components/prefs/scoped_user_pref_update.h"
  17. #include "components/sync/model/string_ordinal.h"
  18. #include "extensions/browser/api/declarative_net_request/ruleset_install_pref.h"
  19. #include "extensions/browser/blocklist_state.h"
  20. #include "extensions/browser/disable_reason.h"
  21. #include "extensions/browser/extension_prefs_scope.h"
  22. #include "extensions/browser/install_flag.h"
  23. #include "extensions/browser/pref_types.h"
  24. #include "extensions/common/api/declarative_net_request/constants.h"
  25. #include "extensions/common/constants.h"
  26. #include "extensions/common/extension.h"
  27. #include "extensions/common/extension_id.h"
  28. #include "extensions/common/mojom/manifest.mojom-shared.h"
  29. #include "extensions/common/url_pattern_set.h"
  30. #include "services/preferences/public/cpp/dictionary_value_update.h"
  31. #include "services/preferences/public/cpp/scoped_pref_update.h"
  32. #include "third_party/abseil-cpp/absl/types/optional.h"
  33. class ExtensionPrefValueMap;
  34. class PrefService;
  35. namespace base {
  36. class Clock;
  37. }
  38. namespace content {
  39. class BrowserContext;
  40. }
  41. namespace prefs {
  42. class DictionaryValueUpdate;
  43. }
  44. namespace user_prefs {
  45. class PrefRegistrySyncable;
  46. }
  47. namespace extensions {
  48. class AppSorting;
  49. class EarlyExtensionPrefsObserver;
  50. class ExtensionPrefsObserver;
  51. class PermissionSet;
  52. class URLPatternSet;
  53. // Class for managing global and per-extension preferences.
  54. //
  55. // This class distinguishes the following kinds of preferences:
  56. // - global preferences:
  57. // internal state for the extension system in general, not associated
  58. // with an individual extension, such as lastUpdateTime.
  59. // - per-extension preferences:
  60. // meta-preferences describing properties of the extension like
  61. // installation time, whether the extension is enabled, etc.
  62. // - extension controlled preferences:
  63. // browser preferences that an extension controls. For example, an
  64. // extension could use the proxy API to specify the browser's proxy
  65. // preference. Extension-controlled preferences are stored in
  66. // PrefValueStore::extension_prefs(), which this class populates and
  67. // maintains as the underlying extensions change.
  68. class ExtensionPrefs : public KeyedService {
  69. public:
  70. using ExtensionsInfo = std::vector<std::unique_ptr<ExtensionInfo>>;
  71. // Vector containing identifiers for preferences.
  72. typedef std::set<std::string> PrefKeySet;
  73. // This enum is used to store the reason an extension's install has been
  74. // delayed. Do not remove items or re-order this enum as it is used in
  75. // preferences.
  76. enum DelayReason {
  77. DELAY_REASON_NONE = 0,
  78. DELAY_REASON_GC = 1,
  79. DELAY_REASON_WAIT_FOR_IDLE = 2,
  80. DELAY_REASON_WAIT_FOR_IMPORTS = 3,
  81. DELAY_REASON_WAIT_FOR_OS_UPDATE = 4,
  82. };
  83. // This enum is used to specify the operation for bit map prefs.
  84. enum BitMapPrefOperation {
  85. BIT_MAP_PREF_ADD,
  86. BIT_MAP_PREF_REMOVE,
  87. BIT_MAP_PREF_REPLACE,
  88. BIT_MAP_PREF_CLEAR
  89. };
  90. // Wrappers around a prefs::ScopedDictionaryPrefUpdate, which allow us to
  91. // access the entry of a particular key for an extension. Use these if you
  92. // need a mutable record of a dictionary or list in the current settings.
  93. // Otherwise, prefer ReadPrefAsT() and UpdateExtensionPref() methods.
  94. class ScopedDictionaryUpdate {
  95. public:
  96. ScopedDictionaryUpdate(ExtensionPrefs* prefs,
  97. const std::string& extension_id,
  98. const std::string& key);
  99. ScopedDictionaryUpdate(const ScopedDictionaryUpdate&) = delete;
  100. ScopedDictionaryUpdate& operator=(const ScopedDictionaryUpdate&) = delete;
  101. ~ScopedDictionaryUpdate();
  102. // Returns a mutable value for the key, if one exists. Otherwise, returns
  103. // NULL.
  104. std::unique_ptr<prefs::DictionaryValueUpdate> Get();
  105. // Creates and returns a mutable value for the key, if one does not already
  106. // exist. Otherwise, returns the current value.
  107. std::unique_ptr<prefs::DictionaryValueUpdate> Create();
  108. private:
  109. std::unique_ptr<prefs::ScopedDictionaryPrefUpdate> update_;
  110. const std::string key_;
  111. };
  112. class ScopedListUpdate {
  113. public:
  114. ScopedListUpdate(ExtensionPrefs* prefs,
  115. const std::string& extension_id,
  116. const std::string& key);
  117. ScopedListUpdate(const ScopedListUpdate&) = delete;
  118. ScopedListUpdate& operator=(const ScopedListUpdate&) = delete;
  119. ~ScopedListUpdate();
  120. // Returns a mutable value for the key (ownership remains with the prefs),
  121. // if one exists. Otherwise, returns NULL.
  122. base::Value::List* Get();
  123. // Creates and returns a mutable value for the key (the prefs own the new
  124. // value), if one does not already exist. Otherwise, returns the current
  125. // value.
  126. base::Value::List* Ensure();
  127. private:
  128. std::unique_ptr<prefs::ScopedDictionaryPrefUpdate> update_;
  129. const std::string key_;
  130. };
  131. // Creates an ExtensionPrefs object.
  132. // Does not take ownership of |prefs| or |extension_pref_value_map|.
  133. // If |extensions_disabled| is true, extension controlled preferences and
  134. // content settings do not become effective. EarlyExtensionPrefsObservers
  135. // should be included in |early_observers| if they need to observe events
  136. // which occur during initialization of the ExtensionPrefs object.
  137. static ExtensionPrefs* Create(
  138. content::BrowserContext* browser_context,
  139. PrefService* prefs,
  140. const base::FilePath& root_dir,
  141. ExtensionPrefValueMap* extension_pref_value_map,
  142. bool extensions_disabled,
  143. const std::vector<EarlyExtensionPrefsObserver*>& early_observers);
  144. // A version of Create which allows injection of a custom base::Time provider.
  145. // Use this as needed for testing.
  146. static ExtensionPrefs* Create(
  147. content::BrowserContext* browser_context,
  148. PrefService* prefs,
  149. const base::FilePath& root_dir,
  150. ExtensionPrefValueMap* extension_pref_value_map,
  151. bool extensions_disabled,
  152. const std::vector<EarlyExtensionPrefsObserver*>& early_observers,
  153. base::Clock* clock);
  154. ExtensionPrefs(const ExtensionPrefs&) = delete;
  155. ExtensionPrefs& operator=(const ExtensionPrefs&) = delete;
  156. ~ExtensionPrefs() override;
  157. // Convenience function to get the ExtensionPrefs for a BrowserContext.
  158. static ExtensionPrefs* Get(content::BrowserContext* context);
  159. // Add or remove an observer from the ExtensionPrefs.
  160. void AddObserver(ExtensionPrefsObserver* observer);
  161. void RemoveObserver(ExtensionPrefsObserver* observer);
  162. // Returns true if the specified external extension was uninstalled by the
  163. // user.
  164. bool IsExternalExtensionUninstalled(const std::string& id) const;
  165. // Checks whether |extension_id| is disabled. If there's no state pref for
  166. // the extension, this will return false. Generally you should use
  167. // ExtensionService::IsExtensionEnabled instead.
  168. // Note that blocklisted extensions are NOT marked as disabled!
  169. bool IsExtensionDisabled(const std::string& id) const;
  170. // Get/Set the set of extensions that are pinned to the toolbar. Only used
  171. // when the experiment ExtensionsMenu is active."
  172. // TODO(crbug.com/943702): Remove reference to experiment when it launches or
  173. // remove code if it does not.
  174. ExtensionIdList GetPinnedExtensions() const;
  175. void SetPinnedExtensions(const ExtensionIdList& extension_ids);
  176. // Called when an extension is installed, so that prefs get created.
  177. // If |page_ordinal| is invalid then a page will be found for the App.
  178. // |install_flags| are a bitmask of extension::InstallFlags.
  179. // |ruleset_install_prefs| contains install prefs needed for the Declarative
  180. // Net Request API.
  181. void OnExtensionInstalled(const Extension* extension,
  182. Extension::State initial_state,
  183. const syncer::StringOrdinal& page_ordinal,
  184. int install_flags,
  185. const std::string& install_parameter,
  186. const declarative_net_request::RulesetInstallPrefs&
  187. ruleset_install_prefs);
  188. // OnExtensionInstalled with no install flags and |ruleset_install_prefs|.
  189. void OnExtensionInstalled(const Extension* extension,
  190. Extension::State initial_state,
  191. const syncer::StringOrdinal& page_ordinal,
  192. const std::string& install_parameter) {
  193. OnExtensionInstalled(extension, initial_state, page_ordinal,
  194. kInstallFlagNone, install_parameter, {});
  195. }
  196. // Called when an extension is uninstalled, so that prefs get cleaned up.
  197. void OnExtensionUninstalled(const std::string& extension_id,
  198. const mojom::ManifestLocation location,
  199. bool external_uninstall);
  200. // Sets the extension's state to enabled and clears disable reasons.
  201. void SetExtensionEnabled(const std::string& extension_id);
  202. // Sets the extension's state to disabled and sets the disable reasons.
  203. // However, if the current state is EXTERNAL_EXTENSION_UNINSTALLED then that
  204. // is preserved (but the disable reasons are still set).
  205. void SetExtensionDisabled(const std::string& extension_id,
  206. int disable_reasons);
  207. // Gets the value of a bit map pref. Gets the value of
  208. // |extension_id| from |pref_key|. If the value is not found or invalid,
  209. // return the |default_bit|.
  210. int GetBitMapPrefBits(const std::string& extension_id,
  211. base::StringPiece pref_key,
  212. int default_bit) const;
  213. // Modifies the extensions bit map pref |pref_key| to add a new bit value,
  214. // remove an existing bit value, or clear all bits. If |operation| is
  215. // BIT_MAP_PREF_CLEAR, then |pending_bits| are ignored. If the updated pref
  216. // value is the same as the |default_bit|, the pref value will be set to null.
  217. void ModifyBitMapPrefBits(const std::string& extension_id,
  218. int pending_bits,
  219. BitMapPrefOperation operation,
  220. base::StringPiece pref_key,
  221. int default_bit);
  222. // Gets or sets profile wide ExtensionPrefs.
  223. void SetIntegerPref(const PrefMap& pref, int value);
  224. void SetBooleanPref(const PrefMap& pref, bool value);
  225. void SetStringPref(const PrefMap& pref, const std::string& value);
  226. void SetTimePref(const PrefMap& pref, base::Time value);
  227. void SetGURLPref(const PrefMap& pref, const GURL& value);
  228. void SetDictionaryPref(const PrefMap& pref,
  229. std::unique_ptr<base::DictionaryValue> value);
  230. int GetPrefAsInteger(const PrefMap& pref) const;
  231. bool GetPrefAsBoolean(const PrefMap& pref) const;
  232. std::string GetPrefAsString(const PrefMap& pref) const;
  233. base::Time GetPrefAsTime(const PrefMap& pref) const;
  234. GURL GetPrefAsGURL(const PrefMap& pref) const;
  235. const base::DictionaryValue* GetPrefAsDictionary(const PrefMap& pref) const;
  236. // Returns a wrapper that allows to update an ExtensionPref with a
  237. // PrefType::kDictionary.
  238. std::unique_ptr<prefs::ScopedDictionaryPrefUpdate> CreatePrefUpdate(
  239. const PrefMap& pref);
  240. // Increments/decrements an ExtensionPref with a PrefType::kInteger.
  241. void IncrementPref(const PrefMap& pref);
  242. void DecrementPref(const PrefMap& pref);
  243. // Populates |out| with the ids of all installed extensions.
  244. void GetExtensions(ExtensionIdList* out) const;
  245. void SetIntegerPref(const std::string& id, const PrefMap& pref, int value);
  246. void SetBooleanPref(const std::string& id, const PrefMap& pref, bool value);
  247. void SetStringPref(const std::string& id,
  248. const PrefMap& pref,
  249. const std::string value);
  250. void SetListPref(const std::string& id,
  251. const PrefMap& pref,
  252. base::Value value);
  253. void SetDictionaryPref(const std::string& id,
  254. const PrefMap& pref,
  255. std::unique_ptr<base::DictionaryValue> value);
  256. void SetTimePref(const std::string& id,
  257. const PrefMap& pref,
  258. const base::Time value);
  259. void UpdateExtensionPref(const std::string& id,
  260. base::StringPiece key,
  261. std::unique_ptr<base::Value> value);
  262. void DeleteExtensionPrefs(const std::string& id);
  263. void DeleteExtensionPrefsIfPrefEmpty(const std::string& id);
  264. bool ReadPrefAsBoolean(const std::string& extension_id,
  265. const PrefMap& pref,
  266. bool* out_value) const;
  267. bool ReadPrefAsInteger(const std::string& extension_id,
  268. const PrefMap& pref,
  269. int* out_value) const;
  270. bool ReadPrefAsString(const std::string& extension_id,
  271. const PrefMap& pref,
  272. std::string* out_value) const;
  273. bool ReadPrefAsList(const std::string& extension_id,
  274. const PrefMap& pref,
  275. const base::ListValue** out_value) const;
  276. bool ReadPrefAsDictionary(const std::string& extension_id,
  277. const PrefMap& pref,
  278. const base::DictionaryValue** out_value) const;
  279. base::Time ReadPrefAsTime(const std::string& extension_id,
  280. const PrefMap& pref) const;
  281. bool ReadPrefAsBoolean(const std::string& extension_id,
  282. base::StringPiece pref_key,
  283. bool* out_value) const;
  284. bool ReadPrefAsInteger(const std::string& extension_id,
  285. base::StringPiece pref_key,
  286. int* out_value) const;
  287. bool ReadPrefAsString(const std::string& extension_id,
  288. base::StringPiece pref_key,
  289. std::string* out_value) const;
  290. bool ReadPrefAsList(const std::string& extension_id,
  291. base::StringPiece pref_key,
  292. const base::ListValue** out_value) const;
  293. // DEPRECATED: prefer ReadPrefAsDict() instead.
  294. bool ReadPrefAsDictionary(const std::string& extension_id,
  295. base::StringPiece pref_key,
  296. const base::DictionaryValue** out_value) const;
  297. const base::Value::Dict* ReadPrefAsDict(const std::string& extension_id,
  298. base::StringPiece pref_key) const;
  299. // Interprets the list pref, |pref_key| in |extension_id|'s preferences, as a
  300. // URLPatternSet. The |valid_schemes| specify how to parse the URLPatterns.
  301. bool ReadPrefAsURLPatternSet(const std::string& extension_id,
  302. base::StringPiece pref_key,
  303. URLPatternSet* result,
  304. int valid_schemes) const;
  305. // Converts |set| to a list of strings and sets the |pref_key| pref belonging
  306. // to |extension_id|. If |set| is empty, the preference for |pref_key| is
  307. // cleared.
  308. void SetExtensionPrefURLPatternSet(const std::string& extension_id,
  309. base::StringPiece pref_key,
  310. const URLPatternSet& set);
  311. bool HasPrefForExtension(const std::string& extension_id) const;
  312. // Did the extension ask to escalate its permission during an upgrade?
  313. bool DidExtensionEscalatePermissions(const std::string& id) const;
  314. // Getters and setters for disabled reason.
  315. // Note that you should rarely need to modify disable reasons directly -
  316. // pass the proper value to SetExtensionState instead when you enable/disable
  317. // an extension. In particular, AddDisableReason(s) is only legal when the
  318. // extension is not enabled.
  319. int GetDisableReasons(const std::string& extension_id) const;
  320. bool HasDisableReason(const std::string& extension_id,
  321. disable_reason::DisableReason disable_reason) const;
  322. void AddDisableReason(const std::string& extension_id,
  323. disable_reason::DisableReason disable_reason);
  324. void AddDisableReasons(const std::string& extension_id, int disable_reasons);
  325. void RemoveDisableReason(const std::string& extension_id,
  326. disable_reason::DisableReason disable_reason);
  327. void ReplaceDisableReasons(const std::string& extension_id,
  328. int disable_reasons);
  329. void ClearDisableReasons(const std::string& extension_id);
  330. // Clears disable reasons that do not apply to component extensions.
  331. void ClearInapplicableDisableReasonsForComponentExtension(
  332. const std::string& component_extension_id);
  333. // Returns the version string for the currently installed extension, or
  334. // the empty string if not found.
  335. std::string GetVersionString(const std::string& extension_id) const;
  336. // Re-writes the extension manifest into the prefs.
  337. // Called to change the extension's manifest when it's re-localized.
  338. void UpdateManifest(const Extension* extension);
  339. // Returns base extensions install directory.
  340. const base::FilePath& install_directory() const { return install_directory_; }
  341. // For updating the prefs when the install location is changed for the
  342. // extension.
  343. void SetInstallLocation(const std::string& extension_id,
  344. mojom::ManifestLocation location);
  345. // Increment the count of how many times we prompted the user to acknowledge
  346. // the given extension, and return the new count.
  347. int IncrementAcknowledgePromptCount(const std::string& extension_id);
  348. // Whether the user has acknowledged an external extension.
  349. bool IsExternalExtensionAcknowledged(const std::string& extension_id) const;
  350. void AcknowledgeExternalExtension(const std::string& extension_id);
  351. // Whether the user has acknowledged a blocklisted extension.
  352. bool IsBlocklistedExtensionAcknowledged(
  353. const std::string& extension_id) const;
  354. void AcknowledgeBlocklistedExtension(const std::string& extension_id);
  355. // Whether the external extension was installed during the first run
  356. // of this profile.
  357. bool IsExternalInstallFirstRun(const std::string& extension_id) const;
  358. void SetExternalInstallFirstRun(const std::string& extension_id);
  359. // Returns true if the extension notification code has already run for the
  360. // first time for this profile. Currently we use this flag to mean that any
  361. // extensions that would trigger notifications should get silently
  362. // acknowledged. This is a fuse. Calling it the first time returns false.
  363. // Subsequent calls return true. It's not possible through an API to ever
  364. // reset it. Don't call it unless you mean it!
  365. bool SetAlertSystemFirstRun();
  366. // Returns the last value set via SetLastPingDay. If there isn't such a
  367. // pref, the returned Time will return true for is_null().
  368. base::Time LastPingDay(const std::string& extension_id) const;
  369. // The time stored is based on the server's perspective of day start time, not
  370. // the client's.
  371. void SetLastPingDay(const std::string& extension_id, const base::Time& time);
  372. // Similar to the 2 above, but for the extensions blocklist.
  373. base::Time BlocklistLastPingDay() const;
  374. void SetBlocklistLastPingDay(const base::Time& time);
  375. // Similar to LastPingDay/SetLastPingDay, but for sending "days since active"
  376. // ping.
  377. base::Time LastActivePingDay(const std::string& extension_id) const;
  378. void SetLastActivePingDay(const std::string& extension_id,
  379. const base::Time& time);
  380. // A bit we use for determining if we should send the "days since active"
  381. // ping. A value of true means the item has been active (launched) since the
  382. // last update check.
  383. bool GetActiveBit(const std::string& extension_id) const;
  384. void SetActiveBit(const std::string& extension_id, bool active);
  385. // Returns the granted permission set for the extension with |extension_id|,
  386. // and NULL if no preferences were found for |extension_id|.
  387. // This passes ownership of the returned set to the caller.
  388. std::unique_ptr<PermissionSet> GetGrantedPermissions(
  389. const std::string& extension_id) const;
  390. // Adds |permissions| to the granted permissions set for the extension with
  391. // |extension_id|. The new granted permissions set will be the union of
  392. // |permissions| and the already granted permissions.
  393. void AddGrantedPermissions(const std::string& extension_id,
  394. const PermissionSet& permissions);
  395. // As above, but subtracts the given |permissions| from the granted set.
  396. void RemoveGrantedPermissions(const std::string& extension_id,
  397. const PermissionSet& permissions);
  398. // Gets the set of permissions that the extension would like to be active.
  399. // This should always include at least the required permissions from the
  400. // manifest and can include a subset of optional permissions, if the extension
  401. // requested and was granted them.
  402. // This differs from the set of permissions *actually* active on the extension
  403. // because the user may have withheld certain permissions, as well as because
  404. // of possible enterprise policy settings. Use `PermissionsData` to determine
  405. // the current effective permissions of an extension.
  406. std::unique_ptr<PermissionSet> GetDesiredActivePermissions(
  407. const std::string& extension_id) const;
  408. // Sets the desired active permissions for the given `extension_id` to
  409. // `permissions`.
  410. void SetDesiredActivePermissions(const std::string& extension_id,
  411. const PermissionSet& permissions);
  412. // Adds `permissions` to the set of permissions the extension desires to be
  413. // active.
  414. void AddDesiredActivePermissions(const ExtensionId& extension_id,
  415. const PermissionSet& permissions);
  416. // Removes `permissions` to the set of permissions the extension desires to be
  417. // active.
  418. void RemoveDesiredActivePermissions(const ExtensionId& extension_id,
  419. const PermissionSet& permissions);
  420. // Sets/Gets the value indicating if an extension should be granted all the
  421. // requested host permissions without requiring explicit runtime-granted
  422. // permissions from the user.
  423. void SetWithholdingPermissions(const ExtensionId& extension_id,
  424. bool should_withhold);
  425. bool GetWithholdingPermissions(const ExtensionId& extension_id) const;
  426. // Returns the set of runtime-granted permissions. These are permissions that
  427. // the user explicitly approved at runtime, rather than install time (such
  428. // as those granted through the permissions API or the runtime host
  429. // permissions feature). Note that, similar to granted permissions, this can
  430. // include permissions granted to the extension, even if they are not active.
  431. std::unique_ptr<PermissionSet> GetRuntimeGrantedPermissions(
  432. const ExtensionId& extension_id) const;
  433. // Adds to the set of runtime-granted permissions.
  434. void AddRuntimeGrantedPermissions(const ExtensionId& extension_id,
  435. const PermissionSet& permissions);
  436. // Removes from the set of runtime-granted permissions.
  437. void RemoveRuntimeGrantedPermissions(const ExtensionId& extension_id,
  438. const PermissionSet& permissions);
  439. // Records whether or not this extension is currently running.
  440. void SetExtensionRunning(const std::string& extension_id, bool is_running);
  441. // Returns whether or not this extension is marked as running. This is used to
  442. // restart apps across browser restarts.
  443. bool IsExtensionRunning(const std::string& extension_id) const;
  444. // Set/Get whether or not the app is active. Used to force a launch of apps
  445. // that don't handle onRestarted() on a restart. We can only safely do that if
  446. // the app was active when it was last running.
  447. void SetIsActive(const std::string& extension_id, bool is_active);
  448. bool IsActive(const std::string& extension_id) const;
  449. // Returns true if the user enabled this extension to be loaded in incognito
  450. // mode.
  451. //
  452. // IMPORTANT: you probably want to use extensions::util::IsIncognitoEnabled
  453. // instead of this method.
  454. bool IsIncognitoEnabled(const std::string& extension_id) const;
  455. void SetIsIncognitoEnabled(const std::string& extension_id, bool enabled);
  456. // Returns true if the user has chosen to allow this extension to inject
  457. // scripts into pages with file URLs.
  458. //
  459. // IMPORTANT: you probably want to use extensions::util::AllowFileAccess
  460. // instead of this method.
  461. bool AllowFileAccess(const std::string& extension_id) const;
  462. void SetAllowFileAccess(const std::string& extension_id, bool allow);
  463. bool HasAllowFileAccessSetting(const std::string& extension_id) const;
  464. // Saves ExtensionInfo for each installed extension with the path to the
  465. // version directory and the location. Blocklisted extensions won't be saved
  466. // and neither will external extensions the user has explicitly uninstalled.
  467. // Caller takes ownership of returned structure.
  468. std::unique_ptr<ExtensionsInfo> GetInstalledExtensionsInfo(
  469. bool include_component_extensions = false) const;
  470. // Returns the ExtensionInfo from the prefs for the given extension. If the
  471. // extension is not present, NULL is returned.
  472. std::unique_ptr<ExtensionInfo> GetInstalledExtensionInfo(
  473. const std::string& extension_id,
  474. bool include_component_extensions = false) const;
  475. // We've downloaded an updated .crx file for the extension, but are waiting
  476. // to install it.
  477. //
  478. // |install_flags| are a bitmask of extension::InstallFlags.
  479. void SetDelayedInstallInfo(const Extension* extension,
  480. Extension::State initial_state,
  481. int install_flags,
  482. DelayReason delay_reason,
  483. const syncer::StringOrdinal& page_ordinal,
  484. const std::string& install_parameter,
  485. const declarative_net_request::RulesetInstallPrefs&
  486. ruleset_install_prefs = {});
  487. // Removes any delayed install information we have for the given
  488. // |extension_id|. Returns true if there was info to remove; false otherwise.
  489. bool RemoveDelayedInstallInfo(const std::string& extension_id);
  490. // Update the prefs to finish the update for an extension.
  491. bool FinishDelayedInstallInfo(const std::string& extension_id);
  492. // Returns the ExtensionInfo from the prefs for delayed install information
  493. // for |extension_id|, if we have any. Otherwise returns NULL.
  494. std::unique_ptr<ExtensionInfo> GetDelayedInstallInfo(
  495. const std::string& extension_id) const;
  496. DelayReason GetDelayedInstallReason(const std::string& extension_id) const;
  497. // Returns information about all the extensions that have delayed install
  498. // information.
  499. std::unique_ptr<ExtensionsInfo> GetAllDelayedInstallInfo() const;
  500. // Returns true if there is an extension which controls the preference value
  501. // for |pref_key| *and* it is specific to incognito mode.
  502. bool HasIncognitoPrefValue(const std::string& pref_key) const;
  503. // Returns the creation flags mask for the extension.
  504. int GetCreationFlags(const std::string& extension_id) const;
  505. // Returns the creation flags mask for a delayed install extension.
  506. int GetDelayedInstallCreationFlags(const std::string& extension_id) const;
  507. // Returns true if the extension was installed from the Chrome Web Store.
  508. bool IsFromWebStore(const std::string& extension_id) const;
  509. // Returns true if the extension was installed as a default app.
  510. bool WasInstalledByDefault(const std::string& extension_id) const;
  511. // Returns true if the extension was installed as an oem app.
  512. bool WasInstalledByOem(const std::string& extension_id) const;
  513. // Helper method to acquire the installation time of an extension.
  514. // Returns base::Time() if the installation time could not be parsed or
  515. // found.
  516. base::Time GetInstallTime(const std::string& extension_id) const;
  517. // Returns true if the extension should not be synced.
  518. bool DoNotSync(const std::string& extension_id) const;
  519. // Gets/sets the last launch time of an extension.
  520. base::Time GetLastLaunchTime(const std::string& extension_id) const;
  521. void SetLastLaunchTime(const std::string& extension_id,
  522. const base::Time& time);
  523. // Clear any launch times. This is called by the browsing data remover when
  524. // history is cleared.
  525. void ClearLastLaunchTimes();
  526. static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
  527. bool extensions_disabled() const { return extensions_disabled_; }
  528. // The underlying PrefService.
  529. PrefService* pref_service() const { return prefs_; }
  530. // The underlying AppSorting.
  531. AppSorting* app_sorting() const;
  532. // Schedules garbage collection of an extension's on-disk data on the next
  533. // start of this ExtensionService. Applies only to extensions with isolated
  534. // storage.
  535. void SetNeedsStorageGarbageCollection(bool value);
  536. bool NeedsStorageGarbageCollection() const;
  537. // Used by AppWindowGeometryCache to persist its cache. These methods
  538. // should not be called directly.
  539. const base::DictionaryValue* GetGeometryCache(
  540. const std::string& extension_id) const;
  541. void SetGeometryCache(const std::string& extension_id,
  542. std::unique_ptr<base::DictionaryValue> cache);
  543. // Used for verification of installed extension ids. For the Set method, pass
  544. // null to remove the preference.
  545. const base::DictionaryValue* GetInstallSignature() const;
  546. void SetInstallSignature(const base::DictionaryValue* signature);
  547. // The installation parameter associated with the extension.
  548. std::string GetInstallParam(const std::string& extension_id) const;
  549. void SetInstallParam(const std::string& extension_id,
  550. const std::string& install_parameter);
  551. // Whether the extension with the given |extension_id| needs to be synced.
  552. // This is set when the state (such as enabled/disabled or allowed in
  553. // incognito) is changed before Sync is ready.
  554. bool NeedsSync(const std::string& extension_id) const;
  555. void SetNeedsSync(const std::string& extension_id, bool needs_sync);
  556. // Returns false if there is no ruleset checksum corresponding to the given
  557. // |extension_id| and |ruleset_id|. On success, returns true and populates the
  558. // checksum.
  559. bool GetDNRStaticRulesetChecksum(
  560. const ExtensionId& extension_id,
  561. declarative_net_request::RulesetID ruleset_id,
  562. int* checksum) const;
  563. void SetDNRStaticRulesetChecksum(
  564. const ExtensionId& extension_id,
  565. declarative_net_request::RulesetID ruleset_id,
  566. int checksum);
  567. // Returns false if there is no dynamic ruleset corresponding to
  568. // |extension_id|. On success, returns true and populates the checksum.
  569. bool GetDNRDynamicRulesetChecksum(const ExtensionId& extension_id,
  570. int* checksum) const;
  571. void SetDNRDynamicRulesetChecksum(const ExtensionId& extension_id,
  572. int checksum);
  573. // Returns the set of enabled static ruleset IDs or absl::nullopt if the
  574. // extension hasn't updated the set of enabled static rulesets.
  575. absl::optional<std::set<declarative_net_request::RulesetID>>
  576. GetDNREnabledStaticRulesets(const ExtensionId& extension_id) const;
  577. // Updates the set of enabled static rulesets for the |extension_id|. This
  578. // preference gets cleared on extension update.
  579. void SetDNREnabledStaticRulesets(
  580. const ExtensionId& extension_id,
  581. const std::set<declarative_net_request::RulesetID>& ids);
  582. // Whether the extension with the given |extension_id| is using its ruleset's
  583. // matched action count for the badge text. This is set via the
  584. // setExtensionActionOptions API call.
  585. bool GetDNRUseActionCountAsBadgeText(const ExtensionId& extension_id) const;
  586. void SetDNRUseActionCountAsBadgeText(const ExtensionId& extension_id,
  587. bool use_action_count_as_badge_text);
  588. // Whether the ruleset for the given |extension_id| and |ruleset_id| should be
  589. // ignored while loading the extension.
  590. bool ShouldIgnoreDNRRuleset(
  591. const ExtensionId& extension_id,
  592. declarative_net_request::RulesetID ruleset_id) const;
  593. // Returns the global rule allocation for the given |extension_id|. If no
  594. // rules are allocated to the extension, false is returned.
  595. bool GetDNRAllocatedGlobalRuleCount(const ExtensionId& extension_id,
  596. size_t* rule_count) const;
  597. void SetDNRAllocatedGlobalRuleCount(const ExtensionId& extension_id,
  598. size_t rule_count);
  599. // Whether the extension with the given |extension_id| should have its excess
  600. // global rules allocation kept during its next load.
  601. bool GetDNRKeepExcessAllocation(const ExtensionId& extension_id) const;
  602. void SetDNRKeepExcessAllocation(const ExtensionId& extension_id,
  603. bool keep_excess_allocation);
  604. // Migrates the disable reasons extension pref for extensions that were
  605. // disabled due to a deprecated reason.
  606. // TODO(archanasimha): Remove this around M89.
  607. void MigrateDeprecatedDisableReasons();
  608. // Looks to see if the Youtube extension is installed, and removes the
  609. // FROM_BOOKMARK flag from it's creation flags.
  610. // TODO(dmurph): Remove this in m90.
  611. void MigrateYoutubeOffBookmarkApps();
  612. // Iterates over the extension pref entries and removes any obsolete keys. We
  613. // need to do this here specially (rather than in
  614. // MigrateObsoleteProfilePrefs()) because these entries are subkeys of the
  615. // extension's dictionary, which is keyed on the extension ID.
  616. void MigrateObsoleteExtensionPrefs();
  617. // Updates an extension to use the new withholding pref key if it doesn't have
  618. // it yet, removing the old key in the process.
  619. // TODO(tjudkins): Remove this and the obsolete key in M83.
  620. void MigrateToNewWithholdingPref();
  621. // Migrates to the new way of recording explicit user uninstalls of external
  622. // extensions (by using a list of IDs rather than a bit set in each extension
  623. // dictionary).
  624. // TODO(devlin): Remove this once clients are migrated over, around M84.
  625. void MigrateToNewExternalUninstallPref();
  626. // Returns true if the given component extension should be installed, even
  627. // though it has been obsoleted. Installing it allows us to ensure it is
  628. // cleaned/deleted up properly. After that cleanup is done, this will return
  629. // false.
  630. bool ShouldInstallObsoleteComponentExtension(const std::string& extension_id);
  631. // Mark the given component extension as deleted. It should not be installed /
  632. // loaded again after this.
  633. void MarkObsoleteComponentExtensionAsRemoved(
  634. const std::string& extension_id,
  635. const mojom::ManifestLocation location);
  636. // When called before the ExtensionService is created, alerts that are
  637. // normally suppressed in first run will still trigger.
  638. static void SetRunAlertsInFirstRunForTest();
  639. void ClearExternalUninstallForTesting(const ExtensionId& id);
  640. static const char kFakeObsoletePrefForTesting[];
  641. private:
  642. friend class ExtensionPrefsBlocklistedExtensions; // Unit test.
  643. friend class ExtensionPrefsComponentExtension; // Unit test.
  644. friend class ExtensionPrefsUninstallExtension; // Unit test.
  645. friend class
  646. ExtensionPrefsBitMapPrefValueClearedIfEqualsDefaultValue; // Unit test.
  647. // See the Create methods.
  648. ExtensionPrefs(
  649. content::BrowserContext* browser_context,
  650. PrefService* prefs,
  651. const base::FilePath& root_dir,
  652. ExtensionPrefValueMap* extension_pref_value_map,
  653. base::Clock* clock,
  654. bool extensions_disabled,
  655. const std::vector<EarlyExtensionPrefsObserver*>& early_observers);
  656. // Sets profile wide ExtensionPrefs.
  657. void SetPref(const PrefMap& pref, std::unique_ptr<base::Value> value);
  658. // Updates ExtensionPrefs for a specific extension.
  659. void UpdateExtensionPref(const std::string& id,
  660. const PrefMap& pref,
  661. std::unique_ptr<base::Value> value);
  662. // Converts absolute paths in the pref to paths relative to the
  663. // install_directory_.
  664. void MakePathsRelative();
  665. // Converts internal relative paths to be absolute. Used for export to
  666. // consumers who expect full paths.
  667. void MakePathsAbsolute(base::DictionaryValue* dict);
  668. // Helper function used by GetInstalledExtensionInfo() and
  669. // GetDelayedInstallInfo() to construct an ExtensionInfo from the provided
  670. // |extension| dictionary.
  671. std::unique_ptr<ExtensionInfo> GetInstalledInfoHelper(
  672. const std::string& extension_id,
  673. const base::Value* extension,
  674. bool include_component_extensions) const;
  675. // Read the boolean preference entry and return true if the preference exists
  676. // and the preference's value is true; false otherwise.
  677. bool ReadPrefAsBooleanAndReturn(const std::string& extension_id,
  678. base::StringPiece pref_key) const;
  679. // Interprets |pref_key| in |extension_id|'s preferences as an
  680. // PermissionSet, and passes ownership of the set to the caller.
  681. std::unique_ptr<PermissionSet> ReadPrefAsPermissionSet(
  682. const std::string& extension_id,
  683. base::StringPiece pref_key) const;
  684. // Converts the |new_value| to its value and sets the |pref_key| pref
  685. // belonging to |extension_id|.
  686. void SetExtensionPrefPermissionSet(const std::string& extension_id,
  687. base::StringPiece pref_key,
  688. const PermissionSet& new_value);
  689. // Common implementation to add permissions to a stored permission set.
  690. void AddToPrefPermissionSet(const ExtensionId& extension_id,
  691. const PermissionSet& permissions,
  692. const char* pref_name);
  693. // Common implementation to remove permissions from a stored permission set.
  694. void RemoveFromPrefPermissionSet(const ExtensionId& extension_id,
  695. const PermissionSet& permissions,
  696. const char* pref_name);
  697. // Returns an immutable dictionary for extension |id|'s prefs, or NULL if it
  698. // doesn't exist.
  699. const base::DictionaryValue* GetExtensionPref(const std::string& id) const;
  700. // Returns an immutable base::Value for extension |id|'s prefs, or nullptr if
  701. // it doesn't exist.
  702. const base::Value* GetPrefAsValue(const std::string& extension_id,
  703. base::StringPiece pref_key) const;
  704. // Modifies the extensions disable reasons to add a new reason, remove an
  705. // existing reason, or clear all reasons. Notifies observers if the set of
  706. // DisableReasons has changed.
  707. // If |operation| is BIT_MAP_PREF_CLEAR, then |reasons| are ignored.
  708. void ModifyDisableReasons(const std::string& extension_id,
  709. int reasons,
  710. BitMapPrefOperation operation);
  711. // Installs the persistent extension preferences into |prefs_|'s extension
  712. // pref store. Does nothing if extensions_disabled_ is true.
  713. void InitPrefStore();
  714. // Checks whether there is a state pref for the extension and if so, whether
  715. // it matches |check_state|.
  716. bool DoesExtensionHaveState(const std::string& id,
  717. Extension::State check_state) const;
  718. // Reads the list of strings for |pref| from user prefs into
  719. // |id_container_out|. Returns false if the pref wasn't found in the user
  720. // pref store.
  721. template <class ExtensionIdContainer>
  722. bool GetUserExtensionPrefIntoContainer(
  723. const char* pref,
  724. ExtensionIdContainer* id_container_out) const;
  725. // Writes the list of strings contained in |strings| to |pref| in prefs.
  726. template <class ExtensionIdContainer>
  727. void SetExtensionPrefFromContainer(const char* pref,
  728. const ExtensionIdContainer& strings);
  729. // Helper function to populate |extension_dict| with the values needed
  730. // by a newly installed extension. Work is broken up between this
  731. // function and FinishExtensionInfoPrefs() to accommodate delayed
  732. // installations.
  733. //
  734. // |install_flags| are a bitmask of extension::InstallFlags.
  735. void PopulateExtensionInfoPrefs(
  736. const Extension* extension,
  737. const base::Time install_time,
  738. Extension::State initial_state,
  739. int install_flags,
  740. const std::string& install_parameter,
  741. const declarative_net_request::RulesetInstallPrefs& ruleset_install_prefs,
  742. prefs::DictionaryValueUpdate* extension_dict);
  743. void InitExtensionControlledPrefs(const ExtensionsInfo& extensions_info);
  744. // Loads preferences for the given |extension_id| into the pref value map.
  745. void LoadExtensionControlledPrefs(const ExtensionId& extension_id,
  746. ExtensionPrefsScope scope);
  747. // Helper function to complete initialization of the values in
  748. // |extension_dict| for an extension install. Also see
  749. // PopulateExtensionInfoPrefs().
  750. void FinishExtensionInfoPrefs(
  751. const std::string& extension_id,
  752. const base::Time install_time,
  753. bool needs_sort_ordinal,
  754. const syncer::StringOrdinal& suggested_page_ordinal,
  755. prefs::DictionaryValueUpdate* extension_dict);
  756. // Returns true if the prefs have any permission withholding setting stored
  757. // for a given extension.
  758. bool HasWithholdingPermissionsSetting(const ExtensionId& extension_id) const;
  759. // Clears the bit indicating that an external extension was uninstalled.
  760. void ClearExternalUninstallBit(const ExtensionId& extension_id);
  761. raw_ptr<content::BrowserContext> browser_context_;
  762. // The pref service specific to this set of extension prefs. Owned by the
  763. // BrowserContext.
  764. raw_ptr<PrefService> prefs_;
  765. // Base extensions install directory.
  766. base::FilePath install_directory_;
  767. // Weak pointer, owned by BrowserContext.
  768. raw_ptr<ExtensionPrefValueMap> extension_pref_value_map_;
  769. raw_ptr<base::Clock> clock_;
  770. bool extensions_disabled_;
  771. base::ObserverList<ExtensionPrefsObserver>::Unchecked observer_list_;
  772. };
  773. } // namespace extensions
  774. #endif // EXTENSIONS_BROWSER_EXTENSION_PREFS_H_