| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "webkit/dom_storage/dom_storage_namespace.h" | |
| 6 | |
| 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" | |
| 15 | |
| 16 namespace dom_storage { | |
| 17 | |
| 18 DomStorageNamespace::DomStorageNamespace( | |
| 19 const base::FilePath& directory, | |
| 20 DomStorageTaskRunner* task_runner) | |
| 21 : namespace_id_(kLocalStorageNamespaceId), | |
| 22 directory_(directory), | |
| 23 task_runner_(task_runner) { | |
| 24 } | |
| 25 | |
| 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); | |
| 36 } | |
| 37 | |
| 38 DomStorageNamespace::~DomStorageNamespace() { | |
| 39 } | |
| 40 | |
| 41 DomStorageArea* DomStorageNamespace::OpenStorageArea(const GURL& origin) { | |
| 42 if (AreaHolder* holder = GetAreaHolder(origin)) { | |
| 43 ++(holder->open_count_); | |
| 44 return holder->area_.get(); | |
| 45 } | |
| 46 DomStorageArea* area; | |
| 47 if (namespace_id_ == kLocalStorageNamespaceId) { | |
| 48 area = new DomStorageArea(origin, directory_, task_runner_.get()); | |
| 49 } else { | |
| 50 area = new DomStorageArea(namespace_id_, | |
| 51 persistent_namespace_id_, | |
| 52 origin, | |
| 53 session_storage_database_.get(), | |
| 54 task_runner_.get()); | |
| 55 } | |
| 56 areas_[origin] = AreaHolder(area, 1); | |
| 57 return area; | |
| 58 } | |
| 59 | |
| 60 void DomStorageNamespace::CloseStorageArea(DomStorageArea* area) { | |
| 61 AreaHolder* holder = GetAreaHolder(area->origin()); | |
| 62 DCHECK(holder); | |
| 63 DCHECK_EQ(holder->area_.get(), area); | |
| 64 --(holder->open_count_); | |
| 65 // TODO(michaeln): Clean up areas that aren't needed in memory anymore. | |
| 66 // The in-process-webkit based impl didn't do this either, but would be nice. | |
| 67 } | |
| 68 | |
| 69 DomStorageArea* DomStorageNamespace::GetOpenStorageArea(const GURL& origin) { | |
| 70 AreaHolder* holder = GetAreaHolder(origin); | |
| 71 if (holder && holder->open_count_) | |
| 72 return holder->area_.get(); | |
| 73 return NULL; | |
| 74 } | |
| 75 | |
| 76 DomStorageNamespace* DomStorageNamespace::Clone( | |
| 77 int64 clone_namespace_id, | |
| 78 const std::string& clone_persistent_namespace_id) { | |
| 79 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); | |
| 80 DCHECK_NE(kLocalStorageNamespaceId, clone_namespace_id); | |
| 81 DomStorageNamespace* clone = | |
| 82 new DomStorageNamespace(clone_namespace_id, | |
| 83 clone_persistent_namespace_id, | |
| 84 session_storage_database_.get(), | |
| 85 task_runner_.get()); | |
| 86 AreaMap::const_iterator it = areas_.begin(); | |
| 87 // Clone the in-memory structures. | |
| 88 for (; it != areas_.end(); ++it) { | |
| 89 DomStorageArea* area = it->second.area_->ShallowCopy( | |
| 90 clone_namespace_id, clone_persistent_namespace_id); | |
| 91 clone->areas_[it->first] = AreaHolder(area, 0); | |
| 92 } | |
| 93 // And clone the on-disk structures, too. | |
| 94 if (session_storage_database_.get()) { | |
| 95 task_runner_->PostShutdownBlockingTask( | |
| 96 FROM_HERE, | |
| 97 DomStorageTaskRunner::COMMIT_SEQUENCE, | |
| 98 base::Bind(base::IgnoreResult(&SessionStorageDatabase::CloneNamespace), | |
| 99 session_storage_database_.get(), | |
| 100 persistent_namespace_id_, | |
| 101 clone_persistent_namespace_id)); | |
| 102 } | |
| 103 return clone; | |
| 104 } | |
| 105 | |
| 106 void DomStorageNamespace::DeleteLocalStorageOrigin(const GURL& origin) { | |
| 107 DCHECK(!session_storage_database_.get()); | |
| 108 AreaHolder* holder = GetAreaHolder(origin); | |
| 109 if (holder) { | |
| 110 holder->area_->DeleteOrigin(); | |
| 111 return; | |
| 112 } | |
| 113 if (!directory_.empty()) { | |
| 114 scoped_refptr<DomStorageArea> area = | |
| 115 new DomStorageArea(origin, directory_, task_runner_.get()); | |
| 116 area->DeleteOrigin(); | |
| 117 } | |
| 118 } | |
| 119 | |
| 120 void DomStorageNamespace::DeleteSessionStorageOrigin(const GURL& origin) { | |
| 121 DomStorageArea* area = OpenStorageArea(origin); | |
| 122 area->FastClear(); | |
| 123 CloseStorageArea(area); | |
| 124 } | |
| 125 | |
| 126 void DomStorageNamespace::PurgeMemory(PurgeOption option) { | |
| 127 if (directory_.empty()) | |
| 128 return; // We can't purge w/o backing on disk. | |
| 129 AreaMap::iterator it = areas_.begin(); | |
| 130 while (it != areas_.end()) { | |
| 131 // Leave it alone if changes are pending | |
| 132 if (it->second.area_->HasUncommittedChanges()) { | |
| 133 ++it; | |
| 134 continue; | |
| 135 } | |
| 136 | |
| 137 // If not in use, we can shut it down and remove | |
| 138 // it from our collection entirely. | |
| 139 if (it->second.open_count_ == 0) { | |
| 140 it->second.area_->Shutdown(); | |
| 141 areas_.erase(it++); | |
| 142 continue; | |
| 143 } | |
| 144 | |
| 145 if (option == PURGE_AGGRESSIVE) { | |
| 146 // If aggressive is true, we clear caches and such | |
| 147 // for opened areas. | |
| 148 it->second.area_->PurgeMemory(); | |
| 149 } | |
| 150 | |
| 151 ++it; | |
| 152 } | |
| 153 } | |
| 154 | |
| 155 void DomStorageNamespace::Shutdown() { | |
| 156 AreaMap::const_iterator it = areas_.begin(); | |
| 157 for (; it != areas_.end(); ++it) | |
| 158 it->second.area_->Shutdown(); | |
| 159 } | |
| 160 | |
| 161 unsigned int DomStorageNamespace::CountInMemoryAreas() const { | |
| 162 unsigned int area_count = 0; | |
| 163 for (AreaMap::const_iterator it = areas_.begin(); it != areas_.end(); ++it) { | |
| 164 if (it->second.area_->IsLoadedInMemory()) | |
| 165 ++area_count; | |
| 166 } | |
| 167 return area_count; | |
| 168 } | |
| 169 | |
| 170 DomStorageNamespace::AreaHolder* | |
| 171 DomStorageNamespace::GetAreaHolder(const GURL& origin) { | |
| 172 AreaMap::iterator found = areas_.find(origin); | |
| 173 if (found == areas_.end()) | |
| 174 return NULL; | |
| 175 return &(found->second); | |
| 176 } | |
| 177 | |
| 178 // AreaHolder | |
| 179 | |
| 180 DomStorageNamespace::AreaHolder::AreaHolder() | |
| 181 : open_count_(0) { | |
| 182 } | |
| 183 | |
| 184 DomStorageNamespace::AreaHolder::AreaHolder( | |
| 185 DomStorageArea* area, int count) | |
| 186 : area_(area), open_count_(count) { | |
| 187 } | |
| 188 | |
| 189 DomStorageNamespace::AreaHolder::~AreaHolder() { | |
| 190 } | |
| 191 | |
| 192 } // namespace dom_storage | |
| OLD | NEW |