| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/service_worker/web_service_worker_impl.h" | 5 #include "content/child/service_worker/web_service_worker_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "content/child/service_worker/service_worker_dispatcher.h" | 10 #include "content/child/service_worker/service_worker_dispatcher.h" |
| 11 #include "content/child/service_worker/service_worker_handle_reference.h" | 11 #include "content/child/service_worker/service_worker_handle_reference.h" |
| 12 #include "content/child/service_worker/web_service_worker_provider_impl.h" |
| 12 #include "content/child/thread_safe_sender.h" | 13 #include "content/child/thread_safe_sender.h" |
| 13 #include "content/child/webmessageportchannel_impl.h" | 14 #include "content/child/webmessageportchannel_impl.h" |
| 14 #include "content/common/service_worker/service_worker_messages.h" | 15 #include "content/common/service_worker/service_worker_messages.h" |
| 16 #include "third_party/WebKit/public/platform/WebSecurityOrigin.h" |
| 15 #include "third_party/WebKit/public/platform/WebString.h" | 17 #include "third_party/WebKit/public/platform/WebString.h" |
| 16 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor
kerProxy.h" | 18 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor
kerProxy.h" |
| 17 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" | 19 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" |
| 18 | 20 |
| 19 using blink::WebMessagePortChannel; | 21 using blink::WebMessagePortChannel; |
| 20 using blink::WebMessagePortChannelArray; | 22 using blink::WebMessagePortChannelArray; |
| 21 using blink::WebMessagePortChannelClient; | 23 using blink::WebMessagePortChannelClient; |
| 22 using blink::WebRuntimeFeatures; | 24 using blink::WebRuntimeFeatures; |
| 25 using blink::WebSecurityOrigin; |
| 23 using blink::WebString; | 26 using blink::WebString; |
| 24 | 27 |
| 25 namespace content { | 28 namespace content { |
| 26 | 29 |
| 27 namespace { | 30 namespace { |
| 28 | 31 |
| 29 class HandleImpl : public blink::WebServiceWorker::Handle { | 32 class HandleImpl : public blink::WebServiceWorker::Handle { |
| 30 public: | 33 public: |
| 31 explicit HandleImpl(const scoped_refptr<WebServiceWorkerImpl>& worker) | 34 explicit HandleImpl(const scoped_refptr<WebServiceWorkerImpl>& worker) |
| 32 : worker_(worker) {} | 35 : worker_(worker) {} |
| 33 ~HandleImpl() override {} | 36 ~HandleImpl() override {} |
| 34 | 37 |
| 35 blink::WebServiceWorker* serviceWorker() override { return worker_.get(); } | 38 blink::WebServiceWorker* serviceWorker() override { return worker_.get(); } |
| 36 | 39 |
| 37 private: | 40 private: |
| 38 scoped_refptr<WebServiceWorkerImpl> worker_; | 41 scoped_refptr<WebServiceWorkerImpl> worker_; |
| 39 | 42 |
| 40 DISALLOW_COPY_AND_ASSIGN(HandleImpl); | 43 DISALLOW_COPY_AND_ASSIGN(HandleImpl); |
| 41 }; | 44 }; |
| 42 | 45 |
| 43 void SendPostMessageToWorkerOnMainThread( | 46 void SendPostMessageToWorkerOnMainThread( |
| 44 ThreadSafeSender* thread_safe_sender, | 47 ThreadSafeSender* thread_safe_sender, |
| 45 int handle_id, | 48 int handle_id, |
| 49 int provider_id, |
| 46 const base::string16& message, | 50 const base::string16& message, |
| 51 const url::Origin& source_origin, |
| 47 scoped_ptr<WebMessagePortChannelArray> channels) { | 52 scoped_ptr<WebMessagePortChannelArray> channels) { |
| 48 if (WebRuntimeFeatures::isServiceWorkerExtendableMessageEventEnabled()) { | 53 if (WebRuntimeFeatures::isServiceWorkerExtendableMessageEventEnabled()) { |
| 49 thread_safe_sender->Send(new ServiceWorkerHostMsg_PostMessageToWorker( | 54 thread_safe_sender->Send(new ServiceWorkerHostMsg_PostMessageToWorker( |
| 50 handle_id, message, | 55 handle_id, provider_id, message, source_origin, |
| 51 WebMessagePortChannelImpl::ExtractMessagePortIDs(std::move(channels)))); | 56 WebMessagePortChannelImpl::ExtractMessagePortIDs(std::move(channels)))); |
| 52 } else { | 57 } else { |
| 53 thread_safe_sender->Send( | 58 thread_safe_sender->Send( |
| 54 new ServiceWorkerHostMsg_DeprecatedPostMessageToWorker( | 59 new ServiceWorkerHostMsg_DeprecatedPostMessageToWorker( |
| 55 handle_id, message, | 60 handle_id, message, |
| 56 WebMessagePortChannelImpl::ExtractMessagePortIDs( | 61 WebMessagePortChannelImpl::ExtractMessagePortIDs( |
| 57 std::move(channels)))); | 62 std::move(channels)))); |
| 58 } | 63 } |
| 59 } | 64 } |
| 60 | 65 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 } | 98 } |
| 94 | 99 |
| 95 blink::WebURL WebServiceWorkerImpl::url() const { | 100 blink::WebURL WebServiceWorkerImpl::url() const { |
| 96 return handle_ref_->url(); | 101 return handle_ref_->url(); |
| 97 } | 102 } |
| 98 | 103 |
| 99 blink::WebServiceWorkerState WebServiceWorkerImpl::state() const { | 104 blink::WebServiceWorkerState WebServiceWorkerImpl::state() const { |
| 100 return state_; | 105 return state_; |
| 101 } | 106 } |
| 102 | 107 |
| 103 void WebServiceWorkerImpl::postMessage(const WebString& message, | 108 void WebServiceWorkerImpl::postMessage( |
| 104 WebMessagePortChannelArray* channels) { | 109 blink::WebServiceWorkerProvider* provider, |
| 110 const WebString& message, |
| 111 const WebSecurityOrigin& source_origin, |
| 112 WebMessagePortChannelArray* channels) { |
| 113 WebServiceWorkerProviderImpl* provider_impl = |
| 114 static_cast<WebServiceWorkerProviderImpl*>(provider); |
| 105 ServiceWorkerDispatcher* dispatcher = | 115 ServiceWorkerDispatcher* dispatcher = |
| 106 ServiceWorkerDispatcher::GetThreadSpecificInstance(); | 116 ServiceWorkerDispatcher::GetThreadSpecificInstance(); |
| 107 DCHECK(dispatcher); | 117 DCHECK(dispatcher); |
| 108 | 118 |
| 109 // This may send channels for MessagePorts, and all internal book-keeping | 119 // This may send channels for MessagePorts, and all internal book-keeping |
| 110 // messages for MessagePort (e.g. QueueMessages) are sent from main thread | 120 // messages for MessagePort (e.g. QueueMessages) are sent from main thread |
| 111 // (with thread hopping), so we need to do the same thread hopping here not | 121 // (with thread hopping), so we need to do the same thread hopping here not |
| 112 // to overtake those messages. | 122 // to overtake those messages. |
| 113 dispatcher->main_thread_task_runner()->PostTask( | 123 dispatcher->main_thread_task_runner()->PostTask( |
| 114 FROM_HERE, base::Bind(&SendPostMessageToWorkerOnMainThread, | 124 FROM_HERE, |
| 115 thread_safe_sender_, handle_ref_->handle_id(), | 125 base::Bind(&SendPostMessageToWorkerOnMainThread, thread_safe_sender_, |
| 116 // We cast WebString to string16 before crossing | 126 handle_ref_->handle_id(), provider_impl->provider_id(), |
| 117 // threads for thread-safety. | 127 // We cast WebString to string16 before crossing |
| 118 static_cast<base::string16>(message), | 128 // threads for thread-safety. |
| 119 base::Passed(make_scoped_ptr(channels)))); | 129 static_cast<base::string16>(message), |
| 130 url::Origin(source_origin), |
| 131 base::Passed(make_scoped_ptr(channels)))); |
| 120 } | 132 } |
| 121 | 133 |
| 122 void WebServiceWorkerImpl::terminate() { | 134 void WebServiceWorkerImpl::terminate() { |
| 123 thread_safe_sender_->Send( | 135 thread_safe_sender_->Send( |
| 124 new ServiceWorkerHostMsg_TerminateWorker(handle_ref_->handle_id())); | 136 new ServiceWorkerHostMsg_TerminateWorker(handle_ref_->handle_id())); |
| 125 } | 137 } |
| 126 | 138 |
| 127 // static | 139 // static |
| 128 blink::WebPassOwnPtr<blink::WebServiceWorker::Handle> | 140 blink::WebPassOwnPtr<blink::WebServiceWorker::Handle> |
| 129 WebServiceWorkerImpl::CreateHandle( | 141 WebServiceWorkerImpl::CreateHandle( |
| 130 const scoped_refptr<WebServiceWorkerImpl>& worker) { | 142 const scoped_refptr<WebServiceWorkerImpl>& worker) { |
| 131 if (!worker) | 143 if (!worker) |
| 132 return nullptr; | 144 return nullptr; |
| 133 return blink::adoptWebPtr(new HandleImpl(worker)); | 145 return blink::adoptWebPtr(new HandleImpl(worker)); |
| 134 } | 146 } |
| 135 | 147 |
| 136 WebServiceWorkerImpl::~WebServiceWorkerImpl() { | 148 WebServiceWorkerImpl::~WebServiceWorkerImpl() { |
| 137 ServiceWorkerDispatcher* dispatcher = | 149 ServiceWorkerDispatcher* dispatcher = |
| 138 ServiceWorkerDispatcher::GetThreadSpecificInstance(); | 150 ServiceWorkerDispatcher::GetThreadSpecificInstance(); |
| 139 if (dispatcher) | 151 if (dispatcher) |
| 140 dispatcher->RemoveServiceWorker(handle_ref_->handle_id()); | 152 dispatcher->RemoveServiceWorker(handle_ref_->handle_id()); |
| 141 } | 153 } |
| 142 | 154 |
| 143 } // namespace content | 155 } // namespace content |
| OLD | NEW |