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

Side by Side Diff: content/browser/dom_storage/local_storage_context_mojo.cc

Issue 2861473002: Clear up session only storage on localstorage shutdown (Closed)
Patch Set: rebase Created 3 years, 7 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 2016 The Chromium Authors. All rights reserved. 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 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/dom_storage/local_storage_context_mojo.h" 5 #include "content/browser/dom_storage/local_storage_context_mojo.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "base/metrics/histogram_macros.h" 8 #include "base/metrics/histogram_macros.h"
9 #include "base/strings/string_number_conversions.h" 9 #include "base/strings/string_number_conversions.h"
10 #include "components/leveldb/public/cpp/util.h" 10 #include "components/leveldb/public/cpp/util.h"
11 #include "components/leveldb/public/interfaces/leveldb.mojom.h" 11 #include "components/leveldb/public/interfaces/leveldb.mojom.h"
12 #include "content/browser/dom_storage/dom_storage_area.h" 12 #include "content/browser/dom_storage/dom_storage_area.h"
13 #include "content/browser/dom_storage/dom_storage_database.h" 13 #include "content/browser/dom_storage/dom_storage_database.h"
14 #include "content/browser/dom_storage/dom_storage_task_runner.h" 14 #include "content/browser/dom_storage/dom_storage_task_runner.h"
15 #include "content/browser/dom_storage/local_storage_database.pb.h" 15 #include "content/browser/dom_storage/local_storage_database.pb.h"
16 #include "content/browser/leveldb_wrapper_impl.h" 16 #include "content/browser/leveldb_wrapper_impl.h"
17 #include "content/common/dom_storage/dom_storage_types.h" 17 #include "content/common/dom_storage/dom_storage_types.h"
18 #include "content/public/browser/local_storage_usage_info.h" 18 #include "content/public/browser/local_storage_usage_info.h"
19 #include "services/file/public/interfaces/constants.mojom.h" 19 #include "services/file/public/interfaces/constants.mojom.h"
20 #include "services/service_manager/public/cpp/connector.h" 20 #include "services/service_manager/public/cpp/connector.h"
21 #include "sql/connection.h" 21 #include "sql/connection.h"
22 #include "storage/browser/quota/special_storage_policy.h"
22 #include "third_party/leveldatabase/env_chromium.h" 23 #include "third_party/leveldatabase/env_chromium.h"
23 24
24 namespace content { 25 namespace content {
25 26
26 // LevelDB database schema 27 // LevelDB database schema
27 // ======================= 28 // =======================
28 // 29 //
29 // Version 1 (in sorted order): 30 // Version 1 (in sorted order):
30 // key: "VERSION" 31 // key: "VERSION"
31 // value: "1" 32 // value: "1"
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 // called and need access to the LevelDBWrapperImpl instance. The unique_ptr 230 // called and need access to the LevelDBWrapperImpl instance. The unique_ptr
230 // could already be null, but this field should still be valid. 231 // could already be null, but this field should still be valid.
231 LevelDBWrapperImpl* level_db_wrapper_ptr_; 232 LevelDBWrapperImpl* level_db_wrapper_ptr_;
232 bool deleted_old_data_ = false; 233 bool deleted_old_data_ = false;
233 }; 234 };
234 235
235 LocalStorageContextMojo::LocalStorageContextMojo( 236 LocalStorageContextMojo::LocalStorageContextMojo(
236 service_manager::Connector* connector, 237 service_manager::Connector* connector,
237 scoped_refptr<DOMStorageTaskRunner> task_runner, 238 scoped_refptr<DOMStorageTaskRunner> task_runner,
238 const base::FilePath& old_localstorage_path, 239 const base::FilePath& old_localstorage_path,
239 const base::FilePath& subdirectory) 240 const base::FilePath& subdirectory,
240 : connector_(connector), 241 storage::SpecialStoragePolicy* special_storage_policy)
242 : connector_(connector ? connector->Clone() : nullptr),
241 subdirectory_(subdirectory), 243 subdirectory_(subdirectory),
244 special_storage_policy_(special_storage_policy),
242 task_runner_(std::move(task_runner)), 245 task_runner_(std::move(task_runner)),
243 old_localstorage_path_(old_localstorage_path), 246 old_localstorage_path_(old_localstorage_path),
244 weak_ptr_factory_(this) {} 247 weak_ptr_factory_(this) {}
245 248
246 LocalStorageContextMojo::~LocalStorageContextMojo() {}
247
248 void LocalStorageContextMojo::OpenLocalStorage( 249 void LocalStorageContextMojo::OpenLocalStorage(
249 const url::Origin& origin, 250 const url::Origin& origin,
250 mojom::LevelDBWrapperRequest request) { 251 mojom::LevelDBWrapperRequest request) {
251 RunWhenConnected(base::BindOnce(&LocalStorageContextMojo::BindLocalStorage, 252 RunWhenConnected(base::BindOnce(&LocalStorageContextMojo::BindLocalStorage,
252 weak_ptr_factory_.GetWeakPtr(), origin, 253 weak_ptr_factory_.GetWeakPtr(), origin,
253 std::move(request))); 254 std::move(request)));
254 } 255 }
255 256
256 void LocalStorageContextMojo::GetStorageUsage( 257 void LocalStorageContextMojo::GetStorageUsage(
257 GetStorageUsageCallback callback) { 258 GetStorageUsageCallback callback) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 void LocalStorageContextMojo::Flush() { 292 void LocalStorageContextMojo::Flush() {
292 if (connection_state_ != CONNECTION_FINISHED) { 293 if (connection_state_ != CONNECTION_FINISHED) {
293 RunWhenConnected(base::BindOnce(&LocalStorageContextMojo::Flush, 294 RunWhenConnected(base::BindOnce(&LocalStorageContextMojo::Flush,
294 weak_ptr_factory_.GetWeakPtr())); 295 weak_ptr_factory_.GetWeakPtr()));
295 return; 296 return;
296 } 297 }
297 for (const auto& it : level_db_wrappers_) 298 for (const auto& it : level_db_wrappers_)
298 it.second->level_db_wrapper()->ScheduleImmediateCommit(); 299 it.second->level_db_wrapper()->ScheduleImmediateCommit();
299 } 300 }
300 301
302 void LocalStorageContextMojo::ShutdownAndDelete() {
303 DCHECK_NE(connection_state_, CONNECTION_SHUTDOWN);
304
305 // Nothing to do if no connection to the database was ever finished.
306 if (connection_state_ != CONNECTION_FINISHED) {
307 connection_state_ = CONNECTION_SHUTDOWN;
308 OnShutdownCommitComplete(leveldb::mojom::DatabaseError::OK);
309 return;
310 }
311
312 connection_state_ = CONNECTION_SHUTDOWN;
313
314 // Flush any uncommitted data.
315 for (const auto& it : level_db_wrappers_)
316 it.second->level_db_wrapper()->ScheduleImmediateCommit();
317
318 // Respect the content policy settings about what to
319 // keep and what to discard.
320 if (force_keep_session_state_) {
321 OnShutdownCommitComplete(leveldb::mojom::DatabaseError::OK);
322 return; // Keep everything.
323 }
324
325 bool has_session_only_origins =
326 special_storage_policy_.get() &&
327 special_storage_policy_->HasSessionOnlyOrigins();
328
329 if (has_session_only_origins) {
330 RetrieveStorageUsage(
331 base::BindOnce(&LocalStorageContextMojo::OnGotStorageUsageForShutdown,
332 base::Unretained(this)));
333 } else {
334 OnShutdownCommitComplete(leveldb::mojom::DatabaseError::OK);
335 }
336 }
337
301 void LocalStorageContextMojo::PurgeMemory() { 338 void LocalStorageContextMojo::PurgeMemory() {
302 for (const auto& it : level_db_wrappers_) 339 for (const auto& it : level_db_wrappers_)
303 it.second->level_db_wrapper()->PurgeMemory(); 340 it.second->level_db_wrapper()->PurgeMemory();
304 } 341 }
305 342
306 leveldb::mojom::LevelDBDatabaseAssociatedRequest 343 leveldb::mojom::LevelDBDatabaseAssociatedRequest
307 LocalStorageContextMojo::DatabaseRequestForTesting() { 344 LocalStorageContextMojo::DatabaseRequestForTesting() {
308 DCHECK_EQ(connection_state_, NO_CONNECTION); 345 DCHECK_EQ(connection_state_, NO_CONNECTION);
309 connection_state_ = CONNECTION_IN_PROGRESS; 346 connection_state_ = CONNECTION_IN_PROGRESS;
310 leveldb::mojom::LevelDBDatabaseAssociatedRequest request = 347 leveldb::mojom::LevelDBDatabaseAssociatedRequest request =
311 MakeIsolatedRequest(&database_); 348 MakeIsolatedRequest(&database_);
312 OnDatabaseOpened(true, leveldb::mojom::DatabaseError::OK); 349 OnDatabaseOpened(true, leveldb::mojom::DatabaseError::OK);
313 return request; 350 return request;
314 } 351 }
315 352
316 // static 353 // static
317 std::vector<uint8_t> LocalStorageContextMojo::MigrateString( 354 std::vector<uint8_t> LocalStorageContextMojo::MigrateString(
318 const base::string16& input) { 355 const base::string16& input) {
319 static const uint8_t kUTF16Format = 0; 356 static const uint8_t kUTF16Format = 0;
320 357
321 const uint8_t* data = reinterpret_cast<const uint8_t*>(input.data()); 358 const uint8_t* data = reinterpret_cast<const uint8_t*>(input.data());
322 std::vector<uint8_t> result; 359 std::vector<uint8_t> result;
323 result.reserve(input.size() * sizeof(base::char16) + 1); 360 result.reserve(input.size() * sizeof(base::char16) + 1);
324 result.push_back(kUTF16Format); 361 result.push_back(kUTF16Format);
325 result.insert(result.end(), data, data + input.size() * sizeof(base::char16)); 362 result.insert(result.end(), data, data + input.size() * sizeof(base::char16));
326 return result; 363 return result;
327 } 364 }
328 365
366 LocalStorageContextMojo::~LocalStorageContextMojo() {
367 DCHECK_EQ(connection_state_, CONNECTION_SHUTDOWN);
368 }
369
329 void LocalStorageContextMojo::RunWhenConnected(base::OnceClosure callback) { 370 void LocalStorageContextMojo::RunWhenConnected(base::OnceClosure callback) {
371 DCHECK_NE(connection_state_, CONNECTION_SHUTDOWN);
372
330 // If we don't have a filesystem_connection_, we'll need to establish one. 373 // If we don't have a filesystem_connection_, we'll need to establish one.
331 if (connection_state_ == NO_CONNECTION) { 374 if (connection_state_ == NO_CONNECTION) {
332 connection_state_ = CONNECTION_IN_PROGRESS; 375 connection_state_ = CONNECTION_IN_PROGRESS;
333 InitiateConnection(); 376 InitiateConnection();
334 } 377 }
335 378
336 if (connection_state_ == CONNECTION_IN_PROGRESS) { 379 if (connection_state_ == CONNECTION_IN_PROGRESS) {
337 // Queue this OpenLocalStorage call for when we have a level db pointer. 380 // Queue this OpenLocalStorage call for when we have a level db pointer.
338 on_database_opened_callbacks_.push_back(std::move(callback)); 381 on_database_opened_callbacks_.push_back(std::move(callback));
339 return; 382 return;
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 std::vector<LocalStorageUsageInfo> usage) { 644 std::vector<LocalStorageUsageInfo> usage) {
602 for (const auto& info : usage) { 645 for (const auto& info : usage) {
603 url::Origin origin_candidate(info.origin); 646 url::Origin origin_candidate(info.origin);
604 if (!origin_candidate.IsSameOriginWith(origin) && 647 if (!origin_candidate.IsSameOriginWith(origin) &&
605 origin_candidate.IsSamePhysicalOriginWith(origin)) 648 origin_candidate.IsSamePhysicalOriginWith(origin))
606 DeleteStorage(origin_candidate); 649 DeleteStorage(origin_candidate);
607 } 650 }
608 DeleteStorage(origin); 651 DeleteStorage(origin);
609 } 652 }
610 653
654 void LocalStorageContextMojo::OnGotStorageUsageForShutdown(
655 std::vector<LocalStorageUsageInfo> usage) {
656 std::vector<leveldb::mojom::BatchedOperationPtr> operations;
657 for (const auto& info : usage) {
658 if (special_storage_policy_->IsStorageProtected(info.origin))
659 continue;
660 if (!special_storage_policy_->IsStorageSessionOnly(info.origin))
661 continue;
662
663 AddDeleteOriginOperations(&operations, url::Origin(info.origin));
664 }
665
666 if (!operations.empty()) {
667 database_->Write(
668 std::move(operations),
669 base::Bind(&LocalStorageContextMojo::OnShutdownCommitComplete,
670 base::Unretained(this)));
671 } else {
672 OnShutdownCommitComplete(leveldb::mojom::DatabaseError::OK);
673 }
674 }
675
676 void LocalStorageContextMojo::OnShutdownCommitComplete(
677 leveldb::mojom::DatabaseError error) {
678 delete this;
679 }
680
611 } // namespace content 681 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698