| 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/quota_message_filter.h" |
| 12 #include "content/child/thread_safe_sender.h" |
| 9 #include "content/common/quota_messages.h" | 13 #include "content/common/quota_messages.h" |
| 10 #include "third_party/WebKit/public/web/WebStorageQuotaCallbacks.h" | 14 #include "third_party/WebKit/public/web/WebStorageQuotaCallbacks.h" |
| 11 #include "third_party/WebKit/public/web/WebStorageQuotaType.h" | 15 #include "third_party/WebKit/public/web/WebStorageQuotaType.h" |
| 12 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 13 | 17 |
| 14 using quota::QuotaStatusCode; | 18 using quota::QuotaStatusCode; |
| 15 using quota::StorageType; | 19 using quota::StorageType; |
| 16 | 20 |
| 17 using WebKit::WebStorageQuotaCallbacks; | 21 using WebKit::WebStorageQuotaCallbacks; |
| 18 using WebKit::WebStorageQuotaError; | 22 using WebKit::WebStorageQuotaError; |
| 19 using WebKit::WebStorageQuotaType; | 23 using WebKit::WebStorageQuotaType; |
| 20 | 24 |
| 25 using webkit_glue::WorkerTaskRunner; |
| 26 |
| 21 namespace content { | 27 namespace content { |
| 28 |
| 29 static base::LazyInstance<base::ThreadLocalPointer<QuotaDispatcher> >::Leaky |
| 30 g_quota_dispatcher_tls = LAZY_INSTANCE_INITIALIZER; |
| 31 |
| 22 namespace { | 32 namespace { |
| 23 | 33 |
| 24 // QuotaDispatcher::Callback implementation for WebStorageQuotaCallbacks. | 34 // QuotaDispatcher::Callback implementation for WebStorageQuotaCallbacks. |
| 25 class WebStorageQuotaDispatcherCallback : public QuotaDispatcher::Callback { | 35 class WebStorageQuotaDispatcherCallback : public QuotaDispatcher::Callback { |
| 26 public: | 36 public: |
| 27 WebStorageQuotaDispatcherCallback(WebKit::WebStorageQuotaCallbacks* callback) | 37 WebStorageQuotaDispatcherCallback(WebKit::WebStorageQuotaCallbacks* callback) |
| 28 : callbacks_(callback) { | 38 : callbacks_(callback) { |
| 29 DCHECK(callbacks_); | 39 DCHECK(callbacks_); |
| 30 } | 40 } |
| 31 virtual ~WebStorageQuotaDispatcherCallback() {} | 41 virtual ~WebStorageQuotaDispatcherCallback() {} |
| 32 virtual void DidQueryStorageUsageAndQuota(int64 usage, int64 quota) OVERRIDE { | 42 virtual void DidQueryStorageUsageAndQuota(int64 usage, int64 quota) OVERRIDE { |
| 33 callbacks_->didQueryStorageUsageAndQuota(usage, quota); | 43 callbacks_->didQueryStorageUsageAndQuota(usage, quota); |
| 34 } | 44 } |
| 35 virtual void DidGrantStorageQuota(int64 granted_quota) OVERRIDE { | 45 virtual void DidGrantStorageQuota(int64 granted_quota) OVERRIDE { |
| 36 callbacks_->didGrantStorageQuota(granted_quota); | 46 callbacks_->didGrantStorageQuota(granted_quota); |
| 37 } | 47 } |
| 38 virtual void DidFail(quota::QuotaStatusCode error) OVERRIDE { | 48 virtual void DidFail(quota::QuotaStatusCode error) OVERRIDE { |
| 39 callbacks_->didFail(static_cast<WebStorageQuotaError>(error)); | 49 callbacks_->didFail(static_cast<WebStorageQuotaError>(error)); |
| 40 } | 50 } |
| 41 | 51 |
| 42 private: | 52 private: |
| 43 // Not owned (self-destructed). | 53 // Not owned (self-destructed). |
| 44 WebKit::WebStorageQuotaCallbacks* callbacks_; | 54 WebKit::WebStorageQuotaCallbacks* callbacks_; |
| 45 }; | 55 }; |
| 46 | 56 |
| 47 } // namespace | 57 } // namespace |
| 48 | 58 |
| 49 QuotaDispatcher::QuotaDispatcher() { | 59 QuotaDispatcher::QuotaDispatcher(ThreadSafeSender* thread_safe_sender, |
| 60 QuotaMessageFilter* quota_message_filter) |
| 61 : thread_safe_sender_(thread_safe_sender), |
| 62 quota_message_filter_(quota_message_filter) { |
| 63 g_quota_dispatcher_tls.Pointer()->Set(this); |
| 50 } | 64 } |
| 51 | 65 |
| 52 QuotaDispatcher::~QuotaDispatcher() { | 66 QuotaDispatcher::~QuotaDispatcher() { |
| 53 IDMap<Callback, IDMapOwnPointer>::iterator iter(&pending_quota_callbacks_); | 67 IDMap<Callback, IDMapOwnPointer>::iterator iter(&pending_quota_callbacks_); |
| 54 while (!iter.IsAtEnd()) { | 68 while (!iter.IsAtEnd()) { |
| 55 iter.GetCurrentValue()->DidFail(quota::kQuotaErrorAbort); | 69 iter.GetCurrentValue()->DidFail(quota::kQuotaErrorAbort); |
| 56 iter.Advance(); | 70 iter.Advance(); |
| 57 } | 71 } |
| 72 |
| 73 g_quota_dispatcher_tls.Pointer()->Set(NULL); |
| 58 } | 74 } |
| 59 | 75 |
| 60 bool QuotaDispatcher::OnMessageReceived(const IPC::Message& msg) { | 76 QuotaDispatcher* QuotaDispatcher::ThreadSpecificInstance( |
| 77 ThreadSafeSender* thread_safe_sender, |
| 78 QuotaMessageFilter* quota_message_filter) { |
| 79 if (g_quota_dispatcher_tls.Pointer()->Get()) |
| 80 return g_quota_dispatcher_tls.Pointer()->Get(); |
| 81 |
| 82 QuotaDispatcher* dispatcher = new QuotaDispatcher( |
| 83 thread_safe_sender, quota_message_filter); |
| 84 if (WorkerTaskRunner::Instance()->CurrentWorkerId()) |
| 85 WorkerTaskRunner::Instance()->AddStopObserver(dispatcher); |
| 86 return dispatcher; |
| 87 } |
| 88 |
| 89 void QuotaDispatcher::OnWorkerRunLoopStopped() { |
| 90 delete this; |
| 91 } |
| 92 |
| 93 void QuotaDispatcher::OnMessageReceived(const IPC::Message& msg) { |
| 61 bool handled = true; | 94 bool handled = true; |
| 62 IPC_BEGIN_MESSAGE_MAP(QuotaDispatcher, msg) | 95 IPC_BEGIN_MESSAGE_MAP(QuotaDispatcher, msg) |
| 63 IPC_MESSAGE_HANDLER(QuotaMsg_DidGrantStorageQuota, | 96 IPC_MESSAGE_HANDLER(QuotaMsg_DidGrantStorageQuota, |
| 64 DidGrantStorageQuota) | 97 DidGrantStorageQuota) |
| 65 IPC_MESSAGE_HANDLER(QuotaMsg_DidQueryStorageUsageAndQuota, | 98 IPC_MESSAGE_HANDLER(QuotaMsg_DidQueryStorageUsageAndQuota, |
| 66 DidQueryStorageUsageAndQuota); | 99 DidQueryStorageUsageAndQuota); |
| 67 IPC_MESSAGE_HANDLER(QuotaMsg_DidFail, DidFail); | 100 IPC_MESSAGE_HANDLER(QuotaMsg_DidFail, DidFail); |
| 68 IPC_MESSAGE_UNHANDLED(handled = false) | 101 IPC_MESSAGE_UNHANDLED(handled = false) |
| 69 IPC_END_MESSAGE_MAP() | 102 IPC_END_MESSAGE_MAP() |
| 70 return handled; | 103 DCHECK(handled) << "Unhandled message:" << msg.type(); |
| 71 } | 104 } |
| 72 | 105 |
| 73 void QuotaDispatcher::QueryStorageUsageAndQuota( | 106 void QuotaDispatcher::QueryStorageUsageAndQuota( |
| 74 const GURL& origin_url, | 107 const GURL& origin_url, |
| 75 StorageType type, | 108 StorageType type, |
| 76 Callback* callback) { | 109 Callback* callback) { |
| 77 DCHECK(callback); | 110 DCHECK(callback); |
| 78 int request_id = pending_quota_callbacks_.Add(callback); | 111 int request_id = pending_quota_callbacks_.Add(callback); |
| 79 ChildThread::current()->Send(new QuotaHostMsg_QueryStorageUsageAndQuota( | 112 int thread_id = WorkerTaskRunner::Instance()->CurrentWorkerId(); |
| 113 quota_message_filter_->RegisterRequestID(request_id, thread_id); |
| 114 thread_safe_sender_->Send(new QuotaHostMsg_QueryStorageUsageAndQuota( |
| 80 request_id, origin_url, type)); | 115 request_id, origin_url, type)); |
| 81 } | 116 } |
| 82 | 117 |
| 83 void QuotaDispatcher::RequestStorageQuota( | 118 void QuotaDispatcher::RequestStorageQuota( |
| 84 int render_view_id, | 119 int render_view_id, |
| 85 const GURL& origin_url, | 120 const GURL& origin_url, |
| 86 StorageType type, | 121 StorageType type, |
| 87 int64 requested_size, | 122 int64 requested_size, |
| 88 Callback* callback) { | 123 Callback* callback) { |
| 89 DCHECK(callback); | 124 DCHECK(callback); |
| 90 int request_id = pending_quota_callbacks_.Add(callback); | 125 int request_id = pending_quota_callbacks_.Add(callback); |
| 91 ChildThread::current()->Send(new QuotaHostMsg_RequestStorageQuota( | 126 int thread_id = WorkerTaskRunner::Instance()->CurrentWorkerId(); |
| 127 DCHECK(thread_id == 0); |
| 128 quota_message_filter_->RegisterRequestID(request_id, thread_id); |
| 129 thread_safe_sender_->Send(new QuotaHostMsg_RequestStorageQuota( |
| 92 render_view_id, request_id, origin_url, type, requested_size)); | 130 render_view_id, request_id, origin_url, type, requested_size)); |
| 93 } | 131 } |
| 94 | 132 |
| 95 // static | 133 // static |
| 96 QuotaDispatcher::Callback* | 134 QuotaDispatcher::Callback* |
| 97 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper( | 135 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper( |
| 98 WebKit::WebStorageQuotaCallbacks* callbacks) { | 136 WebKit::WebStorageQuotaCallbacks* callbacks) { |
| 99 return new WebStorageQuotaDispatcherCallback(callbacks); | 137 return new WebStorageQuotaDispatcherCallback(callbacks); |
| 100 } | 138 } |
| 101 | 139 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 131 int(quota::kStorageTypeTemporary), mismatching_enums); | 169 int(quota::kStorageTypeTemporary), mismatching_enums); |
| 132 COMPILE_ASSERT(int(WebKit::WebStorageQuotaTypePersistent) == \ | 170 COMPILE_ASSERT(int(WebKit::WebStorageQuotaTypePersistent) == \ |
| 133 int(quota::kStorageTypePersistent), mismatching_enums); | 171 int(quota::kStorageTypePersistent), mismatching_enums); |
| 134 | 172 |
| 135 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorNotSupported) == \ | 173 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorNotSupported) == \ |
| 136 int(quota::kQuotaErrorNotSupported), mismatching_enums); | 174 int(quota::kQuotaErrorNotSupported), mismatching_enums); |
| 137 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorAbort) == \ | 175 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorAbort) == \ |
| 138 int(quota::kQuotaErrorAbort), mismatching_enums); | 176 int(quota::kQuotaErrorAbort), mismatching_enums); |
| 139 | 177 |
| 140 } // namespace content | 178 } // namespace content |
| OLD | NEW |