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