[Android] Implement 3-way sensor fallback for Device Orientation.
[chromium-blink-merge.git] / content / child / service_worker / web_service_worker_impl.cc
blobf856cf82aedb3980fa9d2fd027f7c8ae629c2acd
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;
61 proxy_->dispatchStateChangeEvent();
64 void WebServiceWorkerImpl::setProxy(blink::WebServiceWorkerProxy* proxy) {
65 proxy_ = proxy;
68 blink::WebServiceWorkerProxy* WebServiceWorkerImpl::proxy() {
69 return proxy_;
72 blink::WebURL WebServiceWorkerImpl::url() const {
73 return handle_ref_->url();
76 blink::WebServiceWorkerState WebServiceWorkerImpl::state() const {
77 return state_;
80 void WebServiceWorkerImpl::postMessage(const WebString& message,
81 WebMessagePortChannelArray* channels) {
82 ServiceWorkerDispatcher* dispatcher =
83 ServiceWorkerDispatcher::GetThreadSpecificInstance();
84 DCHECK(dispatcher);
86 // This may send channels for MessagePorts, and all internal book-keeping
87 // messages for MessagePort (e.g. QueueMessages) are sent from main thread
88 // (with thread hopping), so we need to do the same thread hopping here not
89 // to overtake those messages.
90 dispatcher->main_thread_task_runner()->PostTask(
91 FROM_HERE, base::Bind(&SendPostMessageToWorkerOnMainThread,
92 thread_safe_sender_, handle_ref_->handle_id(),
93 // We cast WebString to string16 before crossing
94 // threads for thread-safety.
95 static_cast<base::string16>(message),
96 base::Passed(make_scoped_ptr(channels))));
99 void WebServiceWorkerImpl::terminate() {
100 thread_safe_sender_->Send(
101 new ServiceWorkerHostMsg_TerminateWorker(handle_ref_->handle_id()));
104 } // namespace content