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/browser/indexed_db/database_factory_impl.h" |
| 6 |
| 7 #include "content/browser/indexed_db/indexed_db_connection.h" |
| 8 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
| 9 #include "content/browser/indexed_db/indexed_db_database_callbacks.h" |
| 10 #include "content/browser/indexed_db/indexed_db_database_error.h" |
| 11 #include "content/browser/indexed_db/indexed_db_open_request_observer.h" |
| 12 #include "content/public/browser/browser_context.h" |
| 13 #include "content/public/browser/indexed_db_context.h" |
| 14 #include "content/public/browser/storage_partition.h" |
| 15 #include "mojo/common/common_type_converters.h" |
| 16 #include "mojo/public/cpp/bindings/binding.h" |
| 17 #include "mojo/public/cpp/bindings/interface_request.h" |
| 18 #include "net/url_request/url_request_context_getter.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 bool IsValidOrigin(const url::Origin& origin) { |
| 23 return !origin.unique(); |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
| 28 namespace indexed_db { |
| 29 |
| 30 DatabaseFactoryImpl::DatabaseFactoryImpl( |
| 31 content::BrowserContext* context, |
| 32 content::IndexedDBContextImpl* indexed_db_context, |
| 33 net::URLRequestContext* request_context, |
| 34 mojom::DatabaseFactoryRequest request) |
| 35 : context_(context), |
| 36 indexed_db_context_(indexed_db_context), |
| 37 request_context_(request_context), |
| 38 binding_(this, std::move(request)) { |
| 39 DCHECK(request_context != nullptr); |
| 40 // TODO(cmumford): Eventually delete member if not needed. |
| 41 (void)context_; |
| 42 } |
| 43 |
| 44 DatabaseFactoryImpl::~DatabaseFactoryImpl() = default; |
| 45 |
| 46 void DatabaseFactoryImpl::Create( |
| 47 content::BrowserContext* context, |
| 48 net::URLRequestContextGetter* request_context_getter, |
| 49 mojom::DatabaseFactoryRequest request) { |
| 50 // TODO(cmumford): Is |Default| OK? |
| 51 content::StoragePartition* storage_partition = |
| 52 content::BrowserContext::GetDefaultStoragePartition(context); |
| 53 content::IndexedDBContextImpl* indexed_db_context = |
| 54 reinterpret_cast<content::IndexedDBContextImpl*>( |
| 55 storage_partition->GetIndexedDBContext()); |
| 56 new DatabaseFactoryImpl(context, indexed_db_context, |
| 57 request_context_getter->GetURLRequestContext(), |
| 58 std::move(request)); |
| 59 } |
| 60 |
| 61 void DatabaseFactoryImpl::GetDatabaseNames(const url::Origin& origin) {} |
| 62 |
| 63 void DatabaseFactoryImpl::OnOpenResult( |
| 64 const base::TimeTicks& begin_time, |
| 65 const OpenCallback& mojo_callback, |
| 66 std::unique_ptr<content::IndexedDBConnection> connection, |
| 67 const content::IndexedDBDatabaseMetadata& metadata, |
| 68 const content::IndexedDBDatabaseError& error) { |
| 69 // mojo_callback.Run(); |
| 70 } |
| 71 |
| 72 void DatabaseFactoryImpl::Open(const mojo::String& name, |
| 73 int64_t version, |
| 74 int64_t transaction_id, |
| 75 const url::Origin& origin, |
| 76 mojom::OpenRequestObserverPtr open_observer, |
| 77 mojom::DatabaseObserverPtr database_handler, |
| 78 const OpenCallback& callback) { |
| 79 if (!IsValidOrigin(origin)) { |
| 80 CrashRendererAndClosePipe(content::bad_message::IDBDH_INVALID_ORIGIN); |
| 81 return; |
| 82 } |
| 83 |
| 84 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 85 base::FilePath indexed_db_path = indexed_db_context_->data_path(); |
| 86 |
| 87 int64_t host_transaction_id = HostTransactionId(transaction_id); |
| 88 |
| 89 content::OpenResultCallback open_result_cb = base::Bind( |
| 90 &DatabaseFactoryImpl::OnOpenResult, AsWeakPtr(), begin_time, callback); |
| 91 |
| 92 #if 0 |
| 93 // TODO(dgrogan): Don't let a non-existing database be opened (and therefore |
| 94 // created) if this origin is already over quota. |
| 95 scoped_refptr<IndexedDBCallbacks> callbacks = new IndexedDBCallbacks( |
| 96 this, params.ipc_thread_id, params.ipc_callbacks_id, |
| 97 params.ipc_database_callbacks_id, host_transaction_id, params.origin); |
| 98 callbacks->SetConnectionOpenStartTime(begin_time); |
| 99 #endif |
| 100 scoped_refptr<content::IndexedDBDatabaseCallbacks> database_callbacks = |
| 101 new content::IndexedDBDatabaseCallbacks(std::move(database_handler)); |
| 102 NOTREACHED() << "Get the child process ID"; |
| 103 int child_process_id = 0; |
| 104 content::IndexedDBPendingConnection connection( |
| 105 open_result_cb, database_callbacks, |
| 106 new content::IndexedDBOpenRequestObserver(std::move(open_observer)), |
| 107 child_process_id, host_transaction_id, version); |
| 108 |
| 109 DCHECK(request_context_); |
| 110 indexed_db_context_->GetIDBFactory()->Open(name.To<base::string16>(), |
| 111 connection, request_context_, |
| 112 origin, indexed_db_path); |
| 113 } |
| 114 |
| 115 void DatabaseFactoryImpl::DeleteDatabase(const mojo::String& name, |
| 116 const url::Origin& origin) {} |
| 117 |
| 118 base::ProcessId DatabaseFactoryImpl::peer_pid() const { |
| 119 NOTREACHED() << "Implement this function"; |
| 120 return 0; |
| 121 } |
| 122 |
| 123 int64_t DatabaseFactoryImpl::HostTransactionId(int64_t transaction_id) { |
| 124 // Inject the renderer process id into the transaction id, to |
| 125 // uniquely identify this transaction, and effectively bind it to |
| 126 // the renderer that initiated it. The lower 32 bits of |
| 127 // transaction_id are guaranteed to be unique within that renderer. |
| 128 base::ProcessId pid = peer_pid(); |
| 129 DCHECK(!(transaction_id >> 32)) << "Transaction ids can only be 32 bits"; |
| 130 static_assert(sizeof(base::ProcessId) <= sizeof(int32_t), |
| 131 "Process ID must fit in 32 bits"); |
| 132 |
| 133 return transaction_id | (static_cast<uint64_t>(pid) << 32); |
| 134 } |
| 135 |
| 136 int64_t DatabaseFactoryImpl::RendererTransactionId( |
| 137 int64_t host_transaction_id) { |
| 138 DCHECK(host_transaction_id >> 32 == peer_pid()) |
| 139 << "Invalid renderer target for transaction id"; |
| 140 return host_transaction_id & 0xffffffff; |
| 141 } |
| 142 |
| 143 void DatabaseFactoryImpl::CrashRendererAndClosePipe( |
| 144 content::bad_message::BadMessageReason reason) { |
| 145 // TODO(cmumford): Replace with preferred solution once implemented. |
| 146 // https://groups.google.com/a/chromium.org/forum/#!topic/chromium-mojo/aPfAww
28ELo |
| 147 content::bad_message::ReceivedBadMessage(GetRenderProcessHost(), reason); |
| 148 |
| 149 // Looks like we need to use Binding instead of StrongBinding to get Close(). |
| 150 // binding_.Close(); |
| 151 } |
| 152 |
| 153 content::RenderProcessHost* DatabaseFactoryImpl::GetRenderProcessHost() { |
| 154 // TODO(cmumford): Need to implement this. |
| 155 NOTREACHED(); |
| 156 // return render_frame_host_->GetProcess(); |
| 157 return nullptr; |
| 158 } |
| 159 |
| 160 } // namespace indexed_db |
OLD | NEW |