OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_ | 5 #ifndef CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_ |
6 #define CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_ | 6 #define CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <memory> | 9 #include <memory> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "content/browser/shared_worker/worker_document_set.h" | 17 #include "content/browser/shared_worker/worker_document_set.h" |
18 | 18 |
19 class GURL; | 19 class GURL; |
20 | 20 |
21 namespace IPC { | 21 namespace IPC { |
22 class Message; | 22 class Message; |
23 } | 23 } |
24 | 24 |
25 namespace content { | 25 namespace content { |
26 | 26 |
| 27 class MessagePort; |
27 class SharedWorkerMessageFilter; | 28 class SharedWorkerMessageFilter; |
28 class SharedWorkerInstance; | 29 class SharedWorkerInstance; |
29 | 30 |
30 // The SharedWorkerHost is the interface that represents the browser side of | 31 // The SharedWorkerHost is the interface that represents the browser side of |
31 // the browser <-> worker communication channel. This is owned by | 32 // the browser <-> worker communication channel. This is owned by |
32 // SharedWorkerServiceImpl and destructed when a worker context or worker's | 33 // SharedWorkerServiceImpl and destructed when a worker context or worker's |
33 // message filter is closed. | 34 // message filter is closed. |
34 class SharedWorkerHost { | 35 class SharedWorkerHost { |
35 public: | 36 public: |
36 SharedWorkerHost(SharedWorkerInstance* instance, | 37 SharedWorkerHost(SharedWorkerInstance* instance, |
37 SharedWorkerMessageFilter* filter, | 38 SharedWorkerMessageFilter* filter, |
38 int worker_route_id); | 39 int worker_route_id); |
39 ~SharedWorkerHost(); | 40 ~SharedWorkerHost(); |
40 | 41 |
41 // Starts the SharedWorker in the renderer process which is associated with | 42 // Starts the SharedWorker in the renderer process which is associated with |
42 // |filter_|. | 43 // |filter_|. |
43 void Start(bool pause_on_start); | 44 void Start(bool pause_on_start); |
44 | 45 |
45 // Returns true iff the given message from a renderer process was forwarded to | 46 // Returns true iff the given message from a renderer process was forwarded to |
46 // the worker. | 47 // the worker. |
47 bool FilterConnectionMessage(int route_id, | 48 bool SendConnectToWorker(int worker_route_id, |
48 int sent_message_port_id, | 49 const MessagePort& port, |
49 SharedWorkerMessageFilter* incoming_filter); | 50 SharedWorkerMessageFilter* filter); |
50 | 51 |
51 // Handles the shutdown of the filter. If the worker has no other client, | 52 // Handles the shutdown of the filter. If the worker has no other client, |
52 // sends TerminateWorkerContext message to shut it down. | 53 // sends TerminateWorkerContext message to shut it down. |
53 void FilterShutdown(SharedWorkerMessageFilter* filter); | 54 void FilterShutdown(SharedWorkerMessageFilter* filter); |
54 | 55 |
55 // Shuts down any shared workers that are no longer referenced by active | 56 // Shuts down any shared workers that are no longer referenced by active |
56 // documents. | 57 // documents. |
57 void DocumentDetached(SharedWorkerMessageFilter* filter, | 58 void DocumentDetached(SharedWorkerMessageFilter* filter, |
58 unsigned long long document_id); | 59 unsigned long long document_id); |
59 | 60 |
60 // Removes the references to shared workers from the all documents in the | 61 // Removes the references to shared workers from the all documents in the |
61 // renderer frame. And shuts down any shared workers that are no longer | 62 // renderer frame. And shuts down any shared workers that are no longer |
62 // referenced by active documents. | 63 // referenced by active documents. |
63 void RenderFrameDetached(int render_process_id, int render_frame_id); | 64 void RenderFrameDetached(int render_process_id, int render_frame_id); |
64 | 65 |
65 void WorkerContextClosed(); | 66 void WorkerContextClosed(); |
66 void WorkerReadyForInspection(); | 67 void WorkerReadyForInspection(); |
67 void WorkerScriptLoaded(); | 68 void WorkerScriptLoaded(); |
68 void WorkerScriptLoadFailed(); | 69 void WorkerScriptLoadFailed(); |
69 void WorkerConnected(int message_port_id); | 70 void WorkerConnected(int connection_request_id); |
70 void WorkerContextDestroyed(); | 71 void WorkerContextDestroyed(); |
71 void AllowFileSystem(const GURL& url, | 72 void AllowFileSystem(const GURL& url, |
72 std::unique_ptr<IPC::Message> reply_msg); | 73 std::unique_ptr<IPC::Message> reply_msg); |
73 void AllowIndexedDB(const GURL& url, | 74 void AllowIndexedDB(const GURL& url, |
74 const base::string16& name, | 75 const base::string16& name, |
75 bool* result); | 76 bool* result); |
76 | 77 |
77 // Terminates the given worker, i.e. based on a UI action. | 78 // Terminates the given worker, i.e. based on a UI action. |
78 void TerminateWorker(); | 79 void TerminateWorker(); |
79 | 80 |
80 void AddFilter(SharedWorkerMessageFilter* filter, int route_id); | 81 void AddFilter(SharedWorkerMessageFilter* filter, int route_id); |
81 | 82 |
82 SharedWorkerInstance* instance() { return instance_.get(); } | 83 SharedWorkerInstance* instance() { return instance_.get(); } |
83 WorkerDocumentSet* worker_document_set() const { | 84 WorkerDocumentSet* worker_document_set() const { |
84 return worker_document_set_.get(); | 85 return worker_document_set_.get(); |
85 } | 86 } |
86 SharedWorkerMessageFilter* worker_render_filter() const { | 87 SharedWorkerMessageFilter* worker_render_filter() const { |
87 return worker_render_filter_; | 88 return worker_render_filter_; |
88 } | 89 } |
89 int process_id() const { return worker_process_id_; } | 90 int process_id() const { return worker_process_id_; } |
90 int worker_route_id() const { return worker_route_id_; } | 91 int worker_route_id() const { return worker_route_id_; } |
91 bool IsAvailable() const; | 92 bool IsAvailable() const; |
92 | 93 |
93 private: | 94 private: |
94 // Unique identifier for a worker client. | 95 // Unique identifier for a worker client. |
95 class FilterInfo { | 96 class FilterInfo { |
96 public: | 97 public: |
97 FilterInfo(SharedWorkerMessageFilter* filter, int route_id) | 98 FilterInfo(SharedWorkerMessageFilter* filter, int route_id) |
98 : filter_(filter), route_id_(route_id), message_port_id_(0) {} | 99 : filter_(filter), route_id_(route_id), connection_request_id_(0) {} |
99 SharedWorkerMessageFilter* filter() const { return filter_; } | 100 SharedWorkerMessageFilter* filter() const { return filter_; } |
100 int route_id() const { return route_id_; } | 101 int route_id() const { return route_id_; } |
101 int message_port_id() const { return message_port_id_; } | 102 int connection_request_id() const { return connection_request_id_; } |
102 void set_message_port_id(int id) { message_port_id_ = id; } | 103 void set_connection_request_id(int id) { connection_request_id_ = id; } |
103 | 104 |
104 private: | 105 private: |
105 SharedWorkerMessageFilter* filter_; | 106 SharedWorkerMessageFilter* filter_; |
106 const int route_id_; | 107 const int route_id_; |
107 int message_port_id_; | 108 int connection_request_id_; |
108 }; | 109 }; |
109 | 110 |
110 using FilterList = std::list<FilterInfo>; | 111 using FilterList = std::list<FilterInfo>; |
111 | 112 |
112 // Return a vector of all the render process/render frame IDs. | 113 // Return a vector of all the render process/render frame IDs. |
113 std::vector<std::pair<int, int> > GetRenderFrameIDsForWorker(); | 114 std::vector<std::pair<int, int> > GetRenderFrameIDsForWorker(); |
114 | 115 |
115 void RemoveFilters(SharedWorkerMessageFilter* filter); | 116 void RemoveFilters(SharedWorkerMessageFilter* filter); |
116 bool HasFilter(SharedWorkerMessageFilter* filter, int route_id) const; | 117 bool HasFilter(SharedWorkerMessageFilter* filter, int route_id) const; |
117 void Connect(int route_id, | 118 void SetConnectionRequestID(SharedWorkerMessageFilter* filter, |
118 int sent_message_port_id, | 119 int route_id, |
119 SharedWorkerMessageFilter* incoming_filter); | 120 int connection_request_id); |
120 void SetMessagePortID(SharedWorkerMessageFilter* filter, | |
121 int route_id, | |
122 int message_port_id); | |
123 void AllowFileSystemResponse(std::unique_ptr<IPC::Message> reply_msg, | 121 void AllowFileSystemResponse(std::unique_ptr<IPC::Message> reply_msg, |
124 bool allowed); | 122 bool allowed); |
125 | 123 |
126 // Sends |message| to the SharedWorker. | 124 // Sends |message| to the SharedWorker. |
127 bool Send(IPC::Message* message); | 125 bool Send(IPC::Message* message); |
128 | 126 |
129 std::unique_ptr<SharedWorkerInstance> instance_; | 127 std::unique_ptr<SharedWorkerInstance> instance_; |
130 scoped_refptr<WorkerDocumentSet> worker_document_set_; | 128 scoped_refptr<WorkerDocumentSet> worker_document_set_; |
131 FilterList filters_; | 129 FilterList filters_; |
132 | 130 |
133 // A message filter for a renderer process that hosts a worker. This is always | 131 // A message filter for a renderer process that hosts a worker. This is always |
134 // valid because this host is destructed immediately after the filter is | 132 // valid because this host is destructed immediately after the filter is |
135 // closed (see SharedWorkerServiceImpl::OnSharedWorkerMessageFilterClosing). | 133 // closed (see SharedWorkerServiceImpl::OnSharedWorkerMessageFilterClosing). |
136 SharedWorkerMessageFilter* worker_render_filter_; | 134 SharedWorkerMessageFilter* worker_render_filter_; |
137 | 135 |
138 const int worker_process_id_; | 136 const int worker_process_id_; |
139 const int worker_route_id_; | 137 const int worker_route_id_; |
| 138 int next_connection_request_id_; |
140 bool termination_message_sent_ = false; | 139 bool termination_message_sent_ = false; |
141 bool closed_ = false; | 140 bool closed_ = false; |
142 const base::TimeTicks creation_time_; | 141 const base::TimeTicks creation_time_; |
143 | 142 |
144 base::WeakPtrFactory<SharedWorkerHost> weak_factory_; | 143 base::WeakPtrFactory<SharedWorkerHost> weak_factory_; |
145 | 144 |
146 DISALLOW_COPY_AND_ASSIGN(SharedWorkerHost); | 145 DISALLOW_COPY_AND_ASSIGN(SharedWorkerHost); |
147 }; | 146 }; |
148 } // namespace content | 147 } // namespace content |
149 | 148 |
150 #endif // CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_ | 149 #endif // CONTENT_BROWSER_SHARED_WORKER_SHARED_WORKER_HOST_H_ |
OLD | NEW |