| 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/browser/dom_storage/dom_storage_host.h" | |
| 6 | |
| 7 #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 | |
| 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 return false; | |
| 35 references.area_ = references.namespace_->OpenStorageArea(origin); | |
| 36 DCHECK(references.area_.get()); | |
| 37 connections_[connection_id] = references; | |
| 38 return true; | |
| 39 } | |
| 40 | |
| 41 void DomStorageHost::CloseStorageArea(int connection_id) { | |
| 42 AreaMap::iterator found = connections_.find(connection_id); | |
| 43 if (found == connections_.end()) | |
| 44 return; | |
| 45 found->second.namespace_->CloseStorageArea(found->second.area_.get()); | |
| 46 connections_.erase(found); | |
| 47 } | |
| 48 | |
| 49 bool DomStorageHost::ExtractAreaValues( | |
| 50 int connection_id, ValuesMap* map) { | |
| 51 map->clear(); | |
| 52 DomStorageArea* area = GetOpenArea(connection_id); | |
| 53 if (!area) | |
| 54 return false; | |
| 55 if (!area->IsLoadedInMemory()) { | |
| 56 DomStorageNamespace* ns = GetNamespace(connection_id); | |
| 57 DCHECK(ns); | |
| 58 if (ns->CountInMemoryAreas() > kMaxInMemoryAreas) { | |
| 59 ns->PurgeMemory(DomStorageNamespace::PURGE_UNOPENED); | |
| 60 if (ns->CountInMemoryAreas() > kMaxInMemoryAreas) | |
| 61 ns->PurgeMemory(DomStorageNamespace::PURGE_AGGRESSIVE); | |
| 62 } | |
| 63 } | |
| 64 area->ExtractValues(map); | |
| 65 return true; | |
| 66 } | |
| 67 | |
| 68 unsigned DomStorageHost::GetAreaLength(int connection_id) { | |
| 69 DomStorageArea* area = GetOpenArea(connection_id); | |
| 70 if (!area) | |
| 71 return 0; | |
| 72 return area->Length(); | |
| 73 } | |
| 74 | |
| 75 base::NullableString16 DomStorageHost::GetAreaKey(int connection_id, | |
| 76 unsigned index) { | |
| 77 DomStorageArea* area = GetOpenArea(connection_id); | |
| 78 if (!area) | |
| 79 return base::NullableString16(); | |
| 80 return area->Key(index); | |
| 81 } | |
| 82 | |
| 83 base::NullableString16 DomStorageHost::GetAreaItem(int connection_id, | |
| 84 const base::string16& key) { | |
| 85 DomStorageArea* area = GetOpenArea(connection_id); | |
| 86 if (!area) | |
| 87 return base::NullableString16(); | |
| 88 return area->GetItem(key); | |
| 89 } | |
| 90 | |
| 91 bool DomStorageHost::SetAreaItem( | |
| 92 int connection_id, const base::string16& key, | |
| 93 const base::string16& value, const GURL& page_url, | |
| 94 base::NullableString16* old_value) { | |
| 95 DomStorageArea* area = GetOpenArea(connection_id); | |
| 96 if (!area) | |
| 97 return false; | |
| 98 if (!area->SetItem(key, value, old_value)) | |
| 99 return false; | |
| 100 if (old_value->is_null() || old_value->string() != value) | |
| 101 context_->NotifyItemSet(area, key, value, *old_value, page_url); | |
| 102 return true; | |
| 103 } | |
| 104 | |
| 105 bool DomStorageHost::RemoveAreaItem( | |
| 106 int connection_id, const base::string16& key, const GURL& page_url, | |
| 107 base::string16* old_value) { | |
| 108 DomStorageArea* area = GetOpenArea(connection_id); | |
| 109 if (!area) | |
| 110 return false; | |
| 111 if (!area->RemoveItem(key, old_value)) | |
| 112 return false; | |
| 113 context_->NotifyItemRemoved(area, key, *old_value, page_url); | |
| 114 return true; | |
| 115 } | |
| 116 | |
| 117 bool DomStorageHost::ClearArea(int connection_id, const GURL& page_url) { | |
| 118 DomStorageArea* area = GetOpenArea(connection_id); | |
| 119 if (!area) | |
| 120 return false; | |
| 121 if (!area->Clear()) | |
| 122 return false; | |
| 123 context_->NotifyAreaCleared(area, page_url); | |
| 124 return true; | |
| 125 } | |
| 126 | |
| 127 bool DomStorageHost::HasAreaOpen( | |
| 128 int namespace_id, const GURL& origin) const { | |
| 129 AreaMap::const_iterator it = connections_.begin(); | |
| 130 for (; it != connections_.end(); ++it) { | |
| 131 if (namespace_id == it->second.namespace_->namespace_id() && | |
| 132 origin == it->second.area_->origin()) { | |
| 133 return true; | |
| 134 } | |
| 135 } | |
| 136 return false; | |
| 137 } | |
| 138 | |
| 139 DomStorageArea* DomStorageHost::GetOpenArea(int connection_id) { | |
| 140 AreaMap::iterator found = connections_.find(connection_id); | |
| 141 if (found == connections_.end()) | |
| 142 return NULL; | |
| 143 return found->second.area_.get(); | |
| 144 } | |
| 145 | |
| 146 DomStorageNamespace* DomStorageHost::GetNamespace(int connection_id) { | |
| 147 AreaMap::iterator found = connections_.find(connection_id); | |
| 148 if (found == connections_.end()) | |
| 149 return NULL; | |
| 150 return found->second.namespace_.get(); | |
| 151 } | |
| 152 | |
| 153 // NamespaceAndArea | |
| 154 | |
| 155 DomStorageHost::NamespaceAndArea::NamespaceAndArea() {} | |
| 156 DomStorageHost::NamespaceAndArea::~NamespaceAndArea() {} | |
| 157 | |
| 158 } // namespace dom_storage | |
| OLD | NEW |