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 #include "chrome/browser/extensions/api/storage/sync_value_store_cache.h"
8 #include "base/callback.h"
9 #include "base/files/file_path.h"
10 #include "base/sequenced_task_runner.h"
11 #include "chrome/browser/extensions/api/storage/sync_storage_backend.h"
12 #include "chrome/browser/sync/glue/sync_start_util.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "extensions/browser/api/storage/settings_storage_quota_enforcer.h"
15 #include "extensions/browser/api/storage/storage_frontend.h"
16 #include "extensions/common/api/storage.h"
17 #include "extensions/common/constants.h"
18 #include "extensions/common/extension.h"
20 using content::BrowserThread
;
22 namespace extensions
{
26 // Returns the quota limit for sync storage, taken from the schema in
27 // extensions/common/api/storage.json.
28 SettingsStorageQuotaEnforcer::Limits
GetSyncQuotaLimits() {
29 SettingsStorageQuotaEnforcer::Limits limits
= {
30 static_cast<size_t>(api::storage::sync::QUOTA_BYTES
),
31 static_cast<size_t>(api::storage::sync::QUOTA_BYTES_PER_ITEM
),
32 static_cast<size_t>(api::storage::sync::MAX_ITEMS
)};
38 SyncValueStoreCache::SyncValueStoreCache(
39 const scoped_refptr
<SettingsStorageFactory
>& factory
,
40 const scoped_refptr
<SettingsObserverList
>& observers
,
41 const base::FilePath
& profile_path
)
42 : initialized_(false) {
43 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
45 // This post is safe since the destructor can only be invoked from the
46 // same message loop, and any potential post of a deletion task must come
47 // after the constructor returns.
48 BrowserThread::PostTask(
49 BrowserThread::FILE, FROM_HERE
,
50 base::Bind(&SyncValueStoreCache::InitOnFileThread
,
51 base::Unretained(this),
52 factory
, observers
, profile_path
));
55 SyncValueStoreCache::~SyncValueStoreCache() {
56 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
59 syncer::SyncableService
* SyncValueStoreCache::GetSyncableService(
60 syncer::ModelType type
) const {
61 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
65 case syncer::APP_SETTINGS
:
66 return app_backend_
.get();
67 case syncer::EXTENSION_SETTINGS
:
68 return extension_backend_
.get();
75 void SyncValueStoreCache::RunWithValueStoreForExtension(
76 const StorageCallback
& callback
,
77 scoped_refptr
<const Extension
> extension
) {
78 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
80 SyncStorageBackend
* backend
=
81 extension
->is_app() ? app_backend_
.get() : extension_backend_
.get();
82 callback
.Run(backend
->GetStorage(extension
->id()));
85 void SyncValueStoreCache::DeleteStorageSoon(const std::string
& extension_id
) {
86 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
87 app_backend_
->DeleteStorage(extension_id
);
88 extension_backend_
->DeleteStorage(extension_id
);
91 void SyncValueStoreCache::InitOnFileThread(
92 const scoped_refptr
<SettingsStorageFactory
>& factory
,
93 const scoped_refptr
<SettingsObserverList
>& observers
,
94 const base::FilePath
& profile_path
) {
95 DCHECK_CURRENTLY_ON(BrowserThread::FILE);
96 DCHECK(!initialized_
);
97 app_backend_
.reset(new SyncStorageBackend(
99 profile_path
.AppendASCII(kSyncAppSettingsDirectoryName
),
100 GetSyncQuotaLimits(),
102 syncer::APP_SETTINGS
,
103 sync_start_util::GetFlareForSyncableService(profile_path
)));
104 extension_backend_
.reset(new SyncStorageBackend(
106 profile_path
.AppendASCII(kSyncExtensionSettingsDirectoryName
),
107 GetSyncQuotaLimits(),
109 syncer::EXTENSION_SETTINGS
,
110 sync_start_util::GetFlareForSyncableService(profile_path
)));
114 } // namespace extensions