| 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 "content/child/service_worker/service_worker_dispatcher.h" | 7 #include "content/child/service_worker/service_worker_dispatcher.h" |
| 8 #include "content/child/service_worker/service_worker_handle_reference.h" |
| 8 #include "content/child/thread_safe_sender.h" | 9 #include "content/child/thread_safe_sender.h" |
| 9 #include "content/child/webmessageportchannel_impl.h" | 10 #include "content/child/webmessageportchannel_impl.h" |
| 10 #include "content/common/service_worker/service_worker_messages.h" | 11 #include "content/common/service_worker/service_worker_messages.h" |
| 11 #include "third_party/WebKit/public/platform/WebServiceWorkerProxy.h" | 12 #include "third_party/WebKit/public/platform/WebServiceWorkerProxy.h" |
| 12 #include "third_party/WebKit/public/platform/WebString.h" | 13 #include "third_party/WebKit/public/platform/WebString.h" |
| 13 | 14 |
| 14 using blink::WebMessagePortChannel; | 15 using blink::WebMessagePortChannel; |
| 15 using blink::WebMessagePortChannelArray; | 16 using blink::WebMessagePortChannelArray; |
| 16 using blink::WebMessagePortChannelClient; | 17 using blink::WebMessagePortChannelClient; |
| 17 using blink::WebString; | 18 using blink::WebString; |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 | 21 |
| 21 WebServiceWorkerImpl::WebServiceWorkerImpl( | 22 WebServiceWorkerImpl::WebServiceWorkerImpl( |
| 22 const ServiceWorkerObjectInfo& info, | 23 const ServiceWorkerObjectInfo& info, |
| 23 ThreadSafeSender* thread_safe_sender) | 24 ThreadSafeSender* thread_safe_sender) |
| 24 : handle_id_(info.handle_id), | 25 : handle_ref_( |
| 25 scope_(info.scope), | 26 ServiceWorkerHandleReference::CreateForDeleter(info, |
| 26 url_(info.url), | 27 thread_safe_sender)), |
| 27 state_(info.state), | 28 state_(handle_ref_->state()), |
| 28 thread_safe_sender_(thread_safe_sender), | 29 thread_safe_sender_(thread_safe_sender), |
| 29 proxy_(NULL) { | 30 proxy_(NULL) { |
| 30 ServiceWorkerDispatcher* dispatcher = | 31 ServiceWorkerDispatcher* dispatcher = |
| 31 ServiceWorkerDispatcher::GetThreadSpecificInstance(); | 32 ServiceWorkerDispatcher::GetThreadSpecificInstance(); |
| 32 DCHECK(dispatcher); | 33 DCHECK(dispatcher); |
| 33 dispatcher->AddServiceWorker(handle_id_, this); | 34 dispatcher->AddServiceWorker(handle_ref_->handle_id(), this); |
| 35 } |
| 36 |
| 37 WebServiceWorkerImpl::WebServiceWorkerImpl( |
| 38 scoped_ptr<ServiceWorkerHandleReference> handle_ref, |
| 39 ThreadSafeSender* thread_safe_sender) |
| 40 : handle_ref_(handle_ref.Pass()), |
| 41 state_(handle_ref_->state()), |
| 42 thread_safe_sender_(thread_safe_sender), |
| 43 proxy_(NULL) { |
| 44 ServiceWorkerDispatcher* dispatcher = |
| 45 ServiceWorkerDispatcher::GetThreadSpecificInstance(); |
| 46 DCHECK(dispatcher); |
| 47 dispatcher->AddServiceWorker(handle_ref_->handle_id(), this); |
| 34 } | 48 } |
| 35 | 49 |
| 36 WebServiceWorkerImpl::~WebServiceWorkerImpl() { | 50 WebServiceWorkerImpl::~WebServiceWorkerImpl() { |
| 37 if (handle_id_ == kInvalidServiceWorkerHandleId) | 51 if (handle_ref_->handle_id() == kInvalidServiceWorkerHandleId) |
| 38 return; | 52 return; |
| 39 thread_safe_sender_->Send( | |
| 40 new ServiceWorkerHostMsg_ServiceWorkerObjectDestroyed(handle_id_)); | |
| 41 ServiceWorkerDispatcher* dispatcher = | 53 ServiceWorkerDispatcher* dispatcher = |
| 42 ServiceWorkerDispatcher::GetThreadSpecificInstance(); | 54 ServiceWorkerDispatcher::GetThreadSpecificInstance(); |
| 43 if (dispatcher) | 55 if (dispatcher) |
| 44 dispatcher->RemoveServiceWorker(handle_id_); | 56 dispatcher->RemoveServiceWorker(handle_ref_->handle_id()); |
| 45 } | 57 } |
| 46 | 58 |
| 47 void WebServiceWorkerImpl::OnStateChanged( | 59 void WebServiceWorkerImpl::OnStateChanged( |
| 48 blink::WebServiceWorkerState new_state) { | 60 blink::WebServiceWorkerState new_state) { |
| 49 DCHECK(proxy_); | 61 DCHECK(proxy_); |
| 50 if (proxy_->isReady()) | 62 if (proxy_->isReady()) |
| 51 ChangeState(new_state); | 63 ChangeState(new_state); |
| 52 else | 64 else |
| 53 queued_states_.push_back(new_state); | 65 queued_states_.push_back(new_state); |
| 54 } | 66 } |
| 55 | 67 |
| 56 void WebServiceWorkerImpl::setProxy(blink::WebServiceWorkerProxy* proxy) { | 68 void WebServiceWorkerImpl::setProxy(blink::WebServiceWorkerProxy* proxy) { |
| 57 proxy_ = proxy; | 69 proxy_ = proxy; |
| 58 } | 70 } |
| 59 | 71 |
| 60 void WebServiceWorkerImpl::proxyReadyChanged() { | 72 void WebServiceWorkerImpl::proxyReadyChanged() { |
| 61 if (!proxy_->isReady()) | 73 if (!proxy_->isReady()) |
| 62 return; | 74 return; |
| 63 for (std::vector<blink::WebServiceWorkerState>::iterator it = | 75 for (std::vector<blink::WebServiceWorkerState>::iterator it = |
| 64 queued_states_.begin(); | 76 queued_states_.begin(); |
| 65 it != queued_states_.end(); | 77 it != queued_states_.end(); |
| 66 ++it) { | 78 ++it) { |
| 67 ChangeState(*it); | 79 ChangeState(*it); |
| 68 } | 80 } |
| 69 queued_states_.clear(); | 81 queued_states_.clear(); |
| 70 } | 82 } |
| 71 | 83 |
| 72 blink::WebURL WebServiceWorkerImpl::scope() const { | 84 blink::WebURL WebServiceWorkerImpl::scope() const { |
| 73 return scope_; | 85 return handle_ref_->scope(); |
| 74 } | 86 } |
| 75 | 87 |
| 76 blink::WebURL WebServiceWorkerImpl::url() const { | 88 blink::WebURL WebServiceWorkerImpl::url() const { |
| 77 return url_; | 89 return handle_ref_->url(); |
| 78 } | 90 } |
| 79 | 91 |
| 80 blink::WebServiceWorkerState WebServiceWorkerImpl::state() const { | 92 blink::WebServiceWorkerState WebServiceWorkerImpl::state() const { |
| 81 return state_; | 93 return state_; |
| 82 } | 94 } |
| 83 | 95 |
| 84 void WebServiceWorkerImpl::postMessage(const WebString& message, | 96 void WebServiceWorkerImpl::postMessage(const WebString& message, |
| 85 WebMessagePortChannelArray* channels) { | 97 WebMessagePortChannelArray* channels) { |
| 86 thread_safe_sender_->Send(new ServiceWorkerHostMsg_PostMessage( | 98 thread_safe_sender_->Send(new ServiceWorkerHostMsg_PostMessage( |
| 87 handle_id_, | 99 handle_ref_->handle_id(), |
| 88 message, | 100 message, |
| 89 WebMessagePortChannelImpl::ExtractMessagePortIDs(channels))); | 101 WebMessagePortChannelImpl::ExtractMessagePortIDs(channels))); |
| 90 } | 102 } |
| 91 | 103 |
| 92 void WebServiceWorkerImpl::ChangeState(blink::WebServiceWorkerState new_state) { | 104 void WebServiceWorkerImpl::ChangeState(blink::WebServiceWorkerState new_state) { |
| 93 DCHECK(proxy_); | 105 DCHECK(proxy_); |
| 94 DCHECK(proxy_->isReady()); | 106 DCHECK(proxy_->isReady()); |
| 95 state_ = new_state; | 107 state_ = new_state; |
| 96 proxy_->dispatchStateChangeEvent(); | 108 proxy_->dispatchStateChangeEvent(); |
| 97 } | 109 } |
| 98 | 110 |
| 99 } // namespace content | 111 } // namespace content |
| OLD | NEW |