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"
15 DOMStorageHost::DOMStorageHost(DOMStorageContextImpl
* 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
,
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())
35 references
.area_
= references
.namespace_
->OpenStorageArea(origin
);
36 DCHECK(references
.area_
.get());
37 connections_
[connection_id
] = references
;
41 void DOMStorageHost::CloseStorageArea(int connection_id
) {
42 AreaMap::iterator found
= connections_
.find(connection_id
);
43 if (found
== connections_
.end())
45 found
->second
.namespace_
->CloseStorageArea(found
->second
.area_
.get());
46 connections_
.erase(found
);
49 bool DOMStorageHost::ExtractAreaValues(
50 int connection_id
, DOMStorageValuesMap
* map
) {
52 DOMStorageArea
* area
= GetOpenArea(connection_id
);
55 if (!area
->IsLoadedInMemory()) {
56 DOMStorageNamespace
* ns
= GetNamespace(connection_id
);
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
);
68 unsigned DOMStorageHost::GetAreaLength(int connection_id
) {
69 DOMStorageArea
* area
= GetOpenArea(connection_id
);
72 return area
->Length();
75 base::NullableString16
DOMStorageHost::GetAreaKey(int connection_id
,
77 DOMStorageArea
* area
= GetOpenArea(connection_id
);
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
);
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
);
98 if (!area
->SetItem(key
, value
, old_value
))
100 if (old_value
->is_null() || old_value
->string() != value
)
101 context_
->NotifyItemSet(area
, key
, value
, *old_value
, page_url
);
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
);
111 if (!area
->RemoveItem(key
, old_value
))
113 context_
->NotifyItemRemoved(area
, key
, *old_value
, page_url
);
117 bool DOMStorageHost::ClearArea(int connection_id
, const GURL
& page_url
) {
118 DOMStorageArea
* area
= GetOpenArea(connection_id
);
123 context_
->NotifyAreaCleared(area
, page_url
);
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()) {
139 DOMStorageArea
* DOMStorageHost::GetOpenArea(int connection_id
) {
140 AreaMap::iterator found
= connections_
.find(connection_id
);
141 if (found
== connections_
.end())
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())
150 return found
->second
.namespace_
.get();
155 DOMStorageHost::NamespaceAndArea::NamespaceAndArea() {}
156 DOMStorageHost::NamespaceAndArea::~NamespaceAndArea() {}
158 } // namespace content