Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: content/child/indexed_db/indexed_db_callbacks_impl.cc

Issue 2370643004: Port messages sent by WebIDBFactoryImpl to Mojo. (Closed)
Patch Set: Require explicit wrapping when discarding map keys. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/child/indexed_db/indexed_db_callbacks_impl.h"
6
7 #include "content/child/indexed_db/indexed_db_dispatcher.h"
8 #include "content/child/indexed_db/indexed_db_key_builders.h"
9 #include "content/child/thread_safe_sender.h"
10 #include "content/common/indexed_db/indexed_db_constants.h"
11 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBCallbacks.h "
12 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBDatabaseErr or.h"
13 #include "third_party/WebKit/public/platform/modules/indexeddb/WebIDBMetadata.h"
14
15 using blink::WebIDBCallbacks;
16 using blink::WebIDBDatabase;
17 using blink::WebIDBMetadata;
18 using blink::WebString;
19 using blink::WebVector;
20
21 namespace content {
22
23 namespace {
24
25 void ConvertIndexMetadata(const content::IndexedDBIndexMetadata& metadata,
26 WebIDBMetadata::Index* output) {
27 output->id = metadata.id;
28 output->name = metadata.name;
29 output->keyPath = WebIDBKeyPathBuilder::Build(metadata.key_path);
30 output->unique = metadata.unique;
31 output->multiEntry = metadata.multi_entry;
32 }
33
34 void ConvertObjectStoreMetadata(
35 const content::IndexedDBObjectStoreMetadata& metadata,
36 WebIDBMetadata::ObjectStore* output) {
37 output->id = metadata.id;
38 output->name = metadata.name;
39 output->keyPath = WebIDBKeyPathBuilder::Build(metadata.key_path);
40 output->autoIncrement = metadata.auto_increment;
41 output->maxIndexId = metadata.max_index_id;
42 output->indexes = WebVector<WebIDBMetadata::Index>(metadata.indexes.size());
43 size_t i = 0;
44 for (const auto& iter : metadata.indexes)
45 ConvertIndexMetadata(iter.second, &output->indexes[i++]);
46 }
47
48 void ConvertDatabaseMetadata(const content::IndexedDBDatabaseMetadata& metadata,
49 WebIDBMetadata* output) {
50 output->id = metadata.id;
51 output->name = metadata.name;
52 output->version = metadata.version;
53 output->maxObjectStoreId = metadata.max_object_store_id;
54 output->objectStores =
55 WebVector<WebIDBMetadata::ObjectStore>(metadata.object_stores.size());
56 size_t i = 0;
57 for (const auto& iter : metadata.object_stores)
58 ConvertObjectStoreMetadata(iter.second, &output->objectStores[i++]);
59 }
60
61 } // namespace
62
63 class IndexedDBCallbacksImpl::InternalState {
64 public:
65 InternalState(blink::WebIDBCallbacks* callbacks,
66 scoped_refptr<ThreadSafeSender> thread_safe_sender);
67 ~InternalState();
68
69 void Error(int32_t code, const base::string16& message);
70 void SuccessStringList(const std::vector<base::string16>& value);
71 void Blocked(int64_t existing_version);
72 void UpgradeNeeded(int32_t database_id,
73 int64_t old_version,
74 blink::WebIDBDataLoss data_loss,
75 const std::string& data_loss_message,
76 const content::IndexedDBDatabaseMetadata& metadata);
77 void SuccessDatabase(int32_t database_id,
78 const content::IndexedDBDatabaseMetadata& metadata);
79 void SuccessInteger(int64_t value);
80
81 private:
82 std::unique_ptr<WebIDBCallbacks> callbacks_;
83 scoped_refptr<ThreadSafeSender> thread_safe_sender_;
84
85 DISALLOW_COPY_AND_ASSIGN(InternalState);
86 };
87
88 IndexedDBCallbacksImpl::IndexedDBCallbacksImpl(
89 WebIDBCallbacks* callbacks,
90 scoped_refptr<ThreadSafeSender> thread_safe_sender)
91 : internal_state_(
92 new InternalState(callbacks, std::move(thread_safe_sender))),
93 callback_runner_(base::ThreadTaskRunnerHandle::Get()) {}
94
95 IndexedDBCallbacksImpl::~IndexedDBCallbacksImpl() {
96 callback_runner_->DeleteSoon(FROM_HERE, internal_state_);
97 }
98
99 void IndexedDBCallbacksImpl::Error(int32_t code,
100 const base::string16& message) {
101 callback_runner_->PostTask(
102 FROM_HERE, base::Bind(&InternalState::Error,
103 base::Unretained(internal_state_), code, message));
104 }
105
106 void IndexedDBCallbacksImpl::SuccessStringList(
107 const std::vector<base::string16>& value) {
108 callback_runner_->PostTask(
109 FROM_HERE, base::Bind(&InternalState::SuccessStringList,
110 base::Unretained(internal_state_), value));
111 }
112
113 void IndexedDBCallbacksImpl::Blocked(int64_t existing_version) {
114 callback_runner_->PostTask(
115 FROM_HERE,
116 base::Bind(&InternalState::Blocked, base::Unretained(internal_state_),
117 existing_version));
118 }
119
120 void IndexedDBCallbacksImpl::UpgradeNeeded(
121 int32_t database_id,
122 int64_t old_version,
123 blink::WebIDBDataLoss data_loss,
124 const std::string& data_loss_message,
125 const content::IndexedDBDatabaseMetadata& metadata) {
126 callback_runner_->PostTask(
127 FROM_HERE,
128 base::Bind(&InternalState::UpgradeNeeded,
129 base::Unretained(internal_state_), database_id, old_version,
130 data_loss, data_loss_message, metadata));
131 }
132
133 void IndexedDBCallbacksImpl::SuccessDatabase(
134 int32_t database_id,
135 const content::IndexedDBDatabaseMetadata& metadata) {
136 callback_runner_->PostTask(
137 FROM_HERE,
138 base::Bind(&InternalState::SuccessDatabase,
139 base::Unretained(internal_state_), database_id, metadata));
140 }
141
142 void IndexedDBCallbacksImpl::SuccessInteger(int64_t value) {
143 callback_runner_->PostTask(
144 FROM_HERE, base::Bind(&InternalState::SuccessInteger,
145 base::Unretained(internal_state_), value));
146 }
147
148 IndexedDBCallbacksImpl::InternalState::InternalState(
149 blink::WebIDBCallbacks* callbacks,
150 scoped_refptr<ThreadSafeSender> thread_safe_sender)
151 : callbacks_(callbacks),
152 thread_safe_sender_(std::move(thread_safe_sender)) {}
153
154 IndexedDBCallbacksImpl::InternalState::~InternalState() {}
155
156 void IndexedDBCallbacksImpl::InternalState::Error(
157 int32_t code,
158 const base::string16& message) {
159 callbacks_->onError(blink::WebIDBDatabaseError(code, message));
160 }
161
162 void IndexedDBCallbacksImpl::InternalState::SuccessStringList(
163 const std::vector<base::string16>& value) {
164 callbacks_->onSuccess(WebVector<WebString>(value));
165 }
166
167 void IndexedDBCallbacksImpl::InternalState::Blocked(int64_t existing_version) {
168 callbacks_->onBlocked(existing_version);
169 }
170
171 void IndexedDBCallbacksImpl::InternalState::UpgradeNeeded(
172 int32_t database_id,
173 int64_t old_version,
174 blink::WebIDBDataLoss data_loss,
175 const std::string& data_loss_message,
176 const content::IndexedDBDatabaseMetadata& metadata) {
177 IndexedDBDispatcher* dispatcher =
178 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
179 WebIDBDatabase* database = dispatcher->RegisterDatabase(database_id);
180 WebIDBMetadata web_metadata;
181 ConvertDatabaseMetadata(metadata, &web_metadata);
182 callbacks_->onUpgradeNeeded(old_version, database, web_metadata, data_loss,
183 WebString::fromUTF8(data_loss_message));
184 }
185
186 void IndexedDBCallbacksImpl::InternalState::SuccessDatabase(
187 int32_t database_id,
188 const content::IndexedDBDatabaseMetadata& metadata) {
189 WebIDBDatabase* database = nullptr;
190 if (database_id != kNoDatabase) {
191 IndexedDBDispatcher* dispatcher =
192 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get());
193 database = dispatcher->RegisterDatabase(database_id);
194 }
195 WebIDBMetadata web_metadata;
196 ConvertDatabaseMetadata(metadata, &web_metadata);
197 callbacks_->onSuccess(database, web_metadata);
198 }
199
200 void IndexedDBCallbacksImpl::InternalState::SuccessInteger(int64_t value) {
201 callbacks_->onSuccess(value);
202 }
203
204 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698