| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_STORAGE_MANAGED_VALUE_STORE_CACHE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_STORAGE_MANAGED_VALUE_STORE_CACHE_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_STORAGE_MANAGED_VALUE_STORE_CACHE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_STORAGE_MANAGED_VALUE_STORE_CACHE_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
| 12 #include "base/callback_forward.h" | |
| 13 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 14 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 15 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
| 16 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 17 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/memory/weak_ptr.h" | |
| 19 #include "chrome/browser/extensions/api/storage/settings_observer.h" | 17 #include "chrome/browser/extensions/api/storage/settings_observer.h" |
| 20 #include "chrome/browser/extensions/api/storage/value_store_cache.h" | 18 #include "chrome/browser/extensions/api/storage/value_store_cache.h" |
| 21 #include "chrome/browser/extensions/event_router.h" | |
| 22 #include "chrome/browser/policy/policy_service.h" | 19 #include "chrome/browser/policy/policy_service.h" |
| 23 | 20 |
| 24 class Profile; | 21 class Profile; |
| 25 | 22 |
| 26 namespace policy { | 23 namespace policy { |
| 27 class PolicyMap; | 24 class PolicyMap; |
| 28 } | 25 } |
| 29 | 26 |
| 30 namespace extensions { | 27 namespace extensions { |
| 31 | 28 |
| 32 class PolicyValueStore; | 29 class PolicyValueStore; |
| 33 class SettingsStorageFactory; | 30 class SettingsStorageFactory; |
| 34 | 31 |
| 35 // A ValueStoreCache that manages a PolicyValueStore for each extension that | 32 // A ValueStoreCache that manages a PolicyValueStore for each extension that |
| 36 // uses the storage.managed namespace. This class observes policy changes and | 33 // uses the storage.managed namespace. This class observes policy changes and |
| 37 // which extensions listen for storage.onChanged(), and sends the appropriate | 34 // which extensions listen for storage.onChanged(), and sends the appropriate |
| 38 // updates to the corresponding PolicyValueStore on the FILE thread. | 35 // updates to the corresponding PolicyValueStore on the FILE thread. |
| 39 class ManagedValueStoreCache : public ValueStoreCache, | 36 class ManagedValueStoreCache : public ValueStoreCache, |
| 40 public policy::PolicyService::Observer, | 37 public policy::PolicyService::Observer { |
| 41 public EventRouter::Observer { | |
| 42 public: | 38 public: |
| 43 // |factory| is used to create databases for the PolicyValueStores. | 39 // |factory| is used to create databases for the PolicyValueStores. |
| 44 // |observers| is the list of SettingsObservers to notify when a ValueStore | 40 // |observers| is the list of SettingsObservers to notify when a ValueStore |
| 45 // changes. | 41 // changes. |
| 46 ManagedValueStoreCache(Profile* profile, | 42 ManagedValueStoreCache(Profile* profile, |
| 47 const scoped_refptr<SettingsStorageFactory>& factory, | 43 const scoped_refptr<SettingsStorageFactory>& factory, |
| 48 const scoped_refptr<SettingsObserverList>& observers); | 44 const scoped_refptr<SettingsObserverList>& observers); |
| 49 virtual ~ManagedValueStoreCache(); | 45 virtual ~ManagedValueStoreCache(); |
| 50 | 46 |
| 51 private: | 47 private: |
| 52 class ExtensionTracker; | 48 class ExtensionTracker; |
| 53 | 49 |
| 54 // Maps an extension ID to its PolicyValueStoreMap. | 50 // Maps an extension ID to its PolicyValueStoreMap. |
| 55 typedef std::map<std::string, linked_ptr<PolicyValueStore> > | 51 typedef std::map<std::string, linked_ptr<PolicyValueStore> > |
| 56 PolicyValueStoreMap; | 52 PolicyValueStoreMap; |
| 57 | 53 |
| 58 // ValueStoreCache implementation: | 54 // ValueStoreCache implementation: |
| 59 virtual void ShutdownOnUI() OVERRIDE; | 55 virtual void ShutdownOnUI() OVERRIDE; |
| 60 virtual void RunWithValueStoreForExtension( | 56 virtual void RunWithValueStoreForExtension( |
| 61 const StorageCallback& callback, | 57 const StorageCallback& callback, |
| 62 scoped_refptr<const Extension> extension) OVERRIDE; | 58 scoped_refptr<const Extension> extension) OVERRIDE; |
| 63 virtual void DeleteStorageSoon(const std::string& extension_id) OVERRIDE; | 59 virtual void DeleteStorageSoon(const std::string& extension_id) OVERRIDE; |
| 64 | 60 |
| 65 // PolicyService::Observer implementation: | 61 // PolicyService::Observer implementation: |
| 62 virtual void OnPolicyServiceInitialized(policy::PolicyDomain domain) OVERRIDE; |
| 66 virtual void OnPolicyUpdated(const policy::PolicyNamespace& ns, | 63 virtual void OnPolicyUpdated(const policy::PolicyNamespace& ns, |
| 67 const policy::PolicyMap& previous, | 64 const policy::PolicyMap& previous, |
| 68 const policy::PolicyMap& current) OVERRIDE; | 65 const policy::PolicyMap& current) OVERRIDE; |
| 69 | 66 |
| 70 // Posted by OnPolicyUpdated() to update a PolicyValueStore on the FILE | 67 // Posted by OnPolicyUpdated() to update a PolicyValueStore on the FILE |
| 71 // thread. | 68 // thread. |
| 72 void UpdatePolicyOnFILE(const std::string& extension_id, | 69 void UpdatePolicyOnFILE(const std::string& extension_id, |
| 73 scoped_ptr<policy::PolicyMap> current_policy); | 70 scoped_ptr<policy::PolicyMap> current_policy); |
| 74 | 71 |
| 75 // EventRouter::Observer implementation: | |
| 76 virtual void OnListenerAdded(const EventListenerInfo& details) OVERRIDE; | |
| 77 | |
| 78 // Posted by OnListenerAdded() to load or create a PolicyValueStore for the | |
| 79 // given |extension_id|. | |
| 80 void CreateForExtensionOnFILE(const std::string& extension_id); | |
| 81 | |
| 82 // Returns an existing PolicyValueStore for |extension_id|, or NULL. | 72 // Returns an existing PolicyValueStore for |extension_id|, or NULL. |
| 83 PolicyValueStore* GetStoreFor(const std::string& extension_id); | 73 PolicyValueStore* GetStoreFor(const std::string& extension_id); |
| 84 | 74 |
| 85 // Creates a new PolicyValueStore for |extension_id|. This may open an | |
| 86 // existing database, or create a new one. This also sends the current policy | |
| 87 // for |extension_id| to the database. When |notify_if_changed| is true, | |
| 88 // a notification is sent with the changes between the current policy and the | |
| 89 // previously stored policy, if there are any. | |
| 90 // | |
| 91 // Since this is used on FILE but must retrieve the current policy, this | |
| 92 // method first posts GetInitialPolicy() to UI and then resumes in | |
| 93 // CreateStoreWithInitialPolicy(). If |continuation| is not null then it | |
| 94 // will be invoked after the store is created. | |
| 95 // | |
| 96 // CreateStoreFor() can be safely invoked from any method on the FILE thread. | |
| 97 // It posts to UI used |weak_this_on_ui_|, so that the task is dropped if | |
| 98 // ShutdownOnUI() has been invoked. Otherwise, GetInitialPolicy() executes | |
| 99 // on UI and can safely post CreateStoreWithInitialPolicy to FILE. | |
| 100 // CreateStoreWithInitialPolicy then guarantees that a store for | |
| 101 // |extension_id| exists or is created, and then executes the |continuation|; | |
| 102 // so when the |continuation| executes, a store for |extension_id| is | |
| 103 // guaranteed to exist. | |
| 104 void CreateStoreFor(const std::string& extension_id, | |
| 105 bool notify_if_changed, | |
| 106 const base::Closure& continuation); | |
| 107 | |
| 108 // Helper for CreateStoreFor, invoked on UI. | |
| 109 void GetInitialPolicy(const std::string& extension_id, | |
| 110 bool notify_if_changed, | |
| 111 const base::Closure& continuation); | |
| 112 | |
| 113 // Helper for CreateStoreFor, invoked on FILE. | |
| 114 void CreateStoreWithInitialPolicy(const std::string& extension_id, | |
| 115 bool notify_if_changed, | |
| 116 scoped_ptr<policy::PolicyMap> policy, | |
| 117 const base::Closure& continuation); | |
| 118 | |
| 119 // Helper method to get the PolicyService for |profile_|. | |
| 120 policy::PolicyService* GetPolicyService(); | |
| 121 | |
| 122 // Used to create a WeakPtr valid on the UI thread, so that FILE tasks can | |
| 123 // post back to UI. | |
| 124 base::WeakPtrFactory<ManagedValueStoreCache> weak_factory_; | |
| 125 | |
| 126 // A WeakPtr to |this| that is valid on UI. This is used by tasks on the FILE | |
| 127 // thread to post back to UI. | |
| 128 base::WeakPtr<ManagedValueStoreCache> weak_this_on_ui_; | |
| 129 | |
| 130 // The profile that owns the extension system being used. This is used to | 75 // The profile that owns the extension system being used. This is used to |
| 131 // get the PolicyService, the EventRouter and the ExtensionService. | 76 // get the PolicyService, the EventRouter and the ExtensionService. |
| 132 Profile* profile_; | 77 Profile* profile_; |
| 133 | 78 |
| 134 // The EventRouter is created before the SettingsFrontend (which owns the | 79 // The |profile_|'s PolicyService. |
| 135 // instance of this class), and the SettingsFrontend is also destroyed before | 80 policy::PolicyService* policy_service_; |
| 136 // the EventRouter is. |event_router_| is thus valid for the lifetime of this | |
| 137 // object, until ShutdownOnUI() is invoked. Lives on UI. | |
| 138 EventRouter* event_router_; | |
| 139 | 81 |
| 140 // Observes extension loading and unloading, and keeps the Profile's | 82 // Observes extension loading and unloading, and keeps the Profile's |
| 141 // PolicyService aware of the current list of extensions. | 83 // PolicyService aware of the current list of extensions. |
| 142 scoped_ptr<ExtensionTracker> extension_tracker_; | 84 scoped_ptr<ExtensionTracker> extension_tracker_; |
| 143 | 85 |
| 144 // These live on the FILE thread. | 86 // These live on the FILE thread. |
| 145 scoped_refptr<SettingsStorageFactory> storage_factory_; | 87 scoped_refptr<SettingsStorageFactory> storage_factory_; |
| 146 scoped_refptr<SettingsObserverList> observers_; | 88 scoped_refptr<SettingsObserverList> observers_; |
| 147 base::FilePath base_path_; | 89 base::FilePath base_path_; |
| 148 | 90 |
| 149 // All the PolicyValueStores live on the FILE thread, and |store_map_| can be | 91 // All the PolicyValueStores live on the FILE thread, and |store_map_| can be |
| 150 // accessed only on the FILE thread as well. | 92 // accessed only on the FILE thread as well. |
| 151 PolicyValueStoreMap store_map_; | 93 PolicyValueStoreMap store_map_; |
| 152 | 94 |
| 153 DISALLOW_COPY_AND_ASSIGN(ManagedValueStoreCache); | 95 DISALLOW_COPY_AND_ASSIGN(ManagedValueStoreCache); |
| 154 }; | 96 }; |
| 155 | 97 |
| 156 } // namespace extensions | 98 } // namespace extensions |
| 157 | 99 |
| 158 #endif // CHROME_BROWSER_EXTENSIONS_API_STORAGE_MANAGED_VALUE_STORE_CACHE_H_ | 100 #endif // CHROME_BROWSER_EXTENSIONS_API_STORAGE_MANAGED_VALUE_STORE_CACHE_H_ |
| OLD | NEW |