OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
20 #include "base/observer_list.h" | 20 #include "base/observer_list.h" |
21 #include "base/strings/string16.h" | 21 #include "base/strings/string16.h" |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 23 #include "content/browser/service_worker/embedded_worker_status.h" |
23 #include "content/browser/service_worker/service_worker_metrics.h" | 24 #include "content/browser/service_worker/service_worker_metrics.h" |
24 #include "content/common/content_export.h" | 25 #include "content/common/content_export.h" |
25 #include "content/common/service_worker/service_worker_status_code.h" | 26 #include "content/common/service_worker/service_worker_status_code.h" |
26 #include "content/public/common/console_message_level.h" | 27 #include "content/public/common/console_message_level.h" |
27 #include "url/gurl.h" | 28 #include "url/gurl.h" |
28 | 29 |
29 // Windows headers will redefine SendMessage. | 30 // Windows headers will redefine SendMessage. |
30 #ifdef SendMessage | 31 #ifdef SendMessage |
31 #undef SendMessage | 32 #undef SendMessage |
32 #endif | 33 #endif |
(...skipping 11 matching lines...) Expand all Loading... |
44 class ServiceRegistry; | 45 class ServiceRegistry; |
45 class ServiceRegistryImpl; | 46 class ServiceRegistryImpl; |
46 class ServiceWorkerContextCore; | 47 class ServiceWorkerContextCore; |
47 | 48 |
48 // This gives an interface to control one EmbeddedWorker instance, which | 49 // This gives an interface to control one EmbeddedWorker instance, which |
49 // may be 'in-waiting' or running in one of the child processes added by | 50 // may be 'in-waiting' or running in one of the child processes added by |
50 // AddProcessReference(). | 51 // AddProcessReference(). |
51 class CONTENT_EXPORT EmbeddedWorkerInstance { | 52 class CONTENT_EXPORT EmbeddedWorkerInstance { |
52 public: | 53 public: |
53 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; | 54 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; |
54 enum Status { | |
55 STOPPED, | |
56 STARTING, | |
57 RUNNING, | |
58 STOPPING, | |
59 }; | |
60 | 55 |
61 // This enum is used in UMA histograms. Append-only. | 56 // This enum is used in UMA histograms. Append-only. |
62 enum StartingPhase { | 57 enum StartingPhase { |
63 NOT_STARTING, | 58 NOT_STARTING, |
64 ALLOCATING_PROCESS, | 59 ALLOCATING_PROCESS, |
65 REGISTERING_TO_DEVTOOLS, | 60 REGISTERING_TO_DEVTOOLS, |
66 SENT_START_WORKER, | 61 SENT_START_WORKER, |
67 SCRIPT_DOWNLOADING, | 62 SCRIPT_DOWNLOADING, |
68 SCRIPT_LOADED, | 63 SCRIPT_LOADED, |
69 SCRIPT_EVALUATED, | 64 SCRIPT_EVALUATED, |
(...skipping 11 matching lines...) Expand all Loading... |
81 virtual ~Listener() {} | 76 virtual ~Listener() {} |
82 | 77 |
83 virtual void OnStarting() {} | 78 virtual void OnStarting() {} |
84 virtual void OnProcessAllocated() {} | 79 virtual void OnProcessAllocated() {} |
85 virtual void OnStartWorkerMessageSent() {} | 80 virtual void OnStartWorkerMessageSent() {} |
86 virtual void OnThreadStarted() {} | 81 virtual void OnThreadStarted() {} |
87 virtual void OnStarted() {} | 82 virtual void OnStarted() {} |
88 | 83 |
89 virtual void OnStopping() {} | 84 virtual void OnStopping() {} |
90 // Received ACK from renderer that the worker context terminated. | 85 // Received ACK from renderer that the worker context terminated. |
91 virtual void OnStopped(Status old_status) {} | 86 virtual void OnStopped(EmbeddedWorkerStatus old_status) {} |
92 // The browser-side IPC endpoint for communication with the worker died. | 87 // The browser-side IPC endpoint for communication with the worker died. |
93 virtual void OnDetached(Status old_status) {} | 88 virtual void OnDetached(EmbeddedWorkerStatus old_status) {} |
94 virtual void OnScriptLoaded() {} | 89 virtual void OnScriptLoaded() {} |
95 virtual void OnScriptLoadFailed() {} | 90 virtual void OnScriptLoadFailed() {} |
96 virtual void OnReportException(const base::string16& error_message, | 91 virtual void OnReportException(const base::string16& error_message, |
97 int line_number, | 92 int line_number, |
98 int column_number, | 93 int column_number, |
99 const GURL& source_url) {} | 94 const GURL& source_url) {} |
100 virtual void OnReportConsoleMessage(int source_identifier, | 95 virtual void OnReportConsoleMessage(int source_identifier, |
101 int message_level, | 96 int message_level, |
102 const base::string16& message, | 97 const base::string16& message, |
103 int line_number, | 98 int line_number, |
(...skipping 29 matching lines...) Expand all Loading... |
133 ServiceWorkerStatusCode SendMessage(const IPC::Message& message); | 128 ServiceWorkerStatusCode SendMessage(const IPC::Message& message); |
134 | 129 |
135 // Resumes the worker if it paused after download. | 130 // Resumes the worker if it paused after download. |
136 void ResumeAfterDownload(); | 131 void ResumeAfterDownload(); |
137 | 132 |
138 // Returns the ServiceRegistry for this worker. It is invalid to call this | 133 // Returns the ServiceRegistry for this worker. It is invalid to call this |
139 // when the worker is not in STARTING or RUNNING status. | 134 // when the worker is not in STARTING or RUNNING status. |
140 ServiceRegistry* GetServiceRegistry(); | 135 ServiceRegistry* GetServiceRegistry(); |
141 | 136 |
142 int embedded_worker_id() const { return embedded_worker_id_; } | 137 int embedded_worker_id() const { return embedded_worker_id_; } |
143 Status status() const { return status_; } | 138 EmbeddedWorkerStatus status() const { return status_; } |
144 StartingPhase starting_phase() const { | 139 StartingPhase starting_phase() const { |
145 DCHECK_EQ(STARTING, status()); | 140 DCHECK_EQ(EmbeddedWorkerStatus::STARTING, status()); |
146 return starting_phase_; | 141 return starting_phase_; |
147 } | 142 } |
148 int process_id() const; | 143 int process_id() const; |
149 int thread_id() const { return thread_id_; } | 144 int thread_id() const { return thread_id_; } |
150 // This should be called only when the worker instance has a valid process, | 145 // This should be called only when the worker instance has a valid process, |
151 // that is, when |process_id()| returns a valid process id. | 146 // that is, when |process_id()| returns a valid process id. |
152 bool is_new_process() const; | 147 bool is_new_process() const; |
153 int worker_devtools_agent_route_id() const; | 148 int worker_devtools_agent_route_id() const; |
154 MessagePortMessageFilter* message_port_message_filter() const; | 149 MessagePortMessageFilter* message_port_message_filter() const; |
155 | 150 |
156 void AddListener(Listener* listener); | 151 void AddListener(Listener* listener); |
157 void RemoveListener(Listener* listener); | 152 void RemoveListener(Listener* listener); |
158 | 153 |
159 void set_devtools_attached(bool attached) { devtools_attached_ = attached; } | 154 void set_devtools_attached(bool attached) { devtools_attached_ = attached; } |
160 bool devtools_attached() const { return devtools_attached_; } | 155 bool devtools_attached() const { return devtools_attached_; } |
161 | 156 |
162 bool network_accessed_for_script() const { | 157 bool network_accessed_for_script() const { |
163 return network_accessed_for_script_; | 158 return network_accessed_for_script_; |
164 } | 159 } |
165 | 160 |
| 161 ServiceWorkerMetrics::StartSituation start_situation() const { |
| 162 DCHECK(status() == EmbeddedWorkerStatus::STARTING || |
| 163 status() == EmbeddedWorkerStatus::RUNNING); |
| 164 return start_situation_; |
| 165 } |
| 166 |
166 // Called when the main script load accessed the network. | 167 // Called when the main script load accessed the network. |
167 void OnNetworkAccessedForScriptLoad(); | 168 void OnNetworkAccessedForScriptLoad(); |
168 | 169 |
169 // Called when reading the main script from the service worker script cache | 170 // Called when reading the main script from the service worker script cache |
170 // begins and ends. | 171 // begins and ends. |
171 void OnScriptReadStarted(); | 172 void OnScriptReadStarted(); |
172 void OnScriptReadFinished(); | 173 void OnScriptReadFinished(); |
173 | 174 |
174 // Called when the worker is installed. | 175 // Called when the worker is installed. |
175 void OnWorkerVersionInstalled(); | 176 void OnWorkerVersionInstalled(); |
176 | 177 |
177 // Called when the worker is doomed. | 178 // Called when the worker is doomed. |
178 void OnWorkerVersionDoomed(); | 179 void OnWorkerVersionDoomed(); |
179 | 180 |
180 // Called when the net::URLRequestJob to load the service worker script | 181 // Called when the net::URLRequestJob to load the service worker script |
181 // created. Not called for import scripts. | 182 // created. Not called for import scripts. |
182 void OnURLJobCreatedForMainScript(); | 183 void OnURLJobCreatedForMainScript(); |
183 | 184 |
184 // Add message to the devtools console. | 185 // Add message to the devtools console. |
185 void AddMessageToConsole(ConsoleMessageLevel level, | 186 void AddMessageToConsole(ConsoleMessageLevel level, |
186 const std::string& message); | 187 const std::string& message); |
187 | 188 |
188 static std::string StatusToString(Status status); | 189 static std::string StatusToString(EmbeddedWorkerStatus status); |
189 static std::string StartingPhaseToString(StartingPhase phase); | 190 static std::string StartingPhaseToString(StartingPhase phase); |
190 | 191 |
191 void Detach(); | 192 void Detach(); |
192 | 193 |
193 base::WeakPtr<EmbeddedWorkerInstance> AsWeakPtr(); | 194 base::WeakPtr<EmbeddedWorkerInstance> AsWeakPtr(); |
194 | 195 |
195 private: | 196 private: |
196 typedef base::ObserverList<Listener> ListenerList; | 197 typedef base::ObserverList<Listener> ListenerList; |
197 class DevToolsProxy; | 198 class DevToolsProxy; |
198 class StartTask; | 199 class StartTask; |
199 class WorkerProcessHandle; | 200 class WorkerProcessHandle; |
200 friend class EmbeddedWorkerRegistry; | 201 friend class EmbeddedWorkerRegistry; |
201 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop); | 202 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop); |
202 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, DetachDuringStart); | 203 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, DetachDuringStart); |
203 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StopDuringStart); | 204 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StopDuringStart); |
204 | 205 |
205 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker(). | 206 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker(). |
206 // This instance holds a ref of |registry|. | 207 // This instance holds a ref of |registry|. |
207 EmbeddedWorkerInstance(base::WeakPtr<ServiceWorkerContextCore> context, | 208 EmbeddedWorkerInstance(base::WeakPtr<ServiceWorkerContextCore> context, |
208 int embedded_worker_id); | 209 int embedded_worker_id); |
209 | 210 |
210 // Called back from StartTask after a process is allocated on the UI thread. | 211 // Called back from StartTask after a process is allocated on the UI thread. |
211 void OnProcessAllocated(std::unique_ptr<WorkerProcessHandle> handle); | 212 void OnProcessAllocated(std::unique_ptr<WorkerProcessHandle> handle, |
| 213 ServiceWorkerMetrics::StartSituation start_situation); |
212 | 214 |
213 // Called back from StartTask after the worker is registered to | 215 // Called back from StartTask after the worker is registered to |
214 // WorkerDevToolsManager. | 216 // WorkerDevToolsManager. |
215 void OnRegisteredToDevToolsManager(bool is_new_process, | 217 void OnRegisteredToDevToolsManager(bool is_new_process, |
216 int worker_devtools_agent_route_id, | 218 int worker_devtools_agent_route_id, |
217 bool wait_for_debugger); | 219 bool wait_for_debugger); |
218 | 220 |
219 // Called back from StartTask after a start worker message is sent. | 221 // Called back from StartTask after a start worker message is sent. |
220 void OnStartWorkerMessageSent(); | 222 void OnStartWorkerMessageSent(); |
221 | 223 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 void OnStartFailed(const StatusCallback& callback, | 284 void OnStartFailed(const StatusCallback& callback, |
283 ServiceWorkerStatusCode status); | 285 ServiceWorkerStatusCode status); |
284 | 286 |
285 // Returns the time elapsed since |step_time_| and updates |step_time_| | 287 // Returns the time elapsed since |step_time_| and updates |step_time_| |
286 // to the current time. | 288 // to the current time. |
287 base::TimeDelta UpdateStepTime(); | 289 base::TimeDelta UpdateStepTime(); |
288 | 290 |
289 base::WeakPtr<ServiceWorkerContextCore> context_; | 291 base::WeakPtr<ServiceWorkerContextCore> context_; |
290 scoped_refptr<EmbeddedWorkerRegistry> registry_; | 292 scoped_refptr<EmbeddedWorkerRegistry> registry_; |
291 const int embedded_worker_id_; | 293 const int embedded_worker_id_; |
292 Status status_; | 294 EmbeddedWorkerStatus status_; |
293 StartingPhase starting_phase_; | 295 StartingPhase starting_phase_; |
294 | 296 |
295 // Current running information. | 297 // Current running information. |
296 std::unique_ptr<EmbeddedWorkerInstance::WorkerProcessHandle> process_handle_; | 298 std::unique_ptr<EmbeddedWorkerInstance::WorkerProcessHandle> process_handle_; |
297 int thread_id_; | 299 int thread_id_; |
298 std::unique_ptr<ServiceRegistryImpl> service_registry_; | 300 std::unique_ptr<ServiceRegistryImpl> service_registry_; |
299 | 301 |
300 // Whether devtools is attached or not. | 302 // Whether devtools is attached or not. |
301 bool devtools_attached_; | 303 bool devtools_attached_; |
302 | 304 |
303 // True if the script load request accessed the network. If the script was | 305 // True if the script load request accessed the network. If the script was |
304 // served from HTTPCache or ServiceWorkerDatabase this value is false. | 306 // served from HTTPCache or ServiceWorkerDatabase this value is false. |
305 bool network_accessed_for_script_; | 307 bool network_accessed_for_script_; |
306 | 308 |
307 ListenerList listener_list_; | 309 ListenerList listener_list_; |
308 std::unique_ptr<DevToolsProxy> devtools_proxy_; | 310 std::unique_ptr<DevToolsProxy> devtools_proxy_; |
309 | 311 |
310 std::unique_ptr<StartTask> inflight_start_task_; | 312 std::unique_ptr<StartTask> inflight_start_task_; |
311 | 313 |
| 314 // This is valid only after a process is allocated for the worker. |
| 315 ServiceWorkerMetrics::StartSituation start_situation_ = |
| 316 ServiceWorkerMetrics::StartSituation::UNKNOWN; |
| 317 |
312 // Used for UMA. The start time of the current start sequence step. | 318 // Used for UMA. The start time of the current start sequence step. |
313 base::TimeTicks step_time_; | 319 base::TimeTicks step_time_; |
314 | 320 |
315 base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_; | 321 base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_; |
316 | 322 |
317 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); | 323 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); |
318 }; | 324 }; |
319 | 325 |
320 } // namespace content | 326 } // namespace content |
321 | 327 |
322 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ | 328 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ |
OLD | NEW |