| 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 |
| 7 #include "base/bind.h" |
| 8 #include "base/time.h" |
| 6 #include "webkit/dom_storage/dom_storage_map.h" | 9 #include "webkit/dom_storage/dom_storage_map.h" |
| 7 #include "webkit/dom_storage/dom_storage_namespace.h" | 10 #include "webkit/dom_storage/dom_storage_namespace.h" |
| 11 #include "webkit/dom_storage/dom_storage_task_runner.h" |
| 8 #include "webkit/dom_storage/dom_storage_types.h" | 12 #include "webkit/dom_storage/dom_storage_types.h" |
| 13 #include "webkit/fileapi/file_system_util.h" |
| 9 | 14 |
| 10 namespace dom_storage { | 15 namespace dom_storage { |
| 11 | 16 |
| 12 DomStorageArea::DomStorageArea( | 17 DomStorageArea::DomStorageArea( |
| 13 int64 namespace_id, const GURL& origin, | 18 int64 namespace_id, const GURL& origin, |
| 14 const FilePath& directory, DomStorageTaskRunner* task_runner) | 19 const FilePath& directory, DomStorageTaskRunner* task_runner) |
| 15 : namespace_id_(namespace_id), | 20 : namespace_id_(namespace_id), origin_(origin), |
| 16 origin_(origin), | |
| 17 directory_(directory), | 21 directory_(directory), |
| 18 task_runner_(task_runner), | 22 task_runner_(task_runner), |
| 19 map_(new DomStorageMap(kPerAreaQuota)) { | 23 map_(new DomStorageMap(kPerAreaQuota)), |
| 24 backing_(NULL), |
| 25 initial_import_done_(false), |
| 26 clear_all_next_commit_(false), |
| 27 commit_in_flight_(false) { |
| 28 |
| 29 if (namespace_id == kLocalStorageNamespaceId && !directory.empty()) { |
| 30 FilePath path = directory.Append(DatabaseFileNameFromOrigin(origin_)); |
| 31 backing_.reset(new DomStorageDatabase(path)); |
| 32 } else { |
| 33 // Not a local storage area or no directory specified for backing |
| 34 // database, (i.e. it's an incognito profile). |
| 35 initial_import_done_ = true; |
| 36 } |
| 20 } | 37 } |
| 21 | 38 |
| 22 DomStorageArea::~DomStorageArea() { | 39 DomStorageArea::~DomStorageArea() { |
| 40 if (clear_all_next_commit_ || !changed_values_.empty()) { |
| 41 // Still some data left that was not committed to disk, try now. |
| 42 // We do this regardless of whether we think a commit is in flight |
| 43 // as there is no guarantee that that commit will actually get |
| 44 // processed. For example the task_runner_'s message loop could |
| 45 // unexpectedly quit before the delayed task is fired and leave the |
| 46 // commit_in_flight_ flag set. But there's no way for us to determine |
| 47 // that has happened so force a commit now. |
| 48 |
| 49 CommitChanges(); |
| 50 |
| 51 // TODO(benm): It's possible that the commit failed, and in |
| 52 // that case we're going to lose data. Integrate with UMA |
| 53 // to gather stats about how often this actually happens, |
| 54 // so that we can figure out a contingency plan. |
| 55 } |
| 23 } | 56 } |
| 24 | 57 |
| 25 unsigned DomStorageArea::Length() { | 58 unsigned DomStorageArea::Length() { |
| 59 InitialImportIfNeeded(); |
| 26 return map_->Length(); | 60 return map_->Length(); |
| 27 } | 61 } |
| 28 | 62 |
| 29 NullableString16 DomStorageArea::Key(unsigned index) { | 63 NullableString16 DomStorageArea::Key(unsigned index) { |
| 64 InitialImportIfNeeded(); |
| 30 return map_->Key(index); | 65 return map_->Key(index); |
| 31 } | 66 } |
| 32 | 67 |
| 33 NullableString16 DomStorageArea::GetItem(const string16& key) { | 68 NullableString16 DomStorageArea::GetItem(const string16& key) { |
| 69 InitialImportIfNeeded(); |
| 34 return map_->GetItem(key); | 70 return map_->GetItem(key); |
| 35 } | 71 } |
| 36 | 72 |
| 37 bool DomStorageArea::SetItem( | 73 bool DomStorageArea::SetItem(const string16& key, |
| 38 const string16& key, const string16& value, | 74 const string16& value, |
| 39 NullableString16* old_value) { | 75 NullableString16* old_value) { |
| 76 InitialImportIfNeeded(); |
| 77 |
| 40 if (!map_->HasOneRef()) | 78 if (!map_->HasOneRef()) |
| 41 map_ = map_->DeepCopy(); | 79 map_ = map_->DeepCopy(); |
| 42 return map_->SetItem(key, value, old_value); | 80 bool success = map_->SetItem(key, value, old_value); |
| 81 if (success && backing_.get()) { |
| 82 changed_values_[key] = NullableString16(value, false); |
| 83 ScheduleCommitChanges(); |
| 84 } |
| 85 return success; |
| 43 } | 86 } |
| 44 | 87 |
| 45 bool DomStorageArea::RemoveItem( | 88 bool DomStorageArea::RemoveItem(const string16& key, string16* old_value) { |
| 46 const string16& key, | 89 InitialImportIfNeeded(); |
| 47 string16* old_value) { | |
| 48 if (!map_->HasOneRef()) | 90 if (!map_->HasOneRef()) |
| 49 map_ = map_->DeepCopy(); | 91 map_ = map_->DeepCopy(); |
| 50 return map_->RemoveItem(key, old_value); | 92 bool success = map_->RemoveItem(key, old_value); |
| 93 if (success && backing_.get()) { |
| 94 changed_values_[key] = NullableString16(true); |
| 95 ScheduleCommitChanges(); |
| 96 } |
| 97 return success; |
| 51 } | 98 } |
| 52 | 99 |
| 53 bool DomStorageArea::Clear() { | 100 bool DomStorageArea::Clear() { |
| 101 InitialImportIfNeeded(); |
| 54 if (map_->Length() == 0) | 102 if (map_->Length() == 0) |
| 55 return false; | 103 return false; |
| 104 |
| 56 map_ = new DomStorageMap(kPerAreaQuota); | 105 map_ = new DomStorageMap(kPerAreaQuota); |
| 106 |
| 107 if (backing_.get()) { |
| 108 changed_values_.clear(); |
| 109 clear_all_next_commit_ = true; |
| 110 ScheduleCommitChanges(); |
| 111 } |
| 112 |
| 57 return true; | 113 return true; |
| 58 } | 114 } |
| 59 | 115 |
| 60 DomStorageArea* DomStorageArea::ShallowCopy(int64 destination_namespace_id) { | 116 DomStorageArea* DomStorageArea::ShallowCopy(int64 destination_namespace_id) { |
| 61 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); | 117 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); |
| 62 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); | 118 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); |
| 63 // SessionNamespaces aren't backed by files on disk. | 119 // SessionNamespaces aren't backed by files on disk. |
| 120 DCHECK(!backing_.get()); |
| 121 |
| 64 DomStorageArea* copy = new DomStorageArea(destination_namespace_id, origin_, | 122 DomStorageArea* copy = new DomStorageArea(destination_namespace_id, origin_, |
| 65 FilePath(), task_runner_); | 123 FilePath(), task_runner_); |
| 66 copy->map_ = map_; | 124 copy->map_ = map_; |
| 67 return copy; | 125 return copy; |
| 68 } | 126 } |
| 69 | 127 |
| 128 void DomStorageArea::InitialImportIfNeeded() { |
| 129 if (initial_import_done_) |
| 130 return; |
| 131 |
| 132 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); |
| 133 DCHECK(backing_.get()); |
| 134 |
| 135 ValuesMap initial_values; |
| 136 backing_->ReadAllValues(&initial_values); |
| 137 map_->SwapValues(&initial_values); |
| 138 initial_import_done_ = true; |
| 139 } |
| 140 |
| 141 void DomStorageArea::ScheduleCommitChanges() { |
| 142 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); |
| 143 DCHECK(backing_.get()); |
| 144 DCHECK(clear_all_next_commit_ || !changed_values_.empty()); |
| 145 DCHECK(task_runner_.get()); |
| 146 |
| 147 if (commit_in_flight_) |
| 148 return; |
| 149 |
| 150 commit_in_flight_ = task_runner_->PostDelayedTask( |
| 151 FROM_HERE, base::Bind(&DomStorageArea::CommitChanges, this), |
| 152 base::TimeDelta::FromSeconds(1)); |
| 153 DCHECK(commit_in_flight_); |
| 154 } |
| 155 |
| 156 void DomStorageArea::CommitChanges() { |
| 157 DCHECK(backing_.get()); |
| 158 if (backing_->CommitChanges(clear_all_next_commit_, changed_values_)) { |
| 159 clear_all_next_commit_ = false; |
| 160 changed_values_.clear(); |
| 161 } |
| 162 commit_in_flight_ = false; |
| 163 } |
| 164 |
| 165 // static |
| 166 FilePath DomStorageArea::DatabaseFileNameFromOrigin(const GURL& origin) { |
| 167 std::string filename = fileapi::GetOriginIdentifierFromURL(origin) |
| 168 + ".localstorage"; |
| 169 return FilePath().AppendASCII(filename); |
| 170 } |
| 171 |
| 70 } // namespace dom_storage | 172 } // namespace dom_storage |
| OLD | NEW |