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