| 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_host.h" | 5 #include "content/browser/dom_storage/dom_storage_host.h" |
| 6 | 6 |
| 7 #include "content/browser/dom_storage/dom_storage_area.h" |
| 8 #include "content/browser/dom_storage/dom_storage_context_impl.h" |
| 9 #include "content/browser/dom_storage/dom_storage_namespace.h" |
| 10 #include "content/common/dom_storage/dom_storage_types.h" |
| 7 #include "url/gurl.h" | 11 #include "url/gurl.h" |
| 8 #include "webkit/browser/dom_storage/dom_storage_area.h" | |
| 9 #include "webkit/browser/dom_storage/dom_storage_context.h" | |
| 10 #include "webkit/browser/dom_storage/dom_storage_namespace.h" | |
| 11 #include "webkit/common/dom_storage/dom_storage_types.h" | |
| 12 | 12 |
| 13 namespace dom_storage { | 13 namespace content { |
| 14 | 14 |
| 15 DomStorageHost::DomStorageHost(DomStorageContext* context) | 15 DOMStorageHost::DOMStorageHost(DOMStorageContextImpl* context) |
| 16 : context_(context) { | 16 : context_(context) { |
| 17 } | 17 } |
| 18 | 18 |
| 19 DomStorageHost::~DomStorageHost() { | 19 DOMStorageHost::~DOMStorageHost() { |
| 20 AreaMap::const_iterator it = connections_.begin(); | 20 AreaMap::const_iterator it = connections_.begin(); |
| 21 for (; it != connections_.end(); ++it) | 21 for (; it != connections_.end(); ++it) |
| 22 it->second.namespace_->CloseStorageArea(it->second.area_.get()); | 22 it->second.namespace_->CloseStorageArea(it->second.area_.get()); |
| 23 connections_.clear(); // Clear prior to releasing the context_ | 23 connections_.clear(); // Clear prior to releasing the context_ |
| 24 } | 24 } |
| 25 | 25 |
| 26 bool DomStorageHost::OpenStorageArea(int connection_id, int namespace_id, | 26 bool DOMStorageHost::OpenStorageArea(int connection_id, int namespace_id, |
| 27 const GURL& origin) { | 27 const GURL& origin) { |
| 28 DCHECK(!GetOpenArea(connection_id)); | 28 DCHECK(!GetOpenArea(connection_id)); |
| 29 if (GetOpenArea(connection_id)) | 29 if (GetOpenArea(connection_id)) |
| 30 return false; // Indicates the renderer gave us very bad data. | 30 return false; // Indicates the renderer gave us very bad data. |
| 31 NamespaceAndArea references; | 31 NamespaceAndArea references; |
| 32 references.namespace_ = context_->GetStorageNamespace(namespace_id); | 32 references.namespace_ = context_->GetStorageNamespace(namespace_id); |
| 33 if (!references.namespace_.get()) | 33 if (!references.namespace_.get()) |
| 34 return false; | 34 return false; |
| 35 references.area_ = references.namespace_->OpenStorageArea(origin); | 35 references.area_ = references.namespace_->OpenStorageArea(origin); |
| 36 DCHECK(references.area_.get()); | 36 DCHECK(references.area_.get()); |
| 37 connections_[connection_id] = references; | 37 connections_[connection_id] = references; |
| 38 return true; | 38 return true; |
| 39 } | 39 } |
| 40 | 40 |
| 41 void DomStorageHost::CloseStorageArea(int connection_id) { | 41 void DOMStorageHost::CloseStorageArea(int connection_id) { |
| 42 AreaMap::iterator found = connections_.find(connection_id); | 42 AreaMap::iterator found = connections_.find(connection_id); |
| 43 if (found == connections_.end()) | 43 if (found == connections_.end()) |
| 44 return; | 44 return; |
| 45 found->second.namespace_->CloseStorageArea(found->second.area_.get()); | 45 found->second.namespace_->CloseStorageArea(found->second.area_.get()); |
| 46 connections_.erase(found); | 46 connections_.erase(found); |
| 47 } | 47 } |
| 48 | 48 |
| 49 bool DomStorageHost::ExtractAreaValues( | 49 bool DOMStorageHost::ExtractAreaValues( |
| 50 int connection_id, ValuesMap* map) { | 50 int connection_id, DOMStorageValuesMap* map) { |
| 51 map->clear(); | 51 map->clear(); |
| 52 DomStorageArea* area = GetOpenArea(connection_id); | 52 DOMStorageArea* area = GetOpenArea(connection_id); |
| 53 if (!area) | 53 if (!area) |
| 54 return false; | 54 return false; |
| 55 if (!area->IsLoadedInMemory()) { | 55 if (!area->IsLoadedInMemory()) { |
| 56 DomStorageNamespace* ns = GetNamespace(connection_id); | 56 DOMStorageNamespace* ns = GetNamespace(connection_id); |
| 57 DCHECK(ns); | 57 DCHECK(ns); |
| 58 if (ns->CountInMemoryAreas() > kMaxInMemoryAreas) { | 58 if (ns->CountInMemoryAreas() > kMaxInMemoryStorageAreas) { |
| 59 ns->PurgeMemory(DomStorageNamespace::PURGE_UNOPENED); | 59 ns->PurgeMemory(DOMStorageNamespace::PURGE_UNOPENED); |
| 60 if (ns->CountInMemoryAreas() > kMaxInMemoryAreas) | 60 if (ns->CountInMemoryAreas() > kMaxInMemoryStorageAreas) |
| 61 ns->PurgeMemory(DomStorageNamespace::PURGE_AGGRESSIVE); | 61 ns->PurgeMemory(DOMStorageNamespace::PURGE_AGGRESSIVE); |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 area->ExtractValues(map); | 64 area->ExtractValues(map); |
| 65 return true; | 65 return true; |
| 66 } | 66 } |
| 67 | 67 |
| 68 unsigned DomStorageHost::GetAreaLength(int connection_id) { | 68 unsigned DOMStorageHost::GetAreaLength(int connection_id) { |
| 69 DomStorageArea* area = GetOpenArea(connection_id); | 69 DOMStorageArea* area = GetOpenArea(connection_id); |
| 70 if (!area) | 70 if (!area) |
| 71 return 0; | 71 return 0; |
| 72 return area->Length(); | 72 return area->Length(); |
| 73 } | 73 } |
| 74 | 74 |
| 75 base::NullableString16 DomStorageHost::GetAreaKey(int connection_id, | 75 base::NullableString16 DOMStorageHost::GetAreaKey(int connection_id, |
| 76 unsigned index) { | 76 unsigned index) { |
| 77 DomStorageArea* area = GetOpenArea(connection_id); | 77 DOMStorageArea* area = GetOpenArea(connection_id); |
| 78 if (!area) | 78 if (!area) |
| 79 return base::NullableString16(); | 79 return base::NullableString16(); |
| 80 return area->Key(index); | 80 return area->Key(index); |
| 81 } | 81 } |
| 82 | 82 |
| 83 base::NullableString16 DomStorageHost::GetAreaItem(int connection_id, | 83 base::NullableString16 DOMStorageHost::GetAreaItem(int connection_id, |
| 84 const base::string16& key) { | 84 const base::string16& key) { |
| 85 DomStorageArea* area = GetOpenArea(connection_id); | 85 DOMStorageArea* area = GetOpenArea(connection_id); |
| 86 if (!area) | 86 if (!area) |
| 87 return base::NullableString16(); | 87 return base::NullableString16(); |
| 88 return area->GetItem(key); | 88 return area->GetItem(key); |
| 89 } | 89 } |
| 90 | 90 |
| 91 bool DomStorageHost::SetAreaItem( | 91 bool DOMStorageHost::SetAreaItem( |
| 92 int connection_id, const base::string16& key, | 92 int connection_id, const base::string16& key, |
| 93 const base::string16& value, const GURL& page_url, | 93 const base::string16& value, const GURL& page_url, |
| 94 base::NullableString16* old_value) { | 94 base::NullableString16* old_value) { |
| 95 DomStorageArea* area = GetOpenArea(connection_id); | 95 DOMStorageArea* area = GetOpenArea(connection_id); |
| 96 if (!area) | 96 if (!area) |
| 97 return false; | 97 return false; |
| 98 if (!area->SetItem(key, value, old_value)) | 98 if (!area->SetItem(key, value, old_value)) |
| 99 return false; | 99 return false; |
| 100 if (old_value->is_null() || old_value->string() != value) | 100 if (old_value->is_null() || old_value->string() != value) |
| 101 context_->NotifyItemSet(area, key, value, *old_value, page_url); | 101 context_->NotifyItemSet(area, key, value, *old_value, page_url); |
| 102 return true; | 102 return true; |
| 103 } | 103 } |
| 104 | 104 |
| 105 bool DomStorageHost::RemoveAreaItem( | 105 bool DOMStorageHost::RemoveAreaItem( |
| 106 int connection_id, const base::string16& key, const GURL& page_url, | 106 int connection_id, const base::string16& key, const GURL& page_url, |
| 107 base::string16* old_value) { | 107 base::string16* old_value) { |
| 108 DomStorageArea* area = GetOpenArea(connection_id); | 108 DOMStorageArea* area = GetOpenArea(connection_id); |
| 109 if (!area) | 109 if (!area) |
| 110 return false; | 110 return false; |
| 111 if (!area->RemoveItem(key, old_value)) | 111 if (!area->RemoveItem(key, old_value)) |
| 112 return false; | 112 return false; |
| 113 context_->NotifyItemRemoved(area, key, *old_value, page_url); | 113 context_->NotifyItemRemoved(area, key, *old_value, page_url); |
| 114 return true; | 114 return true; |
| 115 } | 115 } |
| 116 | 116 |
| 117 bool DomStorageHost::ClearArea(int connection_id, const GURL& page_url) { | 117 bool DOMStorageHost::ClearArea(int connection_id, const GURL& page_url) { |
| 118 DomStorageArea* area = GetOpenArea(connection_id); | 118 DOMStorageArea* area = GetOpenArea(connection_id); |
| 119 if (!area) | 119 if (!area) |
| 120 return false; | 120 return false; |
| 121 if (!area->Clear()) | 121 if (!area->Clear()) |
| 122 return false; | 122 return false; |
| 123 context_->NotifyAreaCleared(area, page_url); | 123 context_->NotifyAreaCleared(area, page_url); |
| 124 return true; | 124 return true; |
| 125 } | 125 } |
| 126 | 126 |
| 127 bool DomStorageHost::HasAreaOpen( | 127 bool DOMStorageHost::HasAreaOpen( |
| 128 int namespace_id, const GURL& origin) const { | 128 int namespace_id, const GURL& origin) const { |
| 129 AreaMap::const_iterator it = connections_.begin(); | 129 AreaMap::const_iterator it = connections_.begin(); |
| 130 for (; it != connections_.end(); ++it) { | 130 for (; it != connections_.end(); ++it) { |
| 131 if (namespace_id == it->second.namespace_->namespace_id() && | 131 if (namespace_id == it->second.namespace_->namespace_id() && |
| 132 origin == it->second.area_->origin()) { | 132 origin == it->second.area_->origin()) { |
| 133 return true; | 133 return true; |
| 134 } | 134 } |
| 135 } | 135 } |
| 136 return false; | 136 return false; |
| 137 } | 137 } |
| 138 | 138 |
| 139 DomStorageArea* DomStorageHost::GetOpenArea(int connection_id) { | 139 DOMStorageArea* DOMStorageHost::GetOpenArea(int connection_id) { |
| 140 AreaMap::iterator found = connections_.find(connection_id); | 140 AreaMap::iterator found = connections_.find(connection_id); |
| 141 if (found == connections_.end()) | 141 if (found == connections_.end()) |
| 142 return NULL; | 142 return NULL; |
| 143 return found->second.area_.get(); | 143 return found->second.area_.get(); |
| 144 } | 144 } |
| 145 | 145 |
| 146 DomStorageNamespace* DomStorageHost::GetNamespace(int connection_id) { | 146 DOMStorageNamespace* DOMStorageHost::GetNamespace(int connection_id) { |
| 147 AreaMap::iterator found = connections_.find(connection_id); | 147 AreaMap::iterator found = connections_.find(connection_id); |
| 148 if (found == connections_.end()) | 148 if (found == connections_.end()) |
| 149 return NULL; | 149 return NULL; |
| 150 return found->second.namespace_.get(); | 150 return found->second.namespace_.get(); |
| 151 } | 151 } |
| 152 | 152 |
| 153 // NamespaceAndArea | 153 // NamespaceAndArea |
| 154 | 154 |
| 155 DomStorageHost::NamespaceAndArea::NamespaceAndArea() {} | 155 DOMStorageHost::NamespaceAndArea::NamespaceAndArea() {} |
| 156 DomStorageHost::NamespaceAndArea::~NamespaceAndArea() {} | 156 DOMStorageHost::NamespaceAndArea::~NamespaceAndArea() {} |
| 157 | 157 |
| 158 } // namespace dom_storage | 158 } // namespace content |
| OLD | NEW |