OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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) { | 43 : initialized_(false) { |
33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 44 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
34 DCHECK(settings_namespace_ == settings_namespace::LOCAL || | |
35 settings_namespace_ == settings_namespace::SYNC); | |
36 | 45 |
37 // This post is safe since the destructor can only be invoked from the | 46 // 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 | 47 // same message loop, and any potential post of a deletion task must come |
39 // after the constructor returns. | 48 // after the constructor returns. |
40 BrowserThread::PostTask( | 49 BrowserThread::PostTask( |
41 BrowserThread::FILE, FROM_HERE, | 50 BrowserThread::FILE, FROM_HERE, |
42 base::Bind(&SyncOrLocalValueStoreCache::InitOnFileThread, | 51 base::Bind(&SyncValueStoreCache::InitOnFileThread, |
43 base::Unretained(this), | 52 base::Unretained(this), |
44 factory, quota, observers, profile_path)); | 53 factory, observers, profile_path)); |
45 } | 54 } |
46 | 55 |
47 SyncOrLocalValueStoreCache::~SyncOrLocalValueStoreCache() { | 56 SyncValueStoreCache::~SyncValueStoreCache() { |
48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 57 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
49 } | 58 } |
50 | 59 |
51 syncer::SyncableService* SyncOrLocalValueStoreCache::GetSyncableService( | 60 syncer::SyncableService* SyncValueStoreCache::GetSyncableService( |
52 syncer::ModelType type) const { | 61 syncer::ModelType type) const { |
53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
54 DCHECK(initialized_); | 63 DCHECK(initialized_); |
| 64 |
55 switch (type) { | 65 switch (type) { |
56 case syncer::APP_SETTINGS: | 66 case syncer::APP_SETTINGS: |
57 return app_backend_->GetAsSyncableService(); | 67 return app_backend_->GetAsSyncableService(); |
58 case syncer::EXTENSION_SETTINGS: | 68 case syncer::EXTENSION_SETTINGS: |
59 return extension_backend_->GetAsSyncableService(); | 69 return extension_backend_->GetAsSyncableService(); |
60 default: | 70 default: |
61 NOTREACHED(); | 71 NOTREACHED(); |
62 return NULL; | 72 return NULL; |
63 } | 73 } |
64 } | 74 } |
65 | 75 |
66 void SyncOrLocalValueStoreCache::RunWithValueStoreForExtension( | 76 void SyncValueStoreCache::RunWithValueStoreForExtension( |
67 const StorageCallback& callback, | 77 const StorageCallback& callback, |
68 scoped_refptr<const Extension> extension) { | 78 scoped_refptr<const Extension> extension) { |
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
70 DCHECK(initialized_); | 80 DCHECK(initialized_); |
71 | |
72 SettingsBackend* backend = | 81 SettingsBackend* backend = |
73 extension->is_app() ? app_backend_.get() : extension_backend_.get(); | 82 extension->is_app() ? app_backend_.get() : extension_backend_.get(); |
74 ValueStore* storage = backend->GetStorage(extension->id()); | 83 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 } | 84 } |
92 | 85 |
93 void SyncOrLocalValueStoreCache::DeleteStorageSoon( | 86 void SyncValueStoreCache::DeleteStorageSoon(const std::string& extension_id) { |
94 const std::string& extension_id) { | |
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
96 DCHECK(initialized_); | |
97 app_backend_->DeleteStorage(extension_id); | 88 app_backend_->DeleteStorage(extension_id); |
98 extension_backend_->DeleteStorage(extension_id); | 89 extension_backend_->DeleteStorage(extension_id); |
99 } | 90 } |
100 | 91 |
101 void SyncOrLocalValueStoreCache::InitOnFileThread( | 92 void SyncValueStoreCache::InitOnFileThread( |
102 const scoped_refptr<SettingsStorageFactory>& factory, | 93 const scoped_refptr<SettingsStorageFactory>& factory, |
103 const SettingsStorageQuotaEnforcer::Limits& quota, | |
104 const scoped_refptr<SettingsObserverList>& observers, | 94 const scoped_refptr<SettingsObserverList>& observers, |
105 const base::FilePath& profile_path) { | 95 const base::FilePath& profile_path) { |
106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
107 DCHECK(!initialized_); | 97 DCHECK(!initialized_); |
108 switch (settings_namespace_) { | 98 app_backend_.reset(new SyncStorageBackend( |
109 case settings_namespace::LOCAL: | 99 factory, |
110 app_backend_.reset(new LocalStorageBackend( | 100 profile_path.AppendASCII(kSyncAppSettingsDirectoryName), |
111 factory, | 101 GetSyncQuotaLimits(), |
112 profile_path.AppendASCII(kLocalAppSettingsDirectoryName), | 102 observers, |
113 quota)); | 103 syncer::APP_SETTINGS, |
114 extension_backend_.reset(new LocalStorageBackend( | 104 sync_start_util::GetFlareForSyncableService(profile_path))); |
115 factory, | 105 extension_backend_.reset(new SyncStorageBackend( |
116 profile_path.AppendASCII(kLocalExtensionSettingsDirectoryName), | 106 factory, |
117 quota)); | 107 profile_path.AppendASCII(kSyncExtensionSettingsDirectoryName), |
118 break; | 108 GetSyncQuotaLimits(), |
119 case settings_namespace::SYNC: | 109 observers, |
120 app_backend_.reset(new SyncStorageBackend( | 110 syncer::EXTENSION_SETTINGS, |
121 factory, | 111 sync_start_util::GetFlareForSyncableService(profile_path))); |
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; | 112 initialized_ = true; |
140 } | 113 } |
141 | 114 |
142 } // namespace extensions | 115 } // namespace extensions |
OLD | NEW |