OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/indexed_db/indexed_db_dispatcher_host.h" | 5 #include "content/browser/indexed_db/indexed_db_dispatcher_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/guid.h" | 9 #include "base/guid.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
11 #include "base/process/process.h" | 11 #include "base/process/process.h" |
| 12 #include "base/sequenced_task_runner.h" |
12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
14 #include "content/browser/indexed_db/indexed_db_callbacks.h" | 15 #include "content/browser/indexed_db/indexed_db_callbacks.h" |
15 #include "content/browser/indexed_db/indexed_db_connection.h" | 16 #include "content/browser/indexed_db/indexed_db_connection.h" |
16 #include "content/browser/indexed_db/indexed_db_context_impl.h" | 17 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
17 #include "content/browser/indexed_db/indexed_db_database_callbacks.h" | 18 #include "content/browser/indexed_db/indexed_db_database_callbacks.h" |
18 #include "content/browser/indexed_db/indexed_db_pending_connection.h" | 19 #include "content/browser/indexed_db/indexed_db_pending_connection.h" |
19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
20 #include "storage/browser/blob/blob_data_builder.h" | 21 #include "storage/browser/blob/blob_data_builder.h" |
21 #include "storage/browser/blob/blob_storage_context.h" | 22 #include "storage/browser/blob/blob_storage_context.h" |
22 #include "storage/browser/database/database_util.h" | 23 #include "storage/browser/database/database_util.h" |
23 #include "url/origin.h" | 24 #include "url/origin.h" |
24 | 25 |
25 namespace content { | 26 namespace content { |
26 | 27 |
27 namespace { | 28 namespace { |
28 | 29 |
29 const char kInvalidOrigin[] = "Origin is invalid"; | 30 const char kInvalidOrigin[] = "Origin is invalid"; |
30 | 31 |
31 bool IsValidOrigin(const url::Origin& origin) { | 32 bool IsValidOrigin(const url::Origin& origin) { |
32 return !origin.unique(); | 33 return !origin.unique(); |
33 } | 34 } |
34 | 35 |
35 } // namespace | 36 } // namespace |
36 | 37 |
| 38 class IndexedDBDispatcherHost::IDBThreadHelper { |
| 39 public: |
| 40 IDBThreadHelper( |
| 41 int ipc_process_id, |
| 42 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 43 scoped_refptr<IndexedDBContextImpl> indexed_db_context) |
| 44 : ipc_process_id_(ipc_process_id), |
| 45 request_context_getter_(std::move(request_context_getter)), |
| 46 indexed_db_context_(std::move(indexed_db_context)) {} |
| 47 ~IDBThreadHelper() {} |
| 48 |
| 49 void GetDatabaseNamesOnIDBThread(scoped_refptr<IndexedDBCallbacks> callbacks, |
| 50 const url::Origin& origin); |
| 51 void OpenOnIDBThread( |
| 52 scoped_refptr<IndexedDBCallbacks> callbacks, |
| 53 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks, |
| 54 const url::Origin& origin, |
| 55 const base::string16& name, |
| 56 int64_t version, |
| 57 int64_t transaction_id); |
| 58 void DeleteDatabaseOnIDBThread(scoped_refptr<IndexedDBCallbacks> callbacks, |
| 59 const url::Origin& origin, |
| 60 const base::string16& name, |
| 61 bool force_close); |
| 62 |
| 63 private: |
| 64 const int ipc_process_id_; |
| 65 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 66 scoped_refptr<IndexedDBContextImpl> indexed_db_context_; |
| 67 |
| 68 DISALLOW_COPY_AND_ASSIGN(IDBThreadHelper); |
| 69 }; |
| 70 |
37 IndexedDBDispatcherHost::IndexedDBDispatcherHost( | 71 IndexedDBDispatcherHost::IndexedDBDispatcherHost( |
38 int ipc_process_id, | 72 int ipc_process_id, |
39 net::URLRequestContextGetter* request_context_getter, | 73 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
40 IndexedDBContextImpl* indexed_db_context, | 74 scoped_refptr<IndexedDBContextImpl> indexed_db_context, |
41 ChromeBlobStorageContext* blob_storage_context) | 75 scoped_refptr<ChromeBlobStorageContext> blob_storage_context) |
42 : request_context_getter_(request_context_getter), | 76 : indexed_db_context_(std::move(indexed_db_context)), |
43 indexed_db_context_(indexed_db_context), | 77 blob_storage_context_(std::move(blob_storage_context)), |
44 blob_storage_context_(blob_storage_context), | 78 idb_runner_(indexed_db_context_->TaskRunner()), |
45 ipc_process_id_(ipc_process_id) { | 79 ipc_process_id_(ipc_process_id), |
| 80 idb_helper_(new IDBThreadHelper(ipc_process_id_, |
| 81 std::move(request_context_getter), |
| 82 indexed_db_context_)), |
| 83 weak_factory_(this) { |
46 DCHECK(indexed_db_context_.get()); | 84 DCHECK(indexed_db_context_.get()); |
47 } | 85 } |
48 | 86 |
49 IndexedDBDispatcherHost::~IndexedDBDispatcherHost() {} | 87 IndexedDBDispatcherHost::~IndexedDBDispatcherHost() { |
| 88 idb_runner_->DeleteSoon(FROM_HERE, idb_helper_); |
| 89 } |
50 | 90 |
51 void IndexedDBDispatcherHost::AddBinding( | 91 void IndexedDBDispatcherHost::AddBinding( |
52 ::indexed_db::mojom::FactoryAssociatedRequest request) { | 92 ::indexed_db::mojom::FactoryAssociatedRequest request) { |
53 bindings_.AddBinding(this, std::move(request)); | 93 bindings_.AddBinding(this, std::move(request)); |
54 } | 94 } |
55 | 95 |
| 96 void IndexedDBDispatcherHost::AddDatabaseBinding( |
| 97 std::unique_ptr<::indexed_db::mojom::Database> database, |
| 98 ::indexed_db::mojom::DatabaseAssociatedRequest request) { |
| 99 database_bindings_.AddBinding(std::move(database), std::move(request)); |
| 100 } |
| 101 |
| 102 void IndexedDBDispatcherHost::AddCursorBinding( |
| 103 std::unique_ptr<::indexed_db::mojom::Cursor> cursor, |
| 104 ::indexed_db::mojom::CursorAssociatedRequest request) { |
| 105 cursor_bindings_.AddBinding(std::move(cursor), std::move(request)); |
| 106 } |
| 107 |
56 std::string IndexedDBDispatcherHost::HoldBlobData( | 108 std::string IndexedDBDispatcherHost::HoldBlobData( |
57 const IndexedDBBlobInfo& blob_info) { | 109 const IndexedDBBlobInfo& blob_info) { |
58 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 110 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
59 std::string uuid = blob_info.uuid(); | 111 std::string uuid = blob_info.uuid(); |
60 storage::BlobStorageContext* context = blob_storage_context_->context(); | 112 storage::BlobStorageContext* context = blob_storage_context_->context(); |
61 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; | 113 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; |
62 if (uuid.empty()) { | 114 if (uuid.empty()) { |
63 uuid = base::GenerateGUID(); | 115 uuid = base::GenerateGUID(); |
64 storage::BlobDataBuilder blob_data_builder(uuid); | 116 storage::BlobDataBuilder blob_data_builder(uuid); |
65 blob_data_builder.set_content_type(base::UTF16ToUTF8(blob_info.type())); | 117 blob_data_builder.set_content_type(base::UTF16ToUTF8(blob_info.type())); |
(...skipping 22 matching lines...) Expand all Loading... |
88 return; | 140 return; |
89 } | 141 } |
90 | 142 |
91 DCHECK_GE(iter->second.second, 1); | 143 DCHECK_GE(iter->second.second, 1); |
92 if (iter->second.second == 1) | 144 if (iter->second.second == 1) |
93 blob_data_handle_map_.erase(iter); | 145 blob_data_handle_map_.erase(iter); |
94 else | 146 else |
95 --iter->second.second; | 147 --iter->second.second; |
96 } | 148 } |
97 | 149 |
| 150 void IndexedDBDispatcherHost::RenderProcessExited( |
| 151 RenderProcessHost* host, |
| 152 base::TerminationStatus status, |
| 153 int exit_code) { |
| 154 BrowserThread::PostTask( |
| 155 BrowserThread::IO, FROM_HERE, |
| 156 base::Bind(&IndexedDBDispatcherHost::InvalidateWeakPtrsAndClearBindings, |
| 157 base::Unretained(this))); |
| 158 } |
| 159 |
98 void IndexedDBDispatcherHost::GetDatabaseNames( | 160 void IndexedDBDispatcherHost::GetDatabaseNames( |
99 ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, | 161 ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, |
100 const url::Origin& origin) { | 162 const url::Origin& origin) { |
101 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 163 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
102 | 164 |
103 if (!IsValidOrigin(origin)) { | 165 if (!IsValidOrigin(origin)) { |
104 mojo::ReportBadMessage(kInvalidOrigin); | 166 mojo::ReportBadMessage(kInvalidOrigin); |
105 return; | 167 return; |
106 } | 168 } |
107 | 169 |
108 scoped_refptr<IndexedDBCallbacks> callbacks( | 170 scoped_refptr<IndexedDBCallbacks> callbacks(new IndexedDBCallbacks( |
109 new IndexedDBCallbacks(this, origin, std::move(callbacks_info))); | 171 this->AsWeakPtr(), origin, std::move(callbacks_info), idb_runner_)); |
110 indexed_db_context_->TaskRunner()->PostTask( | 172 idb_runner_->PostTask( |
111 FROM_HERE, | 173 FROM_HERE, base::Bind(&IDBThreadHelper::GetDatabaseNamesOnIDBThread, |
112 base::Bind(&IndexedDBDispatcherHost::GetDatabaseNamesOnIDBThread, this, | 174 base::Unretained(idb_helper_), |
113 base::Passed(&callbacks), origin)); | 175 base::Passed(&callbacks), origin)); |
114 } | 176 } |
115 | 177 |
116 void IndexedDBDispatcherHost::Open( | 178 void IndexedDBDispatcherHost::Open( |
117 ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, | 179 ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, |
118 ::indexed_db::mojom::DatabaseCallbacksAssociatedPtrInfo | 180 ::indexed_db::mojom::DatabaseCallbacksAssociatedPtrInfo |
119 database_callbacks_info, | 181 database_callbacks_info, |
120 const url::Origin& origin, | 182 const url::Origin& origin, |
121 const base::string16& name, | 183 const base::string16& name, |
122 int64_t version, | 184 int64_t version, |
123 int64_t transaction_id) { | 185 int64_t transaction_id) { |
124 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 186 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
125 | 187 |
126 if (!IsValidOrigin(origin)) { | 188 if (!IsValidOrigin(origin)) { |
127 mojo::ReportBadMessage(kInvalidOrigin); | 189 mojo::ReportBadMessage(kInvalidOrigin); |
128 return; | 190 return; |
129 } | 191 } |
130 | 192 |
131 scoped_refptr<IndexedDBCallbacks> callbacks( | 193 scoped_refptr<IndexedDBCallbacks> callbacks(new IndexedDBCallbacks( |
132 new IndexedDBCallbacks(this, origin, std::move(callbacks_info))); | 194 this->AsWeakPtr(), origin, std::move(callbacks_info), idb_runner_)); |
133 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks( | 195 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks( |
134 new IndexedDBDatabaseCallbacks(this, std::move(database_callbacks_info))); | 196 new IndexedDBDatabaseCallbacks(indexed_db_context_, |
135 indexed_db_context_->TaskRunner()->PostTask( | 197 std::move(database_callbacks_info))); |
| 198 idb_runner_->PostTask( |
136 FROM_HERE, | 199 FROM_HERE, |
137 base::Bind(&IndexedDBDispatcherHost::OpenOnIDBThread, this, | 200 base::Bind(&IDBThreadHelper::OpenOnIDBThread, |
138 base::Passed(&callbacks), base::Passed(&database_callbacks), | 201 base::Unretained(idb_helper_), base::Passed(&callbacks), |
139 origin, name, version, transaction_id)); | 202 base::Passed(&database_callbacks), origin, name, version, |
| 203 transaction_id)); |
140 } | 204 } |
141 | 205 |
142 void IndexedDBDispatcherHost::DeleteDatabase( | 206 void IndexedDBDispatcherHost::DeleteDatabase( |
143 ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, | 207 ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, |
144 const url::Origin& origin, | 208 const url::Origin& origin, |
145 const base::string16& name, | 209 const base::string16& name, |
146 bool force_close) { | 210 bool force_close) { |
147 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 211 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
148 | 212 |
149 if (!IsValidOrigin(origin)) { | 213 if (!IsValidOrigin(origin)) { |
150 mojo::ReportBadMessage(kInvalidOrigin); | 214 mojo::ReportBadMessage(kInvalidOrigin); |
151 return; | 215 return; |
152 } | 216 } |
153 | 217 |
154 scoped_refptr<IndexedDBCallbacks> callbacks( | 218 scoped_refptr<IndexedDBCallbacks> callbacks(new IndexedDBCallbacks( |
155 new IndexedDBCallbacks(this, origin, std::move(callbacks_info))); | 219 this->AsWeakPtr(), origin, std::move(callbacks_info), idb_runner_)); |
156 indexed_db_context_->TaskRunner()->PostTask( | 220 idb_runner_->PostTask( |
157 FROM_HERE, base::Bind(&IndexedDBDispatcherHost::DeleteDatabaseOnIDBThread, | 221 FROM_HERE, |
158 this, base::Passed(&callbacks), origin, name, | 222 base::Bind(&IDBThreadHelper::DeleteDatabaseOnIDBThread, |
159 force_close)); | 223 base::Unretained(idb_helper_), base::Passed(&callbacks), |
| 224 origin, name, force_close)); |
160 } | 225 } |
161 | 226 |
162 void IndexedDBDispatcherHost::GetDatabaseNamesOnIDBThread( | 227 void IndexedDBDispatcherHost::InvalidateWeakPtrsAndClearBindings() { |
| 228 weak_factory_.InvalidateWeakPtrs(); |
| 229 cursor_bindings_.CloseAllBindings(); |
| 230 database_bindings_.CloseAllBindings(); |
| 231 } |
| 232 |
| 233 void IndexedDBDispatcherHost::IDBThreadHelper::GetDatabaseNamesOnIDBThread( |
163 scoped_refptr<IndexedDBCallbacks> callbacks, | 234 scoped_refptr<IndexedDBCallbacks> callbacks, |
164 const url::Origin& origin) { | 235 const url::Origin& origin) { |
165 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread()); | 236 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread()); |
166 | 237 |
167 base::FilePath indexed_db_path = indexed_db_context_->data_path(); | 238 base::FilePath indexed_db_path = indexed_db_context_->data_path(); |
168 context()->GetIDBFactory()->GetDatabaseNames( | 239 indexed_db_context_->GetIDBFactory()->GetDatabaseNames( |
169 callbacks, origin, indexed_db_path, request_context_getter_); | 240 callbacks, origin, indexed_db_path, request_context_getter_); |
170 } | 241 } |
171 | 242 |
172 void IndexedDBDispatcherHost::OpenOnIDBThread( | 243 void IndexedDBDispatcherHost::IDBThreadHelper::OpenOnIDBThread( |
173 scoped_refptr<IndexedDBCallbacks> callbacks, | 244 scoped_refptr<IndexedDBCallbacks> callbacks, |
174 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks, | 245 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks, |
175 const url::Origin& origin, | 246 const url::Origin& origin, |
176 const base::string16& name, | 247 const base::string16& name, |
177 int64_t version, | 248 int64_t version, |
178 int64_t transaction_id) { | 249 int64_t transaction_id) { |
179 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread()); | 250 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread()); |
180 | 251 |
181 base::TimeTicks begin_time = base::TimeTicks::Now(); | 252 base::TimeTicks begin_time = base::TimeTicks::Now(); |
182 base::FilePath indexed_db_path = indexed_db_context_->data_path(); | 253 base::FilePath indexed_db_path = indexed_db_context_->data_path(); |
183 | 254 |
184 // TODO(dgrogan): Don't let a non-existing database be opened (and therefore | 255 // TODO(dgrogan): Don't let a non-existing database be opened (and therefore |
185 // created) if this origin is already over quota. | 256 // created) if this origin is already over quota. |
186 callbacks->SetConnectionOpenStartTime(begin_time); | 257 callbacks->SetConnectionOpenStartTime(begin_time); |
187 std::unique_ptr<IndexedDBPendingConnection> connection = | 258 std::unique_ptr<IndexedDBPendingConnection> connection = |
188 base::MakeUnique<IndexedDBPendingConnection>( | 259 base::MakeUnique<IndexedDBPendingConnection>( |
189 callbacks, database_callbacks, ipc_process_id_, transaction_id, | 260 callbacks, database_callbacks, ipc_process_id_, transaction_id, |
190 version); | 261 version); |
191 DCHECK(request_context_getter_); | 262 DCHECK(request_context_getter_); |
192 context()->GetIDBFactory()->Open(name, std::move(connection), | 263 indexed_db_context_->GetIDBFactory()->Open(name, std::move(connection), |
193 request_context_getter_, origin, | 264 request_context_getter_, origin, |
194 indexed_db_path); | 265 indexed_db_path); |
195 } | 266 } |
196 | 267 |
197 void IndexedDBDispatcherHost::DeleteDatabaseOnIDBThread( | 268 void IndexedDBDispatcherHost::IDBThreadHelper::DeleteDatabaseOnIDBThread( |
198 scoped_refptr<IndexedDBCallbacks> callbacks, | 269 scoped_refptr<IndexedDBCallbacks> callbacks, |
199 const url::Origin& origin, | 270 const url::Origin& origin, |
200 const base::string16& name, | 271 const base::string16& name, |
201 bool force_close) { | 272 bool force_close) { |
202 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread()); | 273 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread()); |
203 | 274 |
204 base::FilePath indexed_db_path = indexed_db_context_->data_path(); | 275 base::FilePath indexed_db_path = indexed_db_context_->data_path(); |
205 DCHECK(request_context_getter_); | 276 DCHECK(request_context_getter_); |
206 context()->GetIDBFactory()->DeleteDatabase( | 277 indexed_db_context_->GetIDBFactory()->DeleteDatabase( |
207 name, request_context_getter_, callbacks, origin, indexed_db_path, | 278 name, request_context_getter_, callbacks, origin, indexed_db_path, |
208 force_close); | 279 force_close); |
209 } | 280 } |
210 | 281 |
211 } // namespace content | 282 } // namespace content |
OLD | NEW |