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