summaryrefslogtreecommitdiffstats
path: root/webkit/support/simple_dom_storage_system.cc
blob: 34762ac5bd3f6e5aebfaa7f254306fd854702976 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "webkit/support/simple_dom_storage_system.h"

#include "base/auto_reset.h"
#include "googleurl/src/gurl.h"
#include "third_party/WebKit/public/platform/WebStorageArea.h"
#include "third_party/WebKit/public/platform/WebStorageNamespace.h"
#include "third_party/WebKit/public/platform/WebURL.h"
#include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageEventDispatcher.h"
#include "webkit/browser/database/database_util.h"
#include "webkit/browser/dom_storage/dom_storage_area.h"
#include "webkit/browser/dom_storage/dom_storage_host.h"

using dom_storage::DomStorageContext;
using dom_storage::DomStorageHost;
using dom_storage::DomStorageSession;
using webkit_database::DatabaseUtil;
using WebKit::WebStorageArea;
using WebKit::WebStorageNamespace;
using WebKit::WebStorageEventDispatcher;
using WebKit::WebString;
using WebKit::WebURL;

namespace {
const int kInvalidNamespaceId = -1;
}

class SimpleDomStorageSystem::NamespaceImpl : public WebStorageNamespace {
 public:
  explicit NamespaceImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent);
  NamespaceImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent,
                int session_namespace_id);
  virtual ~NamespaceImpl();
  virtual WebStorageArea* createStorageArea(const WebString& origin) OVERRIDE;
  virtual WebStorageNamespace* copy() OVERRIDE;
  virtual bool isSameNamespace(const WebStorageNamespace&) const OVERRIDE;

 private:
  DomStorageContext* Context() {
    if (!parent_.get())
      return NULL;
    return parent_->context_.get();
  }

  base::WeakPtr<SimpleDomStorageSystem> parent_;
  int namespace_id_;
};

class SimpleDomStorageSystem::AreaImpl : public WebStorageArea {
 public:
  AreaImpl(const base::WeakPtr<SimpleDomStorageSystem>& parent,
          int namespace_id, const GURL& origin);
  virtual ~AreaImpl();
  virtual unsigned length() OVERRIDE;
  virtual WebString key(unsigned index) OVERRIDE;
  virtual WebString getItem(const WebString& key) OVERRIDE;
  virtual void setItem(const WebString& key, const WebString& newValue,
                       const WebURL& pageUrl, Result&) OVERRIDE;
  virtual void removeItem(const WebString& key,
                          const WebURL& pageUrl) OVERRIDE;
  virtual void clear(const WebURL& pageUrl) OVERRIDE;

 private:
  DomStorageHost* Host() {
    if (!parent_.get())
      return NULL;
    return parent_->host_.get();
  }

  base::WeakPtr<SimpleDomStorageSystem> parent_;
  int connection_id_;
};

// NamespaceImpl -----------------------------

SimpleDomStorageSystem::NamespaceImpl::NamespaceImpl(
    const base::WeakPtr<SimpleDomStorageSystem>& parent)
    : parent_(parent),
      namespace_id_(dom_storage::kLocalStorageNamespaceId) {
}

SimpleDomStorageSystem::NamespaceImpl::NamespaceImpl(
    const base::WeakPtr<SimpleDomStorageSystem>& parent,
    int session_namespace_id)
    : parent_(parent),
      namespace_id_(session_namespace_id) {
}

SimpleDomStorageSystem::NamespaceImpl::~NamespaceImpl() {
  if (namespace_id_ == dom_storage::kLocalStorageNamespaceId ||
      namespace_id_ == kInvalidNamespaceId || !Context()) {
    return;
  }
  Context()->DeleteSessionNamespace(namespace_id_, false);
}

WebStorageArea* SimpleDomStorageSystem::NamespaceImpl::createStorageArea(
    const WebString& origin) {
  return new AreaImpl(parent_, namespace_id_, GURL(origin));
}

WebStorageNamespace* SimpleDomStorageSystem::NamespaceImpl::copy() {
  DCHECK_NE(dom_storage::kLocalStorageNamespaceId, namespace_id_);
  int new_id = kInvalidNamespaceId;
  if (Context()) {
    new_id = Context()->AllocateSessionId();
    Context()->CloneSessionNamespace(namespace_id_, new_id, std::string());
  }
  return new NamespaceImpl(parent_, new_id);
}

bool SimpleDomStorageSystem::NamespaceImpl::isSameNamespace(
    const WebStorageNamespace& other) const {
  const NamespaceImpl* other_impl = static_cast<const NamespaceImpl*>(&other);
  return namespace_id_ == other_impl->namespace_id_;
}

// AreaImpl -----------------------------

SimpleDomStorageSystem::AreaImpl::AreaImpl(
    const base::WeakPtr<SimpleDomStorageSystem>& parent,
    int namespace_id, const GURL& origin)
    : parent_(parent),
      connection_id_(0) {
  if (Host()) {
    connection_id_ = (parent_->next_connection_id_)++;
    Host()->OpenStorageArea(connection_id_, namespace_id, origin);
  }
}

SimpleDomStorageSystem::AreaImpl::~AreaImpl() {
  if (Host())
    Host()->CloseStorageArea(connection_id_);
}

unsigned SimpleDomStorageSystem::AreaImpl::length() {
  if (Host())
    return Host()->GetAreaLength(connection_id_);
  return 0;
}

WebString SimpleDomStorageSystem::AreaImpl::key(unsigned index) {
  if (Host())
    return Host()->GetAreaKey(connection_id_, index);
  return NullableString16(true);
}

WebString SimpleDomStorageSystem::AreaImpl::getItem(const WebString& key) {
  if (Host())
    return Host()->GetAreaItem(connection_id_, key);
  return NullableString16(true);
}

void SimpleDomStorageSystem::AreaImpl::setItem(
    const WebString& key, const WebString& newValue,
    const WebURL& pageUrl, Result& result) {
  result = ResultBlockedByQuota;
  if (!Host())
    return;

  base::AutoReset<AreaImpl*> auto_reset(&parent_->area_being_processed_, this);
  NullableString16 unused;
  if (!Host()->SetAreaItem(connection_id_, key, newValue, pageUrl,
                           &unused))
    return;

  result = ResultOK;
}

void SimpleDomStorageSystem::AreaImpl::removeItem(
    const WebString& key, const WebURL& pageUrl) {
  if (!Host())
    return;

  base::AutoReset<AreaImpl*> auto_reset(&parent_->area_being_processed_, this);
  base::string16 notused;
  Host()->RemoveAreaItem(connection_id_, key, pageUrl, &notused);
}

void SimpleDomStorageSystem::AreaImpl::clear(const WebURL& pageUrl) {
  if (!Host())
    return;

  base::AutoReset<AreaImpl*> auto_reset(&parent_->area_being_processed_, this);
  Host()->ClearArea(connection_id_, pageUrl);
}

// SimpleDomStorageSystem -----------------------------

SimpleDomStorageSystem* SimpleDomStorageSystem::g_instance_;

SimpleDomStorageSystem::SimpleDomStorageSystem()
    : weak_factory_(this),
      context_(new DomStorageContext(base::FilePath(),
                                     base::FilePath(),
                                     NULL,
                                     NULL)),
      host_(new DomStorageHost(context_.get())),
      area_being_processed_(NULL),
      next_connection_id_(1) {
  DCHECK(!g_instance_);
  g_instance_ = this;
  context_->AddEventObserver(this);
}

SimpleDomStorageSystem::~SimpleDomStorageSystem() {
  g_instance_ = NULL;
  host_.reset();
  context_->RemoveEventObserver(this);
}

WebStorageNamespace* SimpleDomStorageSystem::CreateLocalStorageNamespace() {
  return new NamespaceImpl(weak_factory_.GetWeakPtr());
}

WebStorageNamespace* SimpleDomStorageSystem::CreateSessionStorageNamespace() {
  int id = context_->AllocateSessionId();
  context_->CreateSessionNamespace(id, std::string());
  return new NamespaceImpl(weak_factory_.GetWeakPtr(), id);
}

void SimpleDomStorageSystem::OnDomStorageItemSet(
    const dom_storage::DomStorageArea* area,
    const base::string16& key,
    const base::string16& new_value,
    const NullableString16& old_value,
    const GURL& page_url) {
  DispatchDomStorageEvent(area, page_url,
                          NullableString16(key, false),
                          NullableString16(new_value, false),
                          old_value);
}

void SimpleDomStorageSystem::OnDomStorageItemRemoved(
    const dom_storage::DomStorageArea* area,
    const base::string16& key,
    const base::string16& old_value,
    const GURL& page_url) {
  DispatchDomStorageEvent(area, page_url,
                          NullableString16(key, false),
                          NullableString16(true),
                          NullableString16(old_value, false));
}

void SimpleDomStorageSystem::OnDomStorageAreaCleared(
    const dom_storage::DomStorageArea* area,
    const GURL& page_url) {
  DispatchDomStorageEvent(area, page_url,
                          NullableString16(true),
                          NullableString16(true),
                          NullableString16(true));
}

void SimpleDomStorageSystem::DispatchDomStorageEvent(
    const dom_storage::DomStorageArea* area,
    const GURL& page_url,
    const NullableString16& key,
    const NullableString16& new_value,
    const NullableString16& old_value) {
  DCHECK(area_being_processed_);
  if (area->namespace_id() == dom_storage::kLocalStorageNamespaceId) {
    WebStorageEventDispatcher::dispatchLocalStorageEvent(
        key,
        old_value,
        new_value,
        area->origin(),
        page_url,
        area_being_processed_,
        true  /* originatedInProcess */);
  } else {
    NamespaceImpl session_namespace_for_event_dispatch(
        base::WeakPtr<SimpleDomStorageSystem>(), area->namespace_id());
    WebStorageEventDispatcher::dispatchSessionStorageEvent(
        key,
        old_value,
        new_value,
        area->origin(),
        page_url,
        session_namespace_for_event_dispatch,
        area_being_processed_,
        true  /* originatedInProcess */);
  }
}