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