Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 9 | #include "base/strings/utf_string_conversions.h" |
Ben Murdoch | eb525c5 | 2013-07-10 11:40:50 +0100 | [diff] [blame] | 10 | #include "base/time/time.h" |
Ben Murdoch | bb1529c | 2013-08-08 10:24:53 +0100 | [diff] [blame^] | 11 | #include "content/common/dom_storage/dom_storage_messages.h" |
Torne (Richard Coles) | a36e592 | 2013-08-05 13:57:33 +0100 | [diff] [blame] | 12 | #include "content/renderer/dom_storage/dom_storage_cached_area.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | #include "content/renderer/dom_storage/dom_storage_dispatcher.h" |
| 14 | #include "content/renderer/render_thread_impl.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 15 | #include "third_party/WebKit/public/platform/WebURL.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 16 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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) |
Torne (Richard Coles) | c2e0dbd | 2013-05-09 18:35:53 +0100 | [diff] [blame] | 39 | : connection_id_(g_all_areas_map.Pointer()->Add(this)), |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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()) |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 47 | dispatcher()->CloseCachedArea(connection_id_, cached_area_.get()); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 |