| 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 #include "webkit/dom_storage/dom_storage_map.h" | 6 #include "webkit/dom_storage/dom_storage_map.h" |
| 7 #include "webkit/dom_storage/dom_storage_namespace.h" | 7 #include "webkit/dom_storage/dom_storage_namespace.h" |
| 8 #include "webkit/fileapi/file_system_util.h" |
| 8 | 9 |
| 9 namespace dom_storage { | 10 namespace dom_storage { |
| 10 | 11 |
| 11 DomStorageArea::DomStorageArea( | 12 DomStorageArea::DomStorageArea( |
| 12 int64 namespace_id, const GURL& origin, | 13 int64 namespace_id, const GURL& origin, |
| 13 const FilePath& directory, DomStorageTaskRunner* task_runner) | 14 const FilePath& directory, DomStorageTaskRunner* task_runner) |
| 14 : namespace_id_(namespace_id), origin_(origin), | 15 : namespace_id_(namespace_id), origin_(origin), |
| 15 directory_(directory), task_runner_(task_runner), | 16 directory_(directory), task_runner_(task_runner), |
| 16 map_(new DomStorageMap()) { | 17 map_(new DomStorageMap()), |
| 18 backing_(NULL), |
| 19 initial_import_done_(false), |
| 20 clear_all_next_sync_(false) { |
| 21 |
| 22 if (namespace_id == kLocalStorageNamespaceId && !directory.empty()) { |
| 23 FilePath path = directory.Append(DatabaseFileNameFromOrigin(origin_)); |
| 24 backing_.reset(new DomStorageDatabase(path)); |
| 25 } else { |
| 26 // Not a local storage area or no directory specified for backing |
| 27 // database, (i.e. it's an incognito profile). |
| 28 initial_import_done_ = true; |
| 29 } |
| 17 } | 30 } |
| 18 | 31 |
| 19 DomStorageArea::~DomStorageArea() { | 32 DomStorageArea::~DomStorageArea() { |
| 20 } | 33 } |
| 21 | 34 |
| 22 unsigned DomStorageArea::Length() { | 35 unsigned DomStorageArea::Length() { |
| 36 InitialImportIfNeeded(); |
| 23 return map_->Length(); | 37 return map_->Length(); |
| 24 } | 38 } |
| 25 | 39 |
| 26 NullableString16 DomStorageArea::Key(unsigned index) { | 40 NullableString16 DomStorageArea::Key(unsigned index) { |
| 41 InitialImportIfNeeded(); |
| 27 return map_->Key(index); | 42 return map_->Key(index); |
| 28 } | 43 } |
| 29 | 44 |
| 30 NullableString16 DomStorageArea::GetItem(const string16& key) { | 45 NullableString16 DomStorageArea::GetItem(const string16& key) { |
| 46 InitialImportIfNeeded(); |
| 31 return map_->GetItem(key); | 47 return map_->GetItem(key); |
| 32 } | 48 } |
| 33 | 49 |
| 34 bool DomStorageArea::SetItem( | 50 bool DomStorageArea::SetItem( |
| 35 const string16& key, const string16& value, | 51 const string16& key, const string16& value, |
| 36 NullableString16* old_value) { | 52 NullableString16* old_value) { |
| 53 InitialImportIfNeeded(); |
| 54 |
| 37 if (!map_->HasOneRef()) | 55 if (!map_->HasOneRef()) |
| 38 map_ = map_->DeepCopy(); | 56 map_ = map_->DeepCopy(); |
| 39 return map_->SetItem(key, value, old_value); | 57 bool success = map_->SetItem(key, value, old_value); |
| 58 if (success && backing_.get()) { |
| 59 items_to_sync_[key] = NullableString16(value, false); |
| 60 SyncToBacking(); |
| 61 } |
| 62 return success; |
| 40 } | 63 } |
| 41 | 64 |
| 42 bool DomStorageArea::RemoveItem( | 65 bool DomStorageArea::RemoveItem( |
| 43 const string16& key, | 66 const string16& key, |
| 44 string16* old_value) { | 67 string16* old_value) { |
| 68 InitialImportIfNeeded(); |
| 45 if (!map_->HasOneRef()) | 69 if (!map_->HasOneRef()) |
| 46 map_ = map_->DeepCopy(); | 70 map_ = map_->DeepCopy(); |
| 47 return map_->RemoveItem(key, old_value); | 71 bool success = map_->RemoveItem(key, old_value); |
| 72 if (success && backing_.get()) { |
| 73 items_to_sync_[key] = NullableString16(true); |
| 74 SyncToBacking(); |
| 75 } |
| 76 return success; |
| 48 } | 77 } |
| 49 | 78 |
| 50 bool DomStorageArea::Clear() { | 79 bool DomStorageArea::Clear() { |
| 80 InitialImportIfNeeded(); |
| 51 if (map_->Length() == 0) | 81 if (map_->Length() == 0) |
| 52 return false; | 82 return false; |
| 83 |
| 53 map_ = new DomStorageMap(); | 84 map_ = new DomStorageMap(); |
| 85 |
| 86 if (backing_.get()) { |
| 87 items_to_sync_.clear(); |
| 88 clear_all_next_sync_ = true; |
| 89 SyncToBacking(); |
| 90 } |
| 91 |
| 54 return true; | 92 return true; |
| 55 } | 93 } |
| 56 | 94 |
| 57 DomStorageArea* DomStorageArea::ShallowCopy(int64 destination_namespace_id) { | 95 DomStorageArea* DomStorageArea::ShallowCopy(int64 destination_namespace_id) { |
| 58 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); | 96 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); |
| 59 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); | 97 DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id); |
| 60 // SessionNamespaces aren't backed by files on disk. | 98 // SessionNamespaces aren't backed by files on disk. |
| 99 DCHECK(!backing_.get()); |
| 100 |
| 61 DomStorageArea* copy = new DomStorageArea(destination_namespace_id, origin_, | 101 DomStorageArea* copy = new DomStorageArea(destination_namespace_id, origin_, |
| 62 FilePath(), task_runner_); | 102 FilePath(), task_runner_); |
| 63 copy->map_ = map_; | 103 copy->map_ = map_; |
| 64 return copy; | 104 return copy; |
| 65 } | 105 } |
| 66 | 106 |
| 107 void DomStorageArea::InitialImportIfNeeded() { |
| 108 if (initial_import_done_) |
| 109 return; |
| 110 |
| 111 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); |
| 112 DCHECK(backing_.get()); |
| 113 |
| 114 ValuesMap initial_values; |
| 115 backing_->ReadAllValues(&initial_values); |
| 116 map_->SwapValues(&initial_values); |
| 117 initial_import_done_ = true; |
| 118 } |
| 119 |
| 120 void DomStorageArea::SyncToBacking() { |
| 121 DCHECK_EQ(kLocalStorageNamespaceId, namespace_id_); |
| 122 DCHECK(backing_.get()); |
| 123 |
| 124 if (!clear_all_next_sync_ && items_to_sync_.empty()) |
| 125 return; |
| 126 |
| 127 backing_->CommitChanges(clear_all_next_sync_, items_to_sync_); |
| 128 clear_all_next_sync_ = false; |
| 129 items_to_sync_.clear(); |
| 130 } |
| 131 |
| 132 // static |
| 133 FilePath DomStorageArea::DatabaseFileNameFromOrigin(const GURL& origin) { |
| 134 std::string filename = fileapi::GetOriginIdentifierFromURL(origin) |
| 135 + ".localstorage"; |
| 136 return FilePath().AppendASCII(filename); |
| 137 } |
| 138 |
| 67 } // namespace dom_storage | 139 } // namespace dom_storage |
| OLD | NEW |