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