Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / dom_storage / dom_storage_context_impl.cc
blob0e07dabda9f6729c97ce248e25cd4c8d8547c0e0
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
3 // found in the LICENSE file.
5 #include "content/browser/dom_storage/dom_storage_context_impl.h"
7 #include <stdlib.h>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/files/file_enumerator.h"
12 #include "base/files/file_util.h"
13 #include "base/guid.h"
14 #include "base/location.h"
15 #include "base/time/time.h"
16 #include "content/browser/dom_storage/dom_storage_area.h"
17 #include "content/browser/dom_storage/dom_storage_database.h"
18 #include "content/browser/dom_storage/dom_storage_namespace.h"
19 #include "content/browser/dom_storage/dom_storage_task_runner.h"
20 #include "content/browser/dom_storage/session_storage_database.h"
21 #include "content/common/dom_storage/dom_storage_types.h"
22 #include "content/public/browser/dom_storage_context.h"
23 #include "content/public/browser/local_storage_usage_info.h"
24 #include "content/public/browser/session_storage_usage_info.h"
25 #include "storage/browser/quota/special_storage_policy.h"
27 namespace content {
29 static const int kSessionStoraceScavengingSeconds = 60;
31 // Offset the session storage namespace ids generated by different contexts
32 // to help identify when an id from one is mistakenly used in another.
33 static int g_session_id_offset_sequence = 1;
35 DOMStorageContextImpl::DOMStorageContextImpl(
36 const base::FilePath& localstorage_directory,
37 const base::FilePath& sessionstorage_directory,
38 storage::SpecialStoragePolicy* special_storage_policy,
39 DOMStorageTaskRunner* task_runner)
40 : localstorage_directory_(localstorage_directory),
41 sessionstorage_directory_(sessionstorage_directory),
42 task_runner_(task_runner),
43 session_id_offset_(abs((g_session_id_offset_sequence++ % 10)) * 1000),
44 is_shutdown_(false),
45 force_keep_session_state_(false),
46 special_storage_policy_(special_storage_policy),
47 scavenging_started_(false) {
48 // AtomicSequenceNum starts at 0 but we want to start session
49 // namespace ids at one since zero is reserved for the
50 // kLocalStorageNamespaceId.
51 session_id_sequence_.GetNext();
54 DOMStorageContextImpl::~DOMStorageContextImpl() {
55 if (session_storage_database_.get()) {
56 // SessionStorageDatabase shouldn't be deleted right away: deleting it will
57 // potentially involve waiting in leveldb::DBImpl::~DBImpl, and waiting
58 // shouldn't happen on this thread.
59 SessionStorageDatabase* to_release = session_storage_database_.get();
60 to_release->AddRef();
61 session_storage_database_ = NULL;
62 task_runner_->PostShutdownBlockingTask(
63 FROM_HERE,
64 DOMStorageTaskRunner::COMMIT_SEQUENCE,
65 base::Bind(&SessionStorageDatabase::Release,
66 base::Unretained(to_release)));
70 DOMStorageNamespace* DOMStorageContextImpl::GetStorageNamespace(
71 int64 namespace_id) {
72 if (is_shutdown_)
73 return NULL;
74 StorageNamespaceMap::iterator found = namespaces_.find(namespace_id);
75 if (found == namespaces_.end()) {
76 if (namespace_id == kLocalStorageNamespaceId) {
77 if (!localstorage_directory_.empty()) {
78 if (!base::CreateDirectory(localstorage_directory_)) {
79 LOG(ERROR) << "Failed to create 'Local Storage' directory,"
80 " falling back to in-memory only.";
81 localstorage_directory_ = base::FilePath();
84 DOMStorageNamespace* local =
85 new DOMStorageNamespace(localstorage_directory_, task_runner_.get());
86 namespaces_[kLocalStorageNamespaceId] = local;
87 return local;
89 return NULL;
91 return found->second.get();
94 void DOMStorageContextImpl::GetLocalStorageUsage(
95 std::vector<LocalStorageUsageInfo>* infos,
96 bool include_file_info) {
97 if (localstorage_directory_.empty())
98 return;
99 base::FileEnumerator enumerator(localstorage_directory_, false,
100 base::FileEnumerator::FILES);
101 for (base::FilePath path = enumerator.Next(); !path.empty();
102 path = enumerator.Next()) {
103 if (path.MatchesExtension(DOMStorageArea::kDatabaseFileExtension)) {
104 LocalStorageUsageInfo info;
105 info.origin = DOMStorageArea::OriginFromDatabaseFileName(path);
106 if (include_file_info) {
107 base::FileEnumerator::FileInfo find_info = enumerator.GetInfo();
108 info.data_size = find_info.GetSize();
109 info.last_modified = find_info.GetLastModifiedTime();
111 infos->push_back(info);
116 void DOMStorageContextImpl::GetSessionStorageUsage(
117 std::vector<SessionStorageUsageInfo>* infos) {
118 if (!session_storage_database_.get())
119 return;
120 std::map<std::string, std::vector<GURL> > namespaces_and_origins;
121 session_storage_database_->ReadNamespacesAndOrigins(
122 &namespaces_and_origins);
123 for (std::map<std::string, std::vector<GURL> >::const_iterator it =
124 namespaces_and_origins.begin();
125 it != namespaces_and_origins.end(); ++it) {
126 for (std::vector<GURL>::const_iterator origin_it = it->second.begin();
127 origin_it != it->second.end(); ++origin_it) {
128 SessionStorageUsageInfo info;
129 info.persistent_namespace_id = it->first;
130 info.origin = *origin_it;
131 infos->push_back(info);
136 void DOMStorageContextImpl::DeleteLocalStorage(const GURL& origin) {
137 DCHECK(!is_shutdown_);
138 DOMStorageNamespace* local = GetStorageNamespace(kLocalStorageNamespaceId);
139 local->DeleteLocalStorageOrigin(origin);
140 // Synthesize a 'cleared' event if the area is open so CachedAreas in
141 // renderers get emptied out too.
142 DOMStorageArea* area = local->GetOpenStorageArea(origin);
143 if (area)
144 NotifyAreaCleared(area, origin);
147 void DOMStorageContextImpl::DeleteSessionStorage(
148 const SessionStorageUsageInfo& usage_info) {
149 DCHECK(!is_shutdown_);
150 DOMStorageNamespace* dom_storage_namespace = NULL;
151 std::map<std::string, int64>::const_iterator it =
152 persistent_namespace_id_to_namespace_id_.find(
153 usage_info.persistent_namespace_id);
154 if (it != persistent_namespace_id_to_namespace_id_.end()) {
155 dom_storage_namespace = GetStorageNamespace(it->second);
156 } else {
157 int64 namespace_id = AllocateSessionId();
158 CreateSessionNamespace(namespace_id, usage_info.persistent_namespace_id);
159 dom_storage_namespace = GetStorageNamespace(namespace_id);
161 dom_storage_namespace->DeleteSessionStorageOrigin(usage_info.origin);
162 // Synthesize a 'cleared' event if the area is open so CachedAreas in
163 // renderers get emptied out too.
164 DOMStorageArea* area =
165 dom_storage_namespace->GetOpenStorageArea(usage_info.origin);
166 if (area)
167 NotifyAreaCleared(area, usage_info.origin);
170 void DOMStorageContextImpl::Shutdown() {
171 is_shutdown_ = true;
172 StorageNamespaceMap::const_iterator it = namespaces_.begin();
173 for (; it != namespaces_.end(); ++it)
174 it->second->Shutdown();
176 if (localstorage_directory_.empty() && !session_storage_database_.get())
177 return;
179 // Respect the content policy settings about what to
180 // keep and what to discard.
181 if (force_keep_session_state_)
182 return; // Keep everything.
184 bool has_session_only_origins =
185 special_storage_policy_.get() &&
186 special_storage_policy_->HasSessionOnlyOrigins();
188 if (has_session_only_origins) {
189 // We may have to delete something. We continue on the
190 // commit sequence after area shutdown tasks have cycled
191 // thru that sequence (and closed their database files).
192 bool success = task_runner_->PostShutdownBlockingTask(
193 FROM_HERE,
194 DOMStorageTaskRunner::COMMIT_SEQUENCE,
195 base::Bind(&DOMStorageContextImpl::ClearSessionOnlyOrigins, this));
196 DCHECK(success);
200 void DOMStorageContextImpl::AddEventObserver(EventObserver* observer) {
201 event_observers_.AddObserver(observer);
204 void DOMStorageContextImpl::RemoveEventObserver(EventObserver* observer) {
205 event_observers_.RemoveObserver(observer);
208 void DOMStorageContextImpl::NotifyItemSet(
209 const DOMStorageArea* area,
210 const base::string16& key,
211 const base::string16& new_value,
212 const base::NullableString16& old_value,
213 const GURL& page_url) {
214 FOR_EACH_OBSERVER(
215 EventObserver, event_observers_,
216 OnDOMStorageItemSet(area, key, new_value, old_value, page_url));
219 void DOMStorageContextImpl::NotifyItemRemoved(
220 const DOMStorageArea* area,
221 const base::string16& key,
222 const base::string16& old_value,
223 const GURL& page_url) {
224 FOR_EACH_OBSERVER(
225 EventObserver, event_observers_,
226 OnDOMStorageItemRemoved(area, key, old_value, page_url));
229 void DOMStorageContextImpl::NotifyAreaCleared(
230 const DOMStorageArea* area,
231 const GURL& page_url) {
232 FOR_EACH_OBSERVER(
233 EventObserver, event_observers_,
234 OnDOMStorageAreaCleared(area, page_url));
237 int64 DOMStorageContextImpl::AllocateSessionId() {
238 return session_id_sequence_.GetNext() + session_id_offset_;
241 std::string DOMStorageContextImpl::AllocatePersistentSessionId() {
242 std::string guid = base::GenerateGUID();
243 std::replace(guid.begin(), guid.end(), '-', '_');
244 return guid;
247 void DOMStorageContextImpl::CreateSessionNamespace(
248 int64 namespace_id,
249 const std::string& persistent_namespace_id) {
250 if (is_shutdown_)
251 return;
252 DCHECK(namespace_id != kLocalStorageNamespaceId);
253 DCHECK(namespaces_.find(namespace_id) == namespaces_.end());
254 namespaces_[namespace_id] = new DOMStorageNamespace(
255 namespace_id, persistent_namespace_id, session_storage_database_.get(),
256 task_runner_.get());
257 persistent_namespace_id_to_namespace_id_[persistent_namespace_id] =
258 namespace_id;
261 void DOMStorageContextImpl::DeleteSessionNamespace(
262 int64 namespace_id, bool should_persist_data) {
263 DCHECK_NE(kLocalStorageNamespaceId, namespace_id);
264 StorageNamespaceMap::const_iterator it = namespaces_.find(namespace_id);
265 if (it == namespaces_.end())
266 return;
267 std::string persistent_namespace_id = it->second->persistent_namespace_id();
268 if (session_storage_database_.get()) {
269 if (!should_persist_data) {
270 task_runner_->PostShutdownBlockingTask(
271 FROM_HERE,
272 DOMStorageTaskRunner::COMMIT_SEQUENCE,
273 base::Bind(
274 base::IgnoreResult(&SessionStorageDatabase::DeleteNamespace),
275 session_storage_database_,
276 persistent_namespace_id));
277 } else {
278 // Ensure that the data gets committed before we shut down.
279 it->second->Shutdown();
280 if (!scavenging_started_) {
281 // Protect the persistent namespace ID from scavenging.
282 protected_persistent_session_ids_.insert(persistent_namespace_id);
286 persistent_namespace_id_to_namespace_id_.erase(persistent_namespace_id);
287 namespaces_.erase(namespace_id);
290 void DOMStorageContextImpl::CloneSessionNamespace(
291 int64 existing_id, int64 new_id,
292 const std::string& new_persistent_id) {
293 if (is_shutdown_)
294 return;
295 DCHECK_NE(kLocalStorageNamespaceId, existing_id);
296 DCHECK_NE(kLocalStorageNamespaceId, new_id);
297 StorageNamespaceMap::iterator found = namespaces_.find(existing_id);
298 if (found != namespaces_.end())
299 namespaces_[new_id] = found->second->Clone(new_id, new_persistent_id);
300 else
301 CreateSessionNamespace(new_id, new_persistent_id);
304 void DOMStorageContextImpl::ClearSessionOnlyOrigins() {
305 if (!localstorage_directory_.empty()) {
306 std::vector<LocalStorageUsageInfo> infos;
307 const bool kDontIncludeFileInfo = false;
308 GetLocalStorageUsage(&infos, kDontIncludeFileInfo);
309 for (size_t i = 0; i < infos.size(); ++i) {
310 const GURL& origin = infos[i].origin;
311 if (special_storage_policy_->IsStorageProtected(origin))
312 continue;
313 if (!special_storage_policy_->IsStorageSessionOnly(origin))
314 continue;
316 base::FilePath database_file_path = localstorage_directory_.Append(
317 DOMStorageArea::DatabaseFileNameFromOrigin(origin));
318 sql::Connection::Delete(database_file_path);
321 if (session_storage_database_.get()) {
322 std::vector<SessionStorageUsageInfo> infos;
323 GetSessionStorageUsage(&infos);
324 for (size_t i = 0; i < infos.size(); ++i) {
325 const GURL& origin = infos[i].origin;
326 if (special_storage_policy_->IsStorageProtected(origin))
327 continue;
328 if (!special_storage_policy_->IsStorageSessionOnly(origin))
329 continue;
330 session_storage_database_->DeleteArea(infos[i].persistent_namespace_id,
331 origin);
336 void DOMStorageContextImpl::SetSaveSessionStorageOnDisk() {
337 DCHECK(namespaces_.empty());
338 if (!sessionstorage_directory_.empty()) {
339 session_storage_database_ = new SessionStorageDatabase(
340 sessionstorage_directory_);
344 void DOMStorageContextImpl::StartScavengingUnusedSessionStorage() {
345 if (session_storage_database_.get()) {
346 task_runner_->PostDelayedTask(
347 FROM_HERE, base::Bind(&DOMStorageContextImpl::FindUnusedNamespaces,
348 this),
349 base::TimeDelta::FromSeconds(kSessionStoraceScavengingSeconds));
353 void DOMStorageContextImpl::FindUnusedNamespaces() {
354 DCHECK(session_storage_database_.get());
355 if (scavenging_started_)
356 return;
357 scavenging_started_ = true;
358 std::set<std::string> namespace_ids_in_use;
359 for (StorageNamespaceMap::const_iterator it = namespaces_.begin();
360 it != namespaces_.end(); ++it)
361 namespace_ids_in_use.insert(it->second->persistent_namespace_id());
362 std::set<std::string> protected_persistent_session_ids;
363 protected_persistent_session_ids.swap(protected_persistent_session_ids_);
364 task_runner_->PostShutdownBlockingTask(
365 FROM_HERE, DOMStorageTaskRunner::COMMIT_SEQUENCE,
366 base::Bind(
367 &DOMStorageContextImpl::FindUnusedNamespacesInCommitSequence,
368 this, namespace_ids_in_use, protected_persistent_session_ids));
371 void DOMStorageContextImpl::FindUnusedNamespacesInCommitSequence(
372 const std::set<std::string>& namespace_ids_in_use,
373 const std::set<std::string>& protected_persistent_session_ids) {
374 DCHECK(session_storage_database_.get());
375 // Delete all namespaces which don't have an associated DOMStorageNamespace
376 // alive.
377 std::map<std::string, std::vector<GURL> > namespaces_and_origins;
378 session_storage_database_->ReadNamespacesAndOrigins(&namespaces_and_origins);
379 for (std::map<std::string, std::vector<GURL> >::const_iterator it =
380 namespaces_and_origins.begin();
381 it != namespaces_and_origins.end(); ++it) {
382 if (namespace_ids_in_use.find(it->first) == namespace_ids_in_use.end() &&
383 protected_persistent_session_ids.find(it->first) ==
384 protected_persistent_session_ids.end()) {
385 deletable_persistent_namespace_ids_.push_back(it->first);
388 if (!deletable_persistent_namespace_ids_.empty()) {
389 task_runner_->PostDelayedTask(
390 FROM_HERE, base::Bind(
391 &DOMStorageContextImpl::DeleteNextUnusedNamespace,
392 this),
393 base::TimeDelta::FromSeconds(kSessionStoraceScavengingSeconds));
397 void DOMStorageContextImpl::DeleteNextUnusedNamespace() {
398 if (is_shutdown_)
399 return;
400 task_runner_->PostShutdownBlockingTask(
401 FROM_HERE, DOMStorageTaskRunner::COMMIT_SEQUENCE,
402 base::Bind(
403 &DOMStorageContextImpl::DeleteNextUnusedNamespaceInCommitSequence,
404 this));
407 void DOMStorageContextImpl::DeleteNextUnusedNamespaceInCommitSequence() {
408 if (deletable_persistent_namespace_ids_.empty())
409 return;
410 const std::string& persistent_id = deletable_persistent_namespace_ids_.back();
411 session_storage_database_->DeleteNamespace(persistent_id);
412 deletable_persistent_namespace_ids_.pop_back();
413 if (!deletable_persistent_namespace_ids_.empty()) {
414 task_runner_->PostDelayedTask(
415 FROM_HERE, base::Bind(
416 &DOMStorageContextImpl::DeleteNextUnusedNamespace,
417 this),
418 base::TimeDelta::FromSeconds(kSessionStoraceScavengingSeconds));
422 } // namespace content