1 // Copyright 2014 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.
5 #ifndef EXTENSIONS_BROWSER_API_STORAGE_STORAGE_FRONTEND_H_
6 #define EXTENSIONS_BROWSER_API_STORAGE_STORAGE_FRONTEND_H_
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "extensions/browser/api/storage/settings_namespace.h"
14 #include "extensions/browser/api/storage/settings_observer.h"
15 #include "extensions/browser/api/storage/settings_storage_factory.h"
16 #include "extensions/browser/api/storage/value_store_cache.h"
17 #include "extensions/browser/browser_context_keyed_api_factory.h"
23 namespace extensions
{
25 // The component of the Storage API which runs on the UI thread.
26 class StorageFrontend
: public BrowserContextKeyedAPI
{
28 // Returns the current instance for |context|.
29 static StorageFrontend
* Get(content::BrowserContext
* context
);
31 // Creates with a specific |storage_factory|. Caller owns the object.
32 static StorageFrontend
* CreateForTesting(
33 const scoped_refptr
<SettingsStorageFactory
>& storage_factory
,
34 content::BrowserContext
* context
);
36 // Public so tests can create and delete their own instances.
37 virtual ~StorageFrontend();
39 // Returns the value store cache for |settings_namespace|.
40 ValueStoreCache
* GetValueStoreCache(
41 settings_namespace::Namespace settings_namespace
) const;
43 // Returns true if |settings_namespace| is a valid namespace.
44 bool IsStorageEnabled(settings_namespace::Namespace settings_namespace
) const;
46 // Runs |callback| with the storage area of the given |settings_namespace|
47 // for the |extension|.
48 void RunWithStorage(scoped_refptr
<const Extension
> extension
,
49 settings_namespace::Namespace settings_namespace
,
50 const ValueStoreCache::StorageCallback
& callback
);
52 // Deletes the settings for the given |extension_id|.
53 void DeleteStorageSoon(const std::string
& extension_id
);
55 // Gets the thread-safe observer list.
56 scoped_refptr
<SettingsObserverList
> GetObservers();
58 void DisableStorageForTesting(
59 settings_namespace::Namespace settings_namespace
);
61 // BrowserContextKeyedAPI implementation.
62 static BrowserContextKeyedAPIFactory
<StorageFrontend
>* GetFactoryInstance();
63 static const char* service_name();
64 static const bool kServiceRedirectedInIncognito
= true;
65 static const bool kServiceIsNULLWhileTesting
= true;
68 friend class BrowserContextKeyedAPIFactory
<StorageFrontend
>;
70 typedef std::map
<settings_namespace::Namespace
, ValueStoreCache
*> CacheMap
;
72 // Constructor for normal BrowserContextKeyedAPI usage.
73 explicit StorageFrontend(content::BrowserContext
* context
);
75 // Constructor for tests.
76 StorageFrontend(const scoped_refptr
<SettingsStorageFactory
>& storage_factory
,
77 content::BrowserContext
* context
);
79 void Init(const scoped_refptr
<SettingsStorageFactory
>& storage_factory
);
81 // The (non-incognito) browser context this Frontend belongs to.
82 content::BrowserContext
* const browser_context_
;
84 // List of observers to settings changes.
85 scoped_refptr
<SettingsObserverList
> observers_
;
87 // Observer for |browser_context_|.
88 scoped_ptr
<SettingsObserver
> browser_context_observer_
;
90 // Maps a known namespace to its corresponding ValueStoreCache. The caches
91 // are owned by this object.
94 DISALLOW_COPY_AND_ASSIGN(StorageFrontend
);
97 } // namespace extensions
99 #endif // EXTENSIONS_BROWSER_API_STORAGE_STORAGE_FRONTEND_H_