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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
9 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
12 #include "extensions/browser/value_store/leveldb_value_store.h" | 12 #include "extensions/browser/value_store/leveldb_value_store.h" |
13 | 13 |
14 using content::BrowserThread; | 14 using content::BrowserThread; |
15 | 15 |
16 class ValueStoreFrontend::Backend : public base::RefCountedThreadSafe<Backend> { | 16 class ValueStoreFrontend::Backend : public base::RefCountedThreadSafe<Backend> { |
17 public: | 17 public: |
18 Backend() : storage_(NULL) {} | 18 Backend() : storage_(NULL) {} |
19 | 19 |
20 void Init(const base::FilePath& db_path) { | 20 void Init(const base::FilePath& db_path) { |
21 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 21 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
22 DCHECK(!storage_); | 22 DCHECK(!storage_); |
23 TRACE_EVENT0("ValueStoreFrontend::Backend", "Init"); | 23 TRACE_EVENT0("ValueStoreFrontend::Backend", "Init"); |
24 db_path_ = db_path; | 24 db_path_ = db_path; |
25 storage_ = new LeveldbValueStore(db_path); | 25 storage_ = new LeveldbValueStore(db_path); |
26 } | 26 } |
27 | 27 |
28 // This variant is useful for testing (using a mock ValueStore). | 28 // This variant is useful for testing (using a mock ValueStore). |
29 void InitWithStore(scoped_ptr<ValueStore> storage) { | 29 void InitWithStore(scoped_ptr<ValueStore> storage) { |
30 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 30 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
31 DCHECK(!storage_); | 31 DCHECK(!storage_); |
32 storage_ = storage.release(); | 32 storage_ = storage.release(); |
33 } | 33 } |
34 | 34 |
35 void Get(const std::string& key, | 35 void Get(const std::string& key, |
36 const ValueStoreFrontend::ReadCallback& callback) { | 36 const ValueStoreFrontend::ReadCallback& callback) { |
37 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 37 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
38 ValueStore::ReadResult result = storage_->Get(key); | 38 ValueStore::ReadResult result = storage_->Get(key); |
39 | 39 |
40 // Extract the value from the ReadResult and pass ownership of it to the | 40 // Extract the value from the ReadResult and pass ownership of it to the |
41 // callback. | 41 // callback. |
42 scoped_ptr<base::Value> value; | 42 scoped_ptr<base::Value> value; |
43 if (!result->HasError()) { | 43 if (!result->HasError()) { |
44 result->settings().RemoveWithoutPathExpansion(key, &value); | 44 result->settings().RemoveWithoutPathExpansion(key, &value); |
45 } else { | 45 } else { |
46 LOG(WARNING) << "Reading " << key << " from " << db_path_.value() | 46 LOG(WARNING) << "Reading " << key << " from " << db_path_.value() |
47 << " failed: " << result->error().message; | 47 << " failed: " << result->error().message; |
48 } | 48 } |
49 | 49 |
50 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 50 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
51 base::Bind(&ValueStoreFrontend::Backend::RunCallback, | 51 base::Bind(&ValueStoreFrontend::Backend::RunCallback, |
52 this, callback, base::Passed(&value))); | 52 this, callback, base::Passed(&value))); |
53 } | 53 } |
54 | 54 |
55 void Set(const std::string& key, scoped_ptr<base::Value> value) { | 55 void Set(const std::string& key, scoped_ptr<base::Value> value) { |
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 56 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
57 // We don't need the old value, so skip generating changes. | 57 // We don't need the old value, so skip generating changes. |
58 storage_->Set(ValueStore::IGNORE_QUOTA | ValueStore::NO_GENERATE_CHANGES, | 58 storage_->Set(ValueStore::IGNORE_QUOTA | ValueStore::NO_GENERATE_CHANGES, |
59 key, *value.get()); | 59 key, *value.get()); |
60 } | 60 } |
61 | 61 |
62 void Remove(const std::string& key) { | 62 void Remove(const std::string& key) { |
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 63 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
64 storage_->Remove(key); | 64 storage_->Remove(key); |
65 } | 65 } |
66 | 66 |
67 private: | 67 private: |
68 friend class base::RefCountedThreadSafe<Backend>; | 68 friend class base::RefCountedThreadSafe<Backend>; |
69 | 69 |
70 virtual ~Backend() { | 70 virtual ~Backend() { |
71 if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 71 if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
72 delete storage_; | 72 delete storage_; |
73 } else { | 73 } else { |
74 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, storage_); | 74 BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, storage_); |
75 } | 75 } |
76 } | 76 } |
77 | 77 |
78 void RunCallback(const ValueStoreFrontend::ReadCallback& callback, | 78 void RunCallback(const ValueStoreFrontend::ReadCallback& callback, |
79 scoped_ptr<base::Value> value) { | 79 scoped_ptr<base::Value> value) { |
80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 80 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
81 callback.Run(value.Pass()); | 81 callback.Run(value.Pass()); |
82 } | 82 } |
83 | 83 |
84 // The actual ValueStore that handles persisting the data to disk. Used | 84 // The actual ValueStore that handles persisting the data to disk. Used |
85 // exclusively on the FILE thread. | 85 // exclusively on the FILE thread. |
86 ValueStore* storage_; | 86 ValueStore* storage_; |
87 | 87 |
88 base::FilePath db_path_; | 88 base::FilePath db_path_; |
89 | 89 |
90 DISALLOW_COPY_AND_ASSIGN(Backend); | 90 DISALLOW_COPY_AND_ASSIGN(Backend); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |