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 "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 ~HandleImpl() override {} | 36 ~HandleImpl() override {} |
37 | 37 |
38 blink::WebServiceWorker* serviceWorker() override { return worker_.get(); } | 38 blink::WebServiceWorker* serviceWorker() override { return worker_.get(); } |
39 | 39 |
40 private: | 40 private: |
41 scoped_refptr<WebServiceWorkerImpl> worker_; | 41 scoped_refptr<WebServiceWorkerImpl> worker_; |
42 | 42 |
43 DISALLOW_COPY_AND_ASSIGN(HandleImpl); | 43 DISALLOW_COPY_AND_ASSIGN(HandleImpl); |
44 }; | 44 }; |
45 | 45 |
46 void SendPostMessageToWorkerOnMainThread( | |
47 ThreadSafeSender* thread_safe_sender, | |
48 int handle_id, | |
49 int provider_id, | |
50 const base::string16& message, | |
51 const url::Origin& source_origin, | |
52 std::unique_ptr<WebMessagePortChannelArray> channels) { | |
53 thread_safe_sender->Send(new ServiceWorkerHostMsg_PostMessageToWorker( | |
54 handle_id, provider_id, message, source_origin, | |
55 WebMessagePortChannelImpl::ExtractMessagePortIDs(std::move(channels)))); | |
56 } | |
57 | |
58 } // namespace | 46 } // namespace |
59 | 47 |
60 WebServiceWorkerImpl::WebServiceWorkerImpl( | 48 WebServiceWorkerImpl::WebServiceWorkerImpl( |
61 std::unique_ptr<ServiceWorkerHandleReference> handle_ref, | 49 std::unique_ptr<ServiceWorkerHandleReference> handle_ref, |
62 ThreadSafeSender* thread_safe_sender) | 50 ThreadSafeSender* thread_safe_sender) |
63 : handle_ref_(std::move(handle_ref)), | 51 : handle_ref_(std::move(handle_ref)), |
64 state_(handle_ref_->state()), | 52 state_(handle_ref_->state()), |
65 thread_safe_sender_(thread_safe_sender), | 53 thread_safe_sender_(thread_safe_sender), |
66 proxy_(nullptr) { | 54 proxy_(nullptr) { |
67 DCHECK_NE(kInvalidServiceWorkerHandleId, handle_ref_->handle_id()); | 55 DCHECK_NE(kInvalidServiceWorkerHandleId, handle_ref_->handle_id()); |
(...skipping 26 matching lines...) Expand all Loading... |
94 } | 82 } |
95 | 83 |
96 blink::WebServiceWorkerState WebServiceWorkerImpl::state() const { | 84 blink::WebServiceWorkerState WebServiceWorkerImpl::state() const { |
97 return state_; | 85 return state_; |
98 } | 86 } |
99 | 87 |
100 void WebServiceWorkerImpl::postMessage( | 88 void WebServiceWorkerImpl::postMessage( |
101 blink::WebServiceWorkerProvider* provider, | 89 blink::WebServiceWorkerProvider* provider, |
102 const WebString& message, | 90 const WebString& message, |
103 const WebSecurityOrigin& source_origin, | 91 const WebSecurityOrigin& source_origin, |
104 WebMessagePortChannelArray* channels) { | 92 WebMessagePortChannelArray channels) { |
105 WebServiceWorkerProviderImpl* provider_impl = | 93 thread_safe_sender_->Send( |
106 static_cast<WebServiceWorkerProviderImpl*>(provider); | 94 new ServiceWorkerHostMsg_PostMessageToWorker( |
107 ServiceWorkerDispatcher* dispatcher = | 95 handle_ref_->handle_id(), |
108 ServiceWorkerDispatcher::GetThreadSpecificInstance(); | 96 static_cast<WebServiceWorkerProviderImpl*>(provider)->provider_id(), |
109 DCHECK(dispatcher); | 97 message.utf16(), |
110 | 98 url::Origin(source_origin), |
111 // This may send channels for MessagePorts, and all internal book-keeping | 99 WebMessagePortChannelImpl::ExtractMessagePorts(std::move(channels)))); |
112 // messages for MessagePort (e.g. QueueMessages) are sent from main thread | |
113 // (with thread hopping), so we need to do the same thread hopping here not | |
114 // to overtake those messages. | |
115 dispatcher->main_thread_task_runner()->PostTask( | |
116 FROM_HERE, | |
117 base::Bind(&SendPostMessageToWorkerOnMainThread, | |
118 base::RetainedRef(thread_safe_sender_), | |
119 handle_ref_->handle_id(), provider_impl->provider_id(), | |
120 // We convert WebString to string16 before crossing | |
121 // threads for thread-safety. | |
122 message.utf16(), url::Origin(source_origin), | |
123 base::Passed(base::WrapUnique(channels)))); | |
124 } | 100 } |
125 | 101 |
126 void WebServiceWorkerImpl::terminate() { | 102 void WebServiceWorkerImpl::terminate() { |
127 thread_safe_sender_->Send( | 103 thread_safe_sender_->Send( |
128 new ServiceWorkerHostMsg_TerminateWorker(handle_ref_->handle_id())); | 104 new ServiceWorkerHostMsg_TerminateWorker(handle_ref_->handle_id())); |
129 } | 105 } |
130 | 106 |
131 // static | 107 // static |
132 std::unique_ptr<blink::WebServiceWorker::Handle> | 108 std::unique_ptr<blink::WebServiceWorker::Handle> |
133 WebServiceWorkerImpl::CreateHandle( | 109 WebServiceWorkerImpl::CreateHandle( |
134 const scoped_refptr<WebServiceWorkerImpl>& worker) { | 110 const scoped_refptr<WebServiceWorkerImpl>& worker) { |
135 if (!worker) | 111 if (!worker) |
136 return nullptr; | 112 return nullptr; |
137 return base::MakeUnique<HandleImpl>(worker); | 113 return base::MakeUnique<HandleImpl>(worker); |
138 } | 114 } |
139 | 115 |
140 WebServiceWorkerImpl::~WebServiceWorkerImpl() { | 116 WebServiceWorkerImpl::~WebServiceWorkerImpl() { |
141 ServiceWorkerDispatcher* dispatcher = | 117 ServiceWorkerDispatcher* dispatcher = |
142 ServiceWorkerDispatcher::GetThreadSpecificInstance(); | 118 ServiceWorkerDispatcher::GetThreadSpecificInstance(); |
143 if (dispatcher) | 119 if (dispatcher) |
144 dispatcher->RemoveServiceWorker(handle_ref_->handle_id()); | 120 dispatcher->RemoveServiceWorker(handle_ref_->handle_id()); |
145 } | 121 } |
146 | 122 |
147 } // namespace content | 123 } // namespace content |
OLD | NEW |