| OLD | NEW |
| 1 // Copyright 2014 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 "extensions/browser/value_store/value_store_frontend.h" | 5 #include "extensions/browser/value_store/value_store_frontend.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 : store_factory_(store_factory), backend_type_(backend_type) {} | 25 : store_factory_(store_factory), backend_type_(backend_type) {} |
| 26 | 26 |
| 27 void Get(const std::string& key, | 27 void Get(const std::string& key, |
| 28 const ValueStoreFrontend::ReadCallback& callback) { | 28 const ValueStoreFrontend::ReadCallback& callback) { |
| 29 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 29 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 30 LazyInit(); | 30 LazyInit(); |
| 31 ValueStore::ReadResult result = storage_->Get(key); | 31 ValueStore::ReadResult result = storage_->Get(key); |
| 32 | 32 |
| 33 // Extract the value from the ReadResult and pass ownership of it to the | 33 // Extract the value from the ReadResult and pass ownership of it to the |
| 34 // callback. | 34 // callback. |
| 35 scoped_ptr<base::Value> value; | 35 std::unique_ptr<base::Value> value; |
| 36 if (result->status().ok()) { | 36 if (result->status().ok()) { |
| 37 result->settings().RemoveWithoutPathExpansion(key, &value); | 37 result->settings().RemoveWithoutPathExpansion(key, &value); |
| 38 } else { | 38 } else { |
| 39 LOG(WARNING) << "Reading " << key << " from " << db_path_.value() | 39 LOG(WARNING) << "Reading " << key << " from " << db_path_.value() |
| 40 << " failed: " << result->status().message; | 40 << " failed: " << result->status().message; |
| 41 } | 41 } |
| 42 | 42 |
| 43 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 43 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 44 base::Bind(&ValueStoreFrontend::Backend::RunCallback, | 44 base::Bind(&ValueStoreFrontend::Backend::RunCallback, |
| 45 this, callback, base::Passed(&value))); | 45 this, callback, base::Passed(&value))); |
| 46 } | 46 } |
| 47 | 47 |
| 48 void Set(const std::string& key, scoped_ptr<base::Value> value) { | 48 void Set(const std::string& key, std::unique_ptr<base::Value> value) { |
| 49 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 49 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 50 LazyInit(); | 50 LazyInit(); |
| 51 // We don't need the old value, so skip generating changes. | 51 // We don't need the old value, so skip generating changes. |
| 52 ValueStore::WriteResult result = storage_->Set( | 52 ValueStore::WriteResult result = storage_->Set( |
| 53 ValueStore::IGNORE_QUOTA | ValueStore::NO_GENERATE_CHANGES, | 53 ValueStore::IGNORE_QUOTA | ValueStore::NO_GENERATE_CHANGES, |
| 54 key, | 54 key, |
| 55 *value.get()); | 55 *value.get()); |
| 56 LOG_IF(ERROR, !result->status().ok()) << "Error while writing " << key | 56 LOG_IF(ERROR, !result->status().ok()) << "Error while writing " << key |
| 57 << " to " << db_path_.value(); | 57 << " to " << db_path_.value(); |
| 58 } | 58 } |
| (...skipping 22 matching lines...) Expand all Loading... |
| 81 case BackendType::RULES: | 81 case BackendType::RULES: |
| 82 storage_ = store_factory_->CreateRulesStore(); | 82 storage_ = store_factory_->CreateRulesStore(); |
| 83 break; | 83 break; |
| 84 case BackendType::STATE: | 84 case BackendType::STATE: |
| 85 storage_ = store_factory_->CreateStateStore(); | 85 storage_ = store_factory_->CreateStateStore(); |
| 86 break; | 86 break; |
| 87 } | 87 } |
| 88 } | 88 } |
| 89 | 89 |
| 90 void RunCallback(const ValueStoreFrontend::ReadCallback& callback, | 90 void RunCallback(const ValueStoreFrontend::ReadCallback& callback, |
| 91 scoped_ptr<base::Value> value) { | 91 std::unique_ptr<base::Value> value) { |
| 92 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 92 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 93 callback.Run(std::move(value)); | 93 callback.Run(std::move(value)); |
| 94 } | 94 } |
| 95 | 95 |
| 96 // The factory which will be used to lazily create the ValueStore when needed. | 96 // The factory which will be used to lazily create the ValueStore when needed. |
| 97 // Used exclusively on the FILE thread. | 97 // Used exclusively on the FILE thread. |
| 98 scoped_refptr<ValueStoreFactory> store_factory_; | 98 scoped_refptr<ValueStoreFactory> store_factory_; |
| 99 BackendType backend_type_; | 99 BackendType backend_type_; |
| 100 | 100 |
| 101 // The actual ValueStore that handles persisting the data to disk. Used | 101 // The actual ValueStore that handles persisting the data to disk. Used |
| 102 // exclusively on the FILE thread. | 102 // exclusively on the FILE thread. |
| 103 scoped_ptr<ValueStore> storage_; | 103 std::unique_ptr<ValueStore> storage_; |
| 104 | 104 |
| 105 base::FilePath db_path_; | 105 base::FilePath db_path_; |
| 106 | 106 |
| 107 DISALLOW_COPY_AND_ASSIGN(Backend); | 107 DISALLOW_COPY_AND_ASSIGN(Backend); |
| 108 }; | 108 }; |
| 109 | 109 |
| 110 ValueStoreFrontend::ValueStoreFrontend( | 110 ValueStoreFrontend::ValueStoreFrontend( |
| 111 const scoped_refptr<ValueStoreFactory>& store_factory, | 111 const scoped_refptr<ValueStoreFactory>& store_factory, |
| 112 BackendType backend_type) | 112 BackendType backend_type) |
| 113 : backend_(new Backend(store_factory, backend_type)) {} | 113 : backend_(new Backend(store_factory, backend_type)) {} |
| 114 | 114 |
| 115 ValueStoreFrontend::~ValueStoreFrontend() { | 115 ValueStoreFrontend::~ValueStoreFrontend() { |
| 116 DCHECK(CalledOnValidThread()); | 116 DCHECK(CalledOnValidThread()); |
| 117 } | 117 } |
| 118 | 118 |
| 119 void ValueStoreFrontend::Get(const std::string& key, | 119 void ValueStoreFrontend::Get(const std::string& key, |
| 120 const ReadCallback& callback) { | 120 const ReadCallback& callback) { |
| 121 DCHECK(CalledOnValidThread()); | 121 DCHECK(CalledOnValidThread()); |
| 122 | 122 |
| 123 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 123 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 124 base::Bind(&ValueStoreFrontend::Backend::Get, | 124 base::Bind(&ValueStoreFrontend::Backend::Get, |
| 125 backend_, key, callback)); | 125 backend_, key, callback)); |
| 126 } | 126 } |
| 127 | 127 |
| 128 void ValueStoreFrontend::Set(const std::string& key, | 128 void ValueStoreFrontend::Set(const std::string& key, |
| 129 scoped_ptr<base::Value> value) { | 129 std::unique_ptr<base::Value> value) { |
| 130 DCHECK(CalledOnValidThread()); | 130 DCHECK(CalledOnValidThread()); |
| 131 | 131 |
| 132 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 132 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 133 base::Bind(&ValueStoreFrontend::Backend::Set, | 133 base::Bind(&ValueStoreFrontend::Backend::Set, |
| 134 backend_, key, base::Passed(&value))); | 134 backend_, key, base::Passed(&value))); |
| 135 } | 135 } |
| 136 | 136 |
| 137 void ValueStoreFrontend::Remove(const std::string& key) { | 137 void ValueStoreFrontend::Remove(const std::string& key) { |
| 138 DCHECK(CalledOnValidThread()); | 138 DCHECK(CalledOnValidThread()); |
| 139 | 139 |
| 140 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 140 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
| 141 base::Bind(&ValueStoreFrontend::Backend::Remove, | 141 base::Bind(&ValueStoreFrontend::Backend::Remove, |
| 142 backend_, key)); | 142 backend_, key)); |
| 143 } | 143 } |
| OLD | NEW |