Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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_PREF_VALUE_MAP_H_ | |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_VALUE_MAP_H_ | |
| 7 #pragma once | |
| 8 | |
| 9 #include <map> | |
| 10 #include <set> | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/time.h" | |
| 14 #include "chrome/browser/prefs/value_map_pref_store.h" | |
| 15 | |
| 16 // Non-persistent data container that is shared by ExtensionPrefStores. All | |
| 17 // extension pref values (incognito and regular) are stored herein and | |
| 18 // provided to ExtensionPrefStores. | |
| 19 // | |
| 20 // The semantics of the ExtensionPrefValueMap are: | |
| 21 // - The precedence of extensions is determined by their installation time. | |
| 22 // The extension that has been installed later takes higher precedence. | |
| 23 // - If two extensions set a value for the same preference, the following | |
| 24 // rules determine which value becomes effective (visible). | |
| 25 // - The effective regular extension pref value is determined by the regular | |
| 26 // extension pref value of the extension with the highest precedence. | |
| 27 // - The effective incognito extension pref value is determined by the incognito | |
| 28 // extension pref value of the extension with the highest precedence, unless | |
| 29 // another extension with higher precedence overrides it with a regular | |
| 30 // extension pref value. | |
| 31 // | |
| 32 // The following table illustrates the behavior: | |
| 33 // A.reg | A.inc | B.reg | B.inc | E.reg | E.inc | |
| 34 // 1 | - | - | - | 1 | 1 | |
| 35 // 1 | 2 | - | - | 1 | 2 | |
| 36 // 1 | - | 3 | - | 3 | 3 | |
| 37 // 1 | - | - | 4 | 1 | 4 | |
| 38 // 1 | 2 | 3 | - | 3 | 3(!) | |
| 39 // 1 | 2 | - | 4 | 1 | 4 | |
| 40 // 1 | 2 | 3 | 4 | 3 | 4 | |
| 41 // A = extension A, B = extension B, E = effective value | |
| 42 // .reg = regular value | |
| 43 // .inc = incognito value | |
| 44 // Extension B has higher precedence than A. | |
| 45 class ExtensionPrefValueMap { | |
| 46 public: | |
| 47 // Observer interface for monitoring ExtensionPrefValueMap. | |
| 48 class Observer { | |
| 49 public: | |
| 50 virtual ~Observer() {} | |
| 51 | |
| 52 // Called when the value for the given |key| set by one of the extensions | |
| 53 // changes. This does not necessarily mean that the effective value has | |
| 54 // changed. | |
| 55 virtual void OnPrefValueChanged(const std::string& key) = 0; | |
| 56 // Notification about the ExtensionPrefValueMap being fully initialized. | |
| 57 virtual void OnInitializationCompleted() = 0; | |
| 58 // Called when the ExtensionPrefValueMap is being destroyed. Requires | |
| 59 // unsubscription. | |
|
Mattias Nissler (ping if slow)
2011/01/07 10:12:58
Requires unsubscription is a bit vague, can you ju
battre
2011/01/10 16:55:47
Done.
| |
| 60 virtual void OnExtensionPrefValueMapDestruction() = 0; | |
| 61 }; | |
| 62 | |
| 63 ExtensionPrefValueMap(); | |
| 64 virtual ~ExtensionPrefValueMap(); | |
| 65 | |
| 66 // Set an extension preference |value| for |key| of extension |ext_id|. | |
| 67 // Takes ownership of |value|. | |
| 68 // Note that regular extension pref values need to be reported to | |
| 69 // incognito and to regular ExtensionPrefStores. | |
| 70 // Precondition: the extension must be registered. | |
| 71 void SetExtensionPref(const std::string& ext_id, | |
| 72 const std::string& key, | |
| 73 bool incognito, | |
| 74 Value* value); | |
| 75 | |
| 76 // Remove the extension preference value for |key| of extension |ext_id|. | |
| 77 // Precondition: the extension must be registered. | |
| 78 void RemoveExtensionPref(const std::string& ext_id, | |
| 79 const std::string& key, | |
| 80 bool incognito); | |
| 81 | |
| 82 // Tell the store it's now fully initialized. | |
| 83 void NotifyInitializationCompleted(); | |
| 84 | |
| 85 // Registers the time when an extension |ext_id| is installed. | |
| 86 void RegisterExtension(const std::string& ext_id, | |
| 87 const base::Time& install_time, | |
| 88 bool is_enabled); | |
| 89 | |
| 90 // Deletes all entries related to extension |ext_id|. | |
| 91 void UnregisterExtension(const std::string& ext_id); | |
| 92 | |
| 93 // Hides or makes the extension preference values of the specified extension | |
| 94 // visible. | |
| 95 void SetExtensionState(const std::string& ext_id, bool is_enabled); | |
| 96 | |
| 97 // Adds an observer and notifies it about the currently stored keys. | |
| 98 void AddObserver(Observer* observer); | |
| 99 | |
| 100 void RemoveObserver(Observer* observer); | |
| 101 | |
| 102 void NotifyOfDestruction(); | |
|
Mattias Nissler (ping if slow)
2011/01/07 10:12:58
Why is this public?
battre
2011/01/10 16:55:47
Done.
| |
| 103 | |
| 104 const Value* GetEffectivePrefValue(const std::string& key, | |
| 105 bool incognito) const; | |
| 106 | |
| 107 private: | |
| 108 struct ExtensionEntry { | |
| 109 // Installation time of the extension. | |
| 110 base::Time install_time; | |
| 111 // Whether extension is enabled in the profile. | |
| 112 bool enabled; | |
| 113 // Regular preferences. | |
| 114 PrefValueMap reg_preferences; | |
| 115 // Incognito preferences, empty for regular ExtensionPrefStore. | |
| 116 PrefValueMap inc_preferences; | |
| 117 }; | |
| 118 | |
| 119 typedef std::map<std::string, ExtensionEntry*> ExtensionEntryMap; | |
| 120 | |
| 121 const PrefValueMap* GetExtensionPrefValueMap(const std::string& ext_id, | |
| 122 bool incognito) const; | |
| 123 | |
| 124 PrefValueMap* GetExtensionPrefValueMap(const std::string& ext_id, | |
| 125 bool incognito); | |
| 126 | |
| 127 // Returns all keys of pref values that are set by the extension of entry, | |
|
Mattias Nissler (ping if slow)
2011/01/07 10:12:58
|entry|
battre
2011/01/10 16:55:47
Done.
| |
| 128 // regardless whether they are set for incognito or regular pref values. | |
| 129 void GetExtensionControlledKeys(const ExtensionEntry& entry, | |
| 130 std::set<std::string>* out) const; | |
| 131 | |
| 132 void NotifyPrefValueChanged(const std::string& key); | |
| 133 void NotifyPrefValueChanged(const std::set<std::string>& keys); | |
| 134 | |
| 135 // Mapping of which extension set which preference value. The effective | |
| 136 // preferences values (i.e. the ones with the highest precedence) | |
| 137 // are stored in the datastructures of the base class. | |
|
Mattias Nissler (ping if slow)
2011/01/07 10:12:58
What base class?
battre
2011/01/10 16:55:47
This referred to the old code. I have updated the
| |
| 138 ExtensionEntryMap entries_; | |
| 139 | |
| 140 ObserverList<Observer, true> observers_; | |
| 141 | |
| 142 DISALLOW_COPY_AND_ASSIGN(ExtensionPrefValueMap); | |
| 143 }; | |
| 144 | |
| 145 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_PREF_VALUE_MAP_H_ | |
| OLD | NEW |