Rename isSystemLocationEnabled to isLocationEnabled, as per internal review (185995).
[chromium-blink-merge.git] / content / browser / indexed_db / indexed_db_context_impl.cc
blob39b3a07a70ab7d8aed3cb69d7dc0ff9fc12de55d
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
3 // found in the LICENSE file.
5 #include "content/browser/indexed_db/indexed_db_context_impl.h"
7 #include <algorithm>
8 #include <utility>
10 #include "base/bind.h"
11 #include "base/command_line.h"
12 #include "base/files/file_enumerator.h"
13 #include "base/files/file_util.h"
14 #include "base/logging.h"
15 #include "base/metrics/histogram.h"
16 #include "base/sequenced_task_runner.h"
17 #include "base/strings/string_util.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/threading/thread_restrictions.h"
20 #include "base/time/time.h"
21 #include "base/values.h"
22 #include "content/browser/browser_main_loop.h"
23 #include "content/browser/indexed_db/indexed_db_connection.h"
24 #include "content/browser/indexed_db/indexed_db_database.h"
25 #include "content/browser/indexed_db/indexed_db_dispatcher_host.h"
26 #include "content/browser/indexed_db/indexed_db_factory_impl.h"
27 #include "content/browser/indexed_db/indexed_db_quota_client.h"
28 #include "content/browser/indexed_db/indexed_db_tracing.h"
29 #include "content/browser/indexed_db/indexed_db_transaction.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "content/public/browser/indexed_db_info.h"
32 #include "content/public/common/content_switches.h"
33 #include "storage/browser/database/database_util.h"
34 #include "storage/browser/quota/quota_manager_proxy.h"
35 #include "storage/browser/quota/special_storage_policy.h"
36 #include "storage/common/database/database_identifier.h"
37 #include "ui/base/text/bytes_formatting.h"
39 using base::DictionaryValue;
40 using base::ListValue;
41 using storage::DatabaseUtil;
43 namespace content {
44 const base::FilePath::CharType IndexedDBContextImpl::kIndexedDBDirectory[] =
45 FILE_PATH_LITERAL("IndexedDB");
47 static const base::FilePath::CharType kBlobExtension[] =
48 FILE_PATH_LITERAL(".blob");
50 static const base::FilePath::CharType kIndexedDBExtension[] =
51 FILE_PATH_LITERAL(".indexeddb");
53 static const base::FilePath::CharType kLevelDBExtension[] =
54 FILE_PATH_LITERAL(".leveldb");
56 namespace {
58 // This may be called after the IndexedDBContext is destroyed.
59 void GetAllOriginsAndPaths(const base::FilePath& indexeddb_path,
60 std::vector<GURL>* origins,
61 std::vector<base::FilePath>* file_paths) {
62 // TODO(jsbell): DCHECK that this is running on an IndexedDB thread,
63 // if a global handle to it is ever available.
64 if (indexeddb_path.empty())
65 return;
66 base::FileEnumerator file_enumerator(
67 indexeddb_path, false, base::FileEnumerator::DIRECTORIES);
68 for (base::FilePath file_path = file_enumerator.Next(); !file_path.empty();
69 file_path = file_enumerator.Next()) {
70 if (file_path.Extension() == kLevelDBExtension &&
71 file_path.RemoveExtension().Extension() == kIndexedDBExtension) {
72 std::string origin_id = file_path.BaseName().RemoveExtension()
73 .RemoveExtension().MaybeAsASCII();
74 origins->push_back(storage::GetOriginFromIdentifier(origin_id));
75 if (file_paths)
76 file_paths->push_back(file_path);
81 // This will be called after the IndexedDBContext is destroyed.
82 void ClearSessionOnlyOrigins(
83 const base::FilePath& indexeddb_path,
84 scoped_refptr<storage::SpecialStoragePolicy> special_storage_policy) {
85 // TODO(jsbell): DCHECK that this is running on an IndexedDB thread,
86 // if a global handle to it is ever available.
87 std::vector<GURL> origins;
88 std::vector<base::FilePath> file_paths;
89 GetAllOriginsAndPaths(indexeddb_path, &origins, &file_paths);
90 DCHECK_EQ(origins.size(), file_paths.size());
91 std::vector<base::FilePath>::const_iterator file_path_iter =
92 file_paths.begin();
93 for (std::vector<GURL>::const_iterator iter = origins.begin();
94 iter != origins.end();
95 ++iter, ++file_path_iter) {
96 if (!special_storage_policy->IsStorageSessionOnly(*iter))
97 continue;
98 if (special_storage_policy->IsStorageProtected(*iter))
99 continue;
100 base::DeleteFile(*file_path_iter, true);
104 } // namespace
106 IndexedDBContextImpl::IndexedDBContextImpl(
107 const base::FilePath& data_path,
108 storage::SpecialStoragePolicy* special_storage_policy,
109 storage::QuotaManagerProxy* quota_manager_proxy,
110 base::SequencedTaskRunner* task_runner)
111 : force_keep_session_state_(false),
112 special_storage_policy_(special_storage_policy),
113 quota_manager_proxy_(quota_manager_proxy),
114 task_runner_(task_runner) {
115 IDB_TRACE("init");
116 if (!data_path.empty())
117 data_path_ = data_path.Append(kIndexedDBDirectory);
118 if (quota_manager_proxy) {
119 quota_manager_proxy->RegisterClient(new IndexedDBQuotaClient(this));
123 IndexedDBFactory* IndexedDBContextImpl::GetIDBFactory() {
124 DCHECK(TaskRunner()->RunsTasksOnCurrentThread());
125 if (!factory_.get()) {
126 // Prime our cache of origins with existing databases so we can
127 // detect when dbs are newly created.
128 GetOriginSet();
129 factory_ = new IndexedDBFactoryImpl(this);
131 return factory_.get();
134 std::vector<GURL> IndexedDBContextImpl::GetAllOrigins() {
135 DCHECK(TaskRunner()->RunsTasksOnCurrentThread());
136 std::set<GURL>* origins_set = GetOriginSet();
137 return std::vector<GURL>(origins_set->begin(), origins_set->end());
140 std::vector<IndexedDBInfo> IndexedDBContextImpl::GetAllOriginsInfo() {
141 DCHECK(TaskRunner()->RunsTasksOnCurrentThread());
142 std::vector<GURL> origins = GetAllOrigins();
143 std::vector<IndexedDBInfo> result;
144 for (const auto& origin_url : origins) {
145 base::FilePath idb_directory = GetLevelDBPath(origin_url);
146 size_t connection_count = GetConnectionCount(origin_url);
147 result.push_back(IndexedDBInfo(origin_url,
148 GetOriginDiskUsage(origin_url),
149 GetOriginLastModified(origin_url),
150 idb_directory,
151 connection_count));
153 return result;
156 static bool HostNameComparator(const GURL& i, const GURL& j) {
157 return i.host() < j.host();
160 base::ListValue* IndexedDBContextImpl::GetAllOriginsDetails() {
161 DCHECK(TaskRunner()->RunsTasksOnCurrentThread());
162 std::vector<GURL> origins = GetAllOrigins();
164 std::sort(origins.begin(), origins.end(), HostNameComparator);
166 scoped_ptr<base::ListValue> list(new base::ListValue());
167 for (const auto& origin_url : origins) {
168 scoped_ptr<base::DictionaryValue> info(new base::DictionaryValue());
169 info->SetString("url", origin_url.spec());
170 info->SetString("size", ui::FormatBytes(GetOriginDiskUsage(origin_url)));
171 info->SetDouble("last_modified",
172 GetOriginLastModified(origin_url).ToJsTime());
173 if (!is_incognito()) {
174 scoped_ptr<base::ListValue> paths(new base::ListValue());
175 for (const base::FilePath& path : GetStoragePaths(origin_url))
176 paths->AppendString(path.value());
177 info->Set("paths", paths.release());
179 info->SetDouble("connection_count", GetConnectionCount(origin_url));
181 // This ends up being O(n^2) since we iterate over all open databases
182 // to extract just those in the origin, and we're iterating over all
183 // origins in the outer loop.
185 if (factory_.get()) {
186 std::pair<IndexedDBFactory::OriginDBMapIterator,
187 IndexedDBFactory::OriginDBMapIterator> range =
188 factory_->GetOpenDatabasesForOrigin(origin_url);
189 // TODO(jsbell): Sort by name?
190 scoped_ptr<base::ListValue> database_list(new base::ListValue());
192 for (IndexedDBFactory::OriginDBMapIterator it = range.first;
193 it != range.second;
194 ++it) {
195 const IndexedDBDatabase* db = it->second;
196 scoped_ptr<base::DictionaryValue> db_info(new base::DictionaryValue());
198 db_info->SetString("name", db->name());
199 db_info->SetDouble("pending_opens", db->PendingOpenCount());
200 db_info->SetDouble("pending_upgrades", db->PendingUpgradeCount());
201 db_info->SetDouble("running_upgrades", db->RunningUpgradeCount());
202 db_info->SetDouble("pending_deletes", db->PendingDeleteCount());
203 db_info->SetDouble("connection_count",
204 db->ConnectionCount() - db->PendingUpgradeCount() -
205 db->RunningUpgradeCount());
207 scoped_ptr<base::ListValue> transaction_list(new base::ListValue());
208 std::vector<const IndexedDBTransaction*> transactions =
209 db->transaction_coordinator().GetTransactions();
210 for (const auto* transaction : transactions) {
211 scoped_ptr<base::DictionaryValue> transaction_info(
212 new base::DictionaryValue());
214 const char* kModes[] = { "readonly", "readwrite", "versionchange" };
215 transaction_info->SetString("mode", kModes[transaction->mode()]);
216 switch (transaction->state()) {
217 case IndexedDBTransaction::CREATED:
218 transaction_info->SetString("status", "blocked");
219 break;
220 case IndexedDBTransaction::STARTED:
221 if (transaction->diagnostics().tasks_scheduled > 0)
222 transaction_info->SetString("status", "running");
223 else
224 transaction_info->SetString("status", "started");
225 break;
226 case IndexedDBTransaction::COMMITTING:
227 transaction_info->SetString("status", "committing");
228 break;
229 case IndexedDBTransaction::FINISHED:
230 transaction_info->SetString("status", "finished");
231 break;
234 transaction_info->SetDouble(
235 "pid",
236 IndexedDBDispatcherHost::TransactionIdToProcessId(
237 transaction->id()));
238 transaction_info->SetDouble(
239 "tid",
240 IndexedDBDispatcherHost::TransactionIdToRendererTransactionId(
241 transaction->id()));
242 transaction_info->SetDouble(
243 "age",
244 (base::Time::Now() - transaction->diagnostics().creation_time)
245 .InMillisecondsF());
246 transaction_info->SetDouble(
247 "runtime",
248 (base::Time::Now() - transaction->diagnostics().start_time)
249 .InMillisecondsF());
250 transaction_info->SetDouble(
251 "tasks_scheduled", transaction->diagnostics().tasks_scheduled);
252 transaction_info->SetDouble(
253 "tasks_completed", transaction->diagnostics().tasks_completed);
255 scoped_ptr<base::ListValue> scope(new base::ListValue());
256 for (const auto& id : transaction->scope()) {
257 IndexedDBDatabaseMetadata::ObjectStoreMap::const_iterator it =
258 db->metadata().object_stores.find(id);
259 if (it != db->metadata().object_stores.end())
260 scope->AppendString(it->second.name);
263 transaction_info->Set("scope", scope.release());
264 transaction_list->Append(transaction_info.release());
266 db_info->Set("transactions", transaction_list.release());
268 database_list->Append(db_info.release());
270 info->Set("databases", database_list.release());
273 list->Append(info.release());
275 return list.release();
278 int64 IndexedDBContextImpl::GetOriginDiskUsage(const GURL& origin_url) {
279 DCHECK(TaskRunner()->RunsTasksOnCurrentThread());
280 if (data_path_.empty() || !IsInOriginSet(origin_url))
281 return 0;
282 EnsureDiskUsageCacheInitialized(origin_url);
283 return origin_size_map_[origin_url];
286 base::Time IndexedDBContextImpl::GetOriginLastModified(const GURL& origin_url) {
287 DCHECK(TaskRunner()->RunsTasksOnCurrentThread());
288 if (data_path_.empty() || !IsInOriginSet(origin_url))
289 return base::Time();
290 base::FilePath idb_directory = GetLevelDBPath(origin_url);
291 base::File::Info file_info;
292 if (!base::GetFileInfo(idb_directory, &file_info))
293 return base::Time();
294 return file_info.last_modified;
297 void IndexedDBContextImpl::DeleteForOrigin(const GURL& origin_url) {
298 DCHECK(TaskRunner()->RunsTasksOnCurrentThread());
299 ForceClose(origin_url, FORCE_CLOSE_DELETE_ORIGIN);
300 if (data_path_.empty() || !IsInOriginSet(origin_url))
301 return;
303 base::FilePath idb_directory = GetLevelDBPath(origin_url);
304 EnsureDiskUsageCacheInitialized(origin_url);
305 leveldb::Status s = LevelDBDatabase::Destroy(idb_directory);
306 if (!s.ok()) {
307 LOG(WARNING) << "Failed to delete LevelDB database: "
308 << idb_directory.AsUTF8Unsafe();
309 } else {
310 // LevelDB does not delete empty directories; work around this.
311 // TODO(jsbell): Remove when upstream bug is fixed.
312 // https://code.google.com/p/leveldb/issues/detail?id=209
313 const bool kNonRecursive = false;
314 base::DeleteFile(idb_directory, kNonRecursive);
316 base::DeleteFile(GetBlobPath(storage::GetIdentifierFromOrigin(origin_url)),
317 true /* recursive */);
318 QueryDiskAndUpdateQuotaUsage(origin_url);
319 if (s.ok()) {
320 RemoveFromOriginSet(origin_url);
321 origin_size_map_.erase(origin_url);
322 space_available_map_.erase(origin_url);
326 void IndexedDBContextImpl::ForceClose(const GURL origin_url,
327 ForceCloseReason reason) {
328 DCHECK(TaskRunner()->RunsTasksOnCurrentThread());
329 UMA_HISTOGRAM_ENUMERATION("WebCore.IndexedDB.Context.ForceCloseReason",
330 reason,
331 FORCE_CLOSE_REASON_MAX);
333 if (data_path_.empty() || !IsInOriginSet(origin_url))
334 return;
336 if (factory_.get())
337 factory_->ForceClose(origin_url);
338 DCHECK_EQ(0UL, GetConnectionCount(origin_url));
341 size_t IndexedDBContextImpl::GetConnectionCount(const GURL& origin_url) {
342 DCHECK(TaskRunner()->RunsTasksOnCurrentThread());
343 if (data_path_.empty() || !IsInOriginSet(origin_url))
344 return 0;
346 if (!factory_.get())
347 return 0;
349 return factory_->GetConnectionCount(origin_url);
352 base::FilePath IndexedDBContextImpl::GetLevelDBPath(
353 const GURL& origin_url) const {
354 std::string origin_id = storage::GetIdentifierFromOrigin(origin_url);
355 return GetLevelDBPath(origin_id);
358 std::vector<base::FilePath> IndexedDBContextImpl::GetStoragePaths(
359 const GURL& origin_url) const {
360 std::string origin_id = storage::GetIdentifierFromOrigin(origin_url);
361 std::vector<base::FilePath> paths;
362 paths.push_back(GetLevelDBPath(origin_id));
363 paths.push_back(GetBlobPath(origin_id));
364 return paths;
367 base::FilePath IndexedDBContextImpl::GetFilePathForTesting(
368 const std::string& origin_id) const {
369 return GetLevelDBPath(origin_id);
372 void IndexedDBContextImpl::SetTaskRunnerForTesting(
373 base::SequencedTaskRunner* task_runner) {
374 DCHECK(!task_runner_.get());
375 task_runner_ = task_runner;
378 void IndexedDBContextImpl::ConnectionOpened(const GURL& origin_url,
379 IndexedDBConnection* connection) {
380 DCHECK(TaskRunner()->RunsTasksOnCurrentThread());
381 if (quota_manager_proxy()) {
382 quota_manager_proxy()->NotifyStorageAccessed(
383 storage::QuotaClient::kIndexedDatabase,
384 origin_url,
385 storage::kStorageTypeTemporary);
387 if (AddToOriginSet(origin_url)) {
388 // A newly created db, notify the quota system.
389 QueryDiskAndUpdateQuotaUsage(origin_url);
390 } else {
391 EnsureDiskUsageCacheInitialized(origin_url);
393 QueryAvailableQuota(origin_url);
396 void IndexedDBContextImpl::ConnectionClosed(const GURL& origin_url,
397 IndexedDBConnection* connection) {
398 DCHECK(TaskRunner()->RunsTasksOnCurrentThread());
399 if (quota_manager_proxy()) {
400 quota_manager_proxy()->NotifyStorageAccessed(
401 storage::QuotaClient::kIndexedDatabase,
402 origin_url,
403 storage::kStorageTypeTemporary);
405 if (factory_.get() && factory_->GetConnectionCount(origin_url) == 0)
406 QueryDiskAndUpdateQuotaUsage(origin_url);
409 void IndexedDBContextImpl::TransactionComplete(const GURL& origin_url) {
410 DCHECK(!factory_.get() || factory_->GetConnectionCount(origin_url) > 0);
411 QueryDiskAndUpdateQuotaUsage(origin_url);
412 QueryAvailableQuota(origin_url);
415 void IndexedDBContextImpl::DatabaseDeleted(const GURL& origin_url) {
416 AddToOriginSet(origin_url);
417 QueryDiskAndUpdateQuotaUsage(origin_url);
418 QueryAvailableQuota(origin_url);
421 bool IndexedDBContextImpl::WouldBeOverQuota(const GURL& origin_url,
422 int64 additional_bytes) {
423 if (space_available_map_.find(origin_url) == space_available_map_.end()) {
424 // We haven't heard back from the QuotaManager yet, just let it through.
425 return false;
427 bool over_quota = additional_bytes > space_available_map_[origin_url];
428 return over_quota;
431 bool IndexedDBContextImpl::IsOverQuota(const GURL& origin_url) {
432 const int kOneAdditionalByte = 1;
433 return WouldBeOverQuota(origin_url, kOneAdditionalByte);
436 storage::QuotaManagerProxy* IndexedDBContextImpl::quota_manager_proxy() {
437 return quota_manager_proxy_.get();
440 IndexedDBContextImpl::~IndexedDBContextImpl() {
441 if (factory_.get()) {
442 TaskRunner()->PostTask(
443 FROM_HERE, base::Bind(&IndexedDBFactory::ContextDestroyed, factory_));
444 factory_ = NULL;
447 if (data_path_.empty())
448 return;
450 if (force_keep_session_state_)
451 return;
453 bool has_session_only_databases =
454 special_storage_policy_.get() &&
455 special_storage_policy_->HasSessionOnlyOrigins();
457 // Clearing only session-only databases, and there are none.
458 if (!has_session_only_databases)
459 return;
461 TaskRunner()->PostTask(
462 FROM_HERE,
463 base::Bind(
464 &ClearSessionOnlyOrigins, data_path_, special_storage_policy_));
467 base::FilePath IndexedDBContextImpl::GetBlobPath(
468 const std::string& origin_id) const {
469 DCHECK(!data_path_.empty());
470 return data_path_.AppendASCII(origin_id).AddExtension(kIndexedDBExtension)
471 .AddExtension(kBlobExtension);
474 base::FilePath IndexedDBContextImpl::GetLevelDBPath(
475 const std::string& origin_id) const {
476 DCHECK(!data_path_.empty());
477 return data_path_.AppendASCII(origin_id).AddExtension(kIndexedDBExtension)
478 .AddExtension(kLevelDBExtension);
481 int64 IndexedDBContextImpl::ReadUsageFromDisk(const GURL& origin_url) const {
482 if (data_path_.empty())
483 return 0;
484 int64 total_size = 0;
485 for (const base::FilePath& path : GetStoragePaths(origin_url))
486 total_size += base::ComputeDirectorySize(path);
487 return total_size;
490 void IndexedDBContextImpl::EnsureDiskUsageCacheInitialized(
491 const GURL& origin_url) {
492 if (origin_size_map_.find(origin_url) == origin_size_map_.end())
493 origin_size_map_[origin_url] = ReadUsageFromDisk(origin_url);
496 void IndexedDBContextImpl::QueryDiskAndUpdateQuotaUsage(
497 const GURL& origin_url) {
498 int64 former_disk_usage = origin_size_map_[origin_url];
499 int64 current_disk_usage = ReadUsageFromDisk(origin_url);
500 int64 difference = current_disk_usage - former_disk_usage;
501 if (difference) {
502 origin_size_map_[origin_url] = current_disk_usage;
503 // quota_manager_proxy() is NULL in unit tests.
504 if (quota_manager_proxy()) {
505 quota_manager_proxy()->NotifyStorageModified(
506 storage::QuotaClient::kIndexedDatabase,
507 origin_url,
508 storage::kStorageTypeTemporary,
509 difference);
514 void IndexedDBContextImpl::GotUsageAndQuota(const GURL& origin_url,
515 storage::QuotaStatusCode status,
516 int64 usage,
517 int64 quota) {
518 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
519 DCHECK(status == storage::kQuotaStatusOk ||
520 status == storage::kQuotaErrorAbort)
521 << "status was " << status;
522 if (status == storage::kQuotaErrorAbort) {
523 // We seem to no longer care to wait around for the answer.
524 return;
526 TaskRunner()->PostTask(FROM_HERE,
527 base::Bind(&IndexedDBContextImpl::GotUpdatedQuota,
528 this,
529 origin_url,
530 usage,
531 quota));
534 void IndexedDBContextImpl::GotUpdatedQuota(const GURL& origin_url,
535 int64 usage,
536 int64 quota) {
537 DCHECK(TaskRunner()->RunsTasksOnCurrentThread());
538 space_available_map_[origin_url] = quota - usage;
541 void IndexedDBContextImpl::QueryAvailableQuota(const GURL& origin_url) {
542 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
543 DCHECK(TaskRunner()->RunsTasksOnCurrentThread());
544 if (quota_manager_proxy()) {
545 BrowserThread::PostTask(
546 BrowserThread::IO,
547 FROM_HERE,
548 base::Bind(
549 &IndexedDBContextImpl::QueryAvailableQuota, this, origin_url));
551 return;
553 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
554 if (!quota_manager_proxy() || !quota_manager_proxy()->quota_manager())
555 return;
556 quota_manager_proxy()->quota_manager()->GetUsageAndQuota(
557 origin_url,
558 storage::kStorageTypeTemporary,
559 base::Bind(&IndexedDBContextImpl::GotUsageAndQuota, this, origin_url));
562 std::set<GURL>* IndexedDBContextImpl::GetOriginSet() {
563 if (!origin_set_) {
564 std::vector<GURL> origins;
565 GetAllOriginsAndPaths(data_path_, &origins, NULL);
566 origin_set_.reset(new std::set<GURL>(origins.begin(), origins.end()));
568 return origin_set_.get();
571 void IndexedDBContextImpl::ResetCaches() {
572 origin_set_.reset();
573 origin_size_map_.clear();
574 space_available_map_.clear();
577 base::SequencedTaskRunner* IndexedDBContextImpl::TaskRunner() const {
578 return task_runner_.get();
581 } // namespace content