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_context_impl.h" | 5 #include "content/browser/indexed_db/indexed_db_context_impl.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 #include "content/public/browser/indexed_db_info.h" | 31 #include "content/public/browser/indexed_db_info.h" |
32 #include "content/public/common/content_switches.h" | 32 #include "content/public/common/content_switches.h" |
33 #include "ui/base/text/bytes_formatting.h" | 33 #include "ui/base/text/bytes_formatting.h" |
34 #include "webkit/browser/database/database_util.h" | 34 #include "webkit/browser/database/database_util.h" |
35 #include "webkit/browser/quota/quota_manager_proxy.h" | 35 #include "webkit/browser/quota/quota_manager_proxy.h" |
36 #include "webkit/browser/quota/special_storage_policy.h" | 36 #include "webkit/browser/quota/special_storage_policy.h" |
37 #include "webkit/common/database/database_identifier.h" | 37 #include "webkit/common/database/database_identifier.h" |
38 | 38 |
39 using base::DictionaryValue; | 39 using base::DictionaryValue; |
40 using base::ListValue; | 40 using base::ListValue; |
41 using webkit_database::DatabaseUtil; | 41 using storage::DatabaseUtil; |
42 | 42 |
43 namespace content { | 43 namespace content { |
44 const base::FilePath::CharType IndexedDBContextImpl::kIndexedDBDirectory[] = | 44 const base::FilePath::CharType IndexedDBContextImpl::kIndexedDBDirectory[] = |
45 FILE_PATH_LITERAL("IndexedDB"); | 45 FILE_PATH_LITERAL("IndexedDB"); |
46 | 46 |
47 static const base::FilePath::CharType kIndexedDBExtension[] = | 47 static const base::FilePath::CharType kIndexedDBExtension[] = |
48 FILE_PATH_LITERAL(".indexeddb"); | 48 FILE_PATH_LITERAL(".indexeddb"); |
49 | 49 |
50 static const base::FilePath::CharType kLevelDBExtension[] = | 50 static const base::FilePath::CharType kLevelDBExtension[] = |
51 FILE_PATH_LITERAL(".leveldb"); | 51 FILE_PATH_LITERAL(".leveldb"); |
52 | 52 |
53 namespace { | 53 namespace { |
54 | 54 |
55 // This may be called after the IndexedDBContext is destroyed. | 55 // This may be called after the IndexedDBContext is destroyed. |
56 void GetAllOriginsAndPaths(const base::FilePath& indexeddb_path, | 56 void GetAllOriginsAndPaths(const base::FilePath& indexeddb_path, |
57 std::vector<GURL>* origins, | 57 std::vector<GURL>* origins, |
58 std::vector<base::FilePath>* file_paths) { | 58 std::vector<base::FilePath>* file_paths) { |
59 // TODO(jsbell): DCHECK that this is running on an IndexedDB thread, | 59 // TODO(jsbell): DCHECK that this is running on an IndexedDB thread, |
60 // if a global handle to it is ever available. | 60 // if a global handle to it is ever available. |
61 if (indexeddb_path.empty()) | 61 if (indexeddb_path.empty()) |
62 return; | 62 return; |
63 base::FileEnumerator file_enumerator( | 63 base::FileEnumerator file_enumerator( |
64 indexeddb_path, false, base::FileEnumerator::DIRECTORIES); | 64 indexeddb_path, false, base::FileEnumerator::DIRECTORIES); |
65 for (base::FilePath file_path = file_enumerator.Next(); !file_path.empty(); | 65 for (base::FilePath file_path = file_enumerator.Next(); !file_path.empty(); |
66 file_path = file_enumerator.Next()) { | 66 file_path = file_enumerator.Next()) { |
67 if (file_path.Extension() == kLevelDBExtension && | 67 if (file_path.Extension() == kLevelDBExtension && |
68 file_path.RemoveExtension().Extension() == kIndexedDBExtension) { | 68 file_path.RemoveExtension().Extension() == kIndexedDBExtension) { |
69 std::string origin_id = file_path.BaseName().RemoveExtension() | 69 std::string origin_id = file_path.BaseName().RemoveExtension() |
70 .RemoveExtension().MaybeAsASCII(); | 70 .RemoveExtension().MaybeAsASCII(); |
71 origins->push_back(webkit_database::GetOriginFromIdentifier(origin_id)); | 71 origins->push_back(storage::GetOriginFromIdentifier(origin_id)); |
72 if (file_paths) | 72 if (file_paths) |
73 file_paths->push_back(file_path); | 73 file_paths->push_back(file_path); |
74 } | 74 } |
75 } | 75 } |
76 } | 76 } |
77 | 77 |
78 // This will be called after the IndexedDBContext is destroyed. | 78 // This will be called after the IndexedDBContext is destroyed. |
79 void ClearSessionOnlyOrigins( | 79 void ClearSessionOnlyOrigins( |
80 const base::FilePath& indexeddb_path, | 80 const base::FilePath& indexeddb_path, |
81 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy) { | 81 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy) { |
82 // TODO(jsbell): DCHECK that this is running on an IndexedDB thread, | 82 // TODO(jsbell): DCHECK that this is running on an IndexedDB thread, |
83 // if a global handle to it is ever available. | 83 // if a global handle to it is ever available. |
84 std::vector<GURL> origins; | 84 std::vector<GURL> origins; |
85 std::vector<base::FilePath> file_paths; | 85 std::vector<base::FilePath> file_paths; |
86 GetAllOriginsAndPaths(indexeddb_path, &origins, &file_paths); | 86 GetAllOriginsAndPaths(indexeddb_path, &origins, &file_paths); |
87 DCHECK_EQ(origins.size(), file_paths.size()); | 87 DCHECK_EQ(origins.size(), file_paths.size()); |
88 std::vector<base::FilePath>::const_iterator file_path_iter = | 88 std::vector<base::FilePath>::const_iterator file_path_iter = |
89 file_paths.begin(); | 89 file_paths.begin(); |
90 for (std::vector<GURL>::const_iterator iter = origins.begin(); | 90 for (std::vector<GURL>::const_iterator iter = origins.begin(); |
91 iter != origins.end(); | 91 iter != origins.end(); |
92 ++iter, ++file_path_iter) { | 92 ++iter, ++file_path_iter) { |
93 if (!special_storage_policy->IsStorageSessionOnly(*iter)) | 93 if (!special_storage_policy->IsStorageSessionOnly(*iter)) |
94 continue; | 94 continue; |
95 if (special_storage_policy->IsStorageProtected(*iter)) | 95 if (special_storage_policy->IsStorageProtected(*iter)) |
96 continue; | 96 continue; |
97 base::DeleteFile(*file_path_iter, true); | 97 base::DeleteFile(*file_path_iter, true); |
98 } | 98 } |
99 } | 99 } |
100 | 100 |
101 } // namespace | 101 } // namespace |
102 | 102 |
103 IndexedDBContextImpl::IndexedDBContextImpl( | 103 IndexedDBContextImpl::IndexedDBContextImpl( |
104 const base::FilePath& data_path, | 104 const base::FilePath& data_path, |
105 quota::SpecialStoragePolicy* special_storage_policy, | 105 storage::SpecialStoragePolicy* special_storage_policy, |
106 quota::QuotaManagerProxy* quota_manager_proxy, | 106 storage::QuotaManagerProxy* quota_manager_proxy, |
107 base::SequencedTaskRunner* task_runner) | 107 base::SequencedTaskRunner* task_runner) |
108 : force_keep_session_state_(false), | 108 : force_keep_session_state_(false), |
109 special_storage_policy_(special_storage_policy), | 109 special_storage_policy_(special_storage_policy), |
110 quota_manager_proxy_(quota_manager_proxy), | 110 quota_manager_proxy_(quota_manager_proxy), |
111 task_runner_(task_runner) { | 111 task_runner_(task_runner) { |
112 IDB_TRACE("init"); | 112 IDB_TRACE("init"); |
113 if (!data_path.empty()) | 113 if (!data_path.empty()) |
114 data_path_ = data_path.Append(kIndexedDBDirectory); | 114 data_path_ = data_path.Append(kIndexedDBDirectory); |
115 if (quota_manager_proxy) { | 115 if (quota_manager_proxy) { |
116 quota_manager_proxy->RegisterClient(new IndexedDBQuotaClient(this)); | 116 quota_manager_proxy->RegisterClient(new IndexedDBQuotaClient(this)); |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 if (data_path_.empty() || !IsInOriginSet(origin_url)) | 356 if (data_path_.empty() || !IsInOriginSet(origin_url)) |
357 return 0; | 357 return 0; |
358 | 358 |
359 if (!factory_) | 359 if (!factory_) |
360 return 0; | 360 return 0; |
361 | 361 |
362 return factory_->GetConnectionCount(origin_url); | 362 return factory_->GetConnectionCount(origin_url); |
363 } | 363 } |
364 | 364 |
365 base::FilePath IndexedDBContextImpl::GetFilePath(const GURL& origin_url) const { | 365 base::FilePath IndexedDBContextImpl::GetFilePath(const GURL& origin_url) const { |
366 std::string origin_id = webkit_database::GetIdentifierFromOrigin(origin_url); | 366 std::string origin_id = storage::GetIdentifierFromOrigin(origin_url); |
367 return GetIndexedDBFilePath(origin_id); | 367 return GetIndexedDBFilePath(origin_id); |
368 } | 368 } |
369 | 369 |
370 base::FilePath IndexedDBContextImpl::GetFilePathForTesting( | 370 base::FilePath IndexedDBContextImpl::GetFilePathForTesting( |
371 const std::string& origin_id) const { | 371 const std::string& origin_id) const { |
372 return GetIndexedDBFilePath(origin_id); | 372 return GetIndexedDBFilePath(origin_id); |
373 } | 373 } |
374 | 374 |
375 void IndexedDBContextImpl::SetTaskRunnerForTesting( | 375 void IndexedDBContextImpl::SetTaskRunnerForTesting( |
376 base::SequencedTaskRunner* task_runner) { | 376 base::SequencedTaskRunner* task_runner) { |
377 DCHECK(!task_runner_); | 377 DCHECK(!task_runner_); |
378 task_runner_ = task_runner; | 378 task_runner_ = task_runner; |
379 } | 379 } |
380 | 380 |
381 void IndexedDBContextImpl::ConnectionOpened(const GURL& origin_url, | 381 void IndexedDBContextImpl::ConnectionOpened(const GURL& origin_url, |
382 IndexedDBConnection* connection) { | 382 IndexedDBConnection* connection) { |
383 DCHECK(TaskRunner()->RunsTasksOnCurrentThread()); | 383 DCHECK(TaskRunner()->RunsTasksOnCurrentThread()); |
384 if (quota_manager_proxy()) { | 384 if (quota_manager_proxy()) { |
385 quota_manager_proxy()->NotifyStorageAccessed( | 385 quota_manager_proxy()->NotifyStorageAccessed( |
386 quota::QuotaClient::kIndexedDatabase, | 386 storage::QuotaClient::kIndexedDatabase, |
387 origin_url, | 387 origin_url, |
388 quota::kStorageTypeTemporary); | 388 storage::kStorageTypeTemporary); |
389 } | 389 } |
390 if (AddToOriginSet(origin_url)) { | 390 if (AddToOriginSet(origin_url)) { |
391 // A newly created db, notify the quota system. | 391 // A newly created db, notify the quota system. |
392 QueryDiskAndUpdateQuotaUsage(origin_url); | 392 QueryDiskAndUpdateQuotaUsage(origin_url); |
393 } else { | 393 } else { |
394 EnsureDiskUsageCacheInitialized(origin_url); | 394 EnsureDiskUsageCacheInitialized(origin_url); |
395 } | 395 } |
396 QueryAvailableQuota(origin_url); | 396 QueryAvailableQuota(origin_url); |
397 } | 397 } |
398 | 398 |
399 void IndexedDBContextImpl::ConnectionClosed(const GURL& origin_url, | 399 void IndexedDBContextImpl::ConnectionClosed(const GURL& origin_url, |
400 IndexedDBConnection* connection) { | 400 IndexedDBConnection* connection) { |
401 DCHECK(TaskRunner()->RunsTasksOnCurrentThread()); | 401 DCHECK(TaskRunner()->RunsTasksOnCurrentThread()); |
402 if (quota_manager_proxy()) { | 402 if (quota_manager_proxy()) { |
403 quota_manager_proxy()->NotifyStorageAccessed( | 403 quota_manager_proxy()->NotifyStorageAccessed( |
404 quota::QuotaClient::kIndexedDatabase, | 404 storage::QuotaClient::kIndexedDatabase, |
405 origin_url, | 405 origin_url, |
406 quota::kStorageTypeTemporary); | 406 storage::kStorageTypeTemporary); |
407 } | 407 } |
408 if (factory_ && factory_->GetConnectionCount(origin_url) == 0) | 408 if (factory_ && factory_->GetConnectionCount(origin_url) == 0) |
409 QueryDiskAndUpdateQuotaUsage(origin_url); | 409 QueryDiskAndUpdateQuotaUsage(origin_url); |
410 } | 410 } |
411 | 411 |
412 void IndexedDBContextImpl::TransactionComplete(const GURL& origin_url) { | 412 void IndexedDBContextImpl::TransactionComplete(const GURL& origin_url) { |
413 DCHECK(!factory_ || factory_->GetConnectionCount(origin_url) > 0); | 413 DCHECK(!factory_ || factory_->GetConnectionCount(origin_url) > 0); |
414 QueryDiskAndUpdateQuotaUsage(origin_url); | 414 QueryDiskAndUpdateQuotaUsage(origin_url); |
415 QueryAvailableQuota(origin_url); | 415 QueryAvailableQuota(origin_url); |
416 } | 416 } |
(...skipping 12 matching lines...) Expand all Loading... |
429 } | 429 } |
430 bool over_quota = additional_bytes > space_available_map_[origin_url]; | 430 bool over_quota = additional_bytes > space_available_map_[origin_url]; |
431 return over_quota; | 431 return over_quota; |
432 } | 432 } |
433 | 433 |
434 bool IndexedDBContextImpl::IsOverQuota(const GURL& origin_url) { | 434 bool IndexedDBContextImpl::IsOverQuota(const GURL& origin_url) { |
435 const int kOneAdditionalByte = 1; | 435 const int kOneAdditionalByte = 1; |
436 return WouldBeOverQuota(origin_url, kOneAdditionalByte); | 436 return WouldBeOverQuota(origin_url, kOneAdditionalByte); |
437 } | 437 } |
438 | 438 |
439 quota::QuotaManagerProxy* IndexedDBContextImpl::quota_manager_proxy() { | 439 storage::QuotaManagerProxy* IndexedDBContextImpl::quota_manager_proxy() { |
440 return quota_manager_proxy_; | 440 return quota_manager_proxy_; |
441 } | 441 } |
442 | 442 |
443 IndexedDBContextImpl::~IndexedDBContextImpl() { | 443 IndexedDBContextImpl::~IndexedDBContextImpl() { |
444 if (factory_) { | 444 if (factory_) { |
445 TaskRunner()->PostTask( | 445 TaskRunner()->PostTask( |
446 FROM_HERE, base::Bind(&IndexedDBFactory::ContextDestroyed, factory_)); | 446 FROM_HERE, base::Bind(&IndexedDBFactory::ContextDestroyed, factory_)); |
447 factory_ = NULL; | 447 factory_ = NULL; |
448 } | 448 } |
449 | 449 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 void IndexedDBContextImpl::QueryDiskAndUpdateQuotaUsage( | 490 void IndexedDBContextImpl::QueryDiskAndUpdateQuotaUsage( |
491 const GURL& origin_url) { | 491 const GURL& origin_url) { |
492 int64 former_disk_usage = origin_size_map_[origin_url]; | 492 int64 former_disk_usage = origin_size_map_[origin_url]; |
493 int64 current_disk_usage = ReadUsageFromDisk(origin_url); | 493 int64 current_disk_usage = ReadUsageFromDisk(origin_url); |
494 int64 difference = current_disk_usage - former_disk_usage; | 494 int64 difference = current_disk_usage - former_disk_usage; |
495 if (difference) { | 495 if (difference) { |
496 origin_size_map_[origin_url] = current_disk_usage; | 496 origin_size_map_[origin_url] = current_disk_usage; |
497 // quota_manager_proxy() is NULL in unit tests. | 497 // quota_manager_proxy() is NULL in unit tests. |
498 if (quota_manager_proxy()) { | 498 if (quota_manager_proxy()) { |
499 quota_manager_proxy()->NotifyStorageModified( | 499 quota_manager_proxy()->NotifyStorageModified( |
500 quota::QuotaClient::kIndexedDatabase, | 500 storage::QuotaClient::kIndexedDatabase, |
501 origin_url, | 501 origin_url, |
502 quota::kStorageTypeTemporary, | 502 storage::kStorageTypeTemporary, |
503 difference); | 503 difference); |
504 } | 504 } |
505 } | 505 } |
506 } | 506 } |
507 | 507 |
508 void IndexedDBContextImpl::GotUsageAndQuota(const GURL& origin_url, | 508 void IndexedDBContextImpl::GotUsageAndQuota(const GURL& origin_url, |
509 quota::QuotaStatusCode status, | 509 storage::QuotaStatusCode status, |
510 int64 usage, | 510 int64 usage, |
511 int64 quota) { | 511 int64 quota) { |
512 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 512 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
513 DCHECK(status == quota::kQuotaStatusOk || status == quota::kQuotaErrorAbort) | 513 DCHECK(status == storage::kQuotaStatusOk || |
| 514 status == storage::kQuotaErrorAbort) |
514 << "status was " << status; | 515 << "status was " << status; |
515 if (status == quota::kQuotaErrorAbort) { | 516 if (status == storage::kQuotaErrorAbort) { |
516 // We seem to no longer care to wait around for the answer. | 517 // We seem to no longer care to wait around for the answer. |
517 return; | 518 return; |
518 } | 519 } |
519 TaskRunner()->PostTask(FROM_HERE, | 520 TaskRunner()->PostTask(FROM_HERE, |
520 base::Bind(&IndexedDBContextImpl::GotUpdatedQuota, | 521 base::Bind(&IndexedDBContextImpl::GotUpdatedQuota, |
521 this, | 522 this, |
522 origin_url, | 523 origin_url, |
523 usage, | 524 usage, |
524 quota)); | 525 quota)); |
525 } | 526 } |
(...skipping 15 matching lines...) Expand all Loading... |
541 base::Bind( | 542 base::Bind( |
542 &IndexedDBContextImpl::QueryAvailableQuota, this, origin_url)); | 543 &IndexedDBContextImpl::QueryAvailableQuota, this, origin_url)); |
543 } | 544 } |
544 return; | 545 return; |
545 } | 546 } |
546 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 547 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
547 if (!quota_manager_proxy() || !quota_manager_proxy()->quota_manager()) | 548 if (!quota_manager_proxy() || !quota_manager_proxy()->quota_manager()) |
548 return; | 549 return; |
549 quota_manager_proxy()->quota_manager()->GetUsageAndQuota( | 550 quota_manager_proxy()->quota_manager()->GetUsageAndQuota( |
550 origin_url, | 551 origin_url, |
551 quota::kStorageTypeTemporary, | 552 storage::kStorageTypeTemporary, |
552 base::Bind(&IndexedDBContextImpl::GotUsageAndQuota, this, origin_url)); | 553 base::Bind(&IndexedDBContextImpl::GotUsageAndQuota, this, origin_url)); |
553 } | 554 } |
554 | 555 |
555 std::set<GURL>* IndexedDBContextImpl::GetOriginSet() { | 556 std::set<GURL>* IndexedDBContextImpl::GetOriginSet() { |
556 if (!origin_set_) { | 557 if (!origin_set_) { |
557 origin_set_.reset(new std::set<GURL>); | 558 origin_set_.reset(new std::set<GURL>); |
558 std::vector<GURL> origins; | 559 std::vector<GURL> origins; |
559 GetAllOriginsAndPaths(data_path_, &origins, NULL); | 560 GetAllOriginsAndPaths(data_path_, &origins, NULL); |
560 for (std::vector<GURL>::const_iterator iter = origins.begin(); | 561 for (std::vector<GURL>::const_iterator iter = origins.begin(); |
561 iter != origins.end(); | 562 iter != origins.end(); |
562 ++iter) { | 563 ++iter) { |
563 origin_set_->insert(*iter); | 564 origin_set_->insert(*iter); |
564 } | 565 } |
565 } | 566 } |
566 return origin_set_.get(); | 567 return origin_set_.get(); |
567 } | 568 } |
568 | 569 |
569 void IndexedDBContextImpl::ResetCaches() { | 570 void IndexedDBContextImpl::ResetCaches() { |
570 origin_set_.reset(); | 571 origin_set_.reset(); |
571 origin_size_map_.clear(); | 572 origin_size_map_.clear(); |
572 space_available_map_.clear(); | 573 space_available_map_.clear(); |
573 } | 574 } |
574 | 575 |
575 base::SequencedTaskRunner* IndexedDBContextImpl::TaskRunner() const { | 576 base::SequencedTaskRunner* IndexedDBContextImpl::TaskRunner() const { |
576 return task_runner_; | 577 return task_runner_; |
577 } | 578 } |
578 | 579 |
579 } // namespace content | 580 } // namespace content |
OLD | NEW |