OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/in_process_webkit/indexed_db_context.h" | 5 #include "content/browser/in_process_webkit/indexed_db_context.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 using WebKit::WebIDBDatabase; | 31 using WebKit::WebIDBDatabase; |
32 using WebKit::WebIDBFactory; | 32 using WebKit::WebIDBFactory; |
33 using WebKit::WebSecurityOrigin; | 33 using WebKit::WebSecurityOrigin; |
34 | 34 |
35 namespace { | 35 namespace { |
36 | 36 |
37 void GetAllOriginsAndPaths( | 37 void GetAllOriginsAndPaths( |
38 const FilePath& indexeddb_path, | 38 const FilePath& indexeddb_path, |
39 std::vector<GURL>* origins, | 39 std::vector<GURL>* origins, |
40 std::vector<FilePath>* file_paths) { | 40 std::vector<FilePath>* file_paths) { |
41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 41 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
42 if (indexeddb_path.empty()) | 42 if (indexeddb_path.empty()) |
43 return; | 43 return; |
44 file_util::FileEnumerator file_enumerator(indexeddb_path, | 44 file_util::FileEnumerator file_enumerator(indexeddb_path, |
45 false, file_util::FileEnumerator::DIRECTORIES); | 45 false, file_util::FileEnumerator::DIRECTORIES); |
46 for (FilePath file_path = file_enumerator.Next(); !file_path.empty(); | 46 for (FilePath file_path = file_enumerator.Next(); !file_path.empty(); |
47 file_path = file_enumerator.Next()) { | 47 file_path = file_enumerator.Next()) { |
48 if (file_path.Extension() == IndexedDBContext::kIndexedDBExtension) { | 48 if (file_path.Extension() == IndexedDBContext::kIndexedDBExtension) { |
49 WebKit::WebString origin_id_webstring = | 49 WebKit::WebString origin_id_webstring = |
50 webkit_glue::FilePathToWebString(file_path.BaseName()); | 50 webkit_glue::FilePathToWebString(file_path.BaseName()); |
51 origins->push_back( | 51 origins->push_back( |
52 DatabaseUtil::GetOriginFromIdentifier(origin_id_webstring)); | 52 DatabaseUtil::GetOriginFromIdentifier(origin_id_webstring)); |
53 if (file_paths) | 53 if (file_paths) |
54 file_paths->push_back(file_path); | 54 file_paths->push_back(file_path); |
55 } | 55 } |
56 } | 56 } |
57 } | 57 } |
58 | 58 |
59 // If clear_all_databases is true, deletes all databases not protected by | 59 // If clear_all_databases is true, deletes all databases not protected by |
60 // special storage policy. Otherwise deletes session-only databases. | 60 // special storage policy. Otherwise deletes session-only databases. |
61 void ClearLocalState( | 61 void ClearLocalState( |
62 const FilePath& indexeddb_path, | 62 const FilePath& indexeddb_path, |
63 bool clear_all_databases, | 63 bool clear_all_databases, |
64 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy) { | 64 scoped_refptr<quota::SpecialStoragePolicy> special_storage_policy) { |
65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
66 std::vector<GURL> origins; | 66 std::vector<GURL> origins; |
67 std::vector<FilePath> file_paths; | 67 std::vector<FilePath> file_paths; |
68 GetAllOriginsAndPaths(indexeddb_path, &origins, &file_paths); | 68 GetAllOriginsAndPaths(indexeddb_path, &origins, &file_paths); |
69 DCHECK_EQ(origins.size(), file_paths.size()); | 69 DCHECK_EQ(origins.size(), file_paths.size()); |
70 std::vector<FilePath>::const_iterator file_path_iter = file_paths.begin(); | 70 std::vector<FilePath>::const_iterator file_path_iter = file_paths.begin(); |
71 for (std::vector<GURL>::const_iterator iter = origins.begin(); | 71 for (std::vector<GURL>::const_iterator iter = origins.begin(); |
72 iter != origins.end(); ++iter, ++file_path_iter) { | 72 iter != origins.end(); ++iter, ++file_path_iter) { |
73 if (!clear_all_databases && | 73 if (!clear_all_databases && |
74 !special_storage_policy->IsStorageSessionOnly(*iter)) { | 74 !special_storage_policy->IsStorageSessionOnly(*iter)) { |
75 continue; | 75 continue; |
(...skipping 26 matching lines...) Expand all Loading... |
102 data_path_ = webkit_context->data_path().Append(kIndexedDBDirectory); | 102 data_path_ = webkit_context->data_path().Append(kIndexedDBDirectory); |
103 if (quota_manager_proxy && | 103 if (quota_manager_proxy && |
104 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess)) { | 104 !CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess)) { |
105 quota_manager_proxy->RegisterClient( | 105 quota_manager_proxy->RegisterClient( |
106 new IndexedDBQuotaClient(webkit_thread_loop, this)); | 106 new IndexedDBQuotaClient(webkit_thread_loop, this)); |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
110 IndexedDBContext::~IndexedDBContext() { | 110 IndexedDBContext::~IndexedDBContext() { |
111 WebKit::WebIDBFactory* factory = idb_factory_.release(); | 111 WebKit::WebIDBFactory* factory = idb_factory_.release(); |
112 if (factory) | 112 if (factory) { |
113 BrowserThread::DeleteSoon(BrowserThread::WEBKIT, FROM_HERE, factory); | 113 if (!BrowserThread::DeleteSoon(BrowserThread::WEBKIT_DEPRECATED, |
| 114 FROM_HERE, factory)) |
| 115 delete factory; |
| 116 } |
114 | 117 |
115 if (data_path_.empty()) | 118 if (data_path_.empty()) |
116 return; | 119 return; |
117 | 120 |
118 if (save_session_state_) | 121 if (save_session_state_) |
119 return; | 122 return; |
120 | 123 |
121 bool has_session_only_databases = | 124 bool has_session_only_databases = |
122 special_storage_policy_.get() && | 125 special_storage_policy_.get() && |
123 special_storage_policy_->HasSessionOnlyOrigins(); | 126 special_storage_policy_->HasSessionOnlyOrigins(); |
124 | 127 |
125 // Clearning only session-only databases, and there are none. | 128 // Clearning only session-only databases, and there are none. |
126 if (!clear_local_state_on_exit_ && !has_session_only_databases) | 129 if (!clear_local_state_on_exit_ && !has_session_only_databases) |
127 return; | 130 return; |
128 | 131 |
129 // No WEBKIT thread here means we are running in a unit test where no clean | 132 // No WEBKIT thread here means we are running in a unit test where no clean |
130 // up is needed. | 133 // up is needed. |
131 BrowserThread::PostTask( | 134 BrowserThread::PostTask( |
132 BrowserThread::WEBKIT, FROM_HERE, | 135 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE, |
133 base::Bind(&ClearLocalState, data_path_, clear_local_state_on_exit_, | 136 base::Bind(&ClearLocalState, data_path_, clear_local_state_on_exit_, |
134 special_storage_policy_)); | 137 special_storage_policy_)); |
135 } | 138 } |
136 | 139 |
137 WebIDBFactory* IndexedDBContext::GetIDBFactory() { | 140 WebIDBFactory* IndexedDBContext::GetIDBFactory() { |
138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 141 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
139 if (!idb_factory_.get()) { | 142 if (!idb_factory_.get()) { |
140 // Prime our cache of origins with existing databases so we can | 143 // Prime our cache of origins with existing databases so we can |
141 // detect when dbs are newly created. | 144 // detect when dbs are newly created. |
142 GetOriginSet(); | 145 GetOriginSet(); |
143 idb_factory_.reset(WebIDBFactory::create()); | 146 idb_factory_.reset(WebIDBFactory::create()); |
144 } | 147 } |
145 return idb_factory_.get(); | 148 return idb_factory_.get(); |
146 } | 149 } |
147 | 150 |
148 void IndexedDBContext::DeleteIndexedDBForOrigin(const GURL& origin_url) { | 151 void IndexedDBContext::DeleteIndexedDBForOrigin(const GURL& origin_url) { |
149 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 152 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
150 if (data_path_.empty() || !IsInOriginSet(origin_url)) | 153 if (data_path_.empty() || !IsInOriginSet(origin_url)) |
151 return; | 154 return; |
152 // TODO(michaeln): When asked to delete an origin with open connections, | 155 // TODO(michaeln): When asked to delete an origin with open connections, |
153 // forcibly close those connections then delete. | 156 // forcibly close those connections then delete. |
154 if (connection_count_.find(origin_url) == connection_count_.end()) { | 157 if (connection_count_.find(origin_url) == connection_count_.end()) { |
155 string16 origin_id = DatabaseUtil::GetOriginIdentifier(origin_url); | 158 string16 origin_id = DatabaseUtil::GetOriginIdentifier(origin_url); |
156 FilePath idb_directory = GetIndexedDBFilePath(origin_id); | 159 FilePath idb_directory = GetIndexedDBFilePath(origin_id); |
157 EnsureDiskUsageCacheInitialized(origin_url); | 160 EnsureDiskUsageCacheInitialized(origin_url); |
158 bool deleted = file_util::Delete(idb_directory, true /*recursive*/); | 161 bool deleted = file_util::Delete(idb_directory, true /*recursive*/); |
159 QueryDiskAndUpdateQuotaUsage(origin_url); | 162 QueryDiskAndUpdateQuotaUsage(origin_url); |
160 if (deleted) { | 163 if (deleted) { |
161 RemoveFromOriginSet(origin_url); | 164 RemoveFromOriginSet(origin_url); |
162 origin_size_map_.erase(origin_url); | 165 origin_size_map_.erase(origin_url); |
163 space_available_map_.erase(origin_url); | 166 space_available_map_.erase(origin_url); |
164 } | 167 } |
165 } | 168 } |
166 } | 169 } |
167 | 170 |
168 void IndexedDBContext::GetAllOrigins(std::vector<GURL>* origins) { | 171 void IndexedDBContext::GetAllOrigins(std::vector<GURL>* origins) { |
169 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
170 std::set<GURL>* origins_set = GetOriginSet(); | 173 std::set<GURL>* origins_set = GetOriginSet(); |
171 for (std::set<GURL>::const_iterator iter = origins_set->begin(); | 174 for (std::set<GURL>::const_iterator iter = origins_set->begin(); |
172 iter != origins_set->end(); ++iter) { | 175 iter != origins_set->end(); ++iter) { |
173 origins->push_back(*iter); | 176 origins->push_back(*iter); |
174 } | 177 } |
175 } | 178 } |
176 | 179 |
177 int64 IndexedDBContext::GetOriginDiskUsage(const GURL& origin_url) { | 180 int64 IndexedDBContext::GetOriginDiskUsage(const GURL& origin_url) { |
178 if (data_path_.empty() || !IsInOriginSet(origin_url)) | 181 if (data_path_.empty() || !IsInOriginSet(origin_url)) |
179 return 0; | 182 return 0; |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 quota::QuotaStatusCode status, | 292 quota::QuotaStatusCode status, |
290 int64 usage, int64 quota) { | 293 int64 usage, int64 quota) { |
291 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 294 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
292 DCHECK(status == quota::kQuotaStatusOk || status == quota::kQuotaErrorAbort) | 295 DCHECK(status == quota::kQuotaStatusOk || status == quota::kQuotaErrorAbort) |
293 << "status was " << status; | 296 << "status was " << status; |
294 if (status == quota::kQuotaErrorAbort) { | 297 if (status == quota::kQuotaErrorAbort) { |
295 // We seem to no longer care to wait around for the answer. | 298 // We seem to no longer care to wait around for the answer. |
296 return; | 299 return; |
297 } | 300 } |
298 BrowserThread::PostTask( | 301 BrowserThread::PostTask( |
299 BrowserThread::WEBKIT, FROM_HERE, | 302 BrowserThread::WEBKIT_DEPRECATED, FROM_HERE, |
300 base::Bind(&IndexedDBContext::GotUpdatedQuota, this, origin_url, usage, | 303 base::Bind(&IndexedDBContext::GotUpdatedQuota, this, origin_url, usage, |
301 quota)); | 304 quota)); |
302 } | 305 } |
303 | 306 |
304 void IndexedDBContext::GotUpdatedQuota(const GURL& origin_url, int64 usage, | 307 void IndexedDBContext::GotUpdatedQuota(const GURL& origin_url, int64 usage, |
305 int64 quota) { | 308 int64 quota) { |
306 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 309 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
307 space_available_map_[origin_url] = quota - usage; | 310 space_available_map_[origin_url] = quota - usage; |
308 } | 311 } |
309 | 312 |
310 void IndexedDBContext::QueryAvailableQuota(const GURL& origin_url) { | 313 void IndexedDBContext::QueryAvailableQuota(const GURL& origin_url) { |
311 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 314 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
312 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT)); | 315 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::WEBKIT_DEPRECATED)); |
313 if (quota_manager_proxy()) | 316 if (quota_manager_proxy()) |
314 BrowserThread::PostTask( | 317 BrowserThread::PostTask( |
315 BrowserThread::IO, FROM_HERE, | 318 BrowserThread::IO, FROM_HERE, |
316 base::Bind(&IndexedDBContext::QueryAvailableQuota, this, origin_url)); | 319 base::Bind(&IndexedDBContext::QueryAvailableQuota, this, origin_url)); |
317 return; | 320 return; |
318 } | 321 } |
319 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
320 if (!quota_manager_proxy()->quota_manager()) | 323 if (!quota_manager_proxy()->quota_manager()) |
321 return; | 324 return; |
322 quota_manager_proxy()->quota_manager()->GetUsageAndQuota( | 325 quota_manager_proxy()->quota_manager()->GetUsageAndQuota( |
(...skipping 14 matching lines...) Expand all Loading... |
337 } | 340 } |
338 } | 341 } |
339 return origin_set_.get(); | 342 return origin_set_.get(); |
340 } | 343 } |
341 | 344 |
342 void IndexedDBContext::ResetCaches() { | 345 void IndexedDBContext::ResetCaches() { |
343 origin_set_.reset(); | 346 origin_set_.reset(); |
344 origin_size_map_.clear(); | 347 origin_size_map_.clear(); |
345 space_available_map_.clear(); | 348 space_available_map_.clear(); |
346 } | 349 } |
OLD | NEW |