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 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
,
69 void AllowFileSystem(const GURL
& url
, scoped_ptr
<IPC::Message
> reply_msg
);
70 void AllowIndexedDB(const GURL
& url
,
71 const base::string16
& name
,
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_
; }
92 // Unique identifier for a worker client.
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
; }
103 SharedWorkerMessageFilter
* filter_
;
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
,
122 int message_port_id
);
123 void AllowFileSystemResponse(scoped_ptr
<IPC::Message
> reply_msg
,
125 scoped_ptr
<SharedWorkerInstance
> instance_
;
126 scoped_refptr
<WorkerDocumentSet
> worker_document_set_
;
127 base::WeakPtrFactory
<SharedWorkerHost
> weak_factory_
;
129 SharedWorkerMessageFilter
* container_render_filter_
;
130 int worker_process_id_
;
131 int worker_route_id_
;
134 const base::TimeTicks creation_time_
;
135 DISALLOW_COPY_AND_ASSIGN(SharedWorkerHost
);
137 } // namespace content
139 #endif // CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_