blob: d5bf07dbaf9d9173086f721d82c46ff6eeabbe1c [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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 "content/renderer/dom_storage/webstoragearea_impl.h"
6
7#include "base/lazy_instance.h"
8#include "base/metrics/histogram.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +01009#include "base/strings/utf_string_conversions.h"
Ben Murdocheb525c52013-07-10 11:40:50 +010010#include "base/time/time.h"
Ben Murdochbb1529c2013-08-08 10:24:53 +010011#include "content/common/dom_storage/dom_storage_messages.h"
Torne (Richard Coles)a36e5922013-08-05 13:57:33 +010012#include "content/renderer/dom_storage/dom_storage_cached_area.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000013#include "content/renderer/dom_storage/dom_storage_dispatcher.h"
14#include "content/renderer/render_thread_impl.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010015#include "third_party/WebKit/public/platform/WebURL.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016
Torne (Richard Coles)58218062012-11-14 11:43:16 +000017using WebKit::WebString;
18using WebKit::WebURL;
19
20namespace content {
21
22namespace {
23typedef IDMap<WebStorageAreaImpl> AreaImplMap;
24base::LazyInstance<AreaImplMap>::Leaky
25 g_all_areas_map = LAZY_INSTANCE_INITIALIZER;
26
27DomStorageDispatcher* dispatcher() {
28 return RenderThreadImpl::current()->dom_storage_dispatcher();
29}
30} // namespace
31
32// static
33WebStorageAreaImpl* WebStorageAreaImpl::FromConnectionId(int id) {
34 return g_all_areas_map.Pointer()->Lookup(id);
35}
36
37WebStorageAreaImpl::WebStorageAreaImpl(
38 int64 namespace_id, const GURL& origin)
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010039 : connection_id_(g_all_areas_map.Pointer()->Add(this)),
Torne (Richard Coles)58218062012-11-14 11:43:16 +000040 cached_area_(dispatcher()->
41 OpenCachedArea(connection_id_, namespace_id, origin)) {
42}
43
44WebStorageAreaImpl::~WebStorageAreaImpl() {
45 g_all_areas_map.Pointer()->Remove(connection_id_);
46 if (dispatcher())
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010047 dispatcher()->CloseCachedArea(connection_id_, cached_area_.get());
Torne (Richard Coles)58218062012-11-14 11:43:16 +000048}
49
50unsigned WebStorageAreaImpl::length() {
51 return cached_area_->GetLength(connection_id_);
52}
53
54WebString WebStorageAreaImpl::key(unsigned index) {
55 return cached_area_->GetKey(connection_id_, index);
56}
57
58WebString WebStorageAreaImpl::getItem(const WebString& key) {
59 return cached_area_->GetItem(connection_id_, key);
60}
61
62void WebStorageAreaImpl::setItem(
63 const WebString& key, const WebString& value, const WebURL& page_url,
64 WebStorageArea::Result& result) {
65 if (!cached_area_->SetItem(connection_id_, key, value, page_url))
66 result = ResultBlockedByQuota;
67 else
68 result = ResultOK;
69}
70
71void WebStorageAreaImpl::removeItem(
72 const WebString& key, const WebURL& page_url) {
73 cached_area_->RemoveItem(connection_id_, key, page_url);
74}
75
76void WebStorageAreaImpl::clear(const WebURL& page_url) {
77 cached_area_->Clear(connection_id_, page_url);
78}
79
80size_t WebStorageAreaImpl::memoryBytesUsedByCache() const {
81 return cached_area_->MemoryBytesUsedByCache();
82}
83
84} // namespace content