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 <string> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/strings/utf_string_conversions.h" |
| 11 #include "content/browser/indexed_db/indexed_db_callbacks.h" |
| 12 #include "content/browser/indexed_db/indexed_db_change_handler_impl.h" |
| 13 #include "content/browser/indexed_db/indexed_db_connection.h" |
| 14 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
| 15 #include "content/browser/indexed_db/indexed_db_database_error.h" |
| 16 #include "content/browser/indexed_db/indexed_db_open_request_observer_impl.h" |
| 17 #include "content/browser/indexed_db/indexed_db_pending_connection.h" |
| 18 #include "content/public/browser/browser_context.h" |
| 19 #include "content/public/browser/indexed_db_context.h" |
| 20 #include "content/public/browser/storage_partition.h" |
| 21 #include "mojo/common/common_type_converters.h" |
| 22 #include "mojo/public/cpp/bindings/binding.h" |
| 23 #include "net/url_request/url_request_context_getter.h" |
| 24 #include "services/shell/public/cpp/interface_registry.h" |
| 25 |
| 26 using indexed_db::mojom::DatabaseFactory; |
| 27 using indexed_db::mojom::DatabaseObserverPtr; |
| 28 using indexed_db::mojom::DatabaseOpenData; |
| 29 using indexed_db::mojom::DatabaseOpenDataPtr; |
| 30 using indexed_db::mojom::OpenRequestObserverPtr; |
| 31 using indexed_db::mojom::OpenResult; |
| 32 using indexed_db::mojom::OpenResultPtr; |
| 33 |
| 34 namespace { |
| 35 |
| 36 bool IsValidOrigin(const url::Origin& origin) { |
| 37 return !origin.unique(); |
| 38 } |
| 39 |
| 40 } // namespace |
| 41 |
| 42 namespace content { |
| 43 |
| 44 DatabaseFactoryImpl::DatabaseFactoryImpl( |
| 45 IndexedDBContextImpl* indexed_db_context, |
| 46 net::URLRequestContextGetter* url_request_context_getter, |
| 47 int render_process_host_id) |
| 48 : render_process_host_id_(render_process_host_id), |
| 49 indexed_db_context_(indexed_db_context), |
| 50 request_context_getter_(url_request_context_getter) {} |
| 51 |
| 52 DatabaseFactoryImpl::~DatabaseFactoryImpl() = default; |
| 53 |
| 54 std::unique_ptr<DatabaseFactoryImpl> DatabaseFactoryImpl::Create( |
| 55 IndexedDBContextImpl* indexed_db_context, |
| 56 net::URLRequestContextGetter* url_request_context, |
| 57 int render_process_host_id) { |
| 58 return base::WrapUnique(new DatabaseFactoryImpl( |
| 59 indexed_db_context, url_request_context, render_process_host_id)); |
| 60 } |
| 61 |
| 62 void DatabaseFactoryImpl::GetDatabaseNames(const url::Origin& origin) {} |
| 63 |
| 64 void DatabaseFactoryImpl::OnOpenResult( |
| 65 const base::TimeTicks& begin_time, |
| 66 const OpenCallback& mojo_callback, |
| 67 std::unique_ptr<IndexedDBConnection> connection, |
| 68 const IndexedDBDatabaseMetadata& metadata, |
| 69 const IndexedDBDatabaseError& error) { |
| 70 OpenResultPtr result = OpenResult::New(); |
| 71 if (connection) { |
| 72 DCHECK_EQ(error.code(), 0); |
| 73 ::indexed_db::mojom::DatabasePtr interface; |
| 74 connection->Bind(GetProxy(&interface)); |
| 75 connections_.insert(std::move(connection)); |
| 76 DatabaseOpenDataPtr open_data = DatabaseOpenData::New(); |
| 77 open_data->database = std::move(interface); |
| 78 open_data->metadata = metadata; |
| 79 result->set_data(std::move(open_data)); |
| 80 } else { |
| 81 DCHECK_NE(error.code(), 0); |
| 82 result->set_error(error); |
| 83 } |
| 84 mojo_callback.Run(std::move(result)); |
| 85 } |
| 86 |
| 87 void DatabaseFactoryImpl::Open(const std::string& name, |
| 88 int64_t version, |
| 89 int64_t transaction_id, |
| 90 const url::Origin& origin, |
| 91 OpenRequestObserverPtr open_observer, |
| 92 DatabaseObserverPtr database_observer, |
| 93 const OpenCallback& callback) { |
| 94 if (!IsValidOrigin(origin)) { |
| 95 CrashRendererAndClosePipe(bad_message::IDBDH_INVALID_ORIGIN); |
| 96 return; |
| 97 } |
| 98 |
| 99 // TODO(cmumford): Don't let a non-existing database be opened (and therefore |
| 100 // created) if this origin is already over quota. |
| 101 scoped_refptr<IndexedDBChangeHandler> change_handler = |
| 102 new IndexedDBChangeHandlerImpl(std::move(database_observer)); |
| 103 |
| 104 OpenResultCallback open_result_cb = |
| 105 base::Bind(&DatabaseFactoryImpl::OnOpenResult, AsWeakPtr(), |
| 106 base::TimeTicks::Now(), std::move(callback)); |
| 107 |
| 108 std::unique_ptr<IndexedDBPendingConnection> connection = |
| 109 base::WrapUnique(new IndexedDBPendingConnection( |
| 110 open_result_cb, change_handler, |
| 111 new IndexedDBOpenRequestObserverImpl(std::move(open_observer)), |
| 112 render_process_host_id_, transaction_id, version, origin)); |
| 113 |
| 114 DCHECK(request_context_getter_); |
| 115 indexed_db_context_->GetIDBFactory()->Open( |
| 116 base::UTF8ToUTF16(name), std::move(connection), request_context_getter_, |
| 117 origin, indexed_db_context_->data_path()); |
| 118 } |
| 119 |
| 120 void DatabaseFactoryImpl::DeleteDatabase(const std::string& name, |
| 121 const url::Origin& origin) {} |
| 122 |
| 123 void DatabaseFactoryImpl::CrashRendererAndClosePipe( |
| 124 bad_message::BadMessageReason reason) { |
| 125 // TODO(cmumford): Replace with preferred solution once implemented. |
| 126 // https://groups.google.com/a/chromium.org/forum/#!topic/chromium-mojo/aPfAww
28ELo |
| 127 bad_message::ReceivedBadMessage(GetRenderProcessHost(), reason); |
| 128 |
| 129 // Looks like we need to use Binding instead of StrongBinding to get Close(). |
| 130 // binding_.Close(); |
| 131 } |
| 132 |
| 133 RenderProcessHost* DatabaseFactoryImpl::GetRenderProcessHost() { |
| 134 // TODO(cmumford): Need to implement this. |
| 135 NOTREACHED(); |
| 136 // return render_frame_host_->GetProcess(); |
| 137 return nullptr; |
| 138 } |
| 139 |
| 140 void DatabaseFactoryImpl::Bind( |
| 141 ::indexed_db::mojom::DatabaseFactoryRequest request) { |
| 142 binding_.AddBinding(this, std::move(request)); |
| 143 } |
| 144 |
| 145 void DatabaseFactoryImpl::AddMojoServiceOnIDBThread( |
| 146 shell::InterfaceRegistry* registry) { |
| 147 registry->AddInterface( |
| 148 base::Bind(&DatabaseFactoryImpl::Bind, base::Unretained(this))); |
| 149 } |
| 150 |
| 151 void DatabaseFactoryImpl::AddMojoService(shell::InterfaceRegistry* registry) { |
| 152 indexed_db_context_->TaskRunner()->PostTask( |
| 153 FROM_HERE, |
| 154 base::Bind(&DatabaseFactoryImpl::AddMojoServiceOnIDBThread, |
| 155 base::Unretained(this), base::Unretained(registry))); |
| 156 } |
| 157 |
| 158 } // namespace content |
OLD | NEW |