- add sources.
[platform/framework/web/crosswalk.git] / src / content / renderer / dom_storage / webstoragearea_impl.cc
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 "content/renderer/dom_storage/webstoragearea_impl.h"
6
7 #include "base/lazy_instance.h"
8 #include "base/metrics/histogram.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/time/time.h"
11 #include "content/common/dom_storage/dom_storage_messages.h"
12 #include "content/renderer/dom_storage/dom_storage_cached_area.h"
13 #include "content/renderer/dom_storage/dom_storage_dispatcher.h"
14 #include "content/renderer/render_thread_impl.h"
15 #include "third_party/WebKit/public/platform/WebURL.h"
16
17 using WebKit::WebString;
18 using WebKit::WebURL;
19
20 namespace content {
21
22 namespace {
23 typedef IDMap<WebStorageAreaImpl> AreaImplMap;
24 base::LazyInstance<AreaImplMap>::Leaky
25     g_all_areas_map = LAZY_INSTANCE_INITIALIZER;
26
27 DomStorageDispatcher* dispatcher() {
28   return RenderThreadImpl::current()->dom_storage_dispatcher();
29 }
30 }  // namespace
31
32 // static
33 WebStorageAreaImpl* WebStorageAreaImpl::FromConnectionId(int id) {
34   return g_all_areas_map.Pointer()->Lookup(id);
35 }
36
37 WebStorageAreaImpl::WebStorageAreaImpl(
38     int64 namespace_id, const GURL& origin)
39     : connection_id_(g_all_areas_map.Pointer()->Add(this)),
40       cached_area_(dispatcher()->
41           OpenCachedArea(connection_id_, namespace_id, origin)) {
42 }
43
44 WebStorageAreaImpl::~WebStorageAreaImpl() {
45   g_all_areas_map.Pointer()->Remove(connection_id_);
46   if (dispatcher())
47     dispatcher()->CloseCachedArea(connection_id_, cached_area_.get());
48 }
49
50 unsigned WebStorageAreaImpl::length() {
51   return cached_area_->GetLength(connection_id_);
52 }
53
54 WebString WebStorageAreaImpl::key(unsigned index) {
55   return cached_area_->GetKey(connection_id_, index);
56 }
57
58 WebString WebStorageAreaImpl::getItem(const WebString& key) {
59   return cached_area_->GetItem(connection_id_, key);
60 }
61
62 void 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
71 void WebStorageAreaImpl::removeItem(
72     const WebString& key, const WebURL& page_url) {
73   cached_area_->RemoveItem(connection_id_, key, page_url);
74 }
75
76 void WebStorageAreaImpl::clear(const WebURL& page_url) {
77   cached_area_->Clear(connection_id_, page_url);
78 }
79
80 size_t WebStorageAreaImpl::memoryBytesUsedByCache() const {
81   return cached_area_->MemoryBytesUsedByCache();
82 }
83
84 }  // namespace content