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

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

Issue 9963107: Persist sessionStorage on disk. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Test update. Created 8 years, 7 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_context.h" 5 #include "webkit/dom_storage/dom_storage_context.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/time.h" 11 #include "base/time.h"
12 #include "webkit/dom_storage/dom_storage_area.h" 12 #include "webkit/dom_storage/dom_storage_area.h"
13 #include "webkit/dom_storage/dom_storage_database.h"
13 #include "webkit/dom_storage/dom_storage_namespace.h" 14 #include "webkit/dom_storage/dom_storage_namespace.h"
14 #include "webkit/dom_storage/dom_storage_task_runner.h" 15 #include "webkit/dom_storage/dom_storage_task_runner.h"
15 #include "webkit/dom_storage/dom_storage_types.h" 16 #include "webkit/dom_storage/dom_storage_types.h"
17 #include "webkit/dom_storage/session_storage_database.h"
16 #include "webkit/quota/special_storage_policy.h" 18 #include "webkit/quota/special_storage_policy.h"
17 19
18 using file_util::FileEnumerator; 20 using file_util::FileEnumerator;
19 21
20 namespace dom_storage { 22 namespace dom_storage {
21 23
22 DomStorageContext::UsageInfo::UsageInfo() : data_size(0) {} 24 DomStorageContext::UsageInfo::UsageInfo() : data_size(0) {}
23 DomStorageContext::UsageInfo::~UsageInfo() {} 25 DomStorageContext::UsageInfo::~UsageInfo() {}
24 26
25 DomStorageContext::DomStorageContext( 27 DomStorageContext::DomStorageContext(
26 const FilePath& localstorage_directory, 28 const FilePath& localstorage_directory,
27 const FilePath& sessionstorage_directory, 29 const FilePath& sessionstorage_directory,
28 quota::SpecialStoragePolicy* special_storage_policy, 30 quota::SpecialStoragePolicy* special_storage_policy,
29 DomStorageTaskRunner* task_runner) 31 DomStorageTaskRunner* task_runner)
30 : localstorage_directory_(localstorage_directory), 32 : localstorage_directory_(localstorage_directory),
31 sessionstorage_directory_(sessionstorage_directory), 33 sessionstorage_directory_(sessionstorage_directory),
32 task_runner_(task_runner), 34 task_runner_(task_runner),
33 is_shutdown_(false), 35 is_shutdown_(false),
34 clear_local_state_(false), 36 clear_local_state_(false),
35 save_session_state_(false), 37 save_session_state_(false),
36 special_storage_policy_(special_storage_policy) { 38 special_storage_policy_(special_storage_policy) {
37 // AtomicSequenceNum starts at 0 but we want to start session 39 // AtomicSequenceNum starts at 0 but we want to start session
38 // namespace ids at one since zero is reserved for the 40 // namespace ids at one since zero is reserved for the
39 // kLocalStorageNamespaceId. 41 // kLocalStorageNamespaceId.
40 session_id_sequence_.GetNext(); 42 session_id_sequence_.GetNext();
43 if (!sessionstorage_directory_.empty()) {
44 session_storage_database_ =
45 new SessionStorageDatabase(sessionstorage_directory_);
46 }
41 } 47 }
42 48
43 DomStorageContext::~DomStorageContext() { 49 DomStorageContext::~DomStorageContext() {
44 } 50 }
45 51
46 DomStorageNamespace* DomStorageContext::GetStorageNamespace( 52 DomStorageNamespace* DomStorageContext::GetStorageNamespace(
47 int64 namespace_id) { 53 int64 namespace_id) {
48 if (is_shutdown_) 54 if (is_shutdown_)
49 return NULL; 55 return NULL;
50 StorageNamespaceMap::iterator found = namespaces_.find(namespace_id); 56 StorageNamespaceMap::iterator found = namespaces_.find(namespace_id);
(...skipping 29 matching lines...) Expand all
80 info.origin = DomStorageArea::OriginFromDatabaseFileName(path); 86 info.origin = DomStorageArea::OriginFromDatabaseFileName(path);
81 if (include_file_info) { 87 if (include_file_info) {
82 FileEnumerator::FindInfo find_info; 88 FileEnumerator::FindInfo find_info;
83 enumerator.GetFindInfo(&find_info); 89 enumerator.GetFindInfo(&find_info);
84 info.data_size = FileEnumerator::GetFilesize(find_info); 90 info.data_size = FileEnumerator::GetFilesize(find_info);
85 info.last_modified = FileEnumerator::GetLastModifiedTime(find_info); 91 info.last_modified = FileEnumerator::GetLastModifiedTime(find_info);
86 } 92 }
87 infos->push_back(info); 93 infos->push_back(info);
88 } 94 }
89 } 95 }
96 // FIXME(marja): Get usage infos for sessionStorage (crbug.com/123599).
90 } 97 }
91 98
92 void DomStorageContext::DeleteOrigin(const GURL& origin) { 99 void DomStorageContext::DeleteOrigin(const GURL& origin) {
93 DCHECK(!is_shutdown_); 100 DCHECK(!is_shutdown_);
94 DomStorageNamespace* local = GetStorageNamespace(kLocalStorageNamespaceId); 101 DomStorageNamespace* local = GetStorageNamespace(kLocalStorageNamespaceId);
95 local->DeleteOrigin(origin); 102 local->DeleteOrigin(origin);
96 } 103 }
97 104
98 void DomStorageContext::DeleteDataModifiedSince(const base::Time& cutoff) { 105 void DomStorageContext::DeleteDataModifiedSince(const base::Time& cutoff) {
99 std::vector<UsageInfo> infos; 106 std::vector<UsageInfo> infos;
(...skipping 17 matching lines...) Expand all
117 if (found != namespaces_.end()) 124 if (found != namespaces_.end())
118 found->second->PurgeMemory(); 125 found->second->PurgeMemory();
119 } 126 }
120 127
121 void DomStorageContext::Shutdown() { 128 void DomStorageContext::Shutdown() {
122 is_shutdown_ = true; 129 is_shutdown_ = true;
123 StorageNamespaceMap::const_iterator it = namespaces_.begin(); 130 StorageNamespaceMap::const_iterator it = namespaces_.begin();
124 for (; it != namespaces_.end(); ++it) 131 for (; it != namespaces_.end(); ++it)
125 it->second->Shutdown(); 132 it->second->Shutdown();
126 133
134 // Delete data from sessionStorage. If the previous exit was unclean, the
135 // session storage backing might contain leftover data. Delete it now.
136 if (session_storage_database_.get()) {
137 bool success = task_runner_->PostShutdownBlockingTask(
138 FROM_HERE,
139 DomStorageTaskRunner::COMMIT_SEQUENCE,
140 base::Bind(&DomStorageContext::DeleteLeftoverDataInCommitSequence,
141 this));
142 DCHECK(success);
143 }
144
145 // Delete data from localStorage.
127 if (localstorage_directory_.empty()) 146 if (localstorage_directory_.empty())
128 return; 147 return;
129 148
130 // Respect the content policy settings about what to 149 // Respect the content policy settings about what to
131 // keep and what to discard. 150 // keep and what to discard.
132 if (save_session_state_) 151 if (save_session_state_) // Keep everything.
133 return; // Keep everything. 152 return;
134 153
135 bool has_session_only_origins = 154 bool has_session_only_origins =
136 special_storage_policy_.get() && 155 special_storage_policy_.get() &&
137 special_storage_policy_->HasSessionOnlyOrigins(); 156 special_storage_policy_->HasSessionOnlyOrigins();
138 157
139 if (clear_local_state_ || has_session_only_origins) { 158 if (clear_local_state_ || has_session_only_origins) {
140 // We may have to delete something. We continue on the 159 // We may have to delete something. We continue on the
141 // commit sequence after area shutdown tasks have cycled 160 // commit sequence after area shutdown tasks have cycled
142 // thru that sequence (and closed their database files). 161 // thru that sequence (and closed their database files).
143 bool success = task_runner_->PostShutdownBlockingTask( 162 bool success = task_runner_->PostShutdownBlockingTask(
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 OnDomStorageAreaCleared(area, page_url)); 204 OnDomStorageAreaCleared(area, page_url));
186 } 205 }
187 206
188 void DomStorageContext::CreateSessionNamespace( 207 void DomStorageContext::CreateSessionNamespace(
189 int64 namespace_id) { 208 int64 namespace_id) {
190 if (is_shutdown_) 209 if (is_shutdown_)
191 return; 210 return;
192 DCHECK(namespace_id != kLocalStorageNamespaceId); 211 DCHECK(namespace_id != kLocalStorageNamespaceId);
193 DCHECK(namespaces_.find(namespace_id) == namespaces_.end()); 212 DCHECK(namespaces_.find(namespace_id) == namespaces_.end());
194 namespaces_[namespace_id] = new DomStorageNamespace( 213 namespaces_[namespace_id] = new DomStorageNamespace(
195 namespace_id, task_runner_); 214 namespace_id, session_storage_database_.get(), task_runner_);
196 } 215 }
197 216
198 void DomStorageContext::DeleteSessionNamespace( 217 void DomStorageContext::DeleteSessionNamespace(
199 int64 namespace_id) { 218 int64 namespace_id) {
200 DCHECK_NE(kLocalStorageNamespaceId, namespace_id); 219 DCHECK_NE(kLocalStorageNamespaceId, namespace_id);
201 namespaces_.erase(namespace_id); 220 namespaces_.erase(namespace_id);
221 // TODO(marja): When doing a session restore, protect parts of the data from
222 // deletion.
223 if (session_storage_database_.get()) {
224 bool success = task_runner_->PostShutdownBlockingTask(
225 FROM_HERE,
226 DomStorageTaskRunner::COMMIT_SEQUENCE,
227 base::Bind(&DomStorageContext::DeleteSessionNamespaceInCommitSequence,
228 this, namespace_id));
229 DCHECK(success);
230 }
202 } 231 }
203 232
204 void DomStorageContext::CloneSessionNamespace( 233 void DomStorageContext::CloneSessionNamespace(
205 int64 existing_id, int64 new_id) { 234 int64 existing_id, int64 new_id) {
206 if (is_shutdown_) 235 if (is_shutdown_)
207 return; 236 return;
208 DCHECK_NE(kLocalStorageNamespaceId, existing_id); 237 DCHECK_NE(kLocalStorageNamespaceId, existing_id);
209 DCHECK_NE(kLocalStorageNamespaceId, new_id); 238 DCHECK_NE(kLocalStorageNamespaceId, new_id);
210 StorageNamespaceMap::iterator found = namespaces_.find(existing_id); 239 StorageNamespaceMap::iterator found = namespaces_.find(existing_id);
211 if (found != namespaces_.end()) 240 if (found != namespaces_.end())
(...skipping 18 matching lines...) Expand all
230 const bool kNotRecursive = false; 259 const bool kNotRecursive = false;
231 FilePath database_file_path = localstorage_directory_.Append( 260 FilePath database_file_path = localstorage_directory_.Append(
232 DomStorageArea::DatabaseFileNameFromOrigin(origin)); 261 DomStorageArea::DatabaseFileNameFromOrigin(origin));
233 file_util::Delete(database_file_path, kNotRecursive); 262 file_util::Delete(database_file_path, kNotRecursive);
234 file_util::Delete( 263 file_util::Delete(
235 DomStorageDatabase::GetJournalFilePath(database_file_path), 264 DomStorageDatabase::GetJournalFilePath(database_file_path),
236 kNotRecursive); 265 kNotRecursive);
237 } 266 }
238 } 267 }
239 268
269 void DomStorageContext::DeleteSessionNamespaceInCommitSequence(
270 int64 namespace_id) {
271 session_storage_database_->DeleteNamespace(namespace_id);
272 }
273
274 void DomStorageContext::DeleteLeftoverDataInCommitSequence() {
275 DCHECK(session_storage_database_.get());
276 // Delete all namespaces which don't have an associated DomStorageNamespace
277 // alive.
278 std::vector<int64> namespace_ids;
279 session_storage_database_->ReadNamespaceIds(&namespace_ids);
280 for (std::vector<int64>::const_iterator it = namespace_ids.begin();
281 it != namespace_ids.end(); ++it) {
282 if (namespaces_.find(*it) == namespaces_.end())
283 session_storage_database_->DeleteNamespace(*it);
284 }
285 }
286
240 } // namespace dom_storage 287 } // namespace dom_storage
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698