OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // source code is governed by a BSD-style license that can be found in the | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/in_process_webkit/dom_storage_namespace.h" | 5 #include "webkit/dom_storage/dom_storage_namespace.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/basictypes.h" |
8 #include "content/browser/in_process_webkit/dom_storage_area.h" | 8 #include "base/logging.h" |
9 #include "content/browser/in_process_webkit/dom_storage_context.h" | 9 #include "webkit/dom_storage/dom_storage_area.h" |
10 #include "content/browser/in_process_webkit/dom_storage_message_filter.h" | 10 #include "webkit/dom_storage/dom_storage_types.h" |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageArea.h" | |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageNamespace.h
" | |
13 #include "webkit/glue/webkit_glue.h" | |
14 | 11 |
15 using WebKit::WebStorageArea; | 12 namespace dom_storage { |
16 using WebKit::WebStorageNamespace; | |
17 using WebKit::WebString; | |
18 | 13 |
19 /* static */ | 14 DomStorageNamespace::DomStorageNamespace( |
20 DOMStorageNamespace* DOMStorageNamespace::CreateLocalStorageNamespace( | 15 const FilePath& directory, |
21 DOMStorageContext* dom_storage_context, const FilePath& data_dir_path) { | 16 DomStorageTaskRunner* task_runner) |
22 int64 id = kLocalStorageNamespaceId; | 17 : namespace_id_(kLocalStorageNamespaceId), |
23 DCHECK(!dom_storage_context->GetStorageNamespace(id, false)); | 18 directory_(directory), |
24 return new DOMStorageNamespace(dom_storage_context, id, | 19 task_runner_(task_runner) { |
25 webkit_glue::FilePathToWebString(data_dir_path), DOM_STORAGE_LOCAL); | |
26 } | 20 } |
27 | 21 |
28 /* static */ | 22 DomStorageNamespace::DomStorageNamespace( |
29 DOMStorageNamespace* DOMStorageNamespace::CreateSessionStorageNamespace( | 23 int64 namespace_id, |
30 DOMStorageContext* dom_storage_context, int64 id) { | 24 DomStorageTaskRunner* task_runner) |
31 DCHECK(!dom_storage_context->GetStorageNamespace(id, false)); | 25 : namespace_id_(namespace_id), |
32 return new DOMStorageNamespace(dom_storage_context, id, WebString(), | 26 task_runner_(task_runner) { |
33 DOM_STORAGE_SESSION); | 27 DCHECK_NE(kLocalStorageNamespaceId, namespace_id); |
34 } | 28 } |
35 | 29 |
36 DOMStorageNamespace::DOMStorageNamespace(DOMStorageContext* dom_storage_context, | 30 DomStorageNamespace::~DomStorageNamespace() { |
37 int64 id, | |
38 const WebString& data_dir_path, | |
39 DOMStorageType dom_storage_type) | |
40 : dom_storage_context_(dom_storage_context), | |
41 id_(id), | |
42 data_dir_path_(data_dir_path), | |
43 dom_storage_type_(dom_storage_type) { | |
44 DCHECK(dom_storage_context_); | |
45 } | 31 } |
46 | 32 |
47 DOMStorageNamespace::~DOMStorageNamespace() { | 33 DomStorageArea* DomStorageNamespace::OpenStorageArea(const GURL& origin) { |
48 // TODO(jorlow): If the DOMStorageContext is being destructed, there's no need | 34 if (AreaHolder* holder = GetAreaHolder(origin)) { |
49 // to do these calls. Maybe we should add a fast path? | 35 ++(holder->open_count_); |
50 for (OriginToStorageAreaMap::iterator iter(origin_to_storage_area_.begin()); | 36 return holder->area_; |
51 iter != origin_to_storage_area_.end(); ++iter) { | |
52 dom_storage_context_->UnregisterStorageArea(iter->second); | |
53 delete iter->second; | |
54 } | 37 } |
| 38 DomStorageArea* area = new DomStorageArea(namespace_id_, origin, |
| 39 directory_, task_runner_); |
| 40 areas_[origin] = AreaHolder(area, 1); |
| 41 return area; |
55 } | 42 } |
56 | 43 |
57 DOMStorageArea* DOMStorageNamespace::GetStorageArea(const string16& origin) { | 44 void DomStorageNamespace::CloseStorageArea(DomStorageArea* area) { |
58 // We may have already created it for another dispatcher host. | 45 AreaHolder* holder = GetAreaHolder(area->origin()); |
59 OriginToStorageAreaMap::iterator iter = origin_to_storage_area_.find(origin); | 46 DCHECK(holder); |
60 if (iter != origin_to_storage_area_.end()) | 47 DCHECK_EQ(holder->area_.get(), area); |
61 return iter->second; | 48 --(holder->open_count_); |
62 | 49 // TODO(michaeln): Clean up areas that aren't needed in memory anymore. |
63 // We need to create a new one. | 50 // The in-process-webkit based impl didn't do this either, but would be nice. |
64 int64 id = dom_storage_context_->AllocateStorageAreaId(); | |
65 DCHECK(!dom_storage_context_->GetStorageArea(id)); | |
66 DOMStorageArea* storage_area = new DOMStorageArea(origin, id, this); | |
67 origin_to_storage_area_[origin] = storage_area; | |
68 dom_storage_context_->RegisterStorageArea(storage_area); | |
69 return storage_area; | |
70 } | 51 } |
71 | 52 |
72 DOMStorageNamespace* DOMStorageNamespace::Copy(int64 id) { | 53 DomStorageNamespace* DomStorageNamespace::Clone(int64 clone_namespace_id) { |
73 DCHECK(dom_storage_type_ == DOM_STORAGE_SESSION); | 54 DCHECK_NE(kLocalStorageNamespaceId, namespace_id_); |
74 DCHECK(!dom_storage_context_->GetStorageNamespace(id, false)); | 55 DCHECK_NE(kLocalStorageNamespaceId, clone_namespace_id); |
75 DOMStorageNamespace* new_storage_namespace = new DOMStorageNamespace( | 56 DomStorageNamespace* clone = |
76 dom_storage_context_, id, data_dir_path_, dom_storage_type_); | 57 new DomStorageNamespace(clone_namespace_id, task_runner_); |
77 // If we haven't used the namespace yet, there's nothing to copy. | 58 AreaMap::const_iterator it = areas_.begin(); |
78 if (storage_namespace_.get()) | 59 for (; it != areas_.end(); ++it) { |
79 new_storage_namespace->storage_namespace_.reset(storage_namespace_->copy()); | 60 DomStorageArea* area = it->second.area_->ShallowCopy(clone_namespace_id); |
80 return new_storage_namespace; | 61 clone->areas_[it->first] = AreaHolder(area, 0); |
| 62 } |
| 63 return clone; |
81 } | 64 } |
82 | 65 |
83 void DOMStorageNamespace::PurgeMemory() { | 66 DomStorageNamespace::AreaHolder* |
84 DCHECK(dom_storage_type_ == DOM_STORAGE_LOCAL); | 67 DomStorageNamespace::GetAreaHolder(const GURL& origin) { |
85 for (OriginToStorageAreaMap::iterator iter(origin_to_storage_area_.begin()); | 68 AreaMap::iterator found = areas_.find(origin); |
86 iter != origin_to_storage_area_.end(); ++iter) | 69 if (found == areas_.end()) |
87 iter->second->PurgeMemory(); | 70 return NULL; |
88 storage_namespace_.reset(); | 71 return &(found->second); |
89 } | 72 } |
90 | 73 |
91 WebStorageArea* DOMStorageNamespace::CreateWebStorageArea( | 74 // AreaHolder |
92 const string16& origin) { | 75 |
93 CreateWebStorageNamespaceIfNecessary(); | 76 DomStorageNamespace::AreaHolder::AreaHolder() |
94 return storage_namespace_->createStorageArea(origin); | 77 : open_count_(0) { |
95 } | 78 } |
96 | 79 |
97 void DOMStorageNamespace::CreateWebStorageNamespaceIfNecessary() { | 80 DomStorageNamespace::AreaHolder::AreaHolder( |
98 if (storage_namespace_.get()) | 81 DomStorageArea* area, int count) |
99 return; | 82 : area_(area), open_count_(count) { |
| 83 } |
100 | 84 |
101 if (dom_storage_type_ == DOM_STORAGE_LOCAL) { | 85 DomStorageNamespace::AreaHolder::~AreaHolder() { |
102 storage_namespace_.reset( | |
103 WebStorageNamespace::createLocalStorageNamespace(data_dir_path_, | |
104 WebStorageNamespace::m_localStorageQuota)); | |
105 } else { | |
106 storage_namespace_.reset(WebStorageNamespace::createSessionStorageNamespace( | |
107 WebStorageNamespace::m_sessionStorageQuota)); | |
108 } | |
109 } | 86 } |
| 87 |
| 88 } // namespace dom_storage |
OLD | NEW |