Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Side by Side Diff: chrome/browser/extensions/api/storage/sync_value_store_cache.cc

Issue 184043031: Split up extensions storage API implementations for sync and local storage. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: split sync_or_local Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/api/storage/sync_or_local_value_store_cache. h" 5 #include "chrome/browser/extensions/api/storage/sync_value_store_cache.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h" 8 #include "base/callback.h"
9 #include "base/files/file_path.h" 9 #include "base/files/file_path.h"
10 #include "base/sequenced_task_runner.h" 10 #include "base/sequenced_task_runner.h"
11 #include "chrome/browser/extensions/api/storage/local_storage_backend.h"
12 #include "chrome/browser/extensions/api/storage/settings_frontend.h" 11 #include "chrome/browser/extensions/api/storage/settings_frontend.h"
13 #include "chrome/browser/extensions/api/storage/settings_storage_quota_enforcer. h" 12 #include "chrome/browser/extensions/api/storage/settings_storage_quota_enforcer. h"
14 #include "chrome/browser/extensions/api/storage/sync_storage_backend.h" 13 #include "chrome/browser/extensions/api/storage/sync_storage_backend.h"
15 #include "chrome/browser/extensions/api/storage/weak_unlimited_settings_storage. h"
16 #include "chrome/browser/sync/glue/sync_start_util.h" 14 #include "chrome/browser/sync/glue/sync_start_util.h"
15 #include "chrome/common/extensions/api/storage.h"
17 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
18 #include "extensions/common/constants.h" 17 #include "extensions/common/constants.h"
19 #include "extensions/common/extension.h" 18 #include "extensions/common/extension.h"
20 #include "extensions/common/permissions/api_permission.h"
21 19
22 using content::BrowserThread; 20 using content::BrowserThread;
23 21
24 namespace extensions { 22 namespace extensions {
25 23
26 SyncOrLocalValueStoreCache::SyncOrLocalValueStoreCache( 24 namespace {
27 settings_namespace::Namespace settings_namespace, 25
26 // Returns the quota limit for sync storage, taken from the schema in
27 // chrome/common/extensions/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)
33 };
34 return limits;
35 }
36
37 } // namespace
38
39 SyncValueStoreCache::SyncValueStoreCache(
28 const scoped_refptr<SettingsStorageFactory>& factory, 40 const scoped_refptr<SettingsStorageFactory>& factory,
29 const SettingsStorageQuotaEnforcer::Limits& quota,
30 const scoped_refptr<SettingsObserverList>& observers, 41 const scoped_refptr<SettingsObserverList>& observers,
31 const base::FilePath& profile_path) 42 const base::FilePath& profile_path) {
32 : settings_namespace_(settings_namespace), initialized_(false) {
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
34 DCHECK(settings_namespace_ == settings_namespace::LOCAL ||
35 settings_namespace_ == settings_namespace::SYNC);
36 44
37 // This post is safe since the destructor can only be invoked from the 45 // This post is safe since the destructor can only be invoked from the
38 // same message loop, and any potential post of a deletion task must come 46 // same message loop, and any potential post of a deletion task must come
39 // after the constructor returns. 47 // after the constructor returns.
40 BrowserThread::PostTask( 48 BrowserThread::PostTask(
41 BrowserThread::FILE, FROM_HERE, 49 BrowserThread::FILE, FROM_HERE,
42 base::Bind(&SyncOrLocalValueStoreCache::InitOnFileThread, 50 base::Bind(&SyncValueStoreCache::InitOnFileThread,
43 base::Unretained(this), 51 base::Unretained(this),
44 factory, quota, observers, profile_path)); 52 factory, observers, profile_path));
45 } 53 }
46 54
47 SyncOrLocalValueStoreCache::~SyncOrLocalValueStoreCache() { 55 SyncValueStoreCache::~SyncValueStoreCache() {
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
49 } 57 }
50 58
51 syncer::SyncableService* SyncOrLocalValueStoreCache::GetSyncableService( 59 syncer::SyncableService* SyncValueStoreCache::GetSyncableService(
52 syncer::ModelType type) const { 60 syncer::ModelType type) const {
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
54 DCHECK(initialized_); 62
55 switch (type) { 63 switch (type) {
56 case syncer::APP_SETTINGS: 64 case syncer::APP_SETTINGS:
57 return app_backend_->GetAsSyncableService(); 65 return app_backend_->GetAsSyncableService();
58 case syncer::EXTENSION_SETTINGS: 66 case syncer::EXTENSION_SETTINGS:
59 return extension_backend_->GetAsSyncableService(); 67 return extension_backend_->GetAsSyncableService();
60 default: 68 default:
61 NOTREACHED(); 69 NOTREACHED();
62 return NULL; 70 return NULL;
63 } 71 }
64 } 72 }
65 73
66 void SyncOrLocalValueStoreCache::RunWithValueStoreForExtension( 74 void SyncValueStoreCache::RunWithValueStoreForExtension(
67 const StorageCallback& callback, 75 const StorageCallback& callback,
68 scoped_refptr<const Extension> extension) { 76 scoped_refptr<const Extension> extension) {
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
70 DCHECK(initialized_);
71
72 SettingsBackend* backend = 78 SettingsBackend* backend =
73 extension->is_app() ? app_backend_.get() : extension_backend_.get(); 79 extension->is_app() ? app_backend_.get() : extension_backend_.get();
74 ValueStore* storage = backend->GetStorage(extension->id()); 80 callback.Run(backend->GetStorage(extension->id()));
75
76 // A neat way to implement unlimited storage; if the extension has the
77 // unlimited storage permission, force through all calls to Set() (in the
78 // same way that writes from sync ignore quota).
79 // But only if it's local storage (bad stuff would happen if sync'ed
80 // storage is allowed to be unlimited).
81 bool is_unlimited =
82 settings_namespace_ == settings_namespace::LOCAL &&
83 extension->HasAPIPermission(APIPermission::kUnlimitedStorage);
84
85 if (is_unlimited) {
86 WeakUnlimitedSettingsStorage unlimited_storage(storage);
87 callback.Run(&unlimited_storage);
88 } else {
89 callback.Run(storage);
90 }
91 } 81 }
92 82
93 void SyncOrLocalValueStoreCache::DeleteStorageSoon( 83 void SyncValueStoreCache::DeleteStorageSoon(const std::string& extension_id) {
94 const std::string& extension_id) {
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
96 DCHECK(initialized_);
97 app_backend_->DeleteStorage(extension_id); 85 app_backend_->DeleteStorage(extension_id);
98 extension_backend_->DeleteStorage(extension_id); 86 extension_backend_->DeleteStorage(extension_id);
99 } 87 }
100 88
101 void SyncOrLocalValueStoreCache::InitOnFileThread( 89 void SyncValueStoreCache::InitOnFileThread(
102 const scoped_refptr<SettingsStorageFactory>& factory, 90 const scoped_refptr<SettingsStorageFactory>& factory,
103 const SettingsStorageQuotaEnforcer::Limits& quota,
104 const scoped_refptr<SettingsObserverList>& observers, 91 const scoped_refptr<SettingsObserverList>& observers,
105 const base::FilePath& profile_path) { 92 const base::FilePath& profile_path) {
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 93 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
107 DCHECK(!initialized_); 94 app_backend_.reset(new SyncStorageBackend(
108 switch (settings_namespace_) { 95 factory,
109 case settings_namespace::LOCAL: 96 profile_path.AppendASCII(kSyncAppSettingsDirectoryName),
110 app_backend_.reset(new LocalStorageBackend( 97 GetSyncQuotaLimits(),
111 factory, 98 observers,
112 profile_path.AppendASCII(kLocalAppSettingsDirectoryName), 99 syncer::APP_SETTINGS,
113 quota)); 100 sync_start_util::GetFlareForSyncableService(profile_path)));
114 extension_backend_.reset(new LocalStorageBackend( 101 extension_backend_.reset(new SyncStorageBackend(
115 factory, 102 factory,
116 profile_path.AppendASCII(kLocalExtensionSettingsDirectoryName), 103 profile_path.AppendASCII(kSyncExtensionSettingsDirectoryName),
117 quota)); 104 GetSyncQuotaLimits(),
118 break; 105 observers,
119 case settings_namespace::SYNC: 106 syncer::EXTENSION_SETTINGS,
120 app_backend_.reset(new SyncStorageBackend( 107 sync_start_util::GetFlareForSyncableService(profile_path)));
121 factory,
122 profile_path.AppendASCII(kSyncAppSettingsDirectoryName),
123 quota,
124 observers,
125 syncer::APP_SETTINGS,
126 sync_start_util::GetFlareForSyncableService(profile_path)));
127 extension_backend_.reset(new SyncStorageBackend(
128 factory,
129 profile_path.AppendASCII(kSyncExtensionSettingsDirectoryName),
130 quota,
131 observers,
132 syncer::EXTENSION_SETTINGS,
133 sync_start_util::GetFlareForSyncableService(profile_path)));
134 break;
135 default:
136 NOTREACHED();
137 }
138
139 initialized_ = true;
140 } 108 }
141 109
142 } // namespace extensions 110 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698