Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: webkit/tools/test_shell/simple_dom_storage_system.cc

Issue 9429029: Integrate the new DomStorage backend into DRT and test_shell. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(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/tools/test_shell/simple_dom_storage_system.h"
6
7 #include "googleurl/src/gurl.h"
8 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageArea.h"
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageNamespace.h "
10 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h"
11 #include "webkit/database/database_util.h"
12 #include "webkit/dom_storage/dom_storage_context.h"
13 #include "webkit/dom_storage/dom_storage_host.h"
14 #include "webkit/dom_storage/dom_storage_session.h"
15
16 using dom_storage::DomStorageContext;
17 using dom_storage::DomStorageHost;
18 using dom_storage::DomStorageSession;
19 using webkit_database::DatabaseUtil;
20 using WebKit::WebStorageNamespace;
21 using WebKit::WebStorageArea;
22 using WebKit::WebString;
23 using WebKit::WebURL;
24
25 namespace {
26
27 const int kInvalidNamespaceId = -1;
28
29 }
30
31 class SimpleDomStorageSystem::NamespaceImpl : public WebStorageNamespace {
32 public:
33 NamespaceImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent);
34 NamespaceImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent,
35 int session_namespace_id);
36 virtual ~NamespaceImpl();
37 virtual WebStorageArea* createStorageArea(const WebString& origin) OVERRIDE;
38 virtual WebStorageNamespace* copy() OVERRIDE;
39 virtual void close() OVERRIDE;
40
41 private:
42 DomStorageContext* Context() {
43 if (!parent_.get())
44 return NULL;
45 return parent_.get()->context_.get();
46 }
47
48 base::WeakPtr<SimpleDomStorageSystem> parent_;
49 int namespace_id_;
50 };
51
52 class SimpleDomStorageSystem::AreaImpl : public WebStorageArea {
53 public:
54 AreaImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent,
55 int namespace_id, const GURL& origin);
56 virtual ~AreaImpl();
57 virtual unsigned length() OVERRIDE;
58 virtual WebString key(unsigned index) OVERRIDE;
59 virtual WebString getItem(const WebString& key) OVERRIDE;
60 virtual void setItem(const WebString& key, const WebString& newValue,
61 const WebURL&, Result&, WebString& oldValue) OVERRIDE;
62 virtual void removeItem(const WebString& key, const WebURL& url,
63 WebString& oldValue) OVERRIDE;
64 virtual void clear(const WebURL& url, bool& somethingCleared) OVERRIDE;
65
66 private:
67 DomStorageHost* Host() {
68 if (!parent_.get())
69 return NULL;
70 return parent_.get()->host_.get();
71 }
72
73 base::WeakPtr<SimpleDomStorageSystem> parent_;
74 int connection_id_;
75 };
76
77 // NamespaceImpl -----------------------------
78
79 SimpleDomStorageSystem::NamespaceImpl::NamespaceImpl(
80 const base::WeakPtr<SimpleDomStorageSystem>& parent)
81 : parent_(parent),
82 namespace_id_(dom_storage::kLocalStorageNamespaceId) {
83 }
84
85 SimpleDomStorageSystem::NamespaceImpl::NamespaceImpl(
86 const base::WeakPtr<SimpleDomStorageSystem>& parent,
87 int session_namespace_id)
88 : parent_(parent),
89 namespace_id_(session_namespace_id) {
90 }
91
92 SimpleDomStorageSystem::NamespaceImpl::~NamespaceImpl() {
93 if (namespace_id_ == dom_storage::kLocalStorageNamespaceId ||
94 namespace_id_ == kInvalidNamespaceId || !Context()) {
95 return;
96 }
97 Context()->DeleteSessionNamespace(namespace_id_);
98 }
99
100 WebStorageArea* SimpleDomStorageSystem::NamespaceImpl::createStorageArea(
101 const WebString& origin_identifier) {
102 GURL origin_url(DatabaseUtil::GetOriginFromIdentifier(origin_identifier));
103 return new AreaImpl(parent_, namespace_id_, origin_url);
104 }
105
106 WebStorageNamespace* SimpleDomStorageSystem::NamespaceImpl::copy() {
107 DCHECK_NE(dom_storage::kLocalStorageNamespaceId, namespace_id_);
108 int new_id = kInvalidNamespaceId;
109 if (Context()) {
110 new_id = Context()->AllocateSessionId();
111 Context()->CloneSessionNamespace(namespace_id_, new_id);
112 }
113 return new NamespaceImpl(parent_, new_id);
114 }
115
116 void SimpleDomStorageSystem::NamespaceImpl::close() {
117 }
118
119 // AreaImpl -----------------------------
120
121 SimpleDomStorageSystem::AreaImpl::AreaImpl(
122 const base::WeakPtr<SimpleDomStorageSystem>& parent,
123 int namespace_id, const GURL& origin)
124 : parent_(parent),
125 connection_id_(0) {
126 if (Host())
127 connection_id_ = Host()->OpenStorageArea(namespace_id, origin);
128 }
129
130 SimpleDomStorageSystem::AreaImpl::~AreaImpl() {
131 if (Host())
132 Host()->CloseStorageArea(connection_id_);
133 }
134
135 unsigned SimpleDomStorageSystem::AreaImpl::length() {
136 if (Host())
137 return Host()->GetAreaLength(connection_id_);
138 return 0;
139 }
140
141 WebString SimpleDomStorageSystem::AreaImpl::key(unsigned index) {
142 if (Host())
143 return Host()->GetAreaKey(connection_id_, index);
144 return WebString();
145 }
146
147 WebString SimpleDomStorageSystem::AreaImpl::getItem(const WebString& key) {
148 if (Host())
149 return Host()->GetAreaItem(connection_id_, key);
150 return WebString();
151 }
152
153 void SimpleDomStorageSystem::AreaImpl::setItem(
154 const WebString& key, const WebString& newValue,
155 const WebURL& pageUrl, Result& result, WebString& oldValue) {
benm (inactive) 2012/02/28 16:46:01 should we set oldValue to a null string here to co
michaeln 2012/02/29 00:19:21 Done.
156 result = ResultBlockedByQuota;
benm (inactive) 2012/02/28 16:46:01 wdyt to setting to ResultOK here and setting to Re
michaeln 2012/02/29 00:19:21 I'd like to keep the setting of the two out args t
157 if (!Host())
benm (inactive) 2012/02/28 16:46:01 Maybe we should have a different error code for wh
michaeln 2012/02/29 00:19:21 Atm, there are only two possible values defined in
158 return;
159
160 NullableString16 old_value;
161 if (!Host()->SetAreaItem(connection_id_, key, newValue, pageUrl,
162 &old_value))
163 return;
164
165 result = ResultOK;
166 oldValue = old_value;
167 }
168
169 void SimpleDomStorageSystem::AreaImpl::removeItem(
170 const WebString& key, const WebURL& pageUrl, WebString& oldValue) {
171 oldValue = NullableString16(true);
172 if (!Host())
173 return;
174
175 string16 old_value;
176 if (!Host()->RemoveAreaItem(connection_id_, key, pageUrl, &old_value))
177 return;
178
179 oldValue = old_value;
180 return;
benm (inactive) 2012/02/28 16:46:01 no need for a return
michaeln 2012/02/29 00:19:21 Done.
181 }
182
183 void SimpleDomStorageSystem::AreaImpl::clear(
184 const WebURL& pageUrl, bool& somethingCleared) {
185 if (Host())
186 somethingCleared = Host()->ClearArea(connection_id_, pageUrl);
187 else
188 somethingCleared = false;
189 }
190
191 // SimpleDomStorageSystem -----------------------------
192
193 SimpleDomStorageSystem* SimpleDomStorageSystem::g_instance_;
194
195 SimpleDomStorageSystem::SimpleDomStorageSystem()
196 : weak_factory_(this),
197 context_(new DomStorageContext(FilePath(), NULL, NULL)),
198 host_(new DomStorageHost(context_)) {
199 DCHECK(!g_instance_);
200 g_instance_ = this;
201 }
202
203 SimpleDomStorageSystem::~SimpleDomStorageSystem() {
204 g_instance_ = NULL;
205 host_.reset();
206 }
207
208 WebStorageNamespace* SimpleDomStorageSystem::CreateLocalStorageNamespace() {
209 return new NamespaceImpl(weak_factory_.GetWeakPtr());
210 }
211
212 WebStorageNamespace* SimpleDomStorageSystem::CreateSessionStorageNamespace() {
213 int id = context_->AllocateSessionId();
214 context_->CreateSessionNamespace(id);
215 return new NamespaceImpl(weak_factory_.GetWeakPtr(), id);
216 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698