| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/dom_storage/dom_storage_namespace.h" | 5 #include "content/browser/dom_storage/dom_storage_namespace.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 must_persist_at_shutdown_(false) { | 56 must_persist_at_shutdown_(false) { |
| 57 DCHECK_NE(kLocalStorageNamespaceId, namespace_id); | 57 DCHECK_NE(kLocalStorageNamespaceId, namespace_id); |
| 58 } | 58 } |
| 59 | 59 |
| 60 DOMStorageNamespace::~DOMStorageNamespace() { | 60 DOMStorageNamespace::~DOMStorageNamespace() { |
| 61 STLDeleteValues(&transactions_); | 61 STLDeleteValues(&transactions_); |
| 62 DecrementMasterAliasCount(); | 62 DecrementMasterAliasCount(); |
| 63 } | 63 } |
| 64 | 64 |
| 65 DOMStorageArea* DOMStorageNamespace::OpenStorageArea(const GURL& origin) { | 65 DOMStorageArea* DOMStorageNamespace::OpenStorageArea(const GURL& origin) { |
| 66 if (alias_master_namespace_) | 66 if (alias_master_namespace_.get()) |
| 67 return alias_master_namespace_->OpenStorageArea(origin); | 67 return alias_master_namespace_->OpenStorageArea(origin); |
| 68 if (AreaHolder* holder = GetAreaHolder(origin)) { | 68 if (AreaHolder* holder = GetAreaHolder(origin)) { |
| 69 ++(holder->open_count_); | 69 ++(holder->open_count_); |
| 70 return holder->area_.get(); | 70 return holder->area_.get(); |
| 71 } | 71 } |
| 72 DOMStorageArea* area; | 72 DOMStorageArea* area; |
| 73 if (namespace_id_ == kLocalStorageNamespaceId) { | 73 if (namespace_id_ == kLocalStorageNamespaceId) { |
| 74 area = new DOMStorageArea(origin, directory_, task_runner_.get()); | 74 area = new DOMStorageArea(origin, directory_, task_runner_.get()); |
| 75 } else { | 75 } else { |
| 76 area = new DOMStorageArea( | 76 area = new DOMStorageArea( |
| 77 namespace_id_, persistent_namespace_id_, origin, | 77 namespace_id_, persistent_namespace_id_, origin, |
| 78 session_storage_database_.get(), task_runner_.get()); | 78 session_storage_database_.get(), task_runner_.get()); |
| 79 } | 79 } |
| 80 areas_[origin] = AreaHolder(area, 1); | 80 areas_[origin] = AreaHolder(area, 1); |
| 81 return area; | 81 return area; |
| 82 } | 82 } |
| 83 | 83 |
| 84 void DOMStorageNamespace::CloseStorageArea(DOMStorageArea* area) { | 84 void DOMStorageNamespace::CloseStorageArea(DOMStorageArea* area) { |
| 85 AreaHolder* holder = GetAreaHolder(area->origin()); | 85 AreaHolder* holder = GetAreaHolder(area->origin()); |
| 86 if (alias_master_namespace_) { | 86 if (alias_master_namespace_.get()) { |
| 87 DCHECK(!holder); | 87 DCHECK(!holder); |
| 88 if (old_master_for_close_area_) | 88 if (old_master_for_close_area_) |
| 89 old_master_for_close_area_->CloseStorageArea(area); | 89 old_master_for_close_area_->CloseStorageArea(area); |
| 90 else | 90 else |
| 91 alias_master_namespace_->CloseStorageArea(area); | 91 alias_master_namespace_->CloseStorageArea(area); |
| 92 return; | 92 return; |
| 93 } | 93 } |
| 94 DCHECK(holder); | 94 DCHECK(holder); |
| 95 DCHECK_EQ(holder->area_.get(), area); | 95 DCHECK_EQ(holder->area_.get(), area); |
| 96 --(holder->open_count_); | 96 --(holder->open_count_); |
| 97 // TODO(michaeln): Clean up areas that aren't needed in memory anymore. | 97 // TODO(michaeln): Clean up areas that aren't needed in memory anymore. |
| 98 // The in-process-webkit based impl didn't do this either, but would be nice. | 98 // The in-process-webkit based impl didn't do this either, but would be nice. |
| 99 } | 99 } |
| 100 | 100 |
| 101 DOMStorageArea* DOMStorageNamespace::GetOpenStorageArea(const GURL& origin) { | 101 DOMStorageArea* DOMStorageNamespace::GetOpenStorageArea(const GURL& origin) { |
| 102 if (alias_master_namespace_) | 102 if (alias_master_namespace_.get()) |
| 103 return alias_master_namespace_->GetOpenStorageArea(origin); | 103 return alias_master_namespace_->GetOpenStorageArea(origin); |
| 104 AreaHolder* holder = GetAreaHolder(origin); | 104 AreaHolder* holder = GetAreaHolder(origin); |
| 105 if (holder && holder->open_count_) | 105 if (holder && holder->open_count_) |
| 106 return holder->area_.get(); | 106 return holder->area_.get(); |
| 107 return NULL; | 107 return NULL; |
| 108 } | 108 } |
| 109 | 109 |
| 110 DOMStorageNamespace* DOMStorageNamespace::Clone( | 110 DOMStorageNamespace* DOMStorageNamespace::Clone( |
| 111 int64 clone_namespace_id, | 111 int64 clone_namespace_id, |
| 112 const std::string& clone_persistent_namespace_id) { | 112 const std::string& clone_persistent_namespace_id) { |
| 113 if (alias_master_namespace_) { | 113 if (alias_master_namespace_.get()) { |
| 114 return alias_master_namespace_->Clone(clone_namespace_id, | 114 return alias_master_namespace_->Clone(clone_namespace_id, |
| 115 clone_persistent_namespace_id); | 115 clone_persistent_namespace_id); |
| 116 } | 116 } |
| 117 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); | 117 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); |
| 118 DCHECK_NE(kLocalStorageNamespaceId, clone_namespace_id); | 118 DCHECK_NE(kLocalStorageNamespaceId, clone_namespace_id); |
| 119 DOMStorageNamespace* clone = new DOMStorageNamespace( | 119 DOMStorageNamespace* clone = new DOMStorageNamespace( |
| 120 clone_namespace_id, clone_persistent_namespace_id, | 120 clone_namespace_id, clone_persistent_namespace_id, |
| 121 session_storage_database_.get(), task_runner_.get()); | 121 session_storage_database_.get(), task_runner_.get()); |
| 122 AreaMap::const_iterator it = areas_.begin(); | 122 AreaMap::const_iterator it = areas_.begin(); |
| 123 // Clone the in-memory structures. | 123 // Clone the in-memory structures. |
| (...skipping 22 matching lines...) Expand all Loading... |
| 146 // the alias will never open any areas of its own, but always redirect | 146 // the alias will never open any areas of its own, but always redirect |
| 147 // to the master). Accordingly, an alias will also never undergo the shutdown | 147 // to the master). Accordingly, an alias will also never undergo the shutdown |
| 148 // procedure which initiates persisting to disk, since there is never any data | 148 // procedure which initiates persisting to disk, since there is never any data |
| 149 // of its own to persist to disk. DOMStorageContextImpl is the place where | 149 // of its own to persist to disk. DOMStorageContextImpl is the place where |
| 150 // shutdowns are initiated, but only for non-alias DOMStorageNamespaces. | 150 // shutdowns are initiated, but only for non-alias DOMStorageNamespaces. |
| 151 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); | 151 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); |
| 152 DCHECK_NE(kLocalStorageNamespaceId, alias_namespace_id); | 152 DCHECK_NE(kLocalStorageNamespaceId, alias_namespace_id); |
| 153 DOMStorageNamespace* alias = new DOMStorageNamespace( | 153 DOMStorageNamespace* alias = new DOMStorageNamespace( |
| 154 alias_namespace_id, persistent_namespace_id_, | 154 alias_namespace_id, persistent_namespace_id_, |
| 155 session_storage_database_.get(), task_runner_.get()); | 155 session_storage_database_.get(), task_runner_.get()); |
| 156 if (alias_master_namespace_ != NULL) { | 156 if (alias_master_namespace_.get() != NULL) { |
| 157 DCHECK(alias_master_namespace_->alias_master_namespace_ == NULL); | 157 DCHECK(alias_master_namespace_->alias_master_namespace_.get() == NULL); |
| 158 alias->alias_master_namespace_ = alias_master_namespace_; | 158 alias->alias_master_namespace_ = alias_master_namespace_; |
| 159 } else { | 159 } else { |
| 160 alias->alias_master_namespace_ = this; | 160 alias->alias_master_namespace_ = this; |
| 161 } | 161 } |
| 162 alias->alias_master_namespace_->num_aliases_++; | 162 alias->alias_master_namespace_->num_aliases_++; |
| 163 return alias; | 163 return alias; |
| 164 } | 164 } |
| 165 | 165 |
| 166 void DOMStorageNamespace::DeleteLocalStorageOrigin(const GURL& origin) { | 166 void DOMStorageNamespace::DeleteLocalStorageOrigin(const GURL& origin) { |
| 167 DCHECK(!session_storage_database_.get()); | 167 DCHECK(!session_storage_database_.get()); |
| 168 DCHECK(!alias_master_namespace_.get()); | 168 DCHECK(!alias_master_namespace_.get()); |
| 169 AreaHolder* holder = GetAreaHolder(origin); | 169 AreaHolder* holder = GetAreaHolder(origin); |
| 170 if (holder) { | 170 if (holder) { |
| 171 holder->area_->DeleteOrigin(); | 171 holder->area_->DeleteOrigin(); |
| 172 return; | 172 return; |
| 173 } | 173 } |
| 174 if (!directory_.empty()) { | 174 if (!directory_.empty()) { |
| 175 scoped_refptr<DOMStorageArea> area = | 175 scoped_refptr<DOMStorageArea> area = |
| 176 new DOMStorageArea(origin, directory_, task_runner_.get()); | 176 new DOMStorageArea(origin, directory_, task_runner_.get()); |
| 177 area->DeleteOrigin(); | 177 area->DeleteOrigin(); |
| 178 } | 178 } |
| 179 } | 179 } |
| 180 | 180 |
| 181 void DOMStorageNamespace::DeleteSessionStorageOrigin(const GURL& origin) { | 181 void DOMStorageNamespace::DeleteSessionStorageOrigin(const GURL& origin) { |
| 182 if (alias_master_namespace_) { | 182 if (alias_master_namespace_.get()) { |
| 183 alias_master_namespace_->DeleteSessionStorageOrigin(origin); | 183 alias_master_namespace_->DeleteSessionStorageOrigin(origin); |
| 184 return; | 184 return; |
| 185 } | 185 } |
| 186 DOMStorageArea* area = OpenStorageArea(origin); | 186 DOMStorageArea* area = OpenStorageArea(origin); |
| 187 area->FastClear(); | 187 area->FastClear(); |
| 188 CloseStorageArea(area); | 188 CloseStorageArea(area); |
| 189 } | 189 } |
| 190 | 190 |
| 191 void DOMStorageNamespace::PurgeMemory(PurgeOption option) { | 191 void DOMStorageNamespace::PurgeMemory(PurgeOption option) { |
| 192 if (alias_master_namespace_) { | 192 if (alias_master_namespace_.get()) { |
| 193 alias_master_namespace_->PurgeMemory(option); | 193 alias_master_namespace_->PurgeMemory(option); |
| 194 return; | 194 return; |
| 195 } | 195 } |
| 196 if (directory_.empty()) | 196 if (directory_.empty()) |
| 197 return; // We can't purge w/o backing on disk. | 197 return; // We can't purge w/o backing on disk. |
| 198 AreaMap::iterator it = areas_.begin(); | 198 AreaMap::iterator it = areas_.begin(); |
| 199 while (it != areas_.end()) { | 199 while (it != areas_.end()) { |
| 200 // Leave it alone if changes are pending | 200 // Leave it alone if changes are pending |
| 201 if (it->second.area_->HasUncommittedChanges()) { | 201 if (it->second.area_->HasUncommittedChanges()) { |
| 202 ++it; | 202 ++it; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 221 } | 221 } |
| 222 } | 222 } |
| 223 | 223 |
| 224 void DOMStorageNamespace::Shutdown() { | 224 void DOMStorageNamespace::Shutdown() { |
| 225 AreaMap::const_iterator it = areas_.begin(); | 225 AreaMap::const_iterator it = areas_.begin(); |
| 226 for (; it != areas_.end(); ++it) | 226 for (; it != areas_.end(); ++it) |
| 227 it->second.area_->Shutdown(); | 227 it->second.area_->Shutdown(); |
| 228 } | 228 } |
| 229 | 229 |
| 230 unsigned int DOMStorageNamespace::CountInMemoryAreas() const { | 230 unsigned int DOMStorageNamespace::CountInMemoryAreas() const { |
| 231 if (alias_master_namespace_) | 231 if (alias_master_namespace_.get()) |
| 232 return alias_master_namespace_->CountInMemoryAreas(); | 232 return alias_master_namespace_->CountInMemoryAreas(); |
| 233 unsigned int area_count = 0; | 233 unsigned int area_count = 0; |
| 234 for (AreaMap::const_iterator it = areas_.begin(); it != areas_.end(); ++it) { | 234 for (AreaMap::const_iterator it = areas_.begin(); it != areas_.end(); ++it) { |
| 235 if (it->second.area_->IsLoadedInMemory()) | 235 if (it->second.area_->IsLoadedInMemory()) |
| 236 ++area_count; | 236 ++area_count; |
| 237 } | 237 } |
| 238 return area_count; | 238 return area_count; |
| 239 } | 239 } |
| 240 | 240 |
| 241 DOMStorageNamespace::AreaHolder* | 241 DOMStorageNamespace::AreaHolder* |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 if (transaction_data->max_log_size_exceeded) | 368 if (transaction_data->max_log_size_exceeded) |
| 369 return; | 369 return; |
| 370 transaction_data->log.push_back(transaction); | 370 transaction_data->log.push_back(transaction); |
| 371 if (transaction_data->log.size() > kMaxTransactionLogEntries) { | 371 if (transaction_data->log.size() > kMaxTransactionLogEntries) { |
| 372 transaction_data->max_log_size_exceeded = true; | 372 transaction_data->max_log_size_exceeded = true; |
| 373 transaction_data->log.clear(); | 373 transaction_data->log.clear(); |
| 374 } | 374 } |
| 375 } | 375 } |
| 376 | 376 |
| 377 bool DOMStorageNamespace::DecrementMasterAliasCount() { | 377 bool DOMStorageNamespace::DecrementMasterAliasCount() { |
| 378 if (!alias_master_namespace_ || master_alias_count_decremented_) | 378 if (!alias_master_namespace_.get() || master_alias_count_decremented_) |
| 379 return false; | 379 return false; |
| 380 DCHECK_GT(alias_master_namespace_->num_aliases_, 0); | 380 DCHECK_GT(alias_master_namespace_->num_aliases_, 0); |
| 381 alias_master_namespace_->num_aliases_--; | 381 alias_master_namespace_->num_aliases_--; |
| 382 master_alias_count_decremented_ = true; | 382 master_alias_count_decremented_ = true; |
| 383 return (alias_master_namespace_->num_aliases_ == 0); | 383 return (alias_master_namespace_->num_aliases_ == 0); |
| 384 } | 384 } |
| 385 | 385 |
| 386 void DOMStorageNamespace::SwitchToNewAliasMaster( | 386 void DOMStorageNamespace::SwitchToNewAliasMaster( |
| 387 DOMStorageNamespace* new_master, | 387 DOMStorageNamespace* new_master, |
| 388 DOMStorageContextImpl* context) { | 388 DOMStorageContextImpl* context) { |
| 389 DCHECK(alias_master_namespace()); | 389 DCHECK(alias_master_namespace()); |
| 390 scoped_refptr<DOMStorageNamespace> old_master = alias_master_namespace(); | 390 scoped_refptr<DOMStorageNamespace> old_master = alias_master_namespace(); |
| 391 if (new_master->alias_master_namespace()) | 391 if (new_master->alias_master_namespace()) |
| 392 new_master = new_master->alias_master_namespace(); | 392 new_master = new_master->alias_master_namespace(); |
| 393 DCHECK(!new_master->alias_master_namespace()); | 393 DCHECK(!new_master->alias_master_namespace()); |
| 394 DCHECK(old_master != this); | 394 DCHECK(old_master.get() != this); |
| 395 DCHECK(old_master != new_master); | 395 DCHECK(old_master.get() != new_master); |
| 396 DecrementMasterAliasCount(); | 396 DecrementMasterAliasCount(); |
| 397 alias_master_namespace_ = new_master; | 397 alias_master_namespace_ = new_master; |
| 398 alias_master_namespace_->num_aliases_++; | 398 alias_master_namespace_->num_aliases_++; |
| 399 master_alias_count_decremented_ = false; | 399 master_alias_count_decremented_ = false; |
| 400 // There are three things that we need to clean up: | 400 // There are three things that we need to clean up: |
| 401 // -- the old master may ready for shutdown, if its last alias has disappeared | 401 // -- the old master may ready for shutdown, if its last alias has disappeared |
| 402 // -- The dom_storage hosts need to close and reopen their areas, so that | 402 // -- The dom_storage hosts need to close and reopen their areas, so that |
| 403 // they point to the correct new areas. | 403 // they point to the correct new areas. |
| 404 // -- The renderers will need to reset their local caches. | 404 // -- The renderers will need to reset their local caches. |
| 405 // All three of these things are accomplished with the following call below. | 405 // All three of these things are accomplished with the following call below. |
| (...skipping 29 matching lines...) Expand all Loading... |
| 435 | 435 |
| 436 DOMStorageNamespace::AreaHolder::AreaHolder( | 436 DOMStorageNamespace::AreaHolder::AreaHolder( |
| 437 DOMStorageArea* area, int count) | 437 DOMStorageArea* area, int count) |
| 438 : area_(area), open_count_(count) { | 438 : area_(area), open_count_(count) { |
| 439 } | 439 } |
| 440 | 440 |
| 441 DOMStorageNamespace::AreaHolder::~AreaHolder() { | 441 DOMStorageNamespace::AreaHolder::~AreaHolder() { |
| 442 } | 442 } |
| 443 | 443 |
| 444 } // namespace content | 444 } // namespace content |
| OLD | NEW |