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 : last_connection_id_(0), |
| 17 context_(context) { |
| 18 } |
| 19 |
| 20 DomStorageHost::~DomStorageHost() { |
| 21 AreaMap::const_iterator it = connections_.begin(); |
| 22 for (; it != connections_.end(); ++it) |
| 23 it->second.namespace_->CloseStorageArea(it->second.area_); |
| 24 } |
| 25 |
| 26 // TODO(michaeln): have the caller pass in the 'connection_id' value |
| 27 // instead of generating them here, avoids a sync ipc on open. |
| 28 int DomStorageHost::OpenStorageArea(int namespace_id, const GURL& origin) { |
| 29 NamespaceAndArea references; |
| 30 references.namespace_ = context_->GetStorageNamespace(namespace_id); |
| 31 if (!references.namespace_) |
| 32 return kInvalidAreaId; |
| 33 references.area_ = references.namespace_->OpenStorageArea(origin); |
| 34 if (!references.area_) |
| 35 return kInvalidAreaId; |
| 36 int id = ++last_connection_id_; |
| 37 connections_[id] = references; |
| 38 return id; |
| 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( |
| 46 found->second.area_); |
| 47 connections_.erase(found); |
| 48 } |
| 49 |
| 50 unsigned DomStorageHost::GetAreaLength(int connection_id) { |
| 51 DomStorageArea* area = GetOpenArea(connection_id); |
| 52 if (!area) |
| 53 return 0; |
| 54 return area->Length(); |
| 55 } |
| 56 |
| 57 NullableString16 DomStorageHost::GetAreaKey(int connection_id, unsigned index) { |
| 58 DomStorageArea* area = GetOpenArea(connection_id); |
| 59 if (!area) |
| 60 return NullableString16(true); |
| 61 return area->Key(index); |
| 62 } |
| 63 |
| 64 NullableString16 DomStorageHost::GetAreaItem(int connection_id, |
| 65 const string16& key) { |
| 66 DomStorageArea* area = GetOpenArea(connection_id); |
| 67 if (!area) |
| 68 return NullableString16(true); |
| 69 return area->GetItem(key); |
| 70 } |
| 71 |
| 72 bool DomStorageHost::SetAreaItem( |
| 73 int connection_id, const string16& key, |
| 74 const string16& value, const GURL& page_url, |
| 75 NullableString16* old_value) { |
| 76 DomStorageArea* area = GetOpenArea(connection_id); |
| 77 if (!area) |
| 78 return false; |
| 79 if (!area->SetItem(key, value, old_value)) |
| 80 return false; |
| 81 if (area->namespace_id() == kLocalStorageNamespaceId) |
| 82 context_->NotifyItemSet(area, key, value, *old_value, page_url); |
| 83 return true; |
| 84 } |
| 85 |
| 86 bool DomStorageHost::RemoveAreaItem(int connection_id, const string16& key, |
| 87 const GURL& page_url, |
| 88 string16* old_value) { |
| 89 DomStorageArea* area = GetOpenArea(connection_id); |
| 90 if (!area) |
| 91 return false; |
| 92 if (!area->RemoveItem(key, old_value)) |
| 93 return false; |
| 94 if (area->namespace_id() == kLocalStorageNamespaceId) |
| 95 context_->NotifyItemRemoved(area, key, *old_value, page_url); |
| 96 return true; |
| 97 } |
| 98 |
| 99 bool DomStorageHost::ClearArea(int connection_id, const GURL& page_url) { |
| 100 DomStorageArea* area = GetOpenArea(connection_id); |
| 101 if (!area) |
| 102 return false; |
| 103 if (!area->Clear()) |
| 104 return false; |
| 105 if (area->namespace_id() == kLocalStorageNamespaceId) |
| 106 context_->NotifyAreaCleared(area, page_url); |
| 107 return true; |
| 108 } |
| 109 |
| 110 DomStorageArea* DomStorageHost::GetOpenArea(int connection_id) { |
| 111 AreaMap::iterator found = connections_.find(connection_id); |
| 112 if (found == connections_.end()) |
| 113 return NULL; |
| 114 return found->second.area_; |
| 115 } |
| 116 |
| 117 // NamespaceAndArea |
| 118 |
| 119 DomStorageHost::NamespaceAndArea::NamespaceAndArea() {} |
| 120 DomStorageHost::NamespaceAndArea::~NamespaceAndArea() {} |
| 121 |
| 122 } // namespace dom_storage |
OLD | NEW |