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 |
| 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 IndexedDBCallbacksImpl::IndexedDBCallbacksImpl( |
| 64 std::unique_ptr<WebIDBCallbacks> callbacks, |
| 65 scoped_refptr<ThreadSafeSender> thread_safe_sender) |
| 66 : internal_state_(new InternalState(std::move(callbacks), |
| 67 std::move(thread_safe_sender))), |
| 68 callback_runner_(base::ThreadTaskRunnerHandle::Get()) {} |
| 69 |
| 70 IndexedDBCallbacksImpl::~IndexedDBCallbacksImpl() { |
| 71 callback_runner_->DeleteSoon(FROM_HERE, internal_state_); |
| 72 } |
| 73 |
| 74 void IndexedDBCallbacksImpl::Error(int32_t code, |
| 75 const base::string16& message) { |
| 76 callback_runner_->PostTask( |
| 77 FROM_HERE, base::Bind(&InternalState::Error, |
| 78 base::Unretained(internal_state_), code, message)); |
| 79 } |
| 80 |
| 81 void IndexedDBCallbacksImpl::SuccessStringList( |
| 82 const std::vector<base::string16>& value) { |
| 83 callback_runner_->PostTask( |
| 84 FROM_HERE, base::Bind(&InternalState::SuccessStringList, |
| 85 base::Unretained(internal_state_), value)); |
| 86 } |
| 87 |
| 88 void IndexedDBCallbacksImpl::Blocked(int64_t existing_version) { |
| 89 callback_runner_->PostTask( |
| 90 FROM_HERE, |
| 91 base::Bind(&InternalState::Blocked, base::Unretained(internal_state_), |
| 92 existing_version)); |
| 93 } |
| 94 |
| 95 void IndexedDBCallbacksImpl::UpgradeNeeded( |
| 96 int32_t database_id, |
| 97 int64_t old_version, |
| 98 blink::WebIDBDataLoss data_loss, |
| 99 const std::string& data_loss_message, |
| 100 const content::IndexedDBDatabaseMetadata& metadata) { |
| 101 callback_runner_->PostTask( |
| 102 FROM_HERE, |
| 103 base::Bind(&InternalState::UpgradeNeeded, |
| 104 base::Unretained(internal_state_), database_id, old_version, |
| 105 data_loss, data_loss_message, metadata)); |
| 106 } |
| 107 |
| 108 void IndexedDBCallbacksImpl::SuccessDatabase( |
| 109 int32_t database_id, |
| 110 const content::IndexedDBDatabaseMetadata& metadata) { |
| 111 callback_runner_->PostTask( |
| 112 FROM_HERE, |
| 113 base::Bind(&InternalState::SuccessDatabase, |
| 114 base::Unretained(internal_state_), database_id, metadata)); |
| 115 } |
| 116 |
| 117 void IndexedDBCallbacksImpl::SuccessInteger(int64_t value) { |
| 118 callback_runner_->PostTask( |
| 119 FROM_HERE, base::Bind(&InternalState::SuccessInteger, |
| 120 base::Unretained(internal_state_), value)); |
| 121 } |
| 122 |
| 123 IndexedDBCallbacksImpl::InternalState::InternalState( |
| 124 std::unique_ptr<blink::WebIDBCallbacks> callbacks, |
| 125 scoped_refptr<ThreadSafeSender> thread_safe_sender) |
| 126 : callbacks_(std::move(callbacks)), |
| 127 thread_safe_sender_(std::move(thread_safe_sender)) { |
| 128 IndexedDBDispatcher* dispatcher = |
| 129 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); |
| 130 dispatcher->RegisterMojoOwnedCallbacks(this); |
| 131 } |
| 132 |
| 133 IndexedDBCallbacksImpl::InternalState::~InternalState() { |
| 134 IndexedDBDispatcher* dispatcher = |
| 135 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); |
| 136 dispatcher->UnregisterMojoOwnedCallbacks(this); |
| 137 } |
| 138 |
| 139 void IndexedDBCallbacksImpl::InternalState::Error( |
| 140 int32_t code, |
| 141 const base::string16& message) { |
| 142 callbacks_->onError(blink::WebIDBDatabaseError(code, message)); |
| 143 } |
| 144 |
| 145 void IndexedDBCallbacksImpl::InternalState::SuccessStringList( |
| 146 const std::vector<base::string16>& value) { |
| 147 callbacks_->onSuccess(WebVector<WebString>(value)); |
| 148 } |
| 149 |
| 150 void IndexedDBCallbacksImpl::InternalState::Blocked(int64_t existing_version) { |
| 151 callbacks_->onBlocked(existing_version); |
| 152 } |
| 153 |
| 154 void IndexedDBCallbacksImpl::InternalState::UpgradeNeeded( |
| 155 int32_t database_id, |
| 156 int64_t old_version, |
| 157 blink::WebIDBDataLoss data_loss, |
| 158 const std::string& data_loss_message, |
| 159 const content::IndexedDBDatabaseMetadata& metadata) { |
| 160 IndexedDBDispatcher* dispatcher = |
| 161 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); |
| 162 WebIDBDatabase* database = dispatcher->RegisterDatabase(database_id); |
| 163 WebIDBMetadata web_metadata; |
| 164 ConvertDatabaseMetadata(metadata, &web_metadata); |
| 165 callbacks_->onUpgradeNeeded(old_version, database, web_metadata, data_loss, |
| 166 WebString::fromUTF8(data_loss_message)); |
| 167 } |
| 168 |
| 169 void IndexedDBCallbacksImpl::InternalState::SuccessDatabase( |
| 170 int32_t database_id, |
| 171 const content::IndexedDBDatabaseMetadata& metadata) { |
| 172 WebIDBDatabase* database = nullptr; |
| 173 if (database_id != kNoDatabase) { |
| 174 IndexedDBDispatcher* dispatcher = |
| 175 IndexedDBDispatcher::ThreadSpecificInstance(thread_safe_sender_.get()); |
| 176 database = dispatcher->RegisterDatabase(database_id); |
| 177 } |
| 178 WebIDBMetadata web_metadata; |
| 179 ConvertDatabaseMetadata(metadata, &web_metadata); |
| 180 callbacks_->onSuccess(database, web_metadata); |
| 181 } |
| 182 |
| 183 void IndexedDBCallbacksImpl::InternalState::SuccessInteger(int64_t value) { |
| 184 callbacks_->onSuccess(value); |
| 185 } |
| 186 |
| 187 } // namespace content |
OLD | NEW |