1 // Copyright 2014 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_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_
6 #define CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/strings/string16.h"
14 #include "base/time/time.h"
15 #include "content/browser/shared_worker/shared_worker_message_filter.h"
16 #include "content/browser/shared_worker/worker_document_set.h"
25 class SharedWorkerMessageFilter;
26 class SharedWorkerInstance;
28 // The SharedWorkerHost is the interface that represents the browser side of
29 // the browser <-> worker communication channel.
30 class SharedWorkerHost {
32 SharedWorkerHost(SharedWorkerInstance* instance,
33 SharedWorkerMessageFilter* filter,
37 // Sends |message| to the SharedWorker.
38 bool Send(IPC::Message* message);
40 // Starts the SharedWorker in the renderer process which is associated with
42 void Start(bool pause_on_start);
44 // Returns true iff the given message from a renderer process was forwarded to
46 bool FilterMessage(const IPC::Message& message,
47 SharedWorkerMessageFilter* filter);
49 // Handles the shutdown of the filter. If the worker has no other client,
50 // sends TerminateWorkerContext message to shut it down.
51 void FilterShutdown(SharedWorkerMessageFilter* filter);
53 // Shuts down any shared workers that are no longer referenced by active
55 void DocumentDetached(SharedWorkerMessageFilter* filter,
56 unsigned long long document_id);
58 void WorkerContextClosed();
59 void WorkerScriptLoaded();
60 void WorkerScriptLoadFailed();
61 void WorkerConnected(int message_port_id);
62 void WorkerContextDestroyed();
63 void AllowDatabase(const GURL& url,
64 const base::string16& name,
65 const base::string16& display_name,
66 unsigned long estimated_size,
68 void AllowFileSystem(const GURL& url, scoped_ptr<IPC::Message> reply_msg);
69 void AllowIndexedDB(const GURL& url,
70 const base::string16& name,
73 // Terminates the given worker, i.e. based on a UI action.
74 void TerminateWorker();
76 void AddFilter(SharedWorkerMessageFilter* filter, int route_id);
78 SharedWorkerInstance* instance() { return instance_.get(); }
79 WorkerDocumentSet* worker_document_set() const {
80 return worker_document_set_.get();
82 SharedWorkerMessageFilter* container_render_filter() const {
83 return container_render_filter_;
85 int process_id() const { return worker_process_id_; }
86 int worker_route_id() const { return worker_route_id_; }
87 bool load_failed() const { return load_failed_; }
88 bool closed() const { return closed_; }
91 // Unique identifier for a worker client.
94 FilterInfo(SharedWorkerMessageFilter* filter, int route_id)
95 : filter_(filter), route_id_(route_id), message_port_id_(0) {}
96 SharedWorkerMessageFilter* filter() const { return filter_; }
97 int route_id() const { return route_id_; }
98 int message_port_id() const { return message_port_id_; }
99 void set_message_port_id(int id) { message_port_id_ = id; }
102 SharedWorkerMessageFilter* filter_;
104 int message_port_id_;
107 typedef std::list<FilterInfo> FilterList;
109 // Relays |message| to the SharedWorker. Takes care of parsing the message if
110 // it contains a message port and sending it a valid route id.
111 void RelayMessage(const IPC::Message& message,
112 SharedWorkerMessageFilter* incoming_filter);
114 // Return a vector of all the render process/render frame IDs.
115 std::vector<std::pair<int, int> > GetRenderFrameIDsForWorker();
117 void RemoveFilters(SharedWorkerMessageFilter* filter);
118 bool HasFilter(SharedWorkerMessageFilter* filter, int route_id) const;
119 void SetMessagePortID(SharedWorkerMessageFilter* filter,
121 int message_port_id);
122 void AllowFileSystemResponse(scoped_ptr<IPC::Message> reply_msg,
124 scoped_ptr<SharedWorkerInstance> instance_;
125 scoped_refptr<WorkerDocumentSet> worker_document_set_;
126 base::WeakPtrFactory<SharedWorkerHost> weak_factory_;
128 SharedWorkerMessageFilter* container_render_filter_;
129 int worker_process_id_;
130 int worker_route_id_;
133 const base::TimeTicks creation_time_;
134 DISALLOW_COPY_AND_ASSIGN(SharedWorkerHost);
136 } // namespace content
138 #endif // CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_