Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(58)

Side by Side Diff: chrome/renderer/webworker_base.cc

Issue 340036: Initial pass of shared workers renderer-side code (Closed)
Patch Set: Changes reflecting review feedback Created 11 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2009 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 "chrome/renderer/webworker_base.h"
6
7 #include "chrome/common/child_thread.h"
8 #include "chrome/common/render_messages.h"
9 #include "chrome/common/webmessageportchannel_impl.h"
10 #include "chrome/common/worker_messages.h"
11 #include "webkit/api/public/WebURL.h"
12 #include "webkit/api/public/WebWorkerClient.h"
13
14 using WebKit::WebMessagePortChannel;
15 using WebKit::WebMessagePortChannelArray;
16 using WebKit::WebString;
17 using WebKit::WebURL;
18 using WebKit::WebWorkerClient;
19
20 WebWorkerBase::WebWorkerBase(
21 ChildThread* child_thread,
22 int route_id,
23 int render_view_route_id)
24 : route_id_(route_id),
25 render_view_route_id_(render_view_route_id),
26 child_thread_(child_thread) {
27 if (route_id_ != MSG_ROUTING_NONE)
28 child_thread_->AddRoute(route_id_, this);
29 }
30
31 WebWorkerBase::~WebWorkerBase() {
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
39 void WebWorkerBase::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
51 void WebWorkerBase::CreateWorkerContext(IPC::Message* create_message,
52 const GURL& script_url,
53 const string16& user_agent,
54 const string16& source_code) {
55 DCHECK(route_id_ == MSG_ROUTING_NONE);
56 // create_message is a sync message that sets route_id_
57 child_thread_->Send(create_message);
58 if (route_id_ == MSG_ROUTING_NONE)
59 return;
60
61 child_thread_->AddRoute(route_id_, this);
62
63 // We make sure that the start message is the first, since postMessage or
64 // connect might have already been called.
65 queued_messages_.insert(queued_messages_.begin(),
66 new WorkerMsg_StartWorkerContext(
67 route_id_, script_url, user_agent, source_code));
68 }
69
70 bool WebWorkerBase::IsStarted() {
71 // Worker is started if we have a route ID and there are no queued messages
72 // (meaning we've sent the WorkerMsg_StartWorkerContext already).
73 return (route_id_ != MSG_ROUTING_NONE && queued_messages_.empty());
74 }
75
76 bool WebWorkerBase::Send(IPC::Message* message) {
77 // It's possible that messages will be sent before the worker is created, in
78 // which case route_id_ will be none. Or the worker object can be interacted
79 // with before the browser process told us that it started, in which case we
80 // also want to queue the message.
81 if (!IsStarted()) {
82 queued_messages_.push_back(message);
83 return true;
84 }
85
86 // For now we proxy all messages to the worker process through the browser.
87 // Revisit if we find this slow.
88 // TODO(jabdelmalek): handle sync messages if we need them.
89 IPC::Message* wrapped_msg = new ViewHostMsg_ForwardToWorker(*message);
90 delete message;
91 return child_thread_->Send(wrapped_msg);
92 }
93
94 void WebWorkerBase::SendQueuedMessages() {
95 DCHECK(queued_messages_.size());
96 std::vector<IPC::Message*> queued_messages = queued_messages_;
97 queued_messages_.clear();
98 for (size_t i = 0; i < queued_messages.size(); ++i) {
99 queued_messages[i]->set_routing_id(route_id_);
100 Send(queued_messages[i]);
101 }
102 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698