OLD | NEW |
(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 using indexed_db::mojom::DatabaseMetadataPtr; |
| 21 |
| 22 namespace content { |
| 23 |
| 24 namespace { |
| 25 |
| 26 WebIDBMetadata ConvertMetadata(DatabaseMetadataPtr metadata) { |
| 27 WebIDBMetadata web_metadata; |
| 28 web_metadata.id = metadata->id; |
| 29 web_metadata.name = metadata->name; |
| 30 web_metadata.version = metadata->version; |
| 31 web_metadata.maxObjectStoreId = metadata->max_object_store_id; |
| 32 web_metadata.objectStores = |
| 33 WebVector<WebIDBMetadata::ObjectStore>(metadata->object_stores.size()); |
| 34 |
| 35 for (size_t i = 0; i < metadata->object_stores.size(); ++i) { |
| 36 const indexed_db::mojom::ObjectStoreMetadataPtr& store_metadata = |
| 37 metadata->object_stores[i]; |
| 38 WebIDBMetadata::ObjectStore& web_store_metadata = |
| 39 web_metadata.objectStores[i]; |
| 40 |
| 41 web_store_metadata.id = store_metadata->id; |
| 42 web_store_metadata.name = store_metadata->name; |
| 43 web_store_metadata.keyPath = |
| 44 WebIDBKeyPathBuilder::Build(store_metadata->key_path); |
| 45 web_store_metadata.autoIncrement = store_metadata->auto_increment; |
| 46 web_store_metadata.maxIndexId = store_metadata->max_index_id; |
| 47 web_store_metadata.indexes = |
| 48 WebVector<WebIDBMetadata::Index>(store_metadata->indexes.size()); |
| 49 |
| 50 for (size_t j = 0; j < store_metadata->indexes.size(); ++j) { |
| 51 const indexed_db::mojom::IndexMetadataPtr& index_metadata = |
| 52 store_metadata->indexes[j]; |
| 53 WebIDBMetadata::Index& web_index_metadata = web_store_metadata.indexes[j]; |
| 54 |
| 55 web_index_metadata.id = index_metadata->id; |
| 56 web_index_metadata.name = index_metadata->name; |
| 57 web_index_metadata.keyPath = |
| 58 WebIDBKeyPathBuilder::Build(index_metadata->key_path); |
| 59 web_index_metadata.unique = index_metadata->unique; |
| 60 web_index_metadata.multiEntry = index_metadata->multi_entry; |
| 61 } |
| 62 } |
| 63 |
| 64 return web_metadata; |
| 65 } |
| 66 |
| 67 } // namespace |
| 68 |
| 69 class IndexedDBCallbacksImpl::InternalState { |
| 70 public: |
| 71 InternalState(blink::WebIDBCallbacks* callbacks, |
| 72 scoped_refptr<ThreadSafeSender> thread_safe_sender); |
| 73 ~InternalState(); |
| 74 |
| 75 void Blocked(int64_t existing_version); |
| 76 void Error(int32_t code, const base::string16& message); |
| 77 void SuccessDatabase(int32_t database_id, |
| 78 indexed_db::mojom::DatabaseMetadataPtr metadata); |
| 79 void SuccessInteger(int64_t value); |
| 80 void SuccessStringList(const std::vector<base::string16>& value); |
| 81 void UpgradeNeeded(int32_t database_id, |
| 82 int64_t old_version, |
| 83 indexed_db::mojom::DataLoss data_loss, |
| 84 const std::string& data_loss_message, |
| 85 indexed_db::mojom::DatabaseMetadataPtr metadata); |
| 86 |
| 87 private: |
| 88 std::unique_ptr<WebIDBCallbacks> callbacks_; |
| 89 scoped_refptr<ThreadSafeSender> thread_safe_sender_; |
| 90 }; |
| 91 |
| 92 IndexedDBCallbacksImpl::IndexedDBCallbacksImpl( |
| 93 WebIDBCallbacks* callbacks, |
| 94 scoped_refptr<ThreadSafeSender> thread_safe_sender) |
| 95 : callback_runner_(base::ThreadTaskRunnerHandle::Get()), binding_(this) { |
| 96 internal_state_ = new InternalState(callbacks, std::move(thread_safe_sender)); |
| 97 } |
| 98 |
| 99 IndexedDBCallbacksImpl::~IndexedDBCallbacksImpl() { |
| 100 callback_runner_->DeleteSoon(FROM_HERE, internal_state_); |
| 101 } |
| 102 |
| 103 void IndexedDBCallbacksImpl::Bind( |
| 104 indexed_db::mojom::CallbacksAssociatedPtrInfo* ptr_info, |
| 105 mojo::AssociatedGroup* associated_group) { |
| 106 binding_.Bind(ptr_info, associated_group); |
| 107 binding_.set_connection_error_handler(base::Bind( |
| 108 &IndexedDBCallbacksImpl::OnConnectionError, base::Unretained(this))); |
| 109 } |
| 110 |
| 111 void IndexedDBCallbacksImpl::OnConnectionError() { |
| 112 delete this; |
| 113 } |
| 114 |
| 115 void IndexedDBCallbacksImpl::Blocked(int64_t existing_version) { |
| 116 callback_runner_->PostTask( |
| 117 FROM_HERE, |
| 118 base::Bind(&InternalState::Blocked, base::Unretained(internal_state_), |
| 119 existing_version)); |
| 120 } |
| 121 |
| 122 void IndexedDBCallbacksImpl::Error(int32_t code, |
| 123 const base::string16& message) { |
| 124 callback_runner_->PostTask( |
| 125 FROM_HERE, base::Bind(&InternalState::Error, |
| 126 base::Unretained(internal_state_), code, message)); |
| 127 } |
| 128 |
| 129 void IndexedDBCallbacksImpl::SuccessDatabase(int32_t database_id, |
| 130 DatabaseMetadataPtr metadata) { |
| 131 callback_runner_->PostTask( |
| 132 FROM_HERE, base::Bind(&InternalState::SuccessDatabase, |
| 133 base::Unretained(internal_state_), database_id, |
| 134 base::Passed(&metadata))); |
| 135 } |
| 136 |
| 137 void IndexedDBCallbacksImpl::SuccessInteger(int64_t value) { |
| 138 callback_runner_->PostTask( |
| 139 FROM_HERE, base::Bind(&InternalState::SuccessInteger, |
| 140 base::Unretained(internal_state_), value)); |
| 141 } |
| 142 |
| 143 void IndexedDBCallbacksImpl::SuccessStringList( |
| 144 const std::vector<base::string16>& value) { |
| 145 callback_runner_->PostTask( |
| 146 FROM_HERE, base::Bind(&InternalState::SuccessStringList, |
| 147 base::Unretained(internal_state_), value)); |
| 148 } |
| 149 |
| 150 void IndexedDBCallbacksImpl::UpgradeNeeded( |
| 151 int32_t database_id, |
| 152 int64_t old_version, |
| 153 indexed_db::mojom::DataLoss data_loss, |
| 154 const std::string& data_loss_message, |
| 155 DatabaseMetadataPtr metadata) { |
| 156 callback_runner_->PostTask( |
| 157 FROM_HERE, |
| 158 base::Bind(&InternalState::UpgradeNeeded, |
| 159 base::Unretained(internal_state_), database_id, old_version, |
| 160 data_loss, data_loss_message, base::Passed(&metadata))); |
| 161 } |
| 162 |
| 163 IndexedDBCallbacksImpl::InternalState::InternalState( |
| 164 blink::WebIDBCallbacks* callbacks, |
| 165 scoped_refptr<ThreadSafeSender> thread_safe_sender) |
| 166 : callbacks_(callbacks), |
| 167 thread_safe_sender_(std::move(thread_safe_sender)) {} |
| 168 |
| 169 IndexedDBCallbacksImpl::InternalState::~InternalState() {} |
| 170 |
| 171 void IndexedDBCallbacksImpl::InternalState::Blocked(int64_t existing_version) { |
| 172 callbacks_->onBlocked(existing_version); |
| 173 } |
| 174 |
| 175 void IndexedDBCallbacksImpl::InternalState::Error( |
| 176 int32_t code, |
| 177 const base::string16& message) { |
| 178 callbacks_->onError(blink::WebIDBDatabaseError(code, message)); |
| 179 } |
| 180 |
| 181 void IndexedDBCallbacksImpl::InternalState::SuccessDatabase( |
| 182 int32_t database_id, |
| 183 DatabaseMetadataPtr metadata) { |
| 184 WebIDBDatabase* database = nullptr; |
| 185 if (database_id != kNoDatabase) { |
| 186 IndexedDBDispatcher* dispatcher = |
| 187 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); |
| 188 database = dispatcher->RegisterDatabase(database_id); |
| 189 } |
| 190 callbacks_->onSuccess(database, ConvertMetadata(std::move(metadata))); |
| 191 } |
| 192 |
| 193 void IndexedDBCallbacksImpl::InternalState::SuccessInteger(int64_t value) { |
| 194 callbacks_->onSuccess(value); |
| 195 } |
| 196 |
| 197 void IndexedDBCallbacksImpl::InternalState::SuccessStringList( |
| 198 const std::vector<base::string16>& value) { |
| 199 callbacks_->onSuccess(WebVector<WebString>(value)); |
| 200 } |
| 201 |
| 202 void IndexedDBCallbacksImpl::InternalState::UpgradeNeeded( |
| 203 int32_t database_id, |
| 204 int64_t old_version, |
| 205 indexed_db::mojom::DataLoss data_loss, |
| 206 const std::string& data_loss_message, |
| 207 DatabaseMetadataPtr metadata) { |
| 208 IndexedDBDispatcher* dispatcher = |
| 209 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); |
| 210 WebIDBDatabase* database = dispatcher->RegisterDatabase(database_id); |
| 211 callbacks_->onUpgradeNeeded(old_version, database, |
| 212 ConvertMetadata(std::move(metadata)), |
| 213 static_cast<blink::WebIDBDataLoss>(data_loss), |
| 214 WebString::fromUTF8(data_loss_message)); |
| 215 } |
| 216 |
| 217 } // namespace content |
OLD | NEW |