Add ICU message format support
[chromium-blink-merge.git] / content / browser / dom_storage / dom_storage_host.cc
blob14d288d865393acebe876c1673389641339e3271
1 // Copyright 2013 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.
5 #include "content/browser/dom_storage/dom_storage_host.h"
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"
11 #include "url/gurl.h"
13 namespace content {
15 DOMStorageHost::DOMStorageHost(DOMStorageContextImpl* context)
16 : context_(context) {
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_
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;
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);
49 bool DOMStorageHost::ExtractAreaValues(
50 int connection_id, DOMStorageValuesMap* 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() > kMaxInMemoryStorageAreas) {
59 ns->PurgeMemory(DOMStorageNamespace::PURGE_UNOPENED);
60 if (ns->CountInMemoryAreas() > kMaxInMemoryStorageAreas)
61 ns->PurgeMemory(DOMStorageNamespace::PURGE_AGGRESSIVE);
64 area->ExtractValues(map);
65 return true;
68 unsigned DOMStorageHost::GetAreaLength(int connection_id) {
69 DOMStorageArea* area = GetOpenArea(connection_id);
70 if (!area)
71 return 0;
72 return area->Length();
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);
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);
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;
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;
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;
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;
136 return false;
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();
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();
153 // NamespaceAndArea
155 DOMStorageHost::NamespaceAndArea::NamespaceAndArea() {}
156 DOMStorageHost::NamespaceAndArea::~NamespaceAndArea() {}
158 } // namespace content