| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "chrome/browser/in_process_webkit/indexed_db_context.h" | 5 #include "chrome/browser/in_process_webkit/indexed_db_context.h" |
| 6 | 6 |
| 7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/browser_thread.h" | 11 #include "chrome/browser/browser_thread.h" |
| 12 #include "chrome/browser/in_process_webkit/webkit_context.h" | 12 #include "chrome/browser/in_process_webkit/webkit_context.h" |
| 13 #include "chrome/common/url_constants.h" |
| 13 #include "third_party/WebKit/WebKit/chromium/public/WebCString.h" | 14 #include "third_party/WebKit/WebKit/chromium/public/WebCString.h" |
| 14 #include "third_party/WebKit/WebKit/chromium/public/WebIDBDatabase.h" | 15 #include "third_party/WebKit/WebKit/chromium/public/WebIDBDatabase.h" |
| 15 #include "third_party/WebKit/WebKit/chromium/public/WebIDBFactory.h" | 16 #include "third_party/WebKit/WebKit/chromium/public/WebIDBFactory.h" |
| 16 #include "third_party/WebKit/WebKit/chromium/public/WebSecurityOrigin.h" | 17 #include "third_party/WebKit/WebKit/chromium/public/WebSecurityOrigin.h" |
| 17 #include "third_party/WebKit/WebKit/chromium/public/WebString.h" | 18 #include "third_party/WebKit/WebKit/chromium/public/WebString.h" |
| 18 #include "webkit/glue/webkit_glue.h" | 19 #include "webkit/glue/webkit_glue.h" |
| 19 | 20 |
| 20 using WebKit::WebIDBDatabase; | 21 using WebKit::WebIDBDatabase; |
| 21 using WebKit::WebIDBFactory; | 22 using WebKit::WebIDBFactory; |
| 22 using WebKit::WebSecurityOrigin; | 23 using WebKit::WebSecurityOrigin; |
| 23 | 24 |
| 24 const FilePath::CharType IndexedDBContext::kIndexedDBDirectory[] = | 25 const FilePath::CharType IndexedDBContext::kIndexedDBDirectory[] = |
| 25 FILE_PATH_LITERAL("IndexedDB"); | 26 FILE_PATH_LITERAL("IndexedDB"); |
| 26 | 27 |
| 27 const FilePath::CharType IndexedDBContext::kIndexedDBExtension[] = | 28 const FilePath::CharType IndexedDBContext::kIndexedDBExtension[] = |
| 28 FILE_PATH_LITERAL(".indexeddb"); | 29 FILE_PATH_LITERAL(".indexeddb"); |
| 29 | 30 |
| 30 IndexedDBContext::IndexedDBContext(WebKitContext* webkit_context) | 31 IndexedDBContext::IndexedDBContext(WebKitContext* webkit_context) { |
| 31 : webkit_context_(webkit_context) { | 32 data_path_ = webkit_context->data_path().Append(kIndexedDBDirectory); |
| 32 } | 33 } |
| 33 | 34 |
| 34 IndexedDBContext::~IndexedDBContext() { | 35 IndexedDBContext::~IndexedDBContext() { |
| 36 if (clear_local_state_on_exit_) { |
| 37 const char* url_scheme_skipped = chrome::kExtensionScheme; |
| 38 BrowserThread::PostTask( |
| 39 BrowserThread::WEBKIT, FROM_HERE, |
| 40 NewRunnableFunction(&IndexedDBContext::ClearLocalState, |
| 41 data_path_, url_scheme_skipped)); |
| 42 } |
| 35 } | 43 } |
| 36 | 44 |
| 37 WebIDBFactory* IndexedDBContext::GetIDBFactory() { | 45 WebIDBFactory* IndexedDBContext::GetIDBFactory() { |
| 38 if (!idb_factory_.get()) | 46 if (!idb_factory_.get()) |
| 39 idb_factory_.reset(WebIDBFactory::create()); | 47 idb_factory_.reset(WebIDBFactory::create()); |
| 40 DCHECK(idb_factory_.get()); | 48 DCHECK(idb_factory_.get()); |
| 41 return idb_factory_.get(); | 49 return idb_factory_.get(); |
| 42 } | 50 } |
| 43 | 51 |
| 44 FilePath IndexedDBContext::GetIndexedDBFilePath( | 52 FilePath IndexedDBContext::GetIndexedDBFilePath( |
| 45 const string16& origin_id) const { | 53 const string16& origin_id) const { |
| 46 FilePath storage_dir = webkit_context_->data_path().Append( | |
| 47 kIndexedDBDirectory); | |
| 48 FilePath::StringType id = webkit_glue::WebStringToFilePathString(origin_id); | 54 FilePath::StringType id = webkit_glue::WebStringToFilePathString(origin_id); |
| 49 return storage_dir.Append(id.append(kIndexedDBExtension)); | 55 return data_path_; |
| 50 } | 56 } |
| 51 | 57 |
| 52 // static | 58 // static |
| 53 void IndexedDBContext::ClearLocalState(const FilePath& profile_path, | 59 void IndexedDBContext::ClearLocalState(const FilePath& indexeddb_path, |
| 54 const char* url_scheme_to_be_skipped) { | 60 const char* url_scheme_to_be_skipped) { |
| 55 file_util::FileEnumerator file_enumerator(profile_path.Append( | 61 file_util::FileEnumerator file_enumerator( |
| 56 kIndexedDBDirectory), false, file_util::FileEnumerator::FILES); | 62 indexeddb_path, false, file_util::FileEnumerator::FILES); |
| 57 // TODO(pastarmovj): We might need to consider exchanging this loop for | 63 // TODO(pastarmovj): We might need to consider exchanging this loop for |
| 58 // something more efficient in the future. | 64 // something more efficient in the future. |
| 59 for (FilePath file_path = file_enumerator.Next(); !file_path.empty(); | 65 for (FilePath file_path = file_enumerator.Next(); !file_path.empty(); |
| 60 file_path = file_enumerator.Next()) { | 66 file_path = file_enumerator.Next()) { |
| 61 if (file_path.Extension() != IndexedDBContext::kIndexedDBExtension) | 67 if (file_path.Extension() != IndexedDBContext::kIndexedDBExtension) |
| 62 continue; | 68 continue; |
| 63 WebSecurityOrigin origin = | 69 WebSecurityOrigin origin = |
| 64 WebSecurityOrigin::createFromDatabaseIdentifier( | 70 WebSecurityOrigin::createFromDatabaseIdentifier( |
| 65 webkit_glue::FilePathToWebString(file_path.BaseName())); | 71 webkit_glue::FilePathToWebString(file_path.BaseName())); |
| 66 if (!EqualsASCII(origin.protocol(), url_scheme_to_be_skipped)) | 72 if (!EqualsASCII(origin.protocol(), url_scheme_to_be_skipped)) |
| 67 file_util::Delete(file_path, false); | 73 file_util::Delete(file_path, false); |
| 68 } | 74 } |
| 69 } | 75 } |
| 70 | 76 |
| 71 void IndexedDBContext::DeleteIndexedDBFile(const FilePath& file_path) { | 77 void IndexedDBContext::DeleteIndexedDBFile(const FilePath& file_path) { |
| 72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
| 73 // TODO(pastarmovj): Close all database connections that use that file. | 79 // TODO(pastarmovj): Close all database connections that use that file. |
| 74 file_util::Delete(file_path, false); | 80 file_util::Delete(file_path, false); |
| 75 } | 81 } |
| 76 | 82 |
| 77 void IndexedDBContext::DeleteIndexedDBForOrigin(const string16& origin_id) { | 83 void IndexedDBContext::DeleteIndexedDBForOrigin(const string16& origin_id) { |
| 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 84 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
| 79 // TODO(pastarmovj): Remove this check once we are safe to delete any time. | 85 // TODO(pastarmovj): Remove this check once we are safe to delete any time. |
| 80 FilePath idb_file = GetIndexedDBFilePath(origin_id); | 86 FilePath idb_file = GetIndexedDBFilePath(origin_id); |
| 81 DCHECK_EQ(idb_file.BaseName().value().substr(0, strlen("chrome-extension")), | 87 DCHECK_EQ(idb_file.BaseName().value().substr(0, strlen("chrome-extension")), |
| 82 FILE_PATH_LITERAL("chrome-extension")); | 88 FILE_PATH_LITERAL("chrome-extension")); |
| 83 DeleteIndexedDBFile(GetIndexedDBFilePath(origin_id)); | 89 DeleteIndexedDBFile(GetIndexedDBFilePath(origin_id)); |
| 84 } | 90 } |
| OLD | NEW |