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 |
| 25 namespace { |
| 26 |
| 27 void ClearLocalState(const FilePath& indexeddb_path, |
| 28 const char* url_scheme_to_be_skipped) { |
| 29 file_util::FileEnumerator file_enumerator( |
| 30 indexeddb_path, false, file_util::FileEnumerator::FILES); |
| 31 // TODO(pastarmovj): We might need to consider exchanging this loop for |
| 32 // something more efficient in the future. |
| 33 for (FilePath file_path = file_enumerator.Next(); !file_path.empty(); |
| 34 file_path = file_enumerator.Next()) { |
| 35 if (file_path.Extension() != IndexedDBContext::kIndexedDBExtension) |
| 36 continue; |
| 37 WebSecurityOrigin origin = |
| 38 WebSecurityOrigin::createFromDatabaseIdentifier( |
| 39 webkit_glue::FilePathToWebString(file_path.BaseName())); |
| 40 if (!EqualsASCII(origin.protocol(), url_scheme_to_be_skipped)) |
| 41 file_util::Delete(file_path, false); |
| 42 } |
| 43 } |
| 44 |
| 45 } // namespace |
| 46 |
24 const FilePath::CharType IndexedDBContext::kIndexedDBDirectory[] = | 47 const FilePath::CharType IndexedDBContext::kIndexedDBDirectory[] = |
25 FILE_PATH_LITERAL("IndexedDB"); | 48 FILE_PATH_LITERAL("IndexedDB"); |
26 | 49 |
27 const FilePath::CharType IndexedDBContext::kIndexedDBExtension[] = | 50 const FilePath::CharType IndexedDBContext::kIndexedDBExtension[] = |
28 FILE_PATH_LITERAL(".indexeddb"); | 51 FILE_PATH_LITERAL(".indexeddb"); |
29 | 52 |
30 IndexedDBContext::IndexedDBContext(WebKitContext* webkit_context) | 53 IndexedDBContext::IndexedDBContext(WebKitContext* webkit_context) { |
31 : webkit_context_(webkit_context) { | 54 data_path_ = webkit_context->data_path().Append(kIndexedDBDirectory); |
32 } | 55 } |
33 | 56 |
34 IndexedDBContext::~IndexedDBContext() { | 57 IndexedDBContext::~IndexedDBContext() { |
| 58 // Not being on the WEBKIT thread here means we are running in a unit test |
| 59 // where no clean up is needed. |
| 60 if (clear_local_state_on_exit_ && |
| 61 BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)) { |
| 62 ClearLocalState(data_path_, chrome::kExtensionScheme); |
| 63 } |
35 } | 64 } |
36 | 65 |
37 WebIDBFactory* IndexedDBContext::GetIDBFactory() { | 66 WebIDBFactory* IndexedDBContext::GetIDBFactory() { |
38 if (!idb_factory_.get()) | 67 if (!idb_factory_.get()) |
39 idb_factory_.reset(WebIDBFactory::create()); | 68 idb_factory_.reset(WebIDBFactory::create()); |
40 DCHECK(idb_factory_.get()); | 69 DCHECK(idb_factory_.get()); |
41 return idb_factory_.get(); | 70 return idb_factory_.get(); |
42 } | 71 } |
43 | 72 |
44 FilePath IndexedDBContext::GetIndexedDBFilePath( | 73 FilePath IndexedDBContext::GetIndexedDBFilePath( |
45 const string16& origin_id) const { | 74 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); | 75 FilePath::StringType id = webkit_glue::WebStringToFilePathString(origin_id); |
49 return storage_dir.Append(id.append(kIndexedDBExtension)); | 76 return data_path_.Append(id.append(kIndexedDBExtension)); |
50 } | |
51 | |
52 // static | |
53 void IndexedDBContext::ClearLocalState(const FilePath& profile_path, | |
54 const char* url_scheme_to_be_skipped) { | |
55 file_util::FileEnumerator file_enumerator(profile_path.Append( | |
56 kIndexedDBDirectory), false, file_util::FileEnumerator::FILES); | |
57 // TODO(pastarmovj): We might need to consider exchanging this loop for | |
58 // something more efficient in the future. | |
59 for (FilePath file_path = file_enumerator.Next(); !file_path.empty(); | |
60 file_path = file_enumerator.Next()) { | |
61 if (file_path.Extension() != IndexedDBContext::kIndexedDBExtension) | |
62 continue; | |
63 WebSecurityOrigin origin = | |
64 WebSecurityOrigin::createFromDatabaseIdentifier( | |
65 webkit_glue::FilePathToWebString(file_path.BaseName())); | |
66 if (!EqualsASCII(origin.protocol(), url_scheme_to_be_skipped)) | |
67 file_util::Delete(file_path, false); | |
68 } | |
69 } | 77 } |
70 | 78 |
71 void IndexedDBContext::DeleteIndexedDBFile(const FilePath& file_path) { | 79 void IndexedDBContext::DeleteIndexedDBFile(const FilePath& file_path) { |
72 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
73 // TODO(pastarmovj): Close all database connections that use that file. | 81 // TODO(pastarmovj): Close all database connections that use that file. |
74 file_util::Delete(file_path, false); | 82 file_util::Delete(file_path, false); |
75 } | 83 } |
76 | 84 |
77 void IndexedDBContext::DeleteIndexedDBForOrigin(const string16& origin_id) { | 85 void IndexedDBContext::DeleteIndexedDBForOrigin(const string16& origin_id) { |
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); |
79 // TODO(pastarmovj): Remove this check once we are safe to delete any time. | 87 // TODO(pastarmovj): Remove this check once we are safe to delete any time. |
80 FilePath idb_file = GetIndexedDBFilePath(origin_id); | 88 FilePath idb_file = GetIndexedDBFilePath(origin_id); |
81 DCHECK_EQ(idb_file.BaseName().value().substr(0, strlen("chrome-extension")), | 89 DCHECK_EQ(idb_file.BaseName().value().substr(0, strlen("chrome-extension")), |
82 FILE_PATH_LITERAL("chrome-extension")); | 90 FILE_PATH_LITERAL("chrome-extension")); |
83 DeleteIndexedDBFile(GetIndexedDBFilePath(origin_id)); | 91 DeleteIndexedDBFile(GetIndexedDBFilePath(origin_id)); |
84 } | 92 } |
OLD | NEW |