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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
|
// Copyright 2013 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 "content/browser/dom_storage/dom_storage_area.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
#include "content/browser/dom_storage/dom_storage_namespace.h"
#include "content/browser/dom_storage/dom_storage_task_runner.h"
#include "content/browser/dom_storage/local_storage_database_adapter.h"
#include "content/browser/dom_storage/session_storage_database.h"
#include "content/browser/dom_storage/session_storage_database_adapter.h"
#include "content/common/dom_storage/dom_storage_map.h"
#include "content/common/dom_storage/dom_storage_types.h"
#include "webkit/browser/database/database_util.h"
#include "webkit/common/database/database_identifier.h"
#include "webkit/common/fileapi/file_system_util.h"
using webkit_database::DatabaseUtil;
namespace content {
static const int kCommitTimerSeconds = 1;
DOMStorageArea::CommitBatch::CommitBatch()
: clear_all_first(false) {
}
DOMStorageArea::CommitBatch::~CommitBatch() {}
// static
const base::FilePath::CharType DOMStorageArea::kDatabaseFileExtension[] =
FILE_PATH_LITERAL(".localstorage");
// static
base::FilePath DOMStorageArea::DatabaseFileNameFromOrigin(const GURL& origin) {
std::string filename = webkit_database::GetIdentifierFromOrigin(origin);
// There is no base::FilePath.AppendExtension() method, so start with just the
// extension as the filename, and then InsertBeforeExtension the desired
// name.
return base::FilePath().Append(kDatabaseFileExtension).
InsertBeforeExtensionASCII(filename);
}
// static
GURL DOMStorageArea::OriginFromDatabaseFileName(const base::FilePath& name) {
DCHECK(name.MatchesExtension(kDatabaseFileExtension));
std::string origin_id =
name.BaseName().RemoveExtension().MaybeAsASCII();
return webkit_database::GetOriginFromIdentifier(origin_id);
}
DOMStorageArea::DOMStorageArea(
const GURL& origin, const base::FilePath& directory,
DOMStorageTaskRunner* task_runner)
: namespace_id_(kLocalStorageNamespaceId), origin_(origin),
directory_(directory),
task_runner_(task_runner),
map_(new DOMStorageMap(kPerStorageAreaQuota +
kPerStorageAreaOverQuotaAllowance)),
is_initial_import_done_(true),
is_shutdown_(false),
commit_batches_in_flight_(0) {
if (!directory.empty()) {
base::FilePath path = directory.Append(DatabaseFileNameFromOrigin(origin_));
backing_.reset(new LocalStorageDatabaseAdapter(path));
is_initial_import_done_ = false;
}
}
DOMStorageArea::DOMStorageArea(
int64 namespace_id,
const std::string& persistent_namespace_id,
const GURL& origin,
SessionStorageDatabase* session_storage_backing,
DOMStorageTaskRunner* task_runner)
: namespace_id_(namespace_id),
persistent_namespace_id_(persistent_namespace_id),
origin_(origin),
task_runner_(task_runner),
map_(new DOMStorageMap(kPerStorageAreaQuota +
kPerStorageAreaOverQuotaAllowance)),
session_storage_backing_(session_storage_backing),
is_initial_import_done_(true),
is_shutdown_(false),
commit_batches_in_flight_(0) {
DCHECK(namespace_id != kLocalStorageNamespaceId);
if (session_storage_backing) {
backing_.reset(new SessionStorageDatabaseAdapter(
session_storage_backing, persistent_namespace_id, origin));
is_initial_import_done_ = false;
}
}
DOMStorageArea::~DOMStorageArea() {
}
void DOMStorageArea::ExtractValues(DOMStorageValuesMap* map) {
if (is_shutdown_)
return;
InitialImportIfNeeded();
map_->ExtractValues(map);
}
unsigned DOMStorageArea::Length() {
if (is_shutdown_)
return 0;
InitialImportIfNeeded();
return map_->Length();
}
base::NullableString16 DOMStorageArea::Key(unsigned index) {
if (is_shutdown_)
return base::NullableString16();
InitialImportIfNeeded();
return map_->Key(index);
}
base::NullableString16 DOMStorageArea::GetItem(const base::string16& key) {
if (is_shutdown_)
return base::NullableString16();
InitialImportIfNeeded();
return map_->GetItem(key);
}
bool DOMStorageArea::SetItem(const base::string16& key,
const base::string16& value,
base::NullableString16* old_value) {
if (is_shutdown_)
return false;
InitialImportIfNeeded();
if (!map_->HasOneRef())
map_ = map_->DeepCopy();
bool success = map_->SetItem(key, value, old_value);
if (success && backing_) {
CommitBatch* commit_batch = CreateCommitBatchIfNeeded();
commit_batch->changed_values[key] = base::NullableString16(value, false);
}
return success;
}
bool DOMStorageArea::RemoveItem(const base::string16& key,
base::string16* old_value) {
if (is_shutdown_)
return false;
InitialImportIfNeeded();
if (!map_->HasOneRef())
map_ = map_->DeepCopy();
bool success = map_->RemoveItem(key, old_value);
if (success && backing_) {
CommitBatch* commit_batch = CreateCommitBatchIfNeeded();
commit_batch->changed_values[key] = base::NullableString16();
}
return success;
}
bool DOMStorageArea::Clear() {
if (is_shutdown_)
return false;
InitialImportIfNeeded();
if (map_->Length() == 0)
return false;
map_ = new DOMStorageMap(kPerStorageAreaQuota +
kPerStorageAreaOverQuotaAllowance);
if (backing_) {
CommitBatch* commit_batch = CreateCommitBatchIfNeeded();
commit_batch->clear_all_first = true;
commit_batch->changed_values.clear();
}
return true;
}
void DOMStorageArea::FastClear() {
// TODO(marja): Unify clearing localStorage and sessionStorage. The problem is
// to make the following 3 to work together: 1) FastClear, 2) PurgeMemory and
// 3) not creating events when clearing an empty area.
if (is_shutdown_)
return;
map_ = new DOMStorageMap(kPerStorageAreaQuota +
kPerStorageAreaOverQuotaAllowance);
// This ensures no import will happen while we're waiting to clear the data
// from the database. This mechanism fails if PurgeMemory is called.
is_initial_import_done_ = true;
if (backing_) {
CommitBatch* commit_batch = CreateCommitBatchIfNeeded();
commit_batch->clear_all_first = true;
commit_batch->changed_values.clear();
}
}
DOMStorageArea* DOMStorageArea::ShallowCopy(
int64 destination_namespace_id,
const std::string& destination_persistent_namespace_id) {
DCHECK_NE(kLocalStorageNamespaceId, namespace_id_);
DCHECK_NE(kLocalStorageNamespaceId, destination_namespace_id);
DOMStorageArea* copy = new DOMStorageArea(
destination_namespace_id, destination_persistent_namespace_id, origin_,
session_storage_backing_.get(), task_runner_.get());
copy->map_ = map_;
copy->is_shutdown_ = is_shutdown_;
copy->is_initial_import_done_ = true;
// All the uncommitted changes to this area need to happen before the actual
// shallow copy is made (scheduled by the upper layer). Another OnCommitTimer
// call might be in the event queue at this point, but it's handled gracefully
// when it fires.
if (commit_batch_)
OnCommitTimer();
return copy;
}
bool DOMStorageArea::HasUncommittedChanges() const {
DCHECK(!is_shutdown_);
return commit_batch_.get() || commit_batches_in_flight_;
}
void DOMStorageArea::DeleteOrigin() {
DCHECK(!is_shutdown_);
// This function shouldn't be called for sessionStorage.
DCHECK(!session_storage_backing_.get());
if (HasUncommittedChanges()) {
// TODO(michaeln): This logically deletes the data immediately,
// and in a matter of a second, deletes the rows from the backing
// database file, but the file itself will linger until shutdown
// or purge time. Ideally, this should delete the file more
// quickly.
Clear();
return;
}
map_ = new DOMStorageMap(kPerStorageAreaQuota +
kPerStorageAreaOverQuotaAllowance);
if (backing_) {
is_initial_import_done_ = false;
backing_->Reset();
backing_->DeleteFiles();
}
}
void DOMStorageArea::PurgeMemory() {
DCHECK(!is_shutdown_);
// Purging sessionStorage is not supported; it won't work with FastClear.
DCHECK(!session_storage_backing_.get());
if (!is_initial_import_done_ || // We're not using any memory.
!backing_.get() || // We can't purge anything.
HasUncommittedChanges()) // We leave things alone with changes pending.
return;
// Drop the in memory cache, we'll reload when needed.
is_initial_import_done_ = false;
map_ = new DOMStorageMap(kPerStorageAreaQuota +
kPerStorageAreaOverQuotaAllowance);
// Recreate the database object, this frees up the open sqlite connection
// and its page cache.
backing_->Reset();
}
void DOMStorageArea::Shutdown() {
DCHECK(!is_shutdown_);
is_shutdown_ = true;
map_ = NULL;
if (!backing_)
return;
bool success = task_runner_->PostShutdownBlockingTask(
FROM_HERE,
DOMStorageTaskRunner::COMMIT_SEQUENCE,
base::Bind(&DOMStorageArea::ShutdownInCommitSequence, this));
DCHECK(success);
}
void DOMStorageArea::InitialImportIfNeeded() {
if (is_initial_import_done_)
return;
DCHECK(backing_.get());
base::TimeTicks before = base::TimeTicks::Now();
DOMStorageValuesMap initial_values;
backing_->ReadAllValues(&initial_values);
map_->SwapValues(&initial_values);
is_initial_import_done_ = true;
base::TimeDelta time_to_import = base::TimeTicks::Now() - before;
UMA_HISTOGRAM_TIMES("LocalStorage.BrowserTimeToPrimeLocalStorage",
time_to_import);
size_t local_storage_size_kb = map_->bytes_used() / 1024;
// Track localStorage size, from 0-6MB. Note that the maximum size should be
// 5MB, but we add some slop since we want to make sure the max size is always
// above what we see in practice, since histograms can't change.
UMA_HISTOGRAM_CUSTOM_COUNTS("LocalStorage.BrowserLocalStorageSizeInKB",
local_storage_size_kb,
0, 6 * 1024, 50);
if (local_storage_size_kb < 100) {
UMA_HISTOGRAM_TIMES(
"LocalStorage.BrowserTimeToPrimeLocalStorageUnder100KB",
time_to_import);
} else if (local_storage_size_kb < 1000) {
UMA_HISTOGRAM_TIMES(
"LocalStorage.BrowserTimeToPrimeLocalStorage100KBTo1MB",
time_to_import);
} else {
UMA_HISTOGRAM_TIMES(
"LocalStorage.BrowserTimeToPrimeLocalStorage1MBTo5MB",
time_to_import);
}
}
DOMStorageArea::CommitBatch* DOMStorageArea::CreateCommitBatchIfNeeded() {
DCHECK(!is_shutdown_);
if (!commit_batch_) {
commit_batch_.reset(new CommitBatch());
// Start a timer to commit any changes that accrue in the batch, but only if
// no commits are currently in flight. In that case the timer will be
// started after the commits have happened.
if (!commit_batches_in_flight_) {
task_runner_->PostDelayedTask(
FROM_HERE,
base::Bind(&DOMStorageArea::OnCommitTimer, this),
base::TimeDelta::FromSeconds(kCommitTimerSeconds));
}
}
return commit_batch_.get();
}
void DOMStorageArea::OnCommitTimer() {
if (is_shutdown_)
return;
DCHECK(backing_.get());
// It's possible that there is nothing to commit, since a shallow copy occured
// before the timer fired.
if (!commit_batch_)
return;
// This method executes on the primary sequence, we schedule
// a task for immediate execution on the commit sequence.
DCHECK(task_runner_->IsRunningOnPrimarySequence());
bool success = task_runner_->PostShutdownBlockingTask(
FROM_HERE,
DOMStorageTaskRunner::COMMIT_SEQUENCE,
base::Bind(&DOMStorageArea::CommitChanges, this,
base::Owned(commit_batch_.release())));
++commit_batches_in_flight_;
DCHECK(success);
}
void DOMStorageArea::CommitChanges(const CommitBatch* commit_batch) {
// This method executes on the commit sequence.
DCHECK(task_runner_->IsRunningOnCommitSequence());
backing_->CommitChanges(commit_batch->clear_all_first,
commit_batch->changed_values);
// TODO(michaeln): what if CommitChanges returns false (e.g., we're trying to
// commit to a DB which is in an inconsistent state?)
task_runner_->PostTask(
FROM_HERE,
base::Bind(&DOMStorageArea::OnCommitComplete, this));
}
void DOMStorageArea::OnCommitComplete() {
// We're back on the primary sequence in this method.
DCHECK(task_runner_->IsRunningOnPrimarySequence());
--commit_batches_in_flight_;
if (is_shutdown_)
return;
if (commit_batch_.get() && !commit_batches_in_flight_) {
// More changes have accrued, restart the timer.
task_runner_->PostDelayedTask(
FROM_HERE,
base::Bind(&DOMStorageArea::OnCommitTimer, this),
base::TimeDelta::FromSeconds(kCommitTimerSeconds));
}
}
void DOMStorageArea::ShutdownInCommitSequence() {
// This method executes on the commit sequence.
DCHECK(task_runner_->IsRunningOnCommitSequence());
DCHECK(backing_.get());
if (commit_batch_) {
// Commit any changes that accrued prior to the timer firing.
bool success = backing_->CommitChanges(
commit_batch_->clear_all_first,
commit_batch_->changed_values);
DCHECK(success);
}
commit_batch_.reset();
backing_.reset();
session_storage_backing_ = NULL;
}
} // namespace content
|