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_WORKER_WEBSHAREDWORKER_STUB_H_
6#define CONTENT_WORKER_WEBSHAREDWORKER_STUB_H_
7
8#include "base/memory/scoped_ptr.h"
9#include "content/worker/websharedworkerclient_proxy.h"
10#include "content/worker/worker_webapplicationcachehost_impl.h"
11#include "ipc/ipc_listener.h"
12#include "third_party/WebKit/public/web/WebSharedWorker.h"
13#include "url/gurl.h"
14
15namespace WebKit {
16class WebSharedWorker;
17}
18
19namespace content {
20
21class SharedWorkerDevToolsAgent;
22
23// This class creates a WebSharedWorker, and translates incoming IPCs to the
24// appropriate WebSharedWorker APIs.
25class WebSharedWorkerStub : public IPC::Listener {
26 public:
27  WebSharedWorkerStub(const string16& name, int route_id,
28                      const WorkerAppCacheInitInfo& appcache_init_info);
29
30  // IPC::Listener implementation.
31  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
32  virtual void OnChannelError() OVERRIDE;
33
34  // Invoked when the WebSharedWorkerClientProxy is shutting down.
35  void Shutdown();
36
37  // Called after terminating the worker context to make sure that the worker
38  // actually terminates (is not stuck in an infinite loop).
39  void EnsureWorkerContextTerminates();
40
41  WebSharedWorkerClientProxy* client() { return &client_; }
42
43  const WorkerAppCacheInitInfo& appcache_init_info() const {
44    return appcache_init_info_;
45  }
46
47  // Returns the script url of this worker.
48  const GURL& url();
49
50
51 private:
52  virtual ~WebSharedWorkerStub();
53
54  void OnConnect(int sent_message_port_id, int routing_id);
55  void OnStartWorkerContext(
56      const GURL& url, const string16& user_agent, const string16& source_code,
57      const string16& content_security_policy,
58      WebKit::WebContentSecurityPolicyType policy_type);
59
60  void OnTerminateWorkerContext();
61
62  int route_id_;
63  WorkerAppCacheInitInfo appcache_init_info_;
64
65  // WebSharedWorkerClient that responds to outgoing API calls
66  // from the worker object.
67  WebSharedWorkerClientProxy client_;
68
69  WebKit::WebSharedWorker* impl_;
70  string16 name_;
71  bool started_;
72  GURL url_;
73  scoped_ptr<SharedWorkerDevToolsAgent> worker_devtools_agent_;
74
75  typedef std::pair<int, int> PendingConnectInfo;
76  typedef std::vector<PendingConnectInfo> PendingConnectInfoList;
77  PendingConnectInfoList pending_connects_;
78
79  DISALLOW_COPY_AND_ASSIGN(WebSharedWorkerStub);
80};
81
82}  // namespace content
83
84#endif  // CONTENT_WORKER_WEBSHAREDWORKER_STUB_H_
85