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