OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/sync/core/model_type_store_impl.h" | 5 #include "components/sync/core/model_type_store_impl.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
14 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
16 #include "components/sync/core/model_type_store_backend.h" | 16 #include "components/sync/core/model_type_store_backend.h" |
17 #include "third_party/leveldatabase/src/include/leveldb/env.h" | 17 #include "third_party/leveldatabase/src/include/leveldb/env.h" |
18 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" | 18 #include "third_party/leveldatabase/src/include/leveldb/write_batch.h" |
19 | 19 |
20 namespace syncer_v2 { | 20 namespace syncer { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 // Key prefix for data/metadata records. | 24 // Key prefix for data/metadata records. |
25 const char kDataPrefix[] = "-dt-"; | 25 const char kDataPrefix[] = "-dt-"; |
26 const char kMetadataPrefix[] = "-md-"; | 26 const char kMetadataPrefix[] = "-md-"; |
27 | 27 |
28 // Key for global metadata record. | 28 // Key for global metadata record. |
29 const char kGlobalMetadataKey[] = "GlobalMetadata"; | 29 const char kGlobalMetadataKey[] = "GlobalMetadata"; |
30 | 30 |
31 void NoOpForBackendDtor(scoped_refptr<ModelTypeStoreBackend> backend) { | 31 void NoOpForBackendDtor(scoped_refptr<ModelTypeStoreBackend> backend) { |
32 // This function was intentionally left blank. | 32 // This function was intentionally left blank. |
33 } | 33 } |
34 | 34 |
35 } // namespace | 35 } // namespace |
36 | 36 |
37 // static | 37 // static |
38 std::string ModelTypeStoreImpl::FormatDataPrefix(const syncer::ModelType type) { | 38 std::string ModelTypeStoreImpl::FormatDataPrefix(const ModelType type) { |
39 return std::string(syncer::GetModelTypeRootTag(type)) + kDataPrefix; | 39 return std::string(GetModelTypeRootTag(type)) + kDataPrefix; |
40 } | 40 } |
41 | 41 |
42 // static | 42 // static |
43 std::string ModelTypeStoreImpl::FormatMetaPrefix(const syncer::ModelType type) { | 43 std::string ModelTypeStoreImpl::FormatMetaPrefix(const ModelType type) { |
44 return std::string(syncer::GetModelTypeRootTag(type)) + kMetadataPrefix; | 44 return std::string(GetModelTypeRootTag(type)) + kMetadataPrefix; |
45 } | 45 } |
46 | 46 |
47 // static | 47 // static |
48 leveldb::WriteBatch* ModelTypeStoreImpl::GetLeveldbWriteBatch( | 48 leveldb::WriteBatch* ModelTypeStoreImpl::GetLeveldbWriteBatch( |
49 WriteBatch* write_batch) { | 49 WriteBatch* write_batch) { |
50 return static_cast<WriteBatchImpl*>(write_batch)->leveldb_write_batch_.get(); | 50 return static_cast<WriteBatchImpl*>(write_batch)->leveldb_write_batch_.get(); |
51 } | 51 } |
52 | 52 |
53 std::string ModelTypeStoreImpl::FormatDataKey(const std::string& id) { | 53 std::string ModelTypeStoreImpl::FormatDataKey(const std::string& id) { |
54 return data_prefix_ + id; | 54 return data_prefix_ + id; |
55 } | 55 } |
56 | 56 |
57 std::string ModelTypeStoreImpl::FormatMetadataKey(const std::string& id) { | 57 std::string ModelTypeStoreImpl::FormatMetadataKey(const std::string& id) { |
58 return metadata_prefix_ + id; | 58 return metadata_prefix_ + id; |
59 } | 59 } |
60 | 60 |
61 ModelTypeStoreImpl::ModelTypeStoreImpl( | 61 ModelTypeStoreImpl::ModelTypeStoreImpl( |
62 const syncer::ModelType type, | 62 const ModelType type, |
63 scoped_refptr<ModelTypeStoreBackend> backend, | 63 scoped_refptr<ModelTypeStoreBackend> backend, |
64 scoped_refptr<base::SequencedTaskRunner> backend_task_runner) | 64 scoped_refptr<base::SequencedTaskRunner> backend_task_runner) |
65 : backend_(backend), | 65 : backend_(backend), |
66 backend_task_runner_(backend_task_runner), | 66 backend_task_runner_(backend_task_runner), |
67 data_prefix_(FormatDataPrefix(type)), | 67 data_prefix_(FormatDataPrefix(type)), |
68 metadata_prefix_(FormatMetaPrefix(type)), | 68 metadata_prefix_(FormatMetaPrefix(type)), |
69 weak_ptr_factory_(this) { | 69 weak_ptr_factory_(this) { |
70 DCHECK(backend_); | 70 DCHECK(backend_); |
71 DCHECK(backend_task_runner_); | 71 DCHECK(backend_task_runner_); |
72 } | 72 } |
73 | 73 |
74 ModelTypeStoreImpl::~ModelTypeStoreImpl() { | 74 ModelTypeStoreImpl::~ModelTypeStoreImpl() { |
75 DCHECK(CalledOnValidThread()); | 75 DCHECK(CalledOnValidThread()); |
76 backend_task_runner_->PostTask( | 76 backend_task_runner_->PostTask( |
77 FROM_HERE, base::Bind(&NoOpForBackendDtor, base::Passed(&backend_))); | 77 FROM_HERE, base::Bind(&NoOpForBackendDtor, base::Passed(&backend_))); |
78 } | 78 } |
79 | 79 |
80 // static | 80 // static |
81 void ModelTypeStoreImpl::CreateStore( | 81 void ModelTypeStoreImpl::CreateStore( |
82 const syncer::ModelType type, | 82 const ModelType type, |
83 const std::string& path, | 83 const std::string& path, |
84 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, | 84 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, |
85 const InitCallback& callback) { | 85 const InitCallback& callback) { |
86 DCHECK(!callback.is_null()); | 86 DCHECK(!callback.is_null()); |
87 std::unique_ptr<leveldb::Env> env; | 87 std::unique_ptr<leveldb::Env> env; |
88 std::unique_ptr<Result> result(new Result()); | 88 std::unique_ptr<Result> result(new Result()); |
89 auto task = base::Bind(&ModelTypeStoreBackend::GetOrCreateBackend, path, | 89 auto task = base::Bind(&ModelTypeStoreBackend::GetOrCreateBackend, path, |
90 base::Passed(&env), result.get()); | 90 base::Passed(&env), result.get()); |
91 auto reply = | 91 auto reply = |
92 base::Bind(&ModelTypeStoreImpl::BackendInitDone, type, | 92 base::Bind(&ModelTypeStoreImpl::BackendInitDone, type, |
(...skipping 16 matching lines...) Expand all Loading... |
109 path += "/in-memory"; | 109 path += "/in-memory"; |
110 | 110 |
111 // In-memory store backend works on the same thread as test. | 111 // In-memory store backend works on the same thread as test. |
112 scoped_refptr<base::SequencedTaskRunner> task_runner = | 112 scoped_refptr<base::SequencedTaskRunner> task_runner = |
113 base::ThreadTaskRunnerHandle::Get(); | 113 base::ThreadTaskRunnerHandle::Get(); |
114 | 114 |
115 std::unique_ptr<Result> result(new Result()); | 115 std::unique_ptr<Result> result(new Result()); |
116 | 116 |
117 auto task = base::Bind(&ModelTypeStoreBackend::GetOrCreateBackend, path, | 117 auto task = base::Bind(&ModelTypeStoreBackend::GetOrCreateBackend, path, |
118 base::Passed(&env), result.get()); | 118 base::Passed(&env), result.get()); |
119 auto reply = | 119 auto reply = base::Bind(&ModelTypeStoreImpl::BackendInitDone, UNSPECIFIED, |
120 base::Bind(&ModelTypeStoreImpl::BackendInitDone, syncer::UNSPECIFIED, | 120 base::Passed(&result), task_runner, callback); |
121 base::Passed(&result), task_runner, callback); | |
122 | 121 |
123 base::PostTaskAndReplyWithResult(task_runner.get(), FROM_HERE, task, reply); | 122 base::PostTaskAndReplyWithResult(task_runner.get(), FROM_HERE, task, reply); |
124 } | 123 } |
125 | 124 |
126 // static | 125 // static |
127 void ModelTypeStoreImpl::BackendInitDone( | 126 void ModelTypeStoreImpl::BackendInitDone( |
128 const syncer::ModelType type, | 127 const ModelType type, |
129 std::unique_ptr<Result> result, | 128 std::unique_ptr<Result> result, |
130 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, | 129 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner, |
131 const InitCallback& callback, | 130 const InitCallback& callback, |
132 scoped_refptr<ModelTypeStoreBackend> backend) { | 131 scoped_refptr<ModelTypeStoreBackend> backend) { |
133 std::unique_ptr<ModelTypeStoreImpl> store; | 132 std::unique_ptr<ModelTypeStoreImpl> store; |
134 if (*result == Result::SUCCESS) { | 133 if (*result == Result::SUCCESS) { |
135 store.reset(new ModelTypeStoreImpl(type, backend, blocking_task_runner)); | 134 store.reset(new ModelTypeStoreImpl(type, backend, blocking_task_runner)); |
136 } | 135 } |
137 | 136 |
138 callback.Run(*result, std::move(store)); | 137 callback.Run(*result, std::move(store)); |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 DCHECK(CalledOnValidThread()); | 333 DCHECK(CalledOnValidThread()); |
335 GetLeveldbWriteBatch(write_batch)->Delete(kGlobalMetadataKey); | 334 GetLeveldbWriteBatch(write_batch)->Delete(kGlobalMetadataKey); |
336 } | 335 } |
337 | 336 |
338 ModelTypeStoreImpl::WriteBatchImpl::WriteBatchImpl() { | 337 ModelTypeStoreImpl::WriteBatchImpl::WriteBatchImpl() { |
339 leveldb_write_batch_.reset(new leveldb::WriteBatch()); | 338 leveldb_write_batch_.reset(new leveldb::WriteBatch()); |
340 } | 339 } |
341 | 340 |
342 ModelTypeStoreImpl::WriteBatchImpl::~WriteBatchImpl() {} | 341 ModelTypeStoreImpl::WriteBatchImpl::~WriteBatchImpl() {} |
343 | 342 |
344 } // namespace syncer_v2 | 343 } // namespace syncer |
OLD | NEW |