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/strings/string16.h"
13 #include "base/time/time.h"
14 #include "content/browser/shared_worker/shared_worker_message_filter.h"
15 #include "content/browser/worker_host/worker_document_set.h"
24 class SharedWorkerMessageFilter
;
25 class SharedWorkerInstance
;
27 // The SharedWorkerHost is the interface that represents the browser side of
28 // the browser <-> worker communication channel.
29 class SharedWorkerHost
{
31 SharedWorkerHost(SharedWorkerInstance
* instance
,
32 SharedWorkerMessageFilter
* filter
,
36 // Sends |message| to the SharedWorker.
37 bool Send(IPC::Message
* message
);
39 // Starts the SharedWorker in the renderer process which is associated with
41 void Start(bool pause_on_start
);
43 // Returns true iff the given message from a renderer process was forwarded to
45 bool FilterMessage(const IPC::Message
& message
,
46 SharedWorkerMessageFilter
* filter
);
48 // Handles the shutdown of the filter. If the worker has no other client,
49 // sends TerminateWorkerContext message to shut it down.
50 void FilterShutdown(SharedWorkerMessageFilter
* filter
);
52 // Shuts down any shared workers that are no longer referenced by active
54 void DocumentDetached(SharedWorkerMessageFilter
* filter
,
55 unsigned long long document_id
);
57 void WorkerContextClosed();
58 void WorkerScriptLoaded();
59 void WorkerScriptLoadFailed();
60 void WorkerConnected(int message_port_id
);
61 void WorkerContextDestroyed();
62 void AllowDatabase(const GURL
& url
,
63 const base::string16
& name
,
64 const base::string16
& display_name
,
65 unsigned long estimated_size
,
67 void AllowFileSystem(const GURL
& url
, bool* result
);
68 void AllowIndexedDB(const GURL
& url
,
69 const base::string16
& name
,
72 // Terminates the given worker, i.e. based on a UI action.
73 void TerminateWorker();
75 void AddFilter(SharedWorkerMessageFilter
* filter
, int route_id
);
77 SharedWorkerInstance
* instance() { return instance_
.get(); }
78 WorkerDocumentSet
* worker_document_set() const {
79 return worker_document_set_
.get();
81 SharedWorkerMessageFilter
* container_render_filter() const {
82 return container_render_filter_
;
84 int process_id() const { return worker_process_id_
; }
85 int worker_route_id() const { return worker_route_id_
; }
86 bool load_failed() const { return load_failed_
; }
87 bool closed() const { return closed_
; }
90 // Unique identifier for a worker client.
93 FilterInfo(SharedWorkerMessageFilter
* filter
, int route_id
)
94 : filter_(filter
), route_id_(route_id
), message_port_id_(0) {}
95 SharedWorkerMessageFilter
* filter() const { return filter_
; }
96 int route_id() const { return route_id_
; }
97 int message_port_id() const { return message_port_id_
; }
98 void set_message_port_id(int id
) { message_port_id_
= id
; }
101 SharedWorkerMessageFilter
* filter_
;
103 int message_port_id_
;
106 typedef std::list
<FilterInfo
> FilterList
;
108 // Relays |message| to the SharedWorker. Takes care of parsing the message if
109 // it contains a message port and sending it a valid route id.
110 void RelayMessage(const IPC::Message
& message
,
111 SharedWorkerMessageFilter
* incoming_filter
);
113 // Return a vector of all the render process/render frame IDs.
114 std::vector
<std::pair
<int, int> > GetRenderFrameIDsForWorker();
116 void RemoveFilters(SharedWorkerMessageFilter
* filter
);
117 bool HasFilter(SharedWorkerMessageFilter
* filter
, int route_id
) const;
118 void SetMessagePortID(SharedWorkerMessageFilter
* filter
,
120 int message_port_id
);
122 scoped_ptr
<SharedWorkerInstance
> instance_
;
123 scoped_refptr
<WorkerDocumentSet
> worker_document_set_
;
125 SharedWorkerMessageFilter
* container_render_filter_
;
126 int worker_process_id_
;
127 int worker_route_id_
;
130 const base::TimeTicks creation_time_
;
131 DISALLOW_COPY_AND_ASSIGN(SharedWorkerHost
);
133 } // namespace content
135 #endif // CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_