Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/child/quota_dispatcher.h" | 5 #include "content/child/quota_dispatcher.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/lazy_instance.h" | |
| 9 #include "base/threading/thread_local.h" | |
| 8 #include "content/child/child_thread.h" | 10 #include "content/child/child_thread.h" |
| 11 #include "content/child/thread_safe_sender.h" | |
| 9 #include "content/common/quota_messages.h" | 12 #include "content/common/quota_messages.h" |
| 10 #include "third_party/WebKit/public/web/WebStorageQuotaCallbacks.h" | 13 #include "third_party/WebKit/public/web/WebStorageQuotaCallbacks.h" |
| 11 #include "third_party/WebKit/public/web/WebStorageQuotaType.h" | 14 #include "third_party/WebKit/public/web/WebStorageQuotaType.h" |
| 12 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 13 | 16 |
| 14 using quota::QuotaStatusCode; | 17 using quota::QuotaStatusCode; |
| 15 using quota::StorageType; | 18 using quota::StorageType; |
| 16 | 19 |
| 17 using WebKit::WebStorageQuotaCallbacks; | 20 using WebKit::WebStorageQuotaCallbacks; |
| 18 using WebKit::WebStorageQuotaError; | 21 using WebKit::WebStorageQuotaError; |
| 19 using WebKit::WebStorageQuotaType; | 22 using WebKit::WebStorageQuotaType; |
| 20 | 23 |
| 24 using webkit_glue::WorkerTaskRunner; | |
| 25 | |
| 21 namespace content { | 26 namespace content { |
| 27 | |
| 28 static base::LazyInstance<base::ThreadLocalPointer<QuotaDispatcher> >::Leaky | |
| 29 g_quota_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; | |
| 30 | |
| 22 namespace { | 31 namespace { |
| 23 | 32 |
| 24 // QuotaDispatcher::Callback implementation for WebStorageQuotaCallbacks. | 33 // QuotaDispatcher::Callback implementation for WebStorageQuotaCallbacks. |
| 25 class WebStorageQuotaDispatcherCallback : public QuotaDispatcher::Callback { | 34 class WebStorageQuotaDispatcherCallback : public QuotaDispatcher::Callback { |
| 26 public: | 35 public: |
| 27 WebStorageQuotaDispatcherCallback(WebKit::WebStorageQuotaCallbacks* callback) | 36 WebStorageQuotaDispatcherCallback(WebKit::WebStorageQuotaCallbacks* callback) |
| 28 : callbacks_(callback) { | 37 : callbacks_(callback) { |
| 29 DCHECK(callbacks_); | 38 DCHECK(callbacks_); |
| 30 } | 39 } |
| 31 virtual ~WebStorageQuotaDispatcherCallback() {} | 40 virtual ~WebStorageQuotaDispatcherCallback() {} |
| 32 virtual void DidQueryStorageUsageAndQuota(int64 usage, int64 quota) OVERRIDE { | 41 virtual void DidQueryStorageUsageAndQuota(int64 usage, int64 quota) OVERRIDE { |
| 33 callbacks_->didQueryStorageUsageAndQuota(usage, quota); | 42 callbacks_->didQueryStorageUsageAndQuota(usage, quota); |
| 34 } | 43 } |
| 35 virtual void DidGrantStorageQuota(int64 granted_quota) OVERRIDE { | 44 virtual void DidGrantStorageQuota(int64 granted_quota) OVERRIDE { |
| 36 callbacks_->didGrantStorageQuota(granted_quota); | 45 callbacks_->didGrantStorageQuota(granted_quota); |
| 37 } | 46 } |
| 38 virtual void DidFail(quota::QuotaStatusCode error) OVERRIDE { | 47 virtual void DidFail(quota::QuotaStatusCode error) OVERRIDE { |
| 39 callbacks_->didFail(static_cast<WebStorageQuotaError>(error)); | 48 callbacks_->didFail(static_cast<WebStorageQuotaError>(error)); |
| 40 } | 49 } |
| 41 | 50 |
| 42 private: | 51 private: |
| 43 // Not owned (self-destructed). | 52 // Not owned (self-destructed). |
| 44 WebKit::WebStorageQuotaCallbacks* callbacks_; | 53 WebKit::WebStorageQuotaCallbacks* callbacks_; |
| 45 }; | 54 }; |
| 46 | 55 |
| 56 int CurrentWorkerId() { | |
| 57 return WorkerTaskRunner::Instance()->CurrentWorkerId(); | |
|
jam
2013/07/26 04:57:30
nit: adding this function for 2 calls before doesn
kinuko
2013/07/29 04:31:55
Done.
| |
| 58 } | |
| 59 | |
| 47 } // namespace | 60 } // namespace |
| 48 | 61 |
| 49 QuotaDispatcher::QuotaDispatcher() { | 62 QuotaDispatcher::QuotaDispatcher(ThreadSafeSender* thread_safe_sender) |
| 63 : thread_safe_sender_(thread_safe_sender) { | |
| 64 g_quota_dispatcher_tls.Pointer()->Set(this); | |
| 50 } | 65 } |
| 51 | 66 |
| 52 QuotaDispatcher::~QuotaDispatcher() { | 67 QuotaDispatcher::~QuotaDispatcher() { |
| 53 IDMap<Callback, IDMapOwnPointer>::iterator iter(&pending_quota_callbacks_); | 68 IDMap<Callback, IDMapOwnPointer>::iterator iter(&pending_quota_callbacks_); |
| 54 while (!iter.IsAtEnd()) { | 69 while (!iter.IsAtEnd()) { |
| 55 iter.GetCurrentValue()->DidFail(quota::kQuotaErrorAbort); | 70 iter.GetCurrentValue()->DidFail(quota::kQuotaErrorAbort); |
| 56 iter.Advance(); | 71 iter.Advance(); |
| 57 } | 72 } |
| 73 | |
| 74 g_quota_dispatcher_tls.Pointer()->Set(NULL); | |
| 58 } | 75 } |
| 59 | 76 |
| 60 bool QuotaDispatcher::OnMessageReceived(const IPC::Message& msg) { | 77 QuotaDispatcher* QuotaDispatcher::ThreadSpecificInstance( |
| 78 ThreadSafeSender* thread_safe_sender) { | |
| 79 if (g_quota_dispatcher_tls.Pointer()->Get()) | |
| 80 return g_quota_dispatcher_tls.Pointer()->Get(); | |
| 81 | |
| 82 QuotaDispatcher* dispatcher = new QuotaDispatcher(thread_safe_sender); | |
| 83 if (WorkerTaskRunner::Instance()->CurrentWorkerId()) | |
| 84 WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); | |
| 85 return dispatcher; | |
| 86 } | |
| 87 | |
| 88 void QuotaDispatcher::OnWorkerRunLoopStopped() { | |
| 89 delete this; | |
| 90 } | |
| 91 | |
| 92 void QuotaDispatcher::OnMessageReceived(const IPC::Message& msg) { | |
| 61 bool handled = true; | 93 bool handled = true; |
| 62 IPC_BEGIN_MESSAGE_MAP(QuotaDispatcher, msg) | 94 IPC_BEGIN_MESSAGE_MAP(QuotaDispatcher, msg) |
| 63 IPC_MESSAGE_HANDLER(QuotaMsg_DidGrantStorageQuota, | 95 IPC_MESSAGE_HANDLER(QuotaMsg_DidGrantStorageQuota, |
| 64 DidGrantStorageQuota) | 96 DidGrantStorageQuota) |
| 65 IPC_MESSAGE_HANDLER(QuotaMsg_DidQueryStorageUsageAndQuota, | 97 IPC_MESSAGE_HANDLER(QuotaMsg_DidQueryStorageUsageAndQuota, |
| 66 DidQueryStorageUsageAndQuota); | 98 DidQueryStorageUsageAndQuota); |
| 67 IPC_MESSAGE_HANDLER(QuotaMsg_DidFail, DidFail); | 99 IPC_MESSAGE_HANDLER(QuotaMsg_DidFail, DidFail); |
| 68 IPC_MESSAGE_UNHANDLED(handled = false) | 100 IPC_MESSAGE_UNHANDLED(handled = false) |
| 69 IPC_END_MESSAGE_MAP() | 101 IPC_END_MESSAGE_MAP() |
| 70 return handled; | 102 DCHECK(handled) << "Unhandled message:" << msg.type(); |
| 71 } | 103 } |
| 72 | 104 |
| 73 void QuotaDispatcher::QueryStorageUsageAndQuota( | 105 void QuotaDispatcher::QueryStorageUsageAndQuota( |
| 74 const GURL& origin_url, | 106 const GURL& origin_url, |
| 75 StorageType type, | 107 StorageType type, |
| 76 Callback* callback) { | 108 Callback* callback) { |
| 77 DCHECK(callback); | 109 DCHECK(callback); |
| 78 int request_id = pending_quota_callbacks_.Add(callback); | 110 int request_id = pending_quota_callbacks_.Add(callback); |
| 79 ChildThread::current()->Send(new QuotaHostMsg_QueryStorageUsageAndQuota( | 111 thread_safe_sender_->Send(new QuotaHostMsg_QueryStorageUsageAndQuota( |
| 80 request_id, origin_url, type)); | 112 CurrentWorkerId(), request_id, origin_url, type)); |
| 81 } | 113 } |
| 82 | 114 |
| 83 void QuotaDispatcher::RequestStorageQuota( | 115 void QuotaDispatcher::RequestStorageQuota( |
| 84 int render_view_id, | 116 int render_view_id, |
| 85 const GURL& origin_url, | 117 const GURL& origin_url, |
| 86 StorageType type, | 118 StorageType type, |
| 87 int64 requested_size, | 119 int64 requested_size, |
| 88 Callback* callback) { | 120 Callback* callback) { |
| 89 DCHECK(callback); | 121 DCHECK(callback); |
| 122 DCHECK(CurrentWorkerId() == 0); | |
| 90 int request_id = pending_quota_callbacks_.Add(callback); | 123 int request_id = pending_quota_callbacks_.Add(callback); |
| 91 ChildThread::current()->Send(new QuotaHostMsg_RequestStorageQuota( | 124 thread_safe_sender_->Send(new QuotaHostMsg_RequestStorageQuota( |
| 92 render_view_id, request_id, origin_url, type, requested_size)); | 125 render_view_id, request_id, origin_url, type, requested_size)); |
| 93 } | 126 } |
| 94 | 127 |
| 95 // static | 128 // static |
| 96 QuotaDispatcher::Callback* | 129 QuotaDispatcher::Callback* |
| 97 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper( | 130 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper( |
| 98 WebKit::WebStorageQuotaCallbacks* callbacks) { | 131 WebKit::WebStorageQuotaCallbacks* callbacks) { |
| 99 return new WebStorageQuotaDispatcherCallback(callbacks); | 132 return new WebStorageQuotaDispatcherCallback(callbacks); |
| 100 } | 133 } |
| 101 | 134 |
| 102 void QuotaDispatcher::DidGrantStorageQuota( | 135 void QuotaDispatcher::DidGrantStorageQuota( |
| 136 int ipc_thread_id, | |
| 103 int request_id, | 137 int request_id, |
| 104 int64 granted_quota) { | 138 int64 granted_quota) { |
| 105 Callback* callback = pending_quota_callbacks_.Lookup(request_id); | 139 Callback* callback = pending_quota_callbacks_.Lookup(request_id); |
| 106 DCHECK(callback); | 140 DCHECK(callback); |
| 107 callback->DidGrantStorageQuota(granted_quota); | 141 callback->DidGrantStorageQuota(granted_quota); |
| 108 pending_quota_callbacks_.Remove(request_id); | 142 pending_quota_callbacks_.Remove(request_id); |
| 109 } | 143 } |
| 110 | 144 |
| 111 void QuotaDispatcher::DidQueryStorageUsageAndQuota( | 145 void QuotaDispatcher::DidQueryStorageUsageAndQuota( |
| 146 int ipc_thread_id, | |
| 112 int request_id, | 147 int request_id, |
| 113 int64 current_usage, | 148 int64 current_usage, |
| 114 int64 current_quota) { | 149 int64 current_quota) { |
| 115 Callback* callback = pending_quota_callbacks_.Lookup(request_id); | 150 Callback* callback = pending_quota_callbacks_.Lookup(request_id); |
| 116 DCHECK(callback); | 151 DCHECK(callback); |
| 117 callback->DidQueryStorageUsageAndQuota(current_usage, current_quota); | 152 callback->DidQueryStorageUsageAndQuota(current_usage, current_quota); |
| 118 pending_quota_callbacks_.Remove(request_id); | 153 pending_quota_callbacks_.Remove(request_id); |
| 119 } | 154 } |
| 120 | 155 |
| 121 void QuotaDispatcher::DidFail( | 156 void QuotaDispatcher::DidFail( |
| 157 int ipc_thread_id, | |
| 122 int request_id, | 158 int request_id, |
| 123 QuotaStatusCode error) { | 159 QuotaStatusCode error) { |
| 124 Callback* callback = pending_quota_callbacks_.Lookup(request_id); | 160 Callback* callback = pending_quota_callbacks_.Lookup(request_id); |
| 125 DCHECK(callback); | 161 DCHECK(callback); |
| 126 callback->DidFail(error); | 162 callback->DidFail(error); |
| 127 pending_quota_callbacks_.Remove(request_id); | 163 pending_quota_callbacks_.Remove(request_id); |
| 128 } | 164 } |
| 129 | 165 |
| 130 COMPILE_ASSERT(int(WebKit::WebStorageQuotaTypeTemporary) == \ | 166 COMPILE_ASSERT(int(WebKit::WebStorageQuotaTypeTemporary) == \ |
| 131 int(quota::kStorageTypeTemporary), mismatching_enums); | 167 int(quota::kStorageTypeTemporary), mismatching_enums); |
| 132 COMPILE_ASSERT(int(WebKit::WebStorageQuotaTypePersistent) == \ | 168 COMPILE_ASSERT(int(WebKit::WebStorageQuotaTypePersistent) == \ |
| 133 int(quota::kStorageTypePersistent), mismatching_enums); | 169 int(quota::kStorageTypePersistent), mismatching_enums); |
| 134 | 170 |
| 135 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorNotSupported) == \ | 171 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorNotSupported) == \ |
| 136 int(quota::kQuotaErrorNotSupported), mismatching_enums); | 172 int(quota::kQuotaErrorNotSupported), mismatching_enums); |
| 137 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorAbort) == \ | 173 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorAbort) == \ |
| 138 int(quota::kQuotaErrorAbort), mismatching_enums); | 174 int(quota::kQuotaErrorAbort), mismatching_enums); |
| 139 | 175 |
| 140 } // namespace content | 176 } // namespace content |
| OLD | NEW |