OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/dom_storage/dom_storage_dispatcher.h" | 5 #include "content/renderer/dom_storage/dom_storage_dispatcher.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 #include <map> | 8 #include <map> |
9 | 9 |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 class DomStorageDispatcher::ProxyImpl : public DOMStorageProxy { | 97 class DomStorageDispatcher::ProxyImpl : public DOMStorageProxy { |
98 public: | 98 public: |
99 explicit ProxyImpl(RenderThreadImpl* sender); | 99 explicit ProxyImpl(RenderThreadImpl* sender); |
100 | 100 |
101 // Methods for use by DomStorageDispatcher directly. | 101 // Methods for use by DomStorageDispatcher directly. |
102 DOMStorageCachedArea* OpenCachedArea( | 102 DOMStorageCachedArea* OpenCachedArea( |
103 int64 namespace_id, const GURL& origin); | 103 int64 namespace_id, const GURL& origin); |
104 void CloseCachedArea(DOMStorageCachedArea* area); | 104 void CloseCachedArea(DOMStorageCachedArea* area); |
105 DOMStorageCachedArea* LookupCachedArea( | 105 DOMStorageCachedArea* LookupCachedArea( |
106 int64 namespace_id, const GURL& origin); | 106 int64 namespace_id, const GURL& origin); |
107 void ResetAllCachedAreas(int64 namespace_id); | |
108 void CompleteOnePendingCallback(bool success); | 107 void CompleteOnePendingCallback(bool success); |
109 void Shutdown(); | 108 void Shutdown(); |
110 | 109 |
111 // DOMStorageProxy interface for use by DOMStorageCachedArea. | 110 // DOMStorageProxy interface for use by DOMStorageCachedArea. |
112 void LoadArea(int connection_id, | 111 void LoadArea(int connection_id, |
113 DOMStorageValuesMap* values, | 112 DOMStorageValuesMap* values, |
114 bool* send_log_get_messages, | |
115 const CompletionCallback& callback) override; | 113 const CompletionCallback& callback) override; |
116 void SetItem(int connection_id, | 114 void SetItem(int connection_id, |
117 const base::string16& key, | 115 const base::string16& key, |
118 const base::string16& value, | 116 const base::string16& value, |
119 const GURL& page_url, | 117 const GURL& page_url, |
120 const CompletionCallback& callback) override; | 118 const CompletionCallback& callback) override; |
121 void LogGetItem(int connection_id, | |
122 const base::string16& key, | |
123 const base::NullableString16& value) override; | |
124 void RemoveItem(int connection_id, | 119 void RemoveItem(int connection_id, |
125 const base::string16& key, | 120 const base::string16& key, |
126 const GURL& page_url, | 121 const GURL& page_url, |
127 const CompletionCallback& callback) override; | 122 const CompletionCallback& callback) override; |
128 void ClearArea(int connection_id, | 123 void ClearArea(int connection_id, |
129 const GURL& page_url, | 124 const GURL& page_url, |
130 const CompletionCallback& callback) override; | 125 const CompletionCallback& callback) override; |
131 | 126 |
132 private: | 127 private: |
133 // Struct to hold references to our contained areas and | 128 // Struct to hold references to our contained areas and |
134 // to keep track of how many tabs have a given area open. | 129 // to keep track of how many tabs have a given area open. |
135 struct CachedAreaHolder { | 130 struct CachedAreaHolder { |
136 scoped_refptr<DOMStorageCachedArea> area_; | 131 scoped_refptr<DOMStorageCachedArea> area_; |
137 int open_count_; | 132 int open_count_; |
138 int64 namespace_id_; | |
139 CachedAreaHolder() : open_count_(0) {} | 133 CachedAreaHolder() : open_count_(0) {} |
140 CachedAreaHolder(DOMStorageCachedArea* area, int count, | 134 CachedAreaHolder(DOMStorageCachedArea* area, int count) |
141 int64 namespace_id) | 135 : area_(area), open_count_(count) {} |
142 : area_(area), open_count_(count), namespace_id_(namespace_id) {} | |
143 }; | 136 }; |
144 typedef std::map<std::string, CachedAreaHolder> CachedAreaMap; | 137 typedef std::map<std::string, CachedAreaHolder> CachedAreaMap; |
145 typedef std::list<CompletionCallback> CallbackList; | 138 typedef std::list<CompletionCallback> CallbackList; |
146 | 139 |
147 ~ProxyImpl() override {} | 140 ~ProxyImpl() override {} |
148 | 141 |
149 // Sudden termination is disabled when there are callbacks pending | 142 // Sudden termination is disabled when there are callbacks pending |
150 // to more reliably commit changes during shutdown. | 143 // to more reliably commit changes during shutdown. |
151 void PushPendingCallback(const CompletionCallback& callback) { | 144 void PushPendingCallback(const CompletionCallback& callback) { |
152 if (pending_callbacks_.empty()) | 145 if (pending_callbacks_.empty()) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 | 180 |
188 DOMStorageCachedArea* DomStorageDispatcher::ProxyImpl::OpenCachedArea( | 181 DOMStorageCachedArea* DomStorageDispatcher::ProxyImpl::OpenCachedArea( |
189 int64 namespace_id, const GURL& origin) { | 182 int64 namespace_id, const GURL& origin) { |
190 std::string key = GetCachedAreaKey(namespace_id, origin); | 183 std::string key = GetCachedAreaKey(namespace_id, origin); |
191 if (CachedAreaHolder* holder = GetAreaHolder(key)) { | 184 if (CachedAreaHolder* holder = GetAreaHolder(key)) { |
192 ++(holder->open_count_); | 185 ++(holder->open_count_); |
193 return holder->area_.get(); | 186 return holder->area_.get(); |
194 } | 187 } |
195 scoped_refptr<DOMStorageCachedArea> area = | 188 scoped_refptr<DOMStorageCachedArea> area = |
196 new DOMStorageCachedArea(namespace_id, origin, this); | 189 new DOMStorageCachedArea(namespace_id, origin, this); |
197 cached_areas_[key] = CachedAreaHolder(area.get(), 1, namespace_id); | 190 cached_areas_[key] = CachedAreaHolder(area.get(), 1); |
198 return area.get(); | 191 return area.get(); |
199 } | 192 } |
200 | 193 |
201 void DomStorageDispatcher::ProxyImpl::CloseCachedArea( | 194 void DomStorageDispatcher::ProxyImpl::CloseCachedArea( |
202 DOMStorageCachedArea* area) { | 195 DOMStorageCachedArea* area) { |
203 std::string key = GetCachedAreaKey(area->namespace_id(), area->origin()); | 196 std::string key = GetCachedAreaKey(area->namespace_id(), area->origin()); |
204 CachedAreaHolder* holder = GetAreaHolder(key); | 197 CachedAreaHolder* holder = GetAreaHolder(key); |
205 DCHECK(holder); | 198 DCHECK(holder); |
206 DCHECK_EQ(holder->area_.get(), area); | 199 DCHECK_EQ(holder->area_.get(), area); |
207 DCHECK_GT(holder->open_count_, 0); | 200 DCHECK_GT(holder->open_count_, 0); |
208 if (--(holder->open_count_) == 0) { | 201 if (--(holder->open_count_) == 0) { |
209 cached_areas_.erase(key); | 202 cached_areas_.erase(key); |
210 } | 203 } |
211 } | 204 } |
212 | 205 |
213 DOMStorageCachedArea* DomStorageDispatcher::ProxyImpl::LookupCachedArea( | 206 DOMStorageCachedArea* DomStorageDispatcher::ProxyImpl::LookupCachedArea( |
214 int64 namespace_id, const GURL& origin) { | 207 int64 namespace_id, const GURL& origin) { |
215 std::string key = GetCachedAreaKey(namespace_id, origin); | 208 std::string key = GetCachedAreaKey(namespace_id, origin); |
216 CachedAreaHolder* holder = GetAreaHolder(key); | 209 CachedAreaHolder* holder = GetAreaHolder(key); |
217 if (!holder) | 210 if (!holder) |
218 return NULL; | 211 return NULL; |
219 return holder->area_.get(); | 212 return holder->area_.get(); |
220 } | 213 } |
221 | 214 |
222 void DomStorageDispatcher::ProxyImpl::ResetAllCachedAreas(int64 namespace_id) { | |
223 for (CachedAreaMap::iterator it = cached_areas_.begin(); | |
224 it != cached_areas_.end(); | |
225 ++it) { | |
226 if (it->second.namespace_id_ == namespace_id) | |
227 it->second.area_->Reset(); | |
228 } | |
229 } | |
230 | |
231 void DomStorageDispatcher::ProxyImpl::CompleteOnePendingCallback(bool success) { | 215 void DomStorageDispatcher::ProxyImpl::CompleteOnePendingCallback(bool success) { |
232 PopPendingCallback().Run(success); | 216 PopPendingCallback().Run(success); |
233 } | 217 } |
234 | 218 |
235 void DomStorageDispatcher::ProxyImpl::Shutdown() { | 219 void DomStorageDispatcher::ProxyImpl::Shutdown() { |
236 throttling_filter_->Shutdown(); | 220 throttling_filter_->Shutdown(); |
237 sender_->RemoveFilter(throttling_filter_.get()); | 221 sender_->RemoveFilter(throttling_filter_.get()); |
238 sender_ = NULL; | 222 sender_ = NULL; |
239 cached_areas_.clear(); | 223 cached_areas_.clear(); |
240 pending_callbacks_.clear(); | 224 pending_callbacks_.clear(); |
241 } | 225 } |
242 | 226 |
243 void DomStorageDispatcher::ProxyImpl::LoadArea( | 227 void DomStorageDispatcher::ProxyImpl::LoadArea( |
244 int connection_id, DOMStorageValuesMap* values, bool* send_log_get_messages, | 228 int connection_id, DOMStorageValuesMap* values, |
245 const CompletionCallback& callback) { | 229 const CompletionCallback& callback) { |
246 PushPendingCallback(callback); | 230 PushPendingCallback(callback); |
247 throttling_filter_->SendThrottled(new DOMStorageHostMsg_LoadStorageArea( | 231 throttling_filter_->SendThrottled(new DOMStorageHostMsg_LoadStorageArea( |
248 connection_id, values, send_log_get_messages)); | 232 connection_id, values)); |
249 } | 233 } |
250 | 234 |
251 void DomStorageDispatcher::ProxyImpl::SetItem( | 235 void DomStorageDispatcher::ProxyImpl::SetItem( |
252 int connection_id, const base::string16& key, | 236 int connection_id, const base::string16& key, |
253 const base::string16& value, const GURL& page_url, | 237 const base::string16& value, const GURL& page_url, |
254 const CompletionCallback& callback) { | 238 const CompletionCallback& callback) { |
255 PushPendingCallback(callback); | 239 PushPendingCallback(callback); |
256 throttling_filter_->SendThrottled(new DOMStorageHostMsg_SetItem( | 240 throttling_filter_->SendThrottled(new DOMStorageHostMsg_SetItem( |
257 connection_id, key, value, page_url)); | 241 connection_id, key, value, page_url)); |
258 } | 242 } |
259 | 243 |
260 void DomStorageDispatcher::ProxyImpl::LogGetItem( | |
261 int connection_id, const base::string16& key, | |
262 const base::NullableString16& value) { | |
263 sender_->Send(new DOMStorageHostMsg_LogGetItem(connection_id, key, value)); | |
264 } | |
265 | |
266 void DomStorageDispatcher::ProxyImpl::RemoveItem( | 244 void DomStorageDispatcher::ProxyImpl::RemoveItem( |
267 int connection_id, const base::string16& key, const GURL& page_url, | 245 int connection_id, const base::string16& key, const GURL& page_url, |
268 const CompletionCallback& callback) { | 246 const CompletionCallback& callback) { |
269 PushPendingCallback(callback); | 247 PushPendingCallback(callback); |
270 throttling_filter_->SendThrottled(new DOMStorageHostMsg_RemoveItem( | 248 throttling_filter_->SendThrottled(new DOMStorageHostMsg_RemoveItem( |
271 connection_id, key, page_url)); | 249 connection_id, key, page_url)); |
272 } | 250 } |
273 | 251 |
274 void DomStorageDispatcher::ProxyImpl::ClearArea(int connection_id, | 252 void DomStorageDispatcher::ProxyImpl::ClearArea(int connection_id, |
275 const GURL& page_url, | 253 const GURL& page_url, |
(...skipping 27 matching lines...) Expand all Loading... |
303 new DOMStorageHostMsg_CloseStorageArea(connection_id)); | 281 new DOMStorageHostMsg_CloseStorageArea(connection_id)); |
304 proxy_->CloseCachedArea(area); | 282 proxy_->CloseCachedArea(area); |
305 } | 283 } |
306 | 284 |
307 bool DomStorageDispatcher::OnMessageReceived(const IPC::Message& msg) { | 285 bool DomStorageDispatcher::OnMessageReceived(const IPC::Message& msg) { |
308 bool handled = true; | 286 bool handled = true; |
309 IPC_BEGIN_MESSAGE_MAP(DomStorageDispatcher, msg) | 287 IPC_BEGIN_MESSAGE_MAP(DomStorageDispatcher, msg) |
310 IPC_MESSAGE_HANDLER(DOMStorageMsg_Event, OnStorageEvent) | 288 IPC_MESSAGE_HANDLER(DOMStorageMsg_Event, OnStorageEvent) |
311 IPC_MESSAGE_HANDLER(DOMStorageMsg_AsyncOperationComplete, | 289 IPC_MESSAGE_HANDLER(DOMStorageMsg_AsyncOperationComplete, |
312 OnAsyncOperationComplete) | 290 OnAsyncOperationComplete) |
313 IPC_MESSAGE_HANDLER(DOMStorageMsg_ResetCachedValues, | |
314 OnResetCachedValues) | |
315 IPC_MESSAGE_UNHANDLED(handled = false) | 291 IPC_MESSAGE_UNHANDLED(handled = false) |
316 IPC_END_MESSAGE_MAP() | 292 IPC_END_MESSAGE_MAP() |
317 return handled; | 293 return handled; |
318 } | 294 } |
319 | 295 |
320 void DomStorageDispatcher::OnStorageEvent( | 296 void DomStorageDispatcher::OnStorageEvent( |
321 const DOMStorageMsg_Event_Params& params) { | 297 const DOMStorageMsg_Event_Params& params) { |
322 RenderThreadImpl::current()->EnsureWebKitInitialized(); | 298 RenderThreadImpl::current()->EnsureWebKitInitialized(); |
323 | 299 |
324 bool originated_in_process = params.connection_id != 0; | 300 bool originated_in_process = params.connection_id != 0; |
(...skipping 29 matching lines...) Expand all Loading... |
354 session_namespace_for_event_dispatch, | 330 session_namespace_for_event_dispatch, |
355 originating_area, | 331 originating_area, |
356 originated_in_process); | 332 originated_in_process); |
357 } | 333 } |
358 } | 334 } |
359 | 335 |
360 void DomStorageDispatcher::OnAsyncOperationComplete(bool success) { | 336 void DomStorageDispatcher::OnAsyncOperationComplete(bool success) { |
361 proxy_->CompleteOnePendingCallback(success); | 337 proxy_->CompleteOnePendingCallback(success); |
362 } | 338 } |
363 | 339 |
364 void DomStorageDispatcher::OnResetCachedValues(int64 namespace_id) { | |
365 proxy_->ResetAllCachedAreas(namespace_id); | |
366 } | |
367 | |
368 } // namespace content | 340 } // namespace content |
OLD | NEW |