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