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/compiler_specific.h" 13 #include "base/files/file_path.h" 14 #include "base/memory/linked_ptr.h" 15 #include "base/memory/ref_counted.h" 16 #include "base/memory/scoped_ptr.h" 17 #include "chrome/browser/extensions/api/storage/settings_observer.h" 18 #include "chrome/browser/extensions/api/storage/value_store_cache.h" 19 #include "components/policy/core/common/policy_service.h" 20 21 class Profile; 22 23 namespace policy { 24 class PolicyMap; 25 } 26 27 namespace extensions { 28 29 class PolicyValueStore; 30 class SettingsStorageFactory; 31 32 // A ValueStoreCache that manages a PolicyValueStore for each extension that 33 // uses the storage.managed namespace. This class observes policy changes and 34 // which extensions listen for storage.onChanged(), and sends the appropriate 35 // updates to the corresponding PolicyValueStore on the FILE thread. 36 class ManagedValueStoreCache : public ValueStoreCache, 37 public policy::PolicyService::Observer { 38 public: 39 // |factory| is used to create databases for the PolicyValueStores. 40 // |observers| is the list of SettingsObservers to notify when a ValueStore 41 // changes. 42 ManagedValueStoreCache(Profile* profile, 43 const scoped_refptr<SettingsStorageFactory>& factory, 44 const scoped_refptr<SettingsObserverList>& observers); 45 virtual ~ManagedValueStoreCache(); 46 47 private: 48 class ExtensionTracker; 49 50 // Maps an extension ID to its PolicyValueStoreMap. 51 typedef std::map<std::string, linked_ptr<PolicyValueStore> > 52 PolicyValueStoreMap; 53 54 // ValueStoreCache implementation: 55 virtual void ShutdownOnUI() OVERRIDE; 56 virtual void RunWithValueStoreForExtension( 57 const StorageCallback& callback, 58 scoped_refptr<const Extension> extension) OVERRIDE; 59 virtual void DeleteStorageSoon(const std::string& extension_id) OVERRIDE; 60 61 // PolicyService::Observer implementation: 62 virtual void OnPolicyServiceInitialized(policy::PolicyDomain domain) OVERRIDE; 63 virtual void OnPolicyUpdated(const policy::PolicyNamespace& ns, 64 const policy::PolicyMap& previous, 65 const policy::PolicyMap& current) OVERRIDE; 66 67 // Posted by OnPolicyUpdated() to update a PolicyValueStore on the FILE 68 // thread. 69 void UpdatePolicyOnFILE(const std::string& extension_id, 70 scoped_ptr<policy::PolicyMap> current_policy); 71 72 // Returns an existing PolicyValueStore for |extension_id|, or NULL. 73 PolicyValueStore* GetStoreFor(const std::string& extension_id); 74 75 // Returns true if a backing store has been created for |extension_id|. 76 bool HasStore(const std::string& extension_id) const; 77 78 // The profile that owns the extension system being used. This is used to 79 // get the PolicyService, the EventRouter and the ExtensionService. 80 Profile* profile_; 81 82 // The |profile_|'s PolicyService. 83 policy::PolicyService* policy_service_; 84 85 // Observes extension loading and unloading, and keeps the Profile's 86 // PolicyService aware of the current list of extensions. 87 scoped_ptr<ExtensionTracker> extension_tracker_; 88 89 // These live on the FILE thread. 90 scoped_refptr<SettingsStorageFactory> storage_factory_; 91 scoped_refptr<SettingsObserverList> observers_; 92 base::FilePath base_path_; 93 94 // All the PolicyValueStores live on the FILE thread, and |store_map_| can be 95 // accessed only on the FILE thread as well. 96 PolicyValueStoreMap store_map_; 97 98 DISALLOW_COPY_AND_ASSIGN(ManagedValueStoreCache); 99 }; 100 101 } // namespace extensions 102 103 #endif // CHROME_BROWSER_EXTENSIONS_API_STORAGE_MANAGED_VALUE_STORE_CACHE_H_ 104