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