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