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" | |
| 6 #include "webkit/dom_storage/dom_storage_map.h" | 8 #include "webkit/dom_storage/dom_storage_map.h" |
| 7 #include "webkit/dom_storage/dom_storage_namespace.h" | 9 #include "webkit/dom_storage/dom_storage_namespace.h" |
| 10 #include "webkit/dom_storage/dom_storage_task_runner.h" | |
| 11 #include "webkit/fileapi/file_system_util.h" | |
| 8 | 12 |
| 9 namespace dom_storage { | 13 namespace dom_storage { |
| 10 | 14 |
| 11 DomStorageArea::DomStorageArea( | 15 DomStorageArea::DomStorageArea( |
| 12 int64 namespace_id, const GURL& origin, | 16 int64 namespace_id, const GURL& origin, |
| 13 const FilePath& directory, DomStorageTaskRunner* task_runner) | 17 const FilePath& directory, DomStorageTaskRunner* task_runner) |
| 14 : namespace_id_(namespace_id), origin_(origin), | 18 : namespace_id_(namespace_id), origin_(origin), |
| 15 directory_(directory), task_runner_(task_runner), | 19 directory_(directory), task_runner_(task_runner), |
| 16 map_(new DomStorageMap()) { | 20 map_(new DomStorageMap()), |
| 21 backing_(NULL), | |
| 22 initial_import_done_(false), | |
| 23 clear_all_next_commit_(false) { | |
| 24 | |
| 25 if (namespace_id == kLocalStorageNamespaceId && !directory.empty()) { | |
| 26 FilePath path = directory.Append(DatabaseFileNameFromOrigin(origin_)); | |
| 27 backing_.reset(new DomStorageDatabase(path)); | |
| 28 } else { | |
| 29 // Not a local storage area or no directory specified for backing | |
| 30 // database, (i.e. it's an incognito profile). | |
| 31 initial_import_done_ = true; | |
| 32 } | |
| 17 } | 33 } |
| 18 | 34 |
| 19 DomStorageArea::~DomStorageArea() { | 35 DomStorageArea::~DomStorageArea() { |
| 36 if (clear_all_next_commit_ || !changed_values_.empty()) { | |
| 37 // Still some data left that was not committed to disk, try | |
| 38 // now. | |
| 39 CommitChanges(); | |
| 40 } | |
| 41 // TODO(benm): What if the last commit failed and there's still data | |
| 42 // left not synced to disk? | |
| 43 DCHECK(!clear_all_next_commit_); | |
| 44 DCHECK(changed_values_.empty()); | |
| 20 } | 45 } |
| 21 | 46 |
| 22 unsigned DomStorageArea::Length() { | 47 unsigned DomStorageArea::Length() { |
| 48 InitialImportIfNeeded(); | |
| 23 return map_->Length(); | 49 return map_->Length(); |
| 24 } | 50 } |
| 25 | 51 |
| 26 NullableString16 DomStorageArea::Key(unsigned index) { | 52 NullableString16 DomStorageArea::Key(unsigned index) { |
| 53 InitialImportIfNeeded(); | |
| 27 return map_->Key(index); | 54 return map_->Key(index); |
| 28 } | 55 } |
| 29 | 56 |
| 30 NullableString16 DomStorageArea::GetItem(const string16& key) { | 57 NullableString16 DomStorageArea::GetItem(const string16& key) { |
| 58 InitialImportIfNeeded(); | |
| 31 return map_->GetItem(key); | 59 return map_->GetItem(key); |
| 32 } | 60 } |
| 33 | 61 |
| 34 bool DomStorageArea::SetItem( | 62 bool DomStorageArea::SetItem(const string16& key, |
| 35 const string16& key, const string16& value, | 63 const string16& value, |
| 36 NullableString16* old_value) { | 64 NullableString16* old_value) { |
| 65 InitialImportIfNeeded(); | |
| 66 | |
| 37 if (!map_->HasOneRef()) | 67 if (!map_->HasOneRef()) |
| 38 map_ = map_->DeepCopy(); | 68 map_ = map_->DeepCopy(); |
| 39 return map_->SetItem(key, value, old_value); | 69 bool success = map_->SetItem(key, value, old_value); |
| 70 if (success && backing_.get()) { | |
| 71 changed_values_[key] = NullableString16(value, false); | |
| 72 ScheduleCommitChanges(); | |
| 73 } | |
| 74 return success; | |
| 40 } | 75 } |
| 41 | 76 |
| 42 bool DomStorageArea::RemoveItem( | 77 bool DomStorageArea::RemoveItem(const string16& key, string16* old_value) { |
| 43 const string16& key, | 78 InitialImportIfNeeded(); |
| 44 string16* old_value) { | |
| 45 if (!map_->HasOneRef()) | 79 if (!map_->HasOneRef()) |
| 46 map_ = map_->DeepCopy(); | 80 map_ = map_->DeepCopy(); |
| 47 return map_->RemoveItem(key, old_value); | 81 bool success = map_->RemoveItem(key, old_value); |
| 82 if (success && backing_.get()) { | |
| 83 changed_values_[key] = NullableString16(true); | |
| 84 ScheduleCommitChanges(); | |
| 85 } | |
| 86 return success; | |
| 48 } | 87 } |
| 49 | 88 |
| 50 bool DomStorageArea::Clear() { | 89 bool DomStorageArea::Clear() { |
| 90 InitialImportIfNeeded(); | |
| 51 if (map_->Length() == 0) | 91 if (map_->Length() == 0) |
| 52 return false; | 92 return false; |
| 93 | |
| 53 map_ = new DomStorageMap(); | 94 map_ = new DomStorageMap(); |
| 95 | |
| 96 if (backing_.get()) { | |
| 97 changed_values_.clear(); | |
| 98 clear_all_next_commit_ = true; | |
| 99 ScheduleCommitChanges(); | |
| 100 } | |
| 101 | |
| 54 return true; | 102 return true; |
| 55 } | 103 } |
| 56 | 104 |
| 57 DomStorageArea* DomStorageArea::ShallowCopy(int64 destination_namespace_id) { | 105 DomStorageArea* DomStorageArea::ShallowCopy(int64 destination_namespace_id) { |
| 58 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); | 106 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); |
| 59 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); | 107 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); |
| 60 // SessionNamespaces aren't backed by files on disk. | 108 // SessionNamespaces aren't backed by files on disk. |
| 109 DCHECK(!backing_.get()); | |
| 110 | |
| 61 DomStorageArea* copy = new DomStorageArea(destination_namespace_id, origin_, | 111 DomStorageArea* copy = new DomStorageArea(destination_namespace_id, origin_, |
| 62 FilePath(), task_runner_); | 112 FilePath(), task_runner_); |
| 63 copy->map_ = map_; | 113 copy->map_ = map_; |
| 64 return copy; | 114 return copy; |
| 65 } | 115 } |
| 66 | 116 |
| 117 void DomStorageArea::InitialImportIfNeeded() { | |
| 118 if (initial_import_done_) | |
| 119 return; | |
| 120 | |
| 121 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); | |
| 122 DCHECK(backing_.get()); | |
| 123 | |
| 124 ValuesMap initial_values; | |
| 125 backing_->ReadAllValues(&initial_values); | |
| 126 map_->SwapValues(&initial_values); | |
| 127 initial_import_done_ = true; | |
| 128 } | |
| 129 | |
| 130 void DomStorageArea::ScheduleCommitChanges() { | |
| 131 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); | |
| 132 DCHECK(backing_.get()); | |
| 133 | |
| 134 if (!clear_all_next_commit_ && changed_values_.empty()) | |
|
michaeln
2012/02/15 04:10:53
given the usage of this method, it might make more
benm (inactive)
2012/02/15 17:38:17
sgtm.
| |
| 135 return; | |
| 136 | |
| 137 DCHECK(task_runner_.get()); | |
| 138 if (!task_runner_->PostTask( | |
|
michaeln
2012/02/15 04:10:53
As coded, this will result in many small transacti
benm (inactive)
2012/02/15 17:38:17
Done.
| |
| 139 FROM_HERE, base::Bind(&DomStorageArea::CommitChanges, this))) { | |
| 140 // If we know that the task will not run, try and save changes synchronously | |
| 141 // so that we don't lose data. | |
| 142 CommitChanges(); | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 void DomStorageArea::CommitChanges() { | |
| 147 DCHECK(backing_.get()); | |
| 148 if (backing_->CommitChanges(clear_all_next_commit_, changed_values_)) { | |
| 149 clear_all_next_commit_ = false; | |
| 150 changed_values_.clear(); | |
| 151 } | |
| 152 } | |
| 153 | |
| 154 // static | |
| 155 FilePath DomStorageArea::DatabaseFileNameFromOrigin(const GURL& origin) { | |
| 156 std::string filename = fileapi::GetOriginIdentifierFromURL(origin) | |
| 157 + ".localstorage"; | |
| 158 return FilePath().AppendASCII(filename); | |
| 159 } | |
| 160 | |
| 67 } // namespace dom_storage | 161 } // namespace dom_storage |
| OLD | NEW |