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.
5 #ifndef CONTENT_RENDERER_WEBSHAREDWORKER_PROXY_H_
6 #define CONTENT_RENDERER_WEBSHAREDWORKER_PROXY_H_
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "googleurl/src/gurl.h"
15 #include "ipc/ipc_channel.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSharedWorker.h"
20 // Implementation of the WebSharedWorker APIs. This object is intended to only
21 // live long enough to allow the caller to send a "connect" event to the worker
22 // thread. Once the connect event has been sent, all future communication will
23 // happen via the WebMessagePortChannel, and the WebSharedWorker instance will
25 class WebSharedWorkerProxy
: public WebKit::WebSharedWorker
,
26 private IPC::Channel::Listener
{
28 // If the worker not loaded yet, route_id == MSG_ROUTING_NONE
29 WebSharedWorkerProxy(ChildThread
* child_thread
,
30 unsigned long long document_id
,
33 int render_view_route_id
);
34 virtual ~WebSharedWorkerProxy();
36 // Implementations of WebSharedWorker APIs
37 virtual bool isStarted();
38 virtual void connect(WebKit::WebMessagePortChannel
* channel
,
39 ConnectListener
* listener
);
41 virtual void startWorkerContext(
42 const WebKit::WebURL
& script_url
,
43 const WebKit::WebString
& name
,
44 const WebKit::WebString
& user_agent
,
45 const WebKit::WebString
& source_code
,
46 const WebKit::WebString
& content_security_policy
,
47 WebKit::WebContentSecurityPolicyType policy_type
,
48 long long script_resource_appcache_id
);
50 virtual void terminateWorkerContext();
51 virtual void clientDestroyed();
54 // IPC::Channel::Listener implementation.
55 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
57 // Returns true if the worker is running (can send messages to it).
60 // Disconnects the worker (stops listening for incoming messages).
63 // Sends a message to the worker thread (forwarded via the RenderViewHost).
64 // If WorkerStarted() has not yet been called, message is queued.
65 bool Send(IPC::Message
*);
67 // Returns true if there are queued messages.
68 bool HasQueuedMessages() { return !queued_messages_
.empty(); }
70 // Sends any messages currently in the queue.
71 void SendQueuedMessages();
73 void CreateWorkerContext(const GURL
& script_url
,
76 const string16
& user_agent
,
77 const string16
& source_code
,
78 const string16
& content_security_policy
,
79 WebKit::WebContentSecurityPolicyType policy_type
,
81 int64 script_resource_appcache_id
);
82 void OnWorkerCreated();
85 // Routing id associated with this worker - used to receive messages from the
86 // worker, and also to route messages to the worker (WorkerService contains
87 // a map that maps between these renderer-side route IDs and worker-side
91 // The routing id for the RenderView that created this worker.
92 int render_view_route_id_
;
94 ChildThread
* child_thread_
;
96 // ID of our parent document (used to shutdown workers when the parent
97 // document is detached).
98 unsigned long long document_id_
;
100 // Stores messages that were sent before the StartWorkerContext message.
101 std::vector
<IPC::Message
*> queued_messages_
;
103 // The id for the placeholder worker instance we've stored on the
104 // browser process (we need to pass this same route id back in when creating
106 int pending_route_id_
;
107 ConnectListener
* connect_listener_
;
109 DISALLOW_COPY_AND_ASSIGN(WebSharedWorkerProxy
);
112 #endif // CONTENT_RENDERER_WEBSHAREDWORKER_PROXY_H_