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