| 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 |
| 57 int CurrentWorkerId() { |
| 58 return WorkerTaskRunner::Instance()->CurrentWorkerId(); |
| 59 } |
| 60 |
| 47 } // namespace | 61 } // namespace |
| 48 | 62 |
| 49 QuotaDispatcher::QuotaDispatcher() { | 63 QuotaDispatcher::QuotaDispatcher(ThreadSafeSender* thread_safe_sender, |
| 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); |
| 50 } | 68 } |
| 51 | 69 |
| 52 QuotaDispatcher::~QuotaDispatcher() { | 70 QuotaDispatcher::~QuotaDispatcher() { |
| 53 IDMap<Callback, IDMapOwnPointer>::iterator iter(&pending_quota_callbacks_); | 71 IDMap<Callback, IDMapOwnPointer>::iterator iter(&pending_quota_callbacks_); |
| 54 while (!iter.IsAtEnd()) { | 72 while (!iter.IsAtEnd()) { |
| 55 iter.GetCurrentValue()->DidFail(quota::kQuotaErrorAbort); | 73 iter.GetCurrentValue()->DidFail(quota::kQuotaErrorAbort); |
| 56 iter.Advance(); | 74 iter.Advance(); |
| 57 } | 75 } |
| 76 |
| 77 g_quota_dispatcher_tls.Pointer()->Set(NULL); |
| 58 } | 78 } |
| 59 | 79 |
| 60 bool QuotaDispatcher::OnMessageReceived(const IPC::Message& msg) { | 80 QuotaDispatcher* QuotaDispatcher::ThreadSpecificInstance( |
| 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) { |
| 61 bool handled = true; | 98 bool handled = true; |
| 62 IPC_BEGIN_MESSAGE_MAP(QuotaDispatcher, msg) | 99 IPC_BEGIN_MESSAGE_MAP(QuotaDispatcher, msg) |
| 63 IPC_MESSAGE_HANDLER(QuotaMsg_DidGrantStorageQuota, | 100 IPC_MESSAGE_HANDLER(QuotaMsg_DidGrantStorageQuota, |
| 64 DidGrantStorageQuota) | 101 DidGrantStorageQuota) |
| 65 IPC_MESSAGE_HANDLER(QuotaMsg_DidQueryStorageUsageAndQuota, | 102 IPC_MESSAGE_HANDLER(QuotaMsg_DidQueryStorageUsageAndQuota, |
| 66 DidQueryStorageUsageAndQuota); | 103 DidQueryStorageUsageAndQuota); |
| 67 IPC_MESSAGE_HANDLER(QuotaMsg_DidFail, DidFail); | 104 IPC_MESSAGE_HANDLER(QuotaMsg_DidFail, DidFail); |
| 68 IPC_MESSAGE_UNHANDLED(handled = false) | 105 IPC_MESSAGE_UNHANDLED(handled = false) |
| 69 IPC_END_MESSAGE_MAP() | 106 IPC_END_MESSAGE_MAP() |
| 70 return handled; | 107 DCHECK(handled) << "Unhandled message:" << msg.type(); |
| 71 } | 108 } |
| 72 | 109 |
| 73 void QuotaDispatcher::QueryStorageUsageAndQuota( | 110 void QuotaDispatcher::QueryStorageUsageAndQuota( |
| 74 const GURL& origin_url, | 111 const GURL& origin_url, |
| 75 StorageType type, | 112 StorageType type, |
| 76 Callback* callback) { | 113 Callback* callback) { |
| 77 DCHECK(callback); | 114 DCHECK(callback); |
| 78 int request_id = pending_quota_callbacks_.Add(callback); | 115 int request_id = pending_quota_callbacks_.Add(callback); |
| 79 ChildThread::current()->Send(new QuotaHostMsg_QueryStorageUsageAndQuota( | 116 quota_message_filter_->RegisterRequestID(request_id, CurrentWorkerId()); |
| 117 thread_safe_sender_->Send(new QuotaHostMsg_QueryStorageUsageAndQuota( |
| 80 request_id, origin_url, type)); | 118 request_id, origin_url, type)); |
| 81 } | 119 } |
| 82 | 120 |
| 83 void QuotaDispatcher::RequestStorageQuota( | 121 void QuotaDispatcher::RequestStorageQuota( |
| 84 int render_view_id, | 122 int render_view_id, |
| 85 const GURL& origin_url, | 123 const GURL& origin_url, |
| 86 StorageType type, | 124 StorageType type, |
| 87 int64 requested_size, | 125 int64 requested_size, |
| 88 Callback* callback) { | 126 Callback* callback) { |
| 89 DCHECK(callback); | 127 DCHECK(callback); |
| 128 DCHECK(CurrentWorkerId() == 0); |
| 90 int request_id = pending_quota_callbacks_.Add(callback); | 129 int request_id = pending_quota_callbacks_.Add(callback); |
| 91 ChildThread::current()->Send(new QuotaHostMsg_RequestStorageQuota( | 130 quota_message_filter_->RegisterRequestID(request_id, CurrentWorkerId()); |
| 131 thread_safe_sender_->Send(new QuotaHostMsg_RequestStorageQuota( |
| 92 render_view_id, request_id, origin_url, type, requested_size)); | 132 render_view_id, request_id, origin_url, type, requested_size)); |
| 93 } | 133 } |
| 94 | 134 |
| 95 // static | 135 // static |
| 96 QuotaDispatcher::Callback* | 136 QuotaDispatcher::Callback* |
| 97 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper( | 137 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper( |
| 98 WebKit::WebStorageQuotaCallbacks* callbacks) { | 138 WebKit::WebStorageQuotaCallbacks* callbacks) { |
| 99 return new WebStorageQuotaDispatcherCallback(callbacks); | 139 return new WebStorageQuotaDispatcherCallback(callbacks); |
| 100 } | 140 } |
| 101 | 141 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 131 int(quota::kStorageTypeTemporary), mismatching_enums); | 171 int(quota::kStorageTypeTemporary), mismatching_enums); |
| 132 COMPILE_ASSERT(int(WebKit::WebStorageQuotaTypePersistent) == \ | 172 COMPILE_ASSERT(int(WebKit::WebStorageQuotaTypePersistent) == \ |
| 133 int(quota::kStorageTypePersistent), mismatching_enums); | 173 int(quota::kStorageTypePersistent), mismatching_enums); |
| 134 | 174 |
| 135 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorNotSupported) == \ | 175 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorNotSupported) == \ |
| 136 int(quota::kQuotaErrorNotSupported), mismatching_enums); | 176 int(quota::kQuotaErrorNotSupported), mismatching_enums); |
| 137 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorAbort) == \ | 177 COMPILE_ASSERT(int(WebKit::WebStorageQuotaErrorAbort) == \ |
| 138 int(quota::kQuotaErrorAbort), mismatching_enums); | 178 int(quota::kQuotaErrorAbort), mismatching_enums); |
| 139 | 179 |
| 140 } // namespace content | 180 } // namespace content |
| OLD | NEW |