| 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 "webkit/dom_storage/dom_storage_area.h" | 5 #include "webkit/dom_storage/dom_storage_area.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/location.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 #include "base/time.h" | 11 #include "base/time.h" |
| 10 #include "base/tracked_objects.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h" |
| 13 #include "webkit/database/database_util.h" |
| 11 #include "webkit/dom_storage/dom_storage_map.h" | 14 #include "webkit/dom_storage/dom_storage_map.h" |
| 12 #include "webkit/dom_storage/dom_storage_namespace.h" | 15 #include "webkit/dom_storage/dom_storage_namespace.h" |
| 13 #include "webkit/dom_storage/dom_storage_task_runner.h" | 16 #include "webkit/dom_storage/dom_storage_task_runner.h" |
| 14 #include "webkit/dom_storage/dom_storage_types.h" | 17 #include "webkit/dom_storage/dom_storage_types.h" |
| 15 #include "webkit/fileapi/file_system_util.h" | 18 #include "webkit/fileapi/file_system_util.h" |
| 19 #include "webkit/glue/webkit_glue.h" |
| 20 |
| 21 using webkit_database::DatabaseUtil; |
| 16 | 22 |
| 17 namespace dom_storage { | 23 namespace dom_storage { |
| 18 | 24 |
| 19 static const int kCommitTimerSeconds = 1; | 25 static const int kCommitTimerSeconds = 1; |
| 20 | 26 |
| 21 DomStorageArea::CommitBatch::CommitBatch() | 27 DomStorageArea::CommitBatch::CommitBatch() |
| 22 : clear_all_first(false) { | 28 : clear_all_first(false) { |
| 23 } | 29 } |
| 24 DomStorageArea::CommitBatch::~CommitBatch() {} | 30 DomStorageArea::CommitBatch::~CommitBatch() {} |
| 25 | 31 |
| 26 | 32 |
| 27 // static | 33 // static |
| 28 const FilePath::CharType DomStorageArea::kDatabaseFileExtension[] = | 34 const FilePath::CharType DomStorageArea::kDatabaseFileExtension[] = |
| 29 FILE_PATH_LITERAL(".localstorage"); | 35 FILE_PATH_LITERAL(".localstorage"); |
| 30 | 36 |
| 31 // static | 37 // static |
| 32 FilePath DomStorageArea::DatabaseFileNameFromOrigin(const GURL& origin) { | 38 FilePath DomStorageArea::DatabaseFileNameFromOrigin(const GURL& origin) { |
| 33 std::string filename = fileapi::GetOriginIdentifierFromURL(origin); | 39 std::string filename = fileapi::GetOriginIdentifierFromURL(origin); |
| 34 // There is no FilePath.AppendExtension() method, so start with just the | 40 // There is no FilePath.AppendExtension() method, so start with just the |
| 35 // extension as the filename, and then InsertBeforeExtension the desired | 41 // extension as the filename, and then InsertBeforeExtension the desired |
| 36 // name. | 42 // name. |
| 37 return FilePath().Append(kDatabaseFileExtension). | 43 return FilePath().Append(kDatabaseFileExtension). |
| 38 InsertBeforeExtensionASCII(filename); | 44 InsertBeforeExtensionASCII(filename); |
| 39 } | 45 } |
| 40 | 46 |
| 47 // static |
| 48 GURL DomStorageArea::OriginFromDatabaseFileName(const FilePath& name) { |
| 49 DCHECK(name.MatchesExtension(kDatabaseFileExtension)); |
| 50 WebKit::WebString origin_id = webkit_glue::FilePathToWebString( |
| 51 name.BaseName().RemoveExtension()); |
| 52 return DatabaseUtil::GetOriginFromIdentifier(origin_id); |
| 53 } |
| 54 |
| 41 DomStorageArea::DomStorageArea( | 55 DomStorageArea::DomStorageArea( |
| 42 int64 namespace_id, const GURL& origin, | 56 int64 namespace_id, const GURL& origin, |
| 43 const FilePath& directory, DomStorageTaskRunner* task_runner) | 57 const FilePath& directory, DomStorageTaskRunner* task_runner) |
| 44 : namespace_id_(namespace_id), origin_(origin), | 58 : namespace_id_(namespace_id), origin_(origin), |
| 45 directory_(directory), | 59 directory_(directory), |
| 46 task_runner_(task_runner), | 60 task_runner_(task_runner), |
| 47 map_(new DomStorageMap(kPerAreaQuota)), | 61 map_(new DomStorageMap(kPerAreaQuota)), |
| 48 is_initial_import_done_(true), | 62 is_initial_import_done_(true), |
| 49 is_shutdown_(false) { | 63 is_shutdown_(false) { |
| 50 if (namespace_id == kLocalStorageNamespaceId && !directory.empty()) { | 64 if (namespace_id == kLocalStorageNamespaceId && !directory.empty()) { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); | 145 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); |
| 132 DCHECK(!backing_.get()); // SessionNamespaces aren't stored on disk. | 146 DCHECK(!backing_.get()); // SessionNamespaces aren't stored on disk. |
| 133 | 147 |
| 134 DomStorageArea* copy = new DomStorageArea(destination_namespace_id, origin_, | 148 DomStorageArea* copy = new DomStorageArea(destination_namespace_id, origin_, |
| 135 FilePath(), task_runner_); | 149 FilePath(), task_runner_); |
| 136 copy->map_ = map_; | 150 copy->map_ = map_; |
| 137 copy->is_shutdown_ = is_shutdown_; | 151 copy->is_shutdown_ = is_shutdown_; |
| 138 return copy; | 152 return copy; |
| 139 } | 153 } |
| 140 | 154 |
| 155 bool DomStorageArea::HasUncommittedChanges() const { |
| 156 DCHECK(!is_shutdown_); |
| 157 return commit_batch_.get() || in_flight_commit_batch_.get(); |
| 158 } |
| 159 |
| 160 void DomStorageArea::DeleteOrigin() { |
| 161 DCHECK(!is_shutdown_); |
| 162 if (HasUncommittedChanges()) { |
| 163 // TODO(michaeln): This logically deletes the data immediately, |
| 164 // and in a matter of a second, deletes the rows from the backing |
| 165 // database file, but the file itself will linger until shutdown |
| 166 // or purge time. Ideally, this should delete the file more |
| 167 // quickly. |
| 168 Clear(); |
| 169 return; |
| 170 } |
| 171 map_ = new DomStorageMap(kPerAreaQuota); |
| 172 if (backing_.get()) { |
| 173 is_initial_import_done_ = false; |
| 174 backing_.reset(new DomStorageDatabase(backing_->file_path())); |
| 175 file_util::Delete(backing_->file_path(), false); |
| 176 file_util::Delete( |
| 177 DomStorageDatabase::GetJournalFilePath(backing_->file_path()), false); |
| 178 } |
| 179 } |
| 180 |
| 181 void DomStorageArea::PurgeMemory() { |
| 182 DCHECK(!is_shutdown_); |
| 183 if (!is_initial_import_done_ || // We're not using any memory. |
| 184 !backing_.get() || // We can't purge anything. |
| 185 HasUncommittedChanges()) // We leave things alone with changes pending. |
| 186 return; |
| 187 |
| 188 // Drop the in memory cache, we'll reload when needed. |
| 189 is_initial_import_done_ = false; |
| 190 map_ = new DomStorageMap(kPerAreaQuota); |
| 191 |
| 192 // Recreate the database object, this frees up the open sqlite connection |
| 193 // and its page cache. |
| 194 backing_.reset(new DomStorageDatabase(backing_->file_path())); |
| 195 } |
| 196 |
| 141 void DomStorageArea::Shutdown() { | 197 void DomStorageArea::Shutdown() { |
| 142 DCHECK(!is_shutdown_); | 198 DCHECK(!is_shutdown_); |
| 143 is_shutdown_ = true; | 199 is_shutdown_ = true; |
| 144 map_ = NULL; | 200 map_ = NULL; |
| 145 if (!backing_.get()) | 201 if (!backing_.get()) |
| 146 return; | 202 return; |
| 147 | 203 |
| 148 bool success = task_runner_->PostShutdownBlockingTask( | 204 bool success = task_runner_->PostShutdownBlockingTask( |
| 149 FROM_HERE, | 205 FROM_HERE, |
| 150 DomStorageTaskRunner::COMMIT_SEQUENCE, | 206 DomStorageTaskRunner::COMMIT_SEQUENCE, |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 commit_batch_->clear_all_first, | 293 commit_batch_->clear_all_first, |
| 238 commit_batch_->changed_values); | 294 commit_batch_->changed_values); |
| 239 DCHECK(success); | 295 DCHECK(success); |
| 240 } | 296 } |
| 241 commit_batch_.reset(); | 297 commit_batch_.reset(); |
| 242 in_flight_commit_batch_.reset(); | 298 in_flight_commit_batch_.reset(); |
| 243 backing_.reset(); | 299 backing_.reset(); |
| 244 } | 300 } |
| 245 | 301 |
| 246 } // namespace dom_storage | 302 } // namespace dom_storage |
| OLD | NEW |