Chromium Code Reviews| 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 | |
| 42 // now. | |
| 43 CommitChanges(); | |
| 44 } | |
| 45 // TODO(benm): What if the last commit failed and there's still data | |
| 46 // left not synced to disk? | |
| 47 DCHECK(!clear_all_next_commit_); | |
| 48 DCHECK(changed_values_.empty()); | |
|
michaeln
2012/02/23 05:15:29
Would it make sense to move these checks into the
benm (inactive)
2012/02/23 12:27:40
Yeah, I actually had the same though re. UMA track
| |
| 23 } | 49 } |
| 24 | 50 |
| 25 unsigned DomStorageArea::Length() { | 51 unsigned DomStorageArea::Length() { |
| 52 InitialImportIfNeeded(); | |
| 26 return map_->Length(); | 53 return map_->Length(); |
| 27 } | 54 } |
| 28 | 55 |
| 29 NullableString16 DomStorageArea::Key(unsigned index) { | 56 NullableString16 DomStorageArea::Key(unsigned index) { |
| 57 InitialImportIfNeeded(); | |
| 30 return map_->Key(index); | 58 return map_->Key(index); |
| 31 } | 59 } |
| 32 | 60 |
| 33 NullableString16 DomStorageArea::GetItem(const string16& key) { | 61 NullableString16 DomStorageArea::GetItem(const string16& key) { |
| 62 InitialImportIfNeeded(); | |
| 34 return map_->GetItem(key); | 63 return map_->GetItem(key); |
| 35 } | 64 } |
| 36 | 65 |
| 37 bool DomStorageArea::SetItem( | 66 bool DomStorageArea::SetItem(const string16& key, |
| 38 const string16& key, const string16& value, | 67 const string16& value, |
| 39 NullableString16* old_value) { | 68 NullableString16* old_value) { |
| 69 InitialImportIfNeeded(); | |
| 70 | |
| 40 if (!map_->HasOneRef()) | 71 if (!map_->HasOneRef()) |
| 41 map_ = map_->DeepCopy(); | 72 map_ = map_->DeepCopy(); |
| 42 return map_->SetItem(key, value, old_value); | 73 bool success = map_->SetItem(key, value, old_value); |
| 74 if (success && backing_.get()) { | |
| 75 changed_values_[key] = NullableString16(value, false); | |
| 76 ScheduleCommitChanges(); | |
| 77 } | |
| 78 return success; | |
| 43 } | 79 } |
| 44 | 80 |
| 45 bool DomStorageArea::RemoveItem( | 81 bool DomStorageArea::RemoveItem(const string16& key, string16* old_value) { |
| 46 const string16& key, | 82 InitialImportIfNeeded(); |
| 47 string16* old_value) { | |
| 48 if (!map_->HasOneRef()) | 83 if (!map_->HasOneRef()) |
| 49 map_ = map_->DeepCopy(); | 84 map_ = map_->DeepCopy(); |
| 50 return map_->RemoveItem(key, old_value); | 85 bool success = map_->RemoveItem(key, old_value); |
| 86 if (success && backing_.get()) { | |
| 87 changed_values_[key] = NullableString16(true); | |
| 88 ScheduleCommitChanges(); | |
| 89 } | |
| 90 return success; | |
| 51 } | 91 } |
| 52 | 92 |
| 53 bool DomStorageArea::Clear() { | 93 bool DomStorageArea::Clear() { |
| 94 InitialImportIfNeeded(); | |
| 54 if (map_->Length() == 0) | 95 if (map_->Length() == 0) |
| 55 return false; | 96 return false; |
| 97 | |
| 56 map_ = new DomStorageMap(kPerAreaQuota); | 98 map_ = new DomStorageMap(kPerAreaQuota); |
| 99 | |
| 100 if (backing_.get()) { | |
| 101 changed_values_.clear(); | |
| 102 clear_all_next_commit_ = true; | |
| 103 ScheduleCommitChanges(); | |
| 104 } | |
| 105 | |
| 57 return true; | 106 return true; |
| 58 } | 107 } |
| 59 | 108 |
| 60 DomStorageArea* DomStorageArea::ShallowCopy(int64 destination_namespace_id) { | 109 DomStorageArea* DomStorageArea::ShallowCopy(int64 destination_namespace_id) { |
| 61 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); | 110 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); |
| 62 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); | 111 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); |
| 63 // SessionNamespaces aren't backed by files on disk. | 112 // SessionNamespaces aren't backed by files on disk. |
| 113 DCHECK(!backing_.get()); | |
| 114 | |
| 64 DomStorageArea* copy = new DomStorageArea(destination_namespace_id, origin_, | 115 DomStorageArea* copy = new DomStorageArea(destination_namespace_id, origin_, |
| 65 FilePath(), task_runner_); | 116 FilePath(), task_runner_); |
| 66 copy->map_ = map_; | 117 copy->map_ = map_; |
| 67 return copy; | 118 return copy; |
| 68 } | 119 } |
| 69 | 120 |
| 121 void DomStorageArea::InitialImportIfNeeded() { | |
| 122 if (initial_import_done_) | |
| 123 return; | |
| 124 | |
| 125 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); | |
| 126 DCHECK(backing_.get()); | |
| 127 | |
| 128 ValuesMap initial_values; | |
| 129 backing_->ReadAllValues(&initial_values); | |
| 130 map_->SwapValues(&initial_values); | |
| 131 initial_import_done_ = true; | |
| 132 } | |
| 133 | |
| 134 void DomStorageArea::ScheduleCommitChanges() { | |
| 135 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); | |
| 136 DCHECK(backing_.get()); | |
| 137 DCHECK(clear_all_next_commit_ || !changed_values_.empty()); | |
| 138 DCHECK(task_runner_.get()); | |
| 139 | |
| 140 // There is no guarantee that an in flight commit will | |
| 141 // ever actually get processed. For example the task_runner_'s | |
| 142 // message loop could unexpectedly quit before the delayed task is | |
| 143 // fired and leave this flag set. But there's no way for us to determine | |
| 144 // that has happened, so in that case, we'll rely on the destructor | |
| 145 // to commit all the remaining changes to disk. | |
|
michaeln
2012/02/23 05:15:29
The comment is more about the code in the dtor tha
benm (inactive)
2012/02/23 12:27:40
sgtm
| |
| 146 if (commit_in_flight_) | |
| 147 return; | |
| 148 | |
| 149 if (!task_runner_->PostDelayedTask( | |
| 150 FROM_HERE, base::Bind(&DomStorageArea::CommitChanges, this), | |
| 151 base::TimeDelta::FromSeconds(1))) { | |
| 152 // If we know that the task will not run, try and save changes synchronously | |
| 153 // so that we don't lose data. | |
|
michaeln
2012/02/23 05:15:29
I think we should not handle this unexpected case
benm (inactive)
2012/02/23 12:27:40
ok, sg.
| |
| 154 CommitChanges(); | |
| 155 } else | |
| 156 commit_in_flight_ = true; | |
| 157 } | |
| 158 | |
| 159 void DomStorageArea::CommitChanges() { | |
| 160 DCHECK(backing_.get()); | |
| 161 if (backing_->CommitChanges(clear_all_next_commit_, changed_values_)) { | |
| 162 clear_all_next_commit_ = false; | |
| 163 changed_values_.clear(); | |
| 164 } | |
| 165 commit_in_flight_ = false; | |
| 166 } | |
| 167 | |
| 168 // static | |
| 169 FilePath DomStorageArea::DatabaseFileNameFromOrigin(const GURL& origin) { | |
| 170 std::string filename = fileapi::GetOriginIdentifierFromURL(origin) | |
| 171 + ".localstorage"; | |
| 172 return FilePath().AppendASCII(filename); | |
| 173 } | |
| 174 | |
| 70 } // namespace dom_storage | 175 } // namespace dom_storage |
| OLD | NEW |