Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: content/browser/indexed_db/indexed_db_dispatcher_host.cc

Issue 2727733004: [IndexedDB] Closing mojo connections when renderer quits (Closed)
Patch Set: No more raw pointers where we're not guarenteed lifetime Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 };
Reilly Grant (use Gerrit) 2017/03/11 00:31:40 DISALLOW_COPY_AND_ASSIGN(IDBThreadHelper);
dmurph 2017/03/29 18:40:24 Done.
68
37 IndexedDBDispatcherHost::IndexedDBDispatcherHost( 69 IndexedDBDispatcherHost::IndexedDBDispatcherHost(
38 int ipc_process_id, 70 int ipc_process_id,
39 net::URLRequestContextGetter* request_context_getter, 71 scoped_refptr<net::URLRequestContextGetter> request_context_getter,
40 IndexedDBContextImpl* indexed_db_context, 72 scoped_refptr<IndexedDBContextImpl> indexed_db_context,
41 ChromeBlobStorageContext* blob_storage_context) 73 scoped_refptr<ChromeBlobStorageContext> blob_storage_context)
42 : request_context_getter_(request_context_getter), 74 : indexed_db_context_(std::move(indexed_db_context)),
43 indexed_db_context_(indexed_db_context), 75 blob_storage_context_(std::move(blob_storage_context)),
44 blob_storage_context_(blob_storage_context), 76 idb_runner_(indexed_db_context_->TaskRunner()),
45 ipc_process_id_(ipc_process_id) { 77 ipc_process_id_(ipc_process_id),
78 idb_helper_(new IDBThreadHelper(ipc_process_id_,
79 std::move(request_context_getter),
80 indexed_db_context_)),
81 weak_factory_(this) {
46 DCHECK(indexed_db_context_.get()); 82 DCHECK(indexed_db_context_.get());
47 } 83 }
48 84
49 IndexedDBDispatcherHost::~IndexedDBDispatcherHost() {} 85 IndexedDBDispatcherHost::~IndexedDBDispatcherHost() {
86 idb_runner_->DeleteSoon(FROM_HERE, idb_helper_);
87 }
50 88
51 void IndexedDBDispatcherHost::AddBinding( 89 void IndexedDBDispatcherHost::AddBinding(
52 ::indexed_db::mojom::FactoryAssociatedRequest request) { 90 ::indexed_db::mojom::FactoryAssociatedRequest request) {
53 bindings_.AddBinding(this, std::move(request)); 91 bindings_.AddBinding(this, std::move(request));
54 } 92 }
55 93
94 void IndexedDBDispatcherHost::AddDatabaseBinding(
95 std::unique_ptr<::indexed_db::mojom::Database> database,
96 ::indexed_db::mojom::DatabaseAssociatedRequest request) {
97 database_bindings_.AddBinding(std::move(database), std::move(request));
98 }
99
100 void IndexedDBDispatcherHost::AddCursorBinding(
101 std::unique_ptr<::indexed_db::mojom::Cursor> cursor,
102 ::indexed_db::mojom::CursorAssociatedRequest request) {
103 cursor_bindings_.AddBinding(std::move(cursor), std::move(request));
104 }
105
56 std::string IndexedDBDispatcherHost::HoldBlobData( 106 std::string IndexedDBDispatcherHost::HoldBlobData(
57 const IndexedDBBlobInfo& blob_info) { 107 const IndexedDBBlobInfo& blob_info) {
58 DCHECK_CURRENTLY_ON(BrowserThread::IO); 108 DCHECK_CURRENTLY_ON(BrowserThread::IO);
59 std::string uuid = blob_info.uuid(); 109 std::string uuid = blob_info.uuid();
60 storage::BlobStorageContext* context = blob_storage_context_->context(); 110 storage::BlobStorageContext* context = blob_storage_context_->context();
61 std::unique_ptr<storage::BlobDataHandle> blob_data_handle; 111 std::unique_ptr<storage::BlobDataHandle> blob_data_handle;
62 if (uuid.empty()) { 112 if (uuid.empty()) {
63 uuid = base::GenerateGUID(); 113 uuid = base::GenerateGUID();
64 storage::BlobDataBuilder blob_data_builder(uuid); 114 storage::BlobDataBuilder blob_data_builder(uuid);
65 blob_data_builder.set_content_type(base::UTF16ToUTF8(blob_info.type())); 115 blob_data_builder.set_content_type(base::UTF16ToUTF8(blob_info.type()));
(...skipping 22 matching lines...) Expand all
88 return; 138 return;
89 } 139 }
90 140
91 DCHECK_GE(iter->second.second, 1); 141 DCHECK_GE(iter->second.second, 1);
92 if (iter->second.second == 1) 142 if (iter->second.second == 1)
93 blob_data_handle_map_.erase(iter); 143 blob_data_handle_map_.erase(iter);
94 else 144 else
95 --iter->second.second; 145 --iter->second.second;
96 } 146 }
97 147
148 void IndexedDBDispatcherHost::RenderProcessExited(
149 RenderProcessHost* host,
150 base::TerminationStatus status,
151 int exit_code) {
152 BrowserThread::PostTask(
153 BrowserThread::IO, FROM_HERE,
154 base::Bind(&IndexedDBDispatcherHost::InvalidateWeakPtrsAndClearBindings,
155 base::Unretained(this)));
156 }
157
98 void IndexedDBDispatcherHost::GetDatabaseNames( 158 void IndexedDBDispatcherHost::GetDatabaseNames(
99 ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, 159 ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info,
100 const url::Origin& origin) { 160 const url::Origin& origin) {
101 DCHECK_CURRENTLY_ON(BrowserThread::IO); 161 DCHECK_CURRENTLY_ON(BrowserThread::IO);
102 162
103 if (!IsValidOrigin(origin)) { 163 if (!IsValidOrigin(origin)) {
104 mojo::ReportBadMessage(kInvalidOrigin); 164 mojo::ReportBadMessage(kInvalidOrigin);
105 return; 165 return;
106 } 166 }
107 167
108 scoped_refptr<IndexedDBCallbacks> callbacks( 168 scoped_refptr<IndexedDBCallbacks> callbacks(new IndexedDBCallbacks(
109 new IndexedDBCallbacks(this, origin, std::move(callbacks_info))); 169 this->AsWeakPtr(), origin, std::move(callbacks_info), idb_runner_));
110 indexed_db_context_->TaskRunner()->PostTask( 170 idb_runner_->PostTask(
111 FROM_HERE, 171 FROM_HERE, base::Bind(&IDBThreadHelper::GetDatabaseNamesOnIDBThread,
112 base::Bind(&IndexedDBDispatcherHost::GetDatabaseNamesOnIDBThread, this, 172 base::Unretained(idb_helper_),
113 base::Passed(&callbacks), origin)); 173 base::Passed(&callbacks), origin));
114 } 174 }
115 175
116 void IndexedDBDispatcherHost::Open( 176 void IndexedDBDispatcherHost::Open(
117 ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, 177 ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info,
118 ::indexed_db::mojom::DatabaseCallbacksAssociatedPtrInfo 178 ::indexed_db::mojom::DatabaseCallbacksAssociatedPtrInfo
119 database_callbacks_info, 179 database_callbacks_info,
120 const url::Origin& origin, 180 const url::Origin& origin,
121 const base::string16& name, 181 const base::string16& name,
122 int64_t version, 182 int64_t version,
123 int64_t transaction_id) { 183 int64_t transaction_id) {
124 DCHECK_CURRENTLY_ON(BrowserThread::IO); 184 DCHECK_CURRENTLY_ON(BrowserThread::IO);
125 185
126 if (!IsValidOrigin(origin)) { 186 if (!IsValidOrigin(origin)) {
127 mojo::ReportBadMessage(kInvalidOrigin); 187 mojo::ReportBadMessage(kInvalidOrigin);
128 return; 188 return;
129 } 189 }
130 190
131 scoped_refptr<IndexedDBCallbacks> callbacks( 191 scoped_refptr<IndexedDBCallbacks> callbacks(new IndexedDBCallbacks(
132 new IndexedDBCallbacks(this, origin, std::move(callbacks_info))); 192 this->AsWeakPtr(), origin, std::move(callbacks_info), idb_runner_));
133 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks( 193 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks(
134 new IndexedDBDatabaseCallbacks(this, std::move(database_callbacks_info))); 194 new IndexedDBDatabaseCallbacks(indexed_db_context_,
135 indexed_db_context_->TaskRunner()->PostTask( 195 std::move(database_callbacks_info)));
196 idb_runner_->PostTask(
136 FROM_HERE, 197 FROM_HERE,
137 base::Bind(&IndexedDBDispatcherHost::OpenOnIDBThread, this, 198 base::Bind(&IDBThreadHelper::OpenOnIDBThread,
138 base::Passed(&callbacks), base::Passed(&database_callbacks), 199 base::Unretained(idb_helper_), base::Passed(&callbacks),
139 origin, name, version, transaction_id)); 200 base::Passed(&database_callbacks), origin, name, version,
201 transaction_id));
140 } 202 }
141 203
142 void IndexedDBDispatcherHost::DeleteDatabase( 204 void IndexedDBDispatcherHost::DeleteDatabase(
143 ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info, 205 ::indexed_db::mojom::CallbacksAssociatedPtrInfo callbacks_info,
144 const url::Origin& origin, 206 const url::Origin& origin,
145 const base::string16& name, 207 const base::string16& name,
146 bool force_close) { 208 bool force_close) {
147 DCHECK_CURRENTLY_ON(BrowserThread::IO); 209 DCHECK_CURRENTLY_ON(BrowserThread::IO);
148 210
149 if (!IsValidOrigin(origin)) { 211 if (!IsValidOrigin(origin)) {
150 mojo::ReportBadMessage(kInvalidOrigin); 212 mojo::ReportBadMessage(kInvalidOrigin);
151 return; 213 return;
152 } 214 }
153 215
154 scoped_refptr<IndexedDBCallbacks> callbacks( 216 scoped_refptr<IndexedDBCallbacks> callbacks(new IndexedDBCallbacks(
155 new IndexedDBCallbacks(this, origin, std::move(callbacks_info))); 217 this->AsWeakPtr(), origin, std::move(callbacks_info), idb_runner_));
156 indexed_db_context_->TaskRunner()->PostTask( 218 idb_runner_->PostTask(
157 FROM_HERE, base::Bind(&IndexedDBDispatcherHost::DeleteDatabaseOnIDBThread, 219 FROM_HERE,
158 this, base::Passed(&callbacks), origin, name, 220 base::Bind(&IDBThreadHelper::DeleteDatabaseOnIDBThread,
159 force_close)); 221 base::Unretained(idb_helper_), base::Passed(&callbacks),
222 origin, name, force_close));
160 } 223 }
161 224
162 void IndexedDBDispatcherHost::GetDatabaseNamesOnIDBThread( 225 void IndexedDBDispatcherHost::InvalidateWeakPtrsAndClearBindings() {
226 weak_factory_.InvalidateWeakPtrs();
227 cursor_bindings_.CloseAllBindings();
228 database_bindings_.CloseAllBindings();
229 }
230
231 void IndexedDBDispatcherHost::IDBThreadHelper::GetDatabaseNamesOnIDBThread(
163 scoped_refptr<IndexedDBCallbacks> callbacks, 232 scoped_refptr<IndexedDBCallbacks> callbacks,
164 const url::Origin& origin) { 233 const url::Origin& origin) {
165 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread()); 234 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread());
166 235
167 base::FilePath indexed_db_path = indexed_db_context_->data_path(); 236 base::FilePath indexed_db_path = indexed_db_context_->data_path();
168 context()->GetIDBFactory()->GetDatabaseNames( 237 indexed_db_context_->GetIDBFactory()->GetDatabaseNames(
169 callbacks, origin, indexed_db_path, request_context_getter_); 238 callbacks, origin, indexed_db_path, request_context_getter_);
170 } 239 }
171 240
172 void IndexedDBDispatcherHost::OpenOnIDBThread( 241 void IndexedDBDispatcherHost::IDBThreadHelper::OpenOnIDBThread(
173 scoped_refptr<IndexedDBCallbacks> callbacks, 242 scoped_refptr<IndexedDBCallbacks> callbacks,
174 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks, 243 scoped_refptr<IndexedDBDatabaseCallbacks> database_callbacks,
175 const url::Origin& origin, 244 const url::Origin& origin,
176 const base::string16& name, 245 const base::string16& name,
177 int64_t version, 246 int64_t version,
178 int64_t transaction_id) { 247 int64_t transaction_id) {
179 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread()); 248 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread());
180 249
181 base::TimeTicks begin_time = base::TimeTicks::Now(); 250 base::TimeTicks begin_time = base::TimeTicks::Now();
182 base::FilePath indexed_db_path = indexed_db_context_->data_path(); 251 base::FilePath indexed_db_path = indexed_db_context_->data_path();
183 252
184 // TODO(dgrogan): Don't let a non-existing database be opened (and therefore 253 // TODO(dgrogan): Don't let a non-existing database be opened (and therefore
185 // created) if this origin is already over quota. 254 // created) if this origin is already over quota.
186 callbacks->SetConnectionOpenStartTime(begin_time); 255 callbacks->SetConnectionOpenStartTime(begin_time);
187 std::unique_ptr<IndexedDBPendingConnection> connection = 256 std::unique_ptr<IndexedDBPendingConnection> connection =
188 base::MakeUnique<IndexedDBPendingConnection>( 257 base::MakeUnique<IndexedDBPendingConnection>(
189 callbacks, database_callbacks, ipc_process_id_, transaction_id, 258 callbacks, database_callbacks, ipc_process_id_, transaction_id,
190 version); 259 version);
191 DCHECK(request_context_getter_); 260 DCHECK(request_context_getter_);
192 context()->GetIDBFactory()->Open(name, std::move(connection), 261 indexed_db_context_->GetIDBFactory()->Open(name, std::move(connection),
193 request_context_getter_, origin, 262 request_context_getter_, origin,
194 indexed_db_path); 263 indexed_db_path);
195 } 264 }
196 265
197 void IndexedDBDispatcherHost::DeleteDatabaseOnIDBThread( 266 void IndexedDBDispatcherHost::IDBThreadHelper::DeleteDatabaseOnIDBThread(
198 scoped_refptr<IndexedDBCallbacks> callbacks, 267 scoped_refptr<IndexedDBCallbacks> callbacks,
199 const url::Origin& origin, 268 const url::Origin& origin,
200 const base::string16& name, 269 const base::string16& name,
201 bool force_close) { 270 bool force_close) {
202 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread()); 271 DCHECK(indexed_db_context_->TaskRunner()->RunsTasksOnCurrentThread());
203 272
204 base::FilePath indexed_db_path = indexed_db_context_->data_path(); 273 base::FilePath indexed_db_path = indexed_db_context_->data_path();
205 DCHECK(request_context_getter_); 274 DCHECK(request_context_getter_);
206 context()->GetIDBFactory()->DeleteDatabase( 275 indexed_db_context_->GetIDBFactory()->DeleteDatabase(
207 name, request_context_getter_, callbacks, origin, indexed_db_path, 276 name, request_context_getter_, callbacks, origin, indexed_db_path,
208 force_close); 277 force_close);
209 } 278 }
210 279
211 } // namespace content 280 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698