Home | History | Annotate | Download | only in renderer
      1 // Copyright (c) 2012 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 #ifndef CONTENT_RENDERER_WEBSHAREDWORKER_PROXY_H_
      6 #define CONTENT_RENDERER_WEBSHAREDWORKER_PROXY_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 #include "base/compiler_specific.h"
     13 #include "ipc/ipc_listener.h"
     14 #include "third_party/WebKit/public/web/WebSharedWorkerConnector.h"
     15 #include "url/gurl.h"
     16 
     17 namespace content {
     18 
     19 class ChildThread;
     20 
     21 // Implementation of the WebSharedWorker APIs. This object is intended to only
     22 // live long enough to allow the caller to send a "connect" event to the worker
     23 // thread. Once the connect event has been sent, all future communication will
     24 // happen via the WebMessagePortChannel, and the WebSharedWorker instance will
     25 // be freed.
     26 class WebSharedWorkerProxy : public blink::WebSharedWorkerConnector,
     27                              private IPC::Listener {
     28  public:
     29   // If the worker not loaded yet, route_id == MSG_ROUTING_NONE
     30   WebSharedWorkerProxy(ChildThread* child_thread,
     31                        unsigned long long document_id,
     32                        bool exists,
     33                        int route_id,
     34                        int render_view_route_id);
     35   virtual ~WebSharedWorkerProxy();
     36 
     37   // Implementations of WebSharedWorker APIs
     38   virtual bool isStarted();
     39   virtual void connect(blink::WebMessagePortChannel* channel,
     40                        ConnectListener* listener);
     41 
     42   virtual void startWorkerContext(
     43       const blink::WebURL& script_url,
     44       const blink::WebString& name,
     45       const blink::WebString& user_agent,
     46       const blink::WebString& source_code,
     47       const blink::WebString& content_security_policy,
     48       blink::WebContentSecurityPolicyType policy_type,
     49       long long script_resource_appcache_id);
     50 
     51  private:
     52   // IPC::Listener implementation.
     53   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
     54 
     55   // Returns true if the worker is running (can send messages to it).
     56   bool IsStarted();
     57 
     58   // Disconnects the worker (stops listening for incoming messages).
     59   void Disconnect();
     60 
     61   // Sends a message to the worker thread (forwarded via the RenderViewHost).
     62   // If WorkerStarted() has not yet been called, message is queued.
     63   bool Send(IPC::Message*);
     64 
     65   // Returns true if there are queued messages.
     66   bool HasQueuedMessages() { return !queued_messages_.empty(); }
     67 
     68   // Sends any messages currently in the queue.
     69   void SendQueuedMessages();
     70 
     71   void CreateWorkerContext(const GURL& script_url,
     72                            bool is_shared,
     73                            const base::string16& name,
     74                            const base::string16& user_agent,
     75                            const base::string16& source_code,
     76                            const base::string16& content_security_policy,
     77                            blink::WebContentSecurityPolicyType policy_type,
     78                            int pending_route_id,
     79                            int64 script_resource_appcache_id);
     80   void OnWorkerCreated();
     81 
     82 
     83   // Routing id associated with this worker - used to receive messages from the
     84   // worker, and also to route messages to the worker (WorkerService contains
     85   // a map that maps between these renderer-side route IDs and worker-side
     86   // routing ids).
     87   int route_id_;
     88 
     89   // The routing id for the RenderView that created this worker.
     90   int render_view_route_id_;
     91 
     92   ChildThread* child_thread_;
     93 
     94   // ID of our parent document (used to shutdown workers when the parent
     95   // document is detached).
     96   unsigned long long document_id_;
     97 
     98   // Stores messages that were sent before the StartWorkerContext message.
     99   std::vector<IPC::Message*> queued_messages_;
    100 
    101   // The id for the placeholder worker instance we've stored on the
    102   // browser process (we need to pass this same route id back in when creating
    103   // the worker).
    104   int pending_route_id_;
    105   ConnectListener* connect_listener_;
    106 
    107   DISALLOW_COPY_AND_ASSIGN(WebSharedWorkerProxy);
    108 };
    109 
    110 }  // namespace content
    111 
    112 #endif  // CONTENT_RENDERER_WEBSHAREDWORKER_PROXY_H_
    113