| 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_context.h" | 5 #include "chrome/browser/in_process_webkit/dom_storage_context.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "chrome/browser/chrome_thread.h" | 8 #include "chrome/browser/chrome_thread.h" |
| 9 #include "chrome/browser/in_process_webkit/storage_area.h" | 9 #include "chrome/browser/in_process_webkit/storage_area.h" |
| 10 #include "chrome/browser/in_process_webkit/storage_namespace.h" | 10 #include "chrome/browser/in_process_webkit/storage_namespace.h" |
| 11 #include "chrome/browser/in_process_webkit/webkit_context.h" | 11 #include "chrome/browser/in_process_webkit/webkit_context.h" |
| 12 | 12 |
| 13 DOMStorageContext::DOMStorageContext(WebKitContext* webkit_context) | 13 DOMStorageContext::DOMStorageContext(WebKitContext* webkit_context) |
| 14 : last_storage_area_id_(kFirstStorageAreaId), | 14 : last_storage_area_id_(kFirstStorageAreaId), |
| 15 last_storage_namespace_id_(kFirstStorageNamespaceId), | 15 last_storage_namespace_id_(kFirstStorageNamespaceId), |
| 16 webkit_context_(webkit_context) { | 16 webkit_context_(webkit_context) { |
| 17 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT)); | |
| 18 } | 17 } |
| 19 | 18 |
| 20 DOMStorageContext::~DOMStorageContext() { | 19 DOMStorageContext::~DOMStorageContext() { |
| 20 // This should not go away until all DOM Storage Dispatcher hosts have gone |
| 21 // away. And they remove themselves from this list. |
| 22 DCHECK(dispatcher_host_set_.empty()); |
| 23 |
| 24 // If we don't have any work to do on the WebKit thread, bail. |
| 25 if (storage_namespace_map_.empty()) |
| 26 return; |
| 21 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT)); | 27 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT)); |
| 28 |
| 22 // The storage namespace destructor unregisters the storage namespace, so | 29 // The storage namespace destructor unregisters the storage namespace, so |
| 23 // our iterator becomes invalid. Thus we just keep deleting the first item | 30 // our iterator becomes invalid. Thus we just keep deleting the first item |
| 24 // until there are none left. | 31 // until there are none left. |
| 25 while (!storage_namespace_map_.empty()) | 32 while (!storage_namespace_map_.empty()) |
| 26 delete storage_namespace_map_.begin()->second; | 33 delete storage_namespace_map_.begin()->second; |
| 27 } | 34 } |
| 28 | 35 |
| 29 StorageNamespace* DOMStorageContext::LocalStorage() { | 36 StorageNamespace* DOMStorageContext::LocalStorage() { |
| 37 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT)); |
| 30 StorageNamespace* storage_namespace = GetStorageNamespace( | 38 StorageNamespace* storage_namespace = GetStorageNamespace( |
| 31 kLocalStorageNamespaceId); | 39 kLocalStorageNamespaceId); |
| 32 if (storage_namespace) | 40 if (storage_namespace) |
| 33 return storage_namespace; | 41 return storage_namespace; |
| 34 | 42 |
| 35 FilePath data_path = webkit_context_->data_path(); | 43 FilePath data_path = webkit_context_->data_path(); |
| 36 FilePath dir_path; | 44 FilePath dir_path; |
| 37 if (!data_path.empty()) | 45 if (!data_path.empty()) |
| 38 dir_path = data_path.AppendASCII("localStorage"); | 46 dir_path = data_path.AppendASCII("localStorage"); |
| 39 return StorageNamespace::CreateLocalStorageNamespace(this, dir_path); | 47 return StorageNamespace::CreateLocalStorageNamespace(this, dir_path); |
| 40 } | 48 } |
| 41 | 49 |
| 42 StorageNamespace* DOMStorageContext::NewSessionStorage() { | 50 StorageNamespace* DOMStorageContext::NewSessionStorage() { |
| 51 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT)); |
| 43 return StorageNamespace::CreateSessionStorageNamespace(this); | 52 return StorageNamespace::CreateSessionStorageNamespace(this); |
| 44 } | 53 } |
| 45 | 54 |
| 46 void DOMStorageContext::RegisterStorageArea(StorageArea* storage_area) { | 55 void DOMStorageContext::RegisterStorageArea(StorageArea* storage_area) { |
| 56 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT)); |
| 47 int64 id = storage_area->id(); | 57 int64 id = storage_area->id(); |
| 48 DCHECK(!GetStorageArea(id)); | 58 DCHECK(!GetStorageArea(id)); |
| 49 storage_area_map_[id] = storage_area; | 59 storage_area_map_[id] = storage_area; |
| 50 } | 60 } |
| 51 | 61 |
| 52 void DOMStorageContext::UnregisterStorageArea(StorageArea* storage_area) { | 62 void DOMStorageContext::UnregisterStorageArea(StorageArea* storage_area) { |
| 63 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT)); |
| 53 int64 id = storage_area->id(); | 64 int64 id = storage_area->id(); |
| 54 DCHECK(GetStorageArea(id)); | 65 DCHECK(GetStorageArea(id)); |
| 55 storage_area_map_.erase(id); | 66 storage_area_map_.erase(id); |
| 56 } | 67 } |
| 57 | 68 |
| 58 StorageArea* DOMStorageContext::GetStorageArea(int64 id) { | 69 StorageArea* DOMStorageContext::GetStorageArea(int64 id) { |
| 70 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT)); |
| 59 StorageAreaMap::iterator iter = storage_area_map_.find(id); | 71 StorageAreaMap::iterator iter = storage_area_map_.find(id); |
| 60 if (iter == storage_area_map_.end()) | 72 if (iter == storage_area_map_.end()) |
| 61 return NULL; | 73 return NULL; |
| 62 return iter->second; | 74 return iter->second; |
| 63 } | 75 } |
| 64 | 76 |
| 65 void DOMStorageContext::RegisterStorageNamespace( | 77 void DOMStorageContext::RegisterStorageNamespace( |
| 66 StorageNamespace* storage_namespace) { | 78 StorageNamespace* storage_namespace) { |
| 79 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT)); |
| 67 int64 id = storage_namespace->id(); | 80 int64 id = storage_namespace->id(); |
| 68 DCHECK(!GetStorageNamespace(id)); | 81 DCHECK(!GetStorageNamespace(id)); |
| 69 storage_namespace_map_[id] = storage_namespace; | 82 storage_namespace_map_[id] = storage_namespace; |
| 70 } | 83 } |
| 71 | 84 |
| 72 void DOMStorageContext::UnregisterStorageNamespace( | 85 void DOMStorageContext::UnregisterStorageNamespace( |
| 73 StorageNamespace* storage_namespace) { | 86 StorageNamespace* storage_namespace) { |
| 87 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT)); |
| 74 int64 id = storage_namespace->id(); | 88 int64 id = storage_namespace->id(); |
| 75 DCHECK(GetStorageNamespace(id)); | 89 DCHECK(GetStorageNamespace(id)); |
| 76 storage_namespace_map_.erase(id); | 90 storage_namespace_map_.erase(id); |
| 77 } | 91 } |
| 78 | 92 |
| 79 StorageNamespace* DOMStorageContext::GetStorageNamespace(int64 id) { | 93 StorageNamespace* DOMStorageContext::GetStorageNamespace(int64 id) { |
| 94 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT)); |
| 80 StorageNamespaceMap::iterator iter = storage_namespace_map_.find(id); | 95 StorageNamespaceMap::iterator iter = storage_namespace_map_.find(id); |
| 81 if (iter == storage_namespace_map_.end()) | 96 if (iter == storage_namespace_map_.end()) |
| 82 return NULL; | 97 return NULL; |
| 83 return iter->second; | 98 return iter->second; |
| 84 } | 99 } |
| 100 |
| 101 void DOMStorageContext::RegisterDispatcherHost( |
| 102 DOMStorageDispatcherHost* dispatcher_host) { |
| 103 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 104 DCHECK(dispatcher_host_set_.find(dispatcher_host) == |
| 105 dispatcher_host_set_.end()); |
| 106 dispatcher_host_set_.insert(dispatcher_host); |
| 107 } |
| 108 |
| 109 void DOMStorageContext::UnregisterDispatcherHost( |
| 110 DOMStorageDispatcherHost* dispatcher_host) { |
| 111 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 112 DCHECK(dispatcher_host_set_.find(dispatcher_host) != |
| 113 dispatcher_host_set_.end()); |
| 114 dispatcher_host_set_.erase(dispatcher_host); |
| 115 } |
| 116 |
| 117 const DOMStorageContext::DispatcherHostSet* |
| 118 DOMStorageContext::GetDispatcherHostSet() const { |
| 119 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
| 120 return &dispatcher_host_set_; |
| 121 } |
| OLD | NEW |