|
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/WebStorageArea.h" | |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageNamespace.h " | |
10 #include "webkit/dom_storage/dom_storage_context.h" | |
11 #include "webkit/dom_storage/dom_storage_host.h" | |
12 #include "webkit/dom_storage/dom_storage_session.h" | |
13 | |
14 using dom_storage::DomStorageContext; | |
15 using dom_storage::DomStorageHost; | |
16 using dom_storage::DomStorageSession; | |
17 using WebKit::WebStorageNamespace; | |
18 using WebKit::WebStorageArea; | |
19 using WebKit::WebString; | |
20 using WebKit::WebURL; | |
21 | |
22 class SimpleDomStorageSystem::NamespaceImpl : public WebStorageNamespace { | |
23 public: | |
24 NamespaceImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent); | |
25 NamespaceImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent, | |
26 int session_namespace_id); | |
27 virtual ~NamespaceImpl(); | |
28 virtual WebStorageArea* createStorageArea(const WebString& origin) OVERRIDE; | |
29 virtual WebStorageNamespace* copy() OVERRIDE; | |
30 virtual void close() OVERRIDE; | |
31 | |
32 private: | |
33 DomStorageContext* Context() { | |
34 if (parent_.get()) | |
benm (inactive)
2012/02/23 19:17:15
!parent.get()
michaeln
2012/02/28 01:14:54
Done.
| |
35 return NULL; | |
36 return parent_.get()->context_.get(); | |
37 } | |
38 | |
39 base::WeakPtr<SimpleDomStorageSystem> parent_; | |
40 int namespace_id_; | |
41 }; | |
42 | |
43 class SimpleDomStorageSystem::AreaImpl : public WebStorageArea { | |
44 public: | |
45 AreaImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent, | |
46 int namespace_id, const GURL& origin); | |
47 virtual ~AreaImpl(); | |
48 virtual unsigned length() OVERRIDE; | |
49 virtual WebString key(unsigned index) OVERRIDE; | |
50 virtual WebString getItem(const WebString& key) OVERRIDE; | |
51 virtual void setItem(const WebString& key, const WebString& newValue, | |
52 const WebURL&, Result&, WebString& oldValue) OVERRIDE; | |
53 virtual void removeItem(const WebString& key, const WebURL& url, | |
54 WebString& oldValue) OVERRIDE; | |
55 virtual void clear(const WebURL& url, bool& somethingCleared) OVERRIDE; | |
56 | |
57 private: | |
58 DomStorageHost* Host() { | |
59 if (parent_.get()) | |
benm (inactive)
2012/02/23 19:17:15
!parent.get()
michaeln
2012/02/28 01:14:54
Done.
| |
60 return NULL; | |
61 return parent_.get()->host_.get(); | |
62 } | |
63 | |
64 base::WeakPtr<SimpleDomStorageSystem> parent_; | |
65 int connection_id_; | |
66 }; | |
67 | |
68 // NamespaceImpl ----------------------------- | |
69 | |
70 SimpleDomStorageSystem::NamespaceImpl::NamespaceImpl( | |
71 const base::WeakPtr<SimpleDomStorageSystem>& parent) | |
72 : parent_(parent), | |
73 namespace_id_(dom_storage::kLocalStorageNamespaceId) { | |
74 } | |
75 | |
76 SimpleDomStorageSystem::NamespaceImpl::NamespaceImpl( | |
77 const base::WeakPtr<SimpleDomStorageSystem>& parent, | |
78 int session_namespace_id) | |
79 : parent_(parent), | |
80 namespace_id_(session_namespace_id) { | |
81 } | |
82 | |
83 SimpleDomStorageSystem::NamespaceImpl::~NamespaceImpl() { | |
84 if (namespace_id_ == dom_storage::kLocalStorageNamespaceId || !Context()) | |
85 return; | |
86 Context()->DeleteSessionNamespace(namespace_id_); | |
87 } | |
88 | |
89 WebStorageArea* SimpleDomStorageSystem::NamespaceImpl::createStorageArea( | |
90 const WebString& origin) { | |
91 return new AreaImpl(parent_, namespace_id_, GURL()); | |
benm (inactive)
2012/02/23 19:17:15
Should this be a GURL created from the origin para
michaeln
2012/02/28 01:14:54
Done.
| |
92 } | |
93 | |
94 WebStorageNamespace* SimpleDomStorageSystem::NamespaceImpl::copy() { | |
95 DCHECK_NE(dom_storage::kLocalStorageNamespaceId, namespace_id_); | |
96 int new_id = -1; | |
benm (inactive)
2012/02/23 19:17:15
how about making a kInvalidNamespaceId in dom_stor
michaeln
2012/02/28 01:14:54
Done in a constant in this file
| |
97 if (Context()) { | |
98 new_id = Context()->AllocateSessionId(); | |
99 Context()->CloneSessionNamespace(namespace_id_, new_id); | |
100 } | |
101 return new NamespaceImpl(parent_, new_id); | |
benm (inactive)
2012/02/23 19:17:15
Is it ok for -1 to end up as the id here?
michaeln
2012/02/28 01:14:54
Yes, should be harmless.
| |
102 } | |
103 | |
104 void SimpleDomStorageSystem::NamespaceImpl::close() { | |
105 } | |
106 | |
107 // AreaImpl ----------------------------- | |
108 | |
109 SimpleDomStorageSystem::AreaImpl::AreaImpl( | |
110 const base::WeakPtr<SimpleDomStorageSystem>& parent, | |
111 int namespace_id, const GURL& origin) | |
112 : parent_(parent), | |
113 connection_id_(0) { | |
114 if (Host()) | |
115 connection_id_ = Host()->OpenStorageArea(namespace_id, origin); | |
116 } | |
117 | |
118 SimpleDomStorageSystem::AreaImpl::~AreaImpl() { | |
119 if (Host()) | |
120 Host()->CloseStorageArea(connection_id_); | |
121 } | |
122 | |
123 unsigned SimpleDomStorageSystem::AreaImpl::length() { | |
124 if (Host()) | |
125 return Host()->GetAreaLength(connection_id_); | |
126 return 0; | |
127 } | |
128 | |
129 WebString SimpleDomStorageSystem::AreaImpl::key(unsigned index) { | |
130 if (Host()) | |
131 return Host()->GetAreaKey(connection_id_, index); | |
132 return WebString(); | |
133 } | |
134 | |
135 WebString SimpleDomStorageSystem::AreaImpl::getItem(const WebString& key) { | |
136 if (Host()) | |
137 return Host()->GetAreaItem(connection_id_, key); | |
138 return WebString(); | |
139 } | |
140 | |
141 void SimpleDomStorageSystem::AreaImpl::setItem( | |
142 const WebString& key, const WebString& newValue, | |
143 const WebURL&, Result& result, WebString& oldValue) { | |
144 // host_->SetAreaItem(connection_id_, key, ...); | |
145 result = ResultBlockedByQuota; | |
146 } | |
147 | |
148 void SimpleDomStorageSystem::AreaImpl::removeItem( | |
149 const WebString& key, const WebURL& url, WebString& oldValue) { | |
150 // host_->RemoveAreaItem(connection_id_, key, ...); | |
151 } | |
152 | |
153 void SimpleDomStorageSystem::AreaImpl::clear( | |
154 const WebURL& page_url, bool& somethingCleared) { | |
155 // somethingCleared = host_->SetAreaItem(connection_id_, page_url); | |
156 somethingCleared = false; | |
157 } | |
158 | |
159 // SimpleDomStorageSystem ----------------------------- | |
160 | |
161 SimpleDomStorageSystem* SimpleDomStorageSystem::g_instance_; | |
162 | |
163 SimpleDomStorageSystem::SimpleDomStorageSystem() | |
164 : weak_factory_(this), | |
165 context_(new DomStorageContext(FilePath(), NULL, NULL)), | |
benm (inactive)
2012/02/23 19:17:15
I guess the params here still need filling out.
michaeln
2012/02/25 19:20:32
I think they may not need any more filling out tha
| |
166 host_(new DomStorageHost(context_)) { | |
167 DCHECK(!g_instance_); | |
168 g_instance_ = this; | |
169 } | |
170 | |
171 SimpleDomStorageSystem::~SimpleDomStorageSystem() { | |
172 g_instance_ = NULL; | |
173 host_.reset(); | |
174 } | |
175 | |
176 WebStorageNamespace* SimpleDomStorageSystem::CreateLocalStorageNamespace() { | |
177 return new NamespaceImpl(weak_factory_.GetWeakPtr()); | |
178 } | |
179 | |
180 WebStorageNamespace* SimpleDomStorageSystem::CreateSessionStorageNamespace() { | |
181 int id = context_->AllocateSessionId(); | |
182 context_->CreateSessionNamespace(id); | |
183 return new NamespaceImpl(weak_factory_.GetWeakPtr(), id); | |
184 } | |
OLD | NEW |