| 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/local_value_store_cache.h" |
| 6 |
| 7 #include <limits> |
| 6 | 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/callback.h" | 10 #include "base/callback.h" |
| 9 #include "base/files/file_path.h" | 11 #include "base/files/file_path.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/local_storage_backend.h" |
| 12 #include "chrome/browser/extensions/api/storage/settings_frontend.h" | 13 #include "chrome/browser/extensions/api/storage/settings_storage_factory.h" |
| 13 #include "chrome/browser/extensions/api/storage/settings_storage_quota_enforcer.
h" | 14 #include "chrome/browser/extensions/api/storage/settings_storage_quota_enforcer.
h" |
| 14 #include "chrome/browser/extensions/api/storage/sync_storage_backend.h" | |
| 15 #include "chrome/browser/extensions/api/storage/weak_unlimited_settings_storage.
h" | 15 #include "chrome/browser/extensions/api/storage/weak_unlimited_settings_storage.
h" |
| 16 #include "chrome/browser/sync/glue/sync_start_util.h" | 16 #include "chrome/common/extensions/api/storage.h" |
| 17 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "extensions/common/constants.h" | 18 #include "extensions/common/constants.h" |
| 19 #include "extensions/common/extension.h" | 19 #include "extensions/common/extension.h" |
| 20 #include "extensions/common/permissions/api_permission.h" | 20 #include "extensions/common/permissions/api_permission.h" |
| 21 | 21 |
| 22 using content::BrowserThread; | 22 using content::BrowserThread; |
| 23 | 23 |
| 24 namespace extensions { | 24 namespace extensions { |
| 25 | 25 |
| 26 SyncOrLocalValueStoreCache::SyncOrLocalValueStoreCache( | 26 namespace { |
| 27 settings_namespace::Namespace settings_namespace, | 27 |
| 28 // Returns the quota limit for local storage, taken from the schema in |
| 29 // chrome/common/extensions/api/storage.json. |
| 30 SettingsStorageQuotaEnforcer::Limits GetLocalQuotaLimits() { |
| 31 SettingsStorageQuotaEnforcer::Limits limits = { |
| 32 static_cast<size_t>(api::storage::local::QUOTA_BYTES), |
| 33 std::numeric_limits<size_t>::max(), |
| 34 std::numeric_limits<size_t>::max() |
| 35 }; |
| 36 return limits; |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 41 LocalValueStoreCache::LocalValueStoreCache( |
| 28 const scoped_refptr<SettingsStorageFactory>& factory, | 42 const scoped_refptr<SettingsStorageFactory>& factory, |
| 29 const SettingsStorageQuotaEnforcer::Limits& quota, | |
| 30 const scoped_refptr<SettingsObserverList>& observers, | |
| 31 const base::FilePath& profile_path) | 43 const base::FilePath& profile_path) |
| 32 : settings_namespace_(settings_namespace), initialized_(false) { | 44 : initialized_(false) { |
| 33 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 34 DCHECK(settings_namespace_ == settings_namespace::LOCAL || | |
| 35 settings_namespace_ == settings_namespace::SYNC); | |
| 36 | 46 |
| 37 // This post is safe since the destructor can only be invoked from the | 47 // 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 | 48 // same message loop, and any potential post of a deletion task must come |
| 39 // after the constructor returns. | 49 // after the constructor returns. |
| 40 BrowserThread::PostTask( | 50 BrowserThread::PostTask( |
| 41 BrowserThread::FILE, FROM_HERE, | 51 BrowserThread::FILE, FROM_HERE, |
| 42 base::Bind(&SyncOrLocalValueStoreCache::InitOnFileThread, | 52 base::Bind(&LocalValueStoreCache::InitOnFileThread, |
| 43 base::Unretained(this), | 53 base::Unretained(this), |
| 44 factory, quota, observers, profile_path)); | 54 factory, profile_path)); |
| 45 } | 55 } |
| 46 | 56 |
| 47 SyncOrLocalValueStoreCache::~SyncOrLocalValueStoreCache() { | 57 LocalValueStoreCache::~LocalValueStoreCache() { |
| 48 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 49 } | 59 } |
| 50 | 60 |
| 51 syncer::SyncableService* SyncOrLocalValueStoreCache::GetSyncableService( | 61 void LocalValueStoreCache::RunWithValueStoreForExtension( |
| 52 syncer::ModelType type) const { | |
| 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
| 54 DCHECK(initialized_); | |
| 55 switch (type) { | |
| 56 case syncer::APP_SETTINGS: | |
| 57 return app_backend_->GetAsSyncableService(); | |
| 58 case syncer::EXTENSION_SETTINGS: | |
| 59 return extension_backend_->GetAsSyncableService(); | |
| 60 default: | |
| 61 NOTREACHED(); | |
| 62 return NULL; | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 void SyncOrLocalValueStoreCache::RunWithValueStoreForExtension( | |
| 67 const StorageCallback& callback, | 62 const StorageCallback& callback, |
| 68 scoped_refptr<const Extension> extension) { | 63 scoped_refptr<const Extension> extension) { |
| 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 70 DCHECK(initialized_); | 65 DCHECK(initialized_); |
| 71 | 66 |
| 72 SettingsBackend* backend = | 67 SettingsBackend* backend = |
| 73 extension->is_app() ? app_backend_.get() : extension_backend_.get(); | 68 extension->is_app() ? app_backend_.get() : extension_backend_.get(); |
| 74 ValueStore* storage = backend->GetStorage(extension->id()); | 69 ValueStore* storage = backend->GetStorage(extension->id()); |
| 75 | 70 |
| 76 // A neat way to implement unlimited storage; if the extension has the | 71 // A neat way to implement unlimited storage; if the extension has the |
| 77 // unlimited storage permission, force through all calls to Set() (in the | 72 // unlimited storage permission, force through all calls to Set(). |
| 78 // same way that writes from sync ignore quota). | 73 if (extension->HasAPIPermission(APIPermission::kUnlimitedStorage)) { |
| 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); | 74 WeakUnlimitedSettingsStorage unlimited_storage(storage); |
| 87 callback.Run(&unlimited_storage); | 75 callback.Run(&unlimited_storage); |
| 88 } else { | 76 } else { |
| 89 callback.Run(storage); | 77 callback.Run(storage); |
| 90 } | 78 } |
| 91 } | 79 } |
| 92 | 80 |
| 93 void SyncOrLocalValueStoreCache::DeleteStorageSoon( | 81 void LocalValueStoreCache::DeleteStorageSoon(const std::string& extension_id) { |
| 94 const std::string& extension_id) { | |
| 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 96 DCHECK(initialized_); | 83 DCHECK(initialized_); |
| 97 app_backend_->DeleteStorage(extension_id); | 84 app_backend_->DeleteStorage(extension_id); |
| 98 extension_backend_->DeleteStorage(extension_id); | 85 extension_backend_->DeleteStorage(extension_id); |
| 99 } | 86 } |
| 100 | 87 |
| 101 void SyncOrLocalValueStoreCache::InitOnFileThread( | 88 void LocalValueStoreCache::InitOnFileThread( |
| 102 const scoped_refptr<SettingsStorageFactory>& factory, | 89 const scoped_refptr<SettingsStorageFactory>& factory, |
| 103 const SettingsStorageQuotaEnforcer::Limits& quota, | |
| 104 const scoped_refptr<SettingsObserverList>& observers, | |
| 105 const base::FilePath& profile_path) { | 90 const base::FilePath& profile_path) { |
| 106 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 91 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 107 DCHECK(!initialized_); | 92 DCHECK(!initialized_); |
| 108 switch (settings_namespace_) { | 93 app_backend_.reset(new LocalStorageBackend( |
| 109 case settings_namespace::LOCAL: | 94 factory, |
| 110 app_backend_.reset(new LocalStorageBackend( | 95 profile_path.AppendASCII(kLocalAppSettingsDirectoryName), |
| 111 factory, | 96 GetLocalQuotaLimits())); |
| 112 profile_path.AppendASCII(kLocalAppSettingsDirectoryName), | 97 extension_backend_.reset(new LocalStorageBackend( |
| 113 quota)); | 98 factory, |
| 114 extension_backend_.reset(new LocalStorageBackend( | 99 profile_path.AppendASCII(kLocalExtensionSettingsDirectoryName), |
| 115 factory, | 100 GetLocalQuotaLimits())); |
| 116 profile_path.AppendASCII(kLocalExtensionSettingsDirectoryName), | |
| 117 quota)); | |
| 118 break; | |
| 119 case settings_namespace::SYNC: | |
| 120 app_backend_.reset(new SyncStorageBackend( | |
| 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; | 101 initialized_ = true; |
| 140 } | 102 } |
| 141 | 103 |
| 142 } // namespace extensions | 104 } // namespace extensions |
| OLD | NEW |