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