• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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.
4 
5 #include "content/renderer/websharedworker_proxy.h"
6 
7 #include "content/child/child_thread.h"
8 #include "content/child/webmessageportchannel_impl.h"
9 #include "content/common/view_messages.h"
10 #include "content/common/worker_messages.h"
11 #include "third_party/WebKit/public/platform/WebURL.h"
12 #include "third_party/WebKit/public/web/WebSharedWorkerClient.h"
13 
14 namespace content {
15 
WebSharedWorkerProxy(ChildThread * child_thread,unsigned long long document_id,bool exists,int route_id,int render_view_route_id)16 WebSharedWorkerProxy::WebSharedWorkerProxy(ChildThread* child_thread,
17                                            unsigned long long document_id,
18                                            bool exists,
19                                            int route_id,
20                                            int render_view_route_id)
21     : route_id_(exists ? route_id : MSG_ROUTING_NONE),
22       render_view_route_id_(render_view_route_id),
23       child_thread_(child_thread),
24       document_id_(document_id),
25       pending_route_id_(route_id),
26       connect_listener_(NULL) {
27   if (route_id_ != MSG_ROUTING_NONE)
28     child_thread_->AddRoute(route_id_, this);
29 }
30 
~WebSharedWorkerProxy()31 WebSharedWorkerProxy::~WebSharedWorkerProxy() {
32   Disconnect();
33 
34   // Free up any unsent queued messages.
35   for (size_t i = 0; i < queued_messages_.size(); ++i)
36     delete queued_messages_[i];
37 }
38 
Disconnect()39 void WebSharedWorkerProxy::Disconnect() {
40   if (route_id_ == MSG_ROUTING_NONE)
41     return;
42 
43   // So the messages from WorkerContext (like WorkerContextDestroyed) do not
44   // come after nobody is listening. Since Worker and WorkerContext can
45   // terminate independently, already sent messages may still be in the pipe.
46   child_thread_->RemoveRoute(route_id_);
47 
48   route_id_ = MSG_ROUTING_NONE;
49 }
50 
CreateWorkerContext(const GURL & script_url,bool is_shared,const base::string16 & name,const base::string16 & user_agent,const base::string16 & source_code,const base::string16 & content_security_policy,blink::WebContentSecurityPolicyType policy_type,int pending_route_id,int64 script_resource_appcache_id)51 void WebSharedWorkerProxy::CreateWorkerContext(
52     const GURL& script_url,
53     bool is_shared,
54     const base::string16& name,
55     const base::string16& user_agent,
56     const base::string16& source_code,
57     const base::string16& content_security_policy,
58     blink::WebContentSecurityPolicyType policy_type,
59     int pending_route_id,
60     int64 script_resource_appcache_id) {
61   DCHECK(route_id_ == MSG_ROUTING_NONE);
62   ViewHostMsg_CreateWorker_Params params;
63   params.url = script_url;
64   params.name = name;
65   params.document_id = document_id_;
66   params.render_view_route_id = render_view_route_id_;
67   params.route_id = pending_route_id;
68   params.script_resource_appcache_id = script_resource_appcache_id;
69   IPC::Message* create_message = new ViewHostMsg_CreateWorker(
70       params, &route_id_);
71   child_thread_->Send(create_message);
72   if (route_id_ == MSG_ROUTING_NONE)
73     return;
74 
75   child_thread_->AddRoute(route_id_, this);
76 
77   // We make sure that the start message is the first, since postMessage or
78   // connect might have already been called.
79   queued_messages_.insert(queued_messages_.begin(),
80       new WorkerMsg_StartWorkerContext(
81           route_id_, script_url, user_agent, source_code,
82           content_security_policy, policy_type));
83 }
84 
IsStarted()85 bool WebSharedWorkerProxy::IsStarted() {
86   // Worker is started if we have a route ID and there are no queued messages
87   // (meaning we've sent the WorkerMsg_StartWorkerContext already).
88   return (route_id_ != MSG_ROUTING_NONE && queued_messages_.empty());
89 }
90 
Send(IPC::Message * message)91 bool WebSharedWorkerProxy::Send(IPC::Message* message) {
92   // It's possible that messages will be sent before the worker is created, in
93   // which case route_id_ will be none.  Or the worker object can be interacted
94   // with before the browser process told us that it started, in which case we
95   // also want to queue the message.
96   if (!IsStarted()) {
97     queued_messages_.push_back(message);
98     return true;
99   }
100 
101   // For now we proxy all messages to the worker process through the browser.
102   // Revisit if we find this slow.
103   // TODO(jabdelmalek): handle sync messages if we need them.
104   IPC::Message* wrapped_msg = new ViewHostMsg_ForwardToWorker(*message);
105   delete message;
106   return child_thread_->Send(wrapped_msg);
107 }
108 
SendQueuedMessages()109 void WebSharedWorkerProxy::SendQueuedMessages() {
110   DCHECK(queued_messages_.size());
111   std::vector<IPC::Message*> queued_messages = queued_messages_;
112   queued_messages_.clear();
113   for (size_t i = 0; i < queued_messages.size(); ++i) {
114     queued_messages[i]->set_routing_id(route_id_);
115     Send(queued_messages[i]);
116   }
117 }
118 
isStarted()119 bool WebSharedWorkerProxy::isStarted() {
120   return IsStarted();
121 }
122 
startWorkerContext(const blink::WebURL & script_url,const blink::WebString & name,const blink::WebString & user_agent,const blink::WebString & source_code,const blink::WebString & content_security_policy,blink::WebContentSecurityPolicyType policy_type,long long script_resource_appcache_id)123 void WebSharedWorkerProxy::startWorkerContext(
124     const blink::WebURL& script_url,
125     const blink::WebString& name,
126     const blink::WebString& user_agent,
127     const blink::WebString& source_code,
128     const blink::WebString& content_security_policy,
129     blink::WebContentSecurityPolicyType policy_type,
130     long long script_resource_appcache_id) {
131   DCHECK(!isStarted());
132   CreateWorkerContext(
133       script_url, true, name, user_agent, source_code, content_security_policy,
134       policy_type, pending_route_id_, script_resource_appcache_id);
135 }
136 
connect(blink::WebMessagePortChannel * channel,ConnectListener * listener)137 void WebSharedWorkerProxy::connect(blink::WebMessagePortChannel* channel,
138                                    ConnectListener* listener) {
139   WebMessagePortChannelImpl* webchannel =
140         static_cast<WebMessagePortChannelImpl*>(channel);
141 
142   int message_port_id = webchannel->message_port_id();
143   DCHECK(message_port_id != MSG_ROUTING_NONE);
144   webchannel->QueueMessages();
145 
146   Send(new WorkerMsg_Connect(route_id_, message_port_id, MSG_ROUTING_NONE));
147   if (HasQueuedMessages()) {
148     connect_listener_ = listener;
149   } else {
150     listener->connected();
151     // The listener may free this object, so do not access the object after
152     // this point.
153   }
154 }
155 
OnMessageReceived(const IPC::Message & message)156 bool WebSharedWorkerProxy::OnMessageReceived(const IPC::Message& message) {
157   bool handled = true;
158   IPC_BEGIN_MESSAGE_MAP(WebSharedWorkerProxy, message)
159     IPC_MESSAGE_HANDLER(ViewMsg_WorkerCreated, OnWorkerCreated)
160     IPC_MESSAGE_UNHANDLED(handled = false)
161   IPC_END_MESSAGE_MAP()
162   return handled;
163 }
164 
OnWorkerCreated()165 void WebSharedWorkerProxy::OnWorkerCreated() {
166   // The worker is created - now send off the CreateWorkerContext message and
167   // any other queued messages
168   SendQueuedMessages();
169 
170   // Inform any listener that the pending connect event has been sent
171   // (this can result in this object being freed).
172   if (connect_listener_) {
173     connect_listener_->connected();
174   }
175 }
176 
177 }  // namespace content
178