Roll src/third_party/WebKit eac3800:0237a66 (svn 202606:202607)
[chromium-blink-merge.git] / content / child / service_worker / web_service_worker_impl.cc
blobf7e111c2f342d9aae1772e1c4aa39072b08d727d
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
3 // found in the LICENSE file.
5 #include "content/child/service_worker/web_service_worker_impl.h"
7 #include "content/child/service_worker/service_worker_dispatcher.h"
8 #include "content/child/service_worker/service_worker_handle_reference.h"
9 #include "content/child/thread_safe_sender.h"
10 #include "content/child/webmessageportchannel_impl.h"
11 #include "content/common/service_worker/service_worker_messages.h"
12 #include "third_party/WebKit/public/platform/WebString.h"
13 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWorkerProxy.h"
15 using blink::WebMessagePortChannel;
16 using blink::WebMessagePortChannelArray;
17 using blink::WebMessagePortChannelClient;
18 using blink::WebString;
20 namespace content {
22 namespace {
24 void SendPostMessageToWorkerOnMainThread(
25 ThreadSafeSender* thread_safe_sender,
26 int handle_id,
27 const base::string16& message,
28 scoped_ptr<WebMessagePortChannelArray> channels) {
29 thread_safe_sender->Send(new ServiceWorkerHostMsg_PostMessageToWorker(
30 handle_id, message,
31 WebMessagePortChannelImpl::ExtractMessagePortIDs(channels.Pass())));
34 } // namespace
36 WebServiceWorkerImpl::WebServiceWorkerImpl(
37 scoped_ptr<ServiceWorkerHandleReference> handle_ref,
38 ThreadSafeSender* thread_safe_sender)
39 : handle_ref_(handle_ref.Pass()),
40 state_(handle_ref_->state()),
41 thread_safe_sender_(thread_safe_sender),
42 proxy_(NULL) {
43 ServiceWorkerDispatcher* dispatcher =
44 ServiceWorkerDispatcher::GetThreadSpecificInstance();
45 DCHECK(dispatcher);
46 dispatcher->AddServiceWorker(handle_ref_->handle_id(), this);
49 WebServiceWorkerImpl::~WebServiceWorkerImpl() {
50 if (handle_ref_->handle_id() == kInvalidServiceWorkerHandleId)
51 return;
52 ServiceWorkerDispatcher* dispatcher =
53 ServiceWorkerDispatcher::GetThreadSpecificInstance();
54 if (dispatcher)
55 dispatcher->RemoveServiceWorker(handle_ref_->handle_id());
58 void WebServiceWorkerImpl::OnStateChanged(
59 blink::WebServiceWorkerState new_state) {
60 state_ = new_state;
62 // TODO(nhiroki): This is a quick fix for http://crbug.com/507110
63 DCHECK(proxy_);
64 if (proxy_)
65 proxy_->dispatchStateChangeEvent();
68 void WebServiceWorkerImpl::setProxy(blink::WebServiceWorkerProxy* proxy) {
69 proxy_ = proxy;
72 blink::WebServiceWorkerProxy* WebServiceWorkerImpl::proxy() {
73 return proxy_;
76 blink::WebURL WebServiceWorkerImpl::url() const {
77 return handle_ref_->url();
80 blink::WebServiceWorkerState WebServiceWorkerImpl::state() const {
81 return state_;
84 void WebServiceWorkerImpl::postMessage(const WebString& message,
85 WebMessagePortChannelArray* channels) {
86 ServiceWorkerDispatcher* dispatcher =
87 ServiceWorkerDispatcher::GetThreadSpecificInstance();
88 DCHECK(dispatcher);
90 // This may send channels for MessagePorts, and all internal book-keeping
91 // messages for MessagePort (e.g. QueueMessages) are sent from main thread
92 // (with thread hopping), so we need to do the same thread hopping here not
93 // to overtake those messages.
94 dispatcher->main_thread_task_runner()->PostTask(
95 FROM_HERE, base::Bind(&SendPostMessageToWorkerOnMainThread,
96 thread_safe_sender_, handle_ref_->handle_id(),
97 // We cast WebString to string16 before crossing
98 // threads for thread-safety.
99 static_cast<base::string16>(message),
100 base::Passed(make_scoped_ptr(channels))));
103 void WebServiceWorkerImpl::terminate() {
104 thread_safe_sender_->Send(
105 new ServiceWorkerHostMsg_TerminateWorker(handle_ref_->handle_id()));
108 } // namespace content