|
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/tools/test_shell/simple_dom_storage_system.h" | |
6 | |
7 #include "googleurl/src/gurl.h" | |
8 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebURL.h" | |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageArea.h" | |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageNamespace.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 const int kInvalidNamespaceId = -1; | |
27 } | |
28 | |
29 class SimpleDomStorageSystem::NamespaceImpl : public WebStorageNamespace { | |
30 public: | |
31 explicit NamespaceImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent); | |
32 NamespaceImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent, | |
33 int session_namespace_id); | |
34 virtual ~NamespaceImpl(); | |
35 virtual WebStorageArea* createStorageArea(const WebString& origin) OVERRIDE; | |
36 virtual WebStorageNamespace* copy() OVERRIDE; | |
37 virtual void close() OVERRIDE; | |
38 | |
39 private: | |
40 DomStorageContext* Context() { | |
41 if (!parent_.get()) | |
42 return NULL; | |
43 return parent_.get()->context_.get(); | |
tony
2012/02/29 17:51:15
Nit: Is the .get() after parent_ unnecessary?
michaeln
2012/03/01 00:39:41
Done.
| |
44 } | |
45 | |
46 base::WeakPtr<SimpleDomStorageSystem> parent_; | |
47 int namespace_id_; | |
48 }; | |
49 | |
50 class SimpleDomStorageSystem::AreaImpl : public WebStorageArea { | |
51 public: | |
52 AreaImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent, | |
53 int namespace_id, const GURL& origin); | |
54 virtual ~AreaImpl(); | |
55 virtual unsigned length() OVERRIDE; | |
56 virtual WebString key(unsigned index) OVERRIDE; | |
57 virtual WebString getItem(const WebString& key) OVERRIDE; | |
58 virtual void setItem(const WebString& key, const WebString& newValue, | |
59 const WebURL&, Result&, WebString& oldValue) OVERRIDE; | |
60 virtual void removeItem(const WebString& key, const WebURL& url, | |
61 WebString& oldValue) OVERRIDE; | |
62 virtual void clear(const WebURL& url, bool& somethingCleared) OVERRIDE; | |
63 | |
64 private: | |
65 DomStorageHost* Host() { | |
66 if (!parent_.get()) | |
67 return NULL; | |
68 return parent_.get()->host_.get(); | |
tony
2012/02/29 17:51:15
Ditto.
michaeln
2012/03/01 00:39:41
Done.
| |
69 } | |
70 | |
71 base::WeakPtr<SimpleDomStorageSystem> parent_; | |
72 int connection_id_; | |
73 }; | |
74 | |
75 // NamespaceImpl ----------------------------- | |
76 | |
77 SimpleDomStorageSystem::NamespaceImpl::NamespaceImpl( | |
78 const base::WeakPtr<SimpleDomStorageSystem>& parent) | |
79 : parent_(parent), | |
80 namespace_id_(dom_storage::kLocalStorageNamespaceId) { | |
81 } | |
82 | |
83 SimpleDomStorageSystem::NamespaceImpl::NamespaceImpl( | |
84 const base::WeakPtr<SimpleDomStorageSystem>& parent, | |
85 int session_namespace_id) | |
86 : parent_(parent), | |
87 namespace_id_(session_namespace_id) { | |
88 } | |
89 | |
90 SimpleDomStorageSystem::NamespaceImpl::~NamespaceImpl() { | |
91 if (namespace_id_ == dom_storage::kLocalStorageNamespaceId || | |
92 namespace_id_ == kInvalidNamespaceId || !Context()) { | |
93 return; | |
94 } | |
95 Context()->DeleteSessionNamespace(namespace_id_); | |
96 } | |
97 | |
98 WebStorageArea* SimpleDomStorageSystem::NamespaceImpl::createStorageArea( | |
99 const WebString& origin) { | |
100 return new AreaImpl(parent_, namespace_id_, GURL(origin)); | |
101 } | |
102 | |
103 WebStorageNamespace* SimpleDomStorageSystem::NamespaceImpl::copy() { | |
104 DCHECK_NE(dom_storage::kLocalStorageNamespaceId, namespace_id_); | |
105 int new_id = kInvalidNamespaceId; | |
106 if (Context()) { | |
107 new_id = Context()->AllocateSessionId(); | |
108 Context()->CloneSessionNamespace(namespace_id_, new_id); | |
109 } | |
110 return new NamespaceImpl(parent_, new_id); | |
111 } | |
112 | |
113 void SimpleDomStorageSystem::NamespaceImpl::close() { | |
114 } | |
115 | |
116 // AreaImpl ----------------------------- | |
117 | |
118 SimpleDomStorageSystem::AreaImpl::AreaImpl( | |
119 const base::WeakPtr<SimpleDomStorageSystem>& parent, | |
120 int namespace_id, const GURL& origin) | |
121 : parent_(parent), | |
122 connection_id_(0) { | |
123 if (Host()) | |
124 connection_id_ = Host()->OpenStorageArea(namespace_id, origin); | |
125 } | |
126 | |
127 SimpleDomStorageSystem::AreaImpl::~AreaImpl() { | |
128 if (Host()) | |
129 Host()->CloseStorageArea(connection_id_); | |
130 } | |
131 | |
132 unsigned SimpleDomStorageSystem::AreaImpl::length() { | |
133 if (Host()) | |
134 return Host()->GetAreaLength(connection_id_); | |
135 return 0; | |
136 } | |
137 | |
138 WebString SimpleDomStorageSystem::AreaImpl::key(unsigned index) { | |
139 if (Host()) | |
140 return Host()->GetAreaKey(connection_id_, index); | |
141 return NullableString16(true); | |
tony
2012/02/29 17:51:15
Nit: You could probably just return a WebString()
michaeln
2012/03/01 00:39:41
That's true, but i'm gunning for extra clarity to
| |
142 } | |
143 | |
144 WebString SimpleDomStorageSystem::AreaImpl::getItem(const WebString& key) { | |
145 if (Host()) | |
146 return Host()->GetAreaItem(connection_id_, key); | |
147 return NullableString16(true); | |
148 } | |
149 | |
150 void SimpleDomStorageSystem::AreaImpl::setItem( | |
151 const WebString& key, const WebString& newValue, | |
152 const WebURL& pageUrl, Result& result, WebString& oldValue) { | |
153 result = ResultBlockedByQuota; | |
154 oldValue = NullableString16(true); | |
155 if (!Host()) | |
156 return; | |
157 | |
158 NullableString16 old_value; | |
159 if (!Host()->SetAreaItem(connection_id_, key, newValue, pageUrl, | |
160 &old_value)) | |
161 return; | |
162 | |
163 result = ResultOK; | |
164 oldValue = old_value; | |
165 } | |
166 | |
167 void SimpleDomStorageSystem::AreaImpl::removeItem( | |
168 const WebString& key, const WebURL& pageUrl, WebString& oldValue) { | |
169 oldValue = NullableString16(true); | |
170 if (!Host()) | |
171 return; | |
172 | |
173 string16 old_value; | |
174 if (!Host()->RemoveAreaItem(connection_id_, key, pageUrl, &old_value)) | |
175 return; | |
176 | |
177 oldValue = old_value; | |
178 } | |
179 | |
180 void SimpleDomStorageSystem::AreaImpl::clear( | |
181 const WebURL& pageUrl, bool& somethingCleared) { | |
182 if (Host()) | |
183 somethingCleared = Host()->ClearArea(connection_id_, pageUrl); | |
184 else | |
185 somethingCleared = false; | |
186 } | |
187 | |
188 // SimpleDomStorageSystem ----------------------------- | |
189 | |
190 SimpleDomStorageSystem* SimpleDomStorageSystem::g_instance_; | |
191 | |
192 SimpleDomStorageSystem::SimpleDomStorageSystem() | |
193 : weak_factory_(this), | |
194 context_(new DomStorageContext(FilePath(), NULL, NULL)), | |
195 host_(new DomStorageHost(context_)) { | |
196 DCHECK(!g_instance_); | |
197 g_instance_ = this; | |
198 } | |
199 | |
200 SimpleDomStorageSystem::~SimpleDomStorageSystem() { | |
201 g_instance_ = NULL; | |
202 host_.reset(); | |
203 } | |
204 | |
205 WebStorageNamespace* SimpleDomStorageSystem::CreateLocalStorageNamespace() { | |
206 return new NamespaceImpl(weak_factory_.GetWeakPtr()); | |
207 } | |
208 | |
209 WebStorageNamespace* SimpleDomStorageSystem::CreateSessionStorageNamespace() { | |
210 int id = context_->AllocateSessionId(); | |
211 context_->CreateSessionNamespace(id); | |
212 return new NamespaceImpl(weak_factory_.GetWeakPtr(), id); | |
213 } | |
OLD | NEW |