Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / browser / shared_worker / shared_worker_host.h
blobe729589c2eef919a38b91f9fa883c262118d4bf6
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_
8 #include <list>
9 #include <vector>
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"
18 class GURL;
20 namespace IPC {
21 class Message;
24 namespace content {
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 {
31 public:
32 SharedWorkerHost(SharedWorkerInstance* instance,
33 SharedWorkerMessageFilter* filter,
34 int worker_route_id);
35 ~SharedWorkerHost();
37 // Sends |message| to the SharedWorker.
38 bool Send(IPC::Message* message);
40 // Starts the SharedWorker in the renderer process which is associated with
41 // |filter_|.
42 void Start(bool pause_on_start);
44 // Returns true iff the given message from a renderer process was forwarded to
45 // the worker.
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
54 // documents.
55 void DocumentDetached(SharedWorkerMessageFilter* filter,
56 unsigned long long document_id);
58 void WorkerContextClosed();
59 void WorkerReadyForInspection();
60 void WorkerScriptLoaded();
61 void WorkerScriptLoadFailed();
62 void WorkerConnected(int message_port_id);
63 void WorkerContextDestroyed();
64 void AllowDatabase(const GURL& url,
65 const base::string16& name,
66 const base::string16& display_name,
67 unsigned long estimated_size,
68 bool* result);
69 void AllowFileSystem(const GURL& url, scoped_ptr<IPC::Message> reply_msg);
70 void AllowIndexedDB(const GURL& url,
71 const base::string16& name,
72 bool* result);
74 // Terminates the given worker, i.e. based on a UI action.
75 void TerminateWorker();
77 void AddFilter(SharedWorkerMessageFilter* filter, int route_id);
79 SharedWorkerInstance* instance() { return instance_.get(); }
80 WorkerDocumentSet* worker_document_set() const {
81 return worker_document_set_.get();
83 SharedWorkerMessageFilter* container_render_filter() const {
84 return container_render_filter_;
86 int process_id() const { return worker_process_id_; }
87 int worker_route_id() const { return worker_route_id_; }
88 bool load_failed() const { return load_failed_; }
89 bool closed() const { return closed_; }
91 private:
92 // Unique identifier for a worker client.
93 class FilterInfo {
94 public:
95 FilterInfo(SharedWorkerMessageFilter* filter, int route_id)
96 : filter_(filter), route_id_(route_id), message_port_id_(0) {}
97 SharedWorkerMessageFilter* filter() const { return filter_; }
98 int route_id() const { return route_id_; }
99 int message_port_id() const { return message_port_id_; }
100 void set_message_port_id(int id) { message_port_id_ = id; }
102 private:
103 SharedWorkerMessageFilter* filter_;
104 int route_id_;
105 int message_port_id_;
108 typedef std::list<FilterInfo> FilterList;
110 // Relays |message| to the SharedWorker. Takes care of parsing the message if
111 // it contains a message port and sending it a valid route id.
112 void RelayMessage(const IPC::Message& message,
113 SharedWorkerMessageFilter* incoming_filter);
115 // Return a vector of all the render process/render frame IDs.
116 std::vector<std::pair<int, int> > GetRenderFrameIDsForWorker();
118 void RemoveFilters(SharedWorkerMessageFilter* filter);
119 bool HasFilter(SharedWorkerMessageFilter* filter, int route_id) const;
120 void SetMessagePortID(SharedWorkerMessageFilter* filter,
121 int route_id,
122 int message_port_id);
123 void AllowFileSystemResponse(scoped_ptr<IPC::Message> reply_msg,
124 bool allowed);
125 scoped_ptr<SharedWorkerInstance> instance_;
126 scoped_refptr<WorkerDocumentSet> worker_document_set_;
127 FilterList filters_;
128 SharedWorkerMessageFilter* container_render_filter_;
129 int worker_process_id_;
130 int worker_route_id_;
131 bool load_failed_;
132 bool closed_;
133 const base::TimeTicks creation_time_;
135 base::WeakPtrFactory<SharedWorkerHost> weak_factory_;
137 DISALLOW_COPY_AND_ASSIGN(SharedWorkerHost);
139 } // namespace content
141 #endif // CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_