Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / webkit / dom_storage / dom_storage_namespace.cc
blob4c1cbd7466c63818c755b183a3bcd628882e0225
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 "webkit/dom_storage/dom_storage_namespace.h"
7 #include "base/basictypes.h"
8 #include "base/bind.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "webkit/dom_storage/dom_storage_area.h"
12 #include "webkit/dom_storage/dom_storage_task_runner.h"
13 #include "webkit/dom_storage/dom_storage_types.h"
14 #include "webkit/dom_storage/session_storage_database.h"
16 namespace dom_storage {
18 DomStorageNamespace::DomStorageNamespace(
19 const base::FilePath& directory,
20 DomStorageTaskRunner* task_runner)
21 : namespace_id_(kLocalStorageNamespaceId),
22 directory_(directory),
23 task_runner_(task_runner) {
26 DomStorageNamespace::DomStorageNamespace(
27 int64 namespace_id,
28 const std::string& persistent_namespace_id,
29 SessionStorageDatabase* session_storage_database,
30 DomStorageTaskRunner* task_runner)
31 : namespace_id_(namespace_id),
32 persistent_namespace_id_(persistent_namespace_id),
33 task_runner_(task_runner),
34 session_storage_database_(session_storage_database) {
35 DCHECK_NE(kLocalStorageNamespaceId, namespace_id);
38 DomStorageNamespace::~DomStorageNamespace() {
41 DomStorageArea* DomStorageNamespace::OpenStorageArea(const GURL& origin) {
42 if (AreaHolder* holder = GetAreaHolder(origin)) {
43 ++(holder->open_count_);
44 return holder->area_;
46 DomStorageArea* area;
47 if (namespace_id_ == kLocalStorageNamespaceId) {
48 area = new DomStorageArea(origin, directory_, task_runner_);
49 } else {
50 area = new DomStorageArea(namespace_id_, persistent_namespace_id_, origin,
51 session_storage_database_, task_runner_);
53 areas_[origin] = AreaHolder(area, 1);
54 return area;
57 void DomStorageNamespace::CloseStorageArea(DomStorageArea* area) {
58 AreaHolder* holder = GetAreaHolder(area->origin());
59 DCHECK(holder);
60 DCHECK_EQ(holder->area_.get(), area);
61 --(holder->open_count_);
62 // TODO(michaeln): Clean up areas that aren't needed in memory anymore.
63 // The in-process-webkit based impl didn't do this either, but would be nice.
66 DomStorageArea* DomStorageNamespace::GetOpenStorageArea(const GURL& origin) {
67 AreaHolder* holder = GetAreaHolder(origin);
68 if (holder && holder->open_count_)
69 return holder->area_;
70 return NULL;
73 DomStorageNamespace* DomStorageNamespace::Clone(
74 int64 clone_namespace_id,
75 const std::string& clone_persistent_namespace_id) {
76 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_);
77 DCHECK_NE(kLocalStorageNamespaceId, clone_namespace_id);
78 DomStorageNamespace* clone = new DomStorageNamespace(
79 clone_namespace_id, clone_persistent_namespace_id,
80 session_storage_database_, task_runner_);
81 AreaMap::const_iterator it = areas_.begin();
82 // Clone the in-memory structures.
83 for (; it != areas_.end(); ++it) {
84 DomStorageArea* area = it->second.area_->ShallowCopy(
85 clone_namespace_id, clone_persistent_namespace_id);
86 clone->areas_[it->first] = AreaHolder(area, 0);
88 // And clone the on-disk structures, too.
89 if (session_storage_database_.get()) {
90 task_runner_->PostShutdownBlockingTask(
91 FROM_HERE,
92 DomStorageTaskRunner::COMMIT_SEQUENCE,
93 base::Bind(base::IgnoreResult(&SessionStorageDatabase::CloneNamespace),
94 session_storage_database_.get(), persistent_namespace_id_,
95 clone_persistent_namespace_id));
97 return clone;
100 void DomStorageNamespace::DeleteLocalStorageOrigin(const GURL& origin) {
101 DCHECK(!session_storage_database_.get());
102 AreaHolder* holder = GetAreaHolder(origin);
103 if (holder) {
104 holder->area_->DeleteOrigin();
105 return;
107 if (!directory_.empty()) {
108 scoped_refptr<DomStorageArea> area =
109 new DomStorageArea(origin, directory_, task_runner_);
110 area->DeleteOrigin();
114 void DomStorageNamespace::DeleteSessionStorageOrigin(const GURL& origin) {
115 DomStorageArea* area = OpenStorageArea(origin);
116 area->FastClear();
117 CloseStorageArea(area);
120 void DomStorageNamespace::PurgeMemory() {
121 if (directory_.empty())
122 return; // We can't purge w/o backing on disk.
123 AreaMap::iterator it = areas_.begin();
124 while (it != areas_.end()) {
125 // Leave it alone if changes are pending
126 if (it->second.area_->HasUncommittedChanges()) {
127 ++it;
128 continue;
131 // If not in use, we can shut it down and remove
132 // it from our collection entirely.
133 if (it->second.open_count_ == 0) {
134 it->second.area_->Shutdown();
135 areas_.erase(it++);
136 continue;
139 // Otherwise, we can clear caches and such.
140 it->second.area_->PurgeMemory();
141 ++it;
145 void DomStorageNamespace::Shutdown() {
146 AreaMap::const_iterator it = areas_.begin();
147 for (; it != areas_.end(); ++it)
148 it->second.area_->Shutdown();
152 DomStorageNamespace::AreaHolder*
153 DomStorageNamespace::GetAreaHolder(const GURL& origin) {
154 AreaMap::iterator found = areas_.find(origin);
155 if (found == areas_.end())
156 return NULL;
157 return &(found->second);
160 // AreaHolder
162 DomStorageNamespace::AreaHolder::AreaHolder()
163 : open_count_(0) {
166 DomStorageNamespace::AreaHolder::AreaHolder(
167 DomStorageArea* area, int count)
168 : area_(area), open_count_(count) {
171 DomStorageNamespace::AreaHolder::~AreaHolder() {
174 } // namespace dom_storage