Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(113)

Side by Side Diff: webkit/dom_storage/dom_storage_namespace.cc

Issue 9963107: Persist sessionStorage on disk. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Cleanup. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_namespace.h" 5 #include "webkit/dom_storage/dom_storage_namespace.h"
6 6
7 #include "base/basictypes.h" 7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/location.h"
8 #include "base/logging.h" 10 #include "base/logging.h"
9 #include "webkit/dom_storage/dom_storage_area.h" 11 #include "webkit/dom_storage/dom_storage_area.h"
10 #include "webkit/dom_storage/dom_storage_task_runner.h" 12 #include "webkit/dom_storage/dom_storage_task_runner.h"
11 #include "webkit/dom_storage/dom_storage_types.h" 13 #include "webkit/dom_storage/dom_storage_types.h"
14 #include "webkit/dom_storage/session_storage_database.h"
12 15
13 namespace dom_storage { 16 namespace dom_storage {
14 17
15 DomStorageNamespace::DomStorageNamespace( 18 DomStorageNamespace::DomStorageNamespace(
16 const FilePath& directory, 19 const FilePath& directory,
17 DomStorageTaskRunner* task_runner) 20 DomStorageTaskRunner* task_runner)
18 : namespace_id_(kLocalStorageNamespaceId), 21 : namespace_id_(kLocalStorageNamespaceId),
19 directory_(directory), 22 directory_(directory),
20 task_runner_(task_runner) { 23 task_runner_(task_runner) {
21 } 24 }
22 25
23 DomStorageNamespace::DomStorageNamespace( 26 DomStorageNamespace::DomStorageNamespace(
24 int64 namespace_id, 27 int64 namespace_id,
25 DomStorageTaskRunner* task_runner) 28 DomStorageTaskRunner* task_runner)
26 : namespace_id_(namespace_id), 29 : namespace_id_(namespace_id),
27 task_runner_(task_runner) { 30 task_runner_(task_runner) {
28 DCHECK_NE(kLocalStorageNamespaceId, namespace_id); 31 DCHECK_NE(kLocalStorageNamespaceId, namespace_id);
29 } 32 }
30 33
34 DomStorageNamespace::DomStorageNamespace(
35 int64 namespace_id,
36 SessionStorageDatabase* session_storage_database,
37 DomStorageTaskRunner* task_runner)
38 : namespace_id_(namespace_id),
39 task_runner_(task_runner),
40 session_storage_database_(session_storage_database) {
41 DCHECK_NE(kLocalStorageNamespaceId, namespace_id);
42 }
43
31 DomStorageNamespace::~DomStorageNamespace() { 44 DomStorageNamespace::~DomStorageNamespace() {
45 if (session_storage_database_.get())
46 session_storage_database_->DeleteNamespace(namespace_id_);
michaeln 2012/04/22 21:43:35 should this be here given the logic in DomStorageC
marja 2012/05/11 12:18:32 Done.
32 } 47 }
33 48
34 DomStorageArea* DomStorageNamespace::OpenStorageArea(const GURL& origin) { 49 DomStorageArea* DomStorageNamespace::OpenStorageArea(const GURL& origin) {
35 if (AreaHolder* holder = GetAreaHolder(origin)) { 50 if (AreaHolder* holder = GetAreaHolder(origin)) {
36 ++(holder->open_count_); 51 ++(holder->open_count_);
37 return holder->area_; 52 return holder->area_;
38 } 53 }
39 DomStorageArea* area = new DomStorageArea(namespace_id_, origin, 54 DomStorageArea* area;
40 directory_, task_runner_); 55 if (!directory_.empty()) {
56 // Local storage backed on disk.
57 area = new DomStorageArea(namespace_id_, origin, directory_, task_runner_);
58 } else if (session_storage_database_.get()) {
59 // Session storage backed on disk.
60 area = new DomStorageArea(namespace_id_, origin, session_storage_database_,
61 task_runner_);
62 } else {
63 // In-memory.
64 area = new DomStorageArea(namespace_id_, origin, task_runner_);
65 }
41 areas_[origin] = AreaHolder(area, 1); 66 areas_[origin] = AreaHolder(area, 1);
42 return area; 67 return area;
43 } 68 }
44 69
45 void DomStorageNamespace::CloseStorageArea(DomStorageArea* area) { 70 void DomStorageNamespace::CloseStorageArea(DomStorageArea* area) {
46 AreaHolder* holder = GetAreaHolder(area->origin()); 71 AreaHolder* holder = GetAreaHolder(area->origin());
47 DCHECK(holder); 72 DCHECK(holder);
48 DCHECK_EQ(holder->area_.get(), area); 73 DCHECK_EQ(holder->area_.get(), area);
49 --(holder->open_count_); 74 --(holder->open_count_);
50 // TODO(michaeln): Clean up areas that aren't needed in memory anymore. 75 // TODO(michaeln): Clean up areas that aren't needed in memory anymore.
51 // The in-process-webkit based impl didn't do this either, but would be nice. 76 // The in-process-webkit based impl didn't do this either, but would be nice.
52 } 77 }
53 78
54 DomStorageNamespace* DomStorageNamespace::Clone(int64 clone_namespace_id) { 79 DomStorageNamespace* DomStorageNamespace::Clone(int64 clone_namespace_id) {
55 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); 80 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_);
56 DCHECK_NE(kLocalStorageNamespaceId, clone_namespace_id); 81 DCHECK_NE(kLocalStorageNamespaceId, clone_namespace_id);
57 DomStorageNamespace* clone = 82 DomStorageNamespace* clone = new DomStorageNamespace(
58 new DomStorageNamespace(clone_namespace_id, task_runner_); 83 clone_namespace_id, session_storage_database_, task_runner_);
59 AreaMap::const_iterator it = areas_.begin(); 84 AreaMap::const_iterator it = areas_.begin();
85 // We need to commit all data to the database before making a shallow copy,
86 // otherwise the data which is written will cause a deep copy. ShallowCopy
87 // will ensure that the next task in the commit sequence will be committing
88 // the data.
michaeln 2012/04/22 21:43:35 not sure this elaborate comment is needed here //
marja 2012/05/11 12:18:32 Done. DomStorageArea::ShallowCopy will also put th
60 for (; it != areas_.end(); ++it) { 89 for (; it != areas_.end(); ++it) {
61 DomStorageArea* area = it->second.area_->ShallowCopy(clone_namespace_id); 90 DomStorageArea* area = it->second.area_->ShallowCopy(clone_namespace_id);
62 clone->areas_[it->first] = AreaHolder(area, 0); 91 clone->areas_[it->first] = AreaHolder(area, 0);
63 } 92 }
93 // CloneNamespaceInCommitSequence will be done after writing data.
michaeln 2012/04/22 21:43:35 // And clone the on-disk structures too.
marja 2012/05/11 12:18:32 Done.
94 bool success = task_runner_->PostShutdownBlockingTask(
michaeln 2012/04/22 21:43:35 in mem-only cases (chromiumDRT and some tests) its
marja 2012/05/11 12:18:32 Done.
95 FROM_HERE,
96 DomStorageTaskRunner::COMMIT_SEQUENCE,
97 base::Bind(&DomStorageNamespace::CloneNamespaceInCommitSequence, this,
michaeln 2012/04/22 21:43:35 instead of binding to a helper method of this obje
marja 2012/05/11 12:18:32 Done. For that to work, I needed to change the ret
michaeln 2012/05/16 08:10:55 The base::IgnoreResult template could probably wor
98 clone_namespace_id));
99 DCHECK(success);
64 return clone; 100 return clone;
65 } 101 }
66 102
67 void DomStorageNamespace::DeleteOrigin(const GURL& origin) { 103 void DomStorageNamespace::DeleteOrigin(const GURL& origin) {
68 AreaHolder* holder = GetAreaHolder(origin); 104 AreaHolder* holder = GetAreaHolder(origin);
69 if (holder) { 105 if (holder) {
70 holder->area_->DeleteOrigin(); 106 holder->area_->DeleteOrigin();
71 return; 107 return;
72 } 108 }
73 if (!directory_.empty()) { 109 if (!directory_.empty()) {
74 scoped_refptr<DomStorageArea> area = 110 scoped_refptr<DomStorageArea> area =
75 new DomStorageArea(namespace_id_, origin, directory_, task_runner_); 111 new DomStorageArea(namespace_id_, origin, directory_, task_runner_);
76 area->DeleteOrigin(); 112 area->DeleteOrigin();
113 } else if (session_storage_database_.get()) {
114 scoped_refptr<DomStorageArea> area =
115 new DomStorageArea(namespace_id_, origin,
116 session_storage_database_.get(), task_runner_);
117 area->DeleteOrigin();
77 } 118 }
78 } 119 }
79 120
80 void DomStorageNamespace::PurgeMemory() { 121 void DomStorageNamespace::PurgeMemory() {
81 if (directory_.empty()) 122 if (directory_.empty())
82 return; // We can't purge w/o backing on disk. 123 return; // We can't purge w/o backing on disk.
83 AreaMap::iterator it = areas_.begin(); 124 AreaMap::iterator it = areas_.begin();
84 while (it != areas_.end()) { 125 while (it != areas_.end()) {
85 // Leave it alone if changes are pending 126 // Leave it alone if changes are pending
86 if (it->second.area_->HasUncommittedChanges()) { 127 if (it->second.area_->HasUncommittedChanges()) {
(...skipping 23 matching lines...) Expand all
110 151
111 152
112 DomStorageNamespace::AreaHolder* 153 DomStorageNamespace::AreaHolder*
113 DomStorageNamespace::GetAreaHolder(const GURL& origin) { 154 DomStorageNamespace::GetAreaHolder(const GURL& origin) {
114 AreaMap::iterator found = areas_.find(origin); 155 AreaMap::iterator found = areas_.find(origin);
115 if (found == areas_.end()) 156 if (found == areas_.end())
116 return NULL; 157 return NULL;
117 return &(found->second); 158 return &(found->second);
118 } 159 }
119 160
161 void DomStorageNamespace::CloneNamespaceInCommitSequence(
162 int64 clone_namespace_id) {
163 session_storage_database_->ShallowCopyNamespace(
164 namespace_id_, clone_namespace_id);
165 }
166
120 // AreaHolder 167 // AreaHolder
121 168
122 DomStorageNamespace::AreaHolder::AreaHolder() 169 DomStorageNamespace::AreaHolder::AreaHolder()
123 : open_count_(0) { 170 : open_count_(0) {
124 } 171 }
125 172
126 DomStorageNamespace::AreaHolder::AreaHolder( 173 DomStorageNamespace::AreaHolder::AreaHolder(
127 DomStorageArea* area, int count) 174 DomStorageArea* area, int count)
128 : area_(area), open_count_(count) { 175 : area_(area), open_count_(count) {
129 } 176 }
130 177
131 DomStorageNamespace::AreaHolder::~AreaHolder() { 178 DomStorageNamespace::AreaHolder::~AreaHolder() {
132 } 179 }
133 180
134 } // namespace dom_storage 181 } // namespace dom_storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698