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 <map> | 8 #include <map> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/callback_forward.h" | 12 #include "base/callback_forward.h" |
13 #include "base/files/file_path.h" | |
13 #include "base/gtest_prod_util.h" | 14 #include "base/gtest_prod_util.h" |
14 #include "base/logging.h" | 15 #include "base/logging.h" |
15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
16 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
17 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
18 #include "content/common/content_export.h" | 19 #include "content/common/content_export.h" |
19 #include "content/common/service_worker/service_worker_status_code.h" | 20 #include "content/common/service_worker/service_worker_status_code.h" |
20 | 21 #include "url/gurl.h" |
21 class GURL; | |
22 | 22 |
23 namespace IPC { | 23 namespace IPC { |
24 class Message; | 24 class Message; |
25 } | 25 } |
26 | 26 |
27 namespace content { | 27 namespace content { |
28 | 28 |
29 class EmbeddedWorkerRegistry; | 29 class EmbeddedWorkerRegistry; |
30 struct ServiceWorkerFetchRequest; | 30 struct ServiceWorkerFetchRequest; |
31 | 31 |
32 // This gives an interface to control one EmbeddedWorker instance, which | 32 // This gives an interface to control one EmbeddedWorker instance, which |
33 // may be 'in-waiting' or running in one of the child processes added by | 33 // may be 'in-waiting' or running in one of the child processes added by |
34 // AddProcessReference(). | 34 // AddProcessReference(). |
35 class CONTENT_EXPORT EmbeddedWorkerInstance { | 35 class CONTENT_EXPORT EmbeddedWorkerInstance { |
36 public: | 36 public: |
37 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; | 37 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; |
38 enum Status { | 38 enum Status { |
39 STOPPED, | 39 STOPPED, |
40 STARTING, | 40 STARTING, |
41 RUNNING, | 41 RUNNING, |
42 STOPPING, | 42 STOPPING, |
43 }; | 43 }; |
44 | 44 |
45 class Listener { | 45 class Listener { |
46 public: | 46 public: |
47 virtual ~Listener() {} | 47 virtual ~Listener() {} |
48 virtual void OnStarted() = 0; | 48 virtual void OnStarted() = 0; |
49 virtual void OnStopped() = 0; | 49 virtual void OnStopped() = 0; |
50 virtual void OnScriptLoaded() {} | |
50 virtual void OnReportException(const base::string16& error_message, | 51 virtual void OnReportException(const base::string16& error_message, |
51 int line_number, | 52 int line_number, |
52 int column_number, | 53 int column_number, |
53 const GURL& source_url) {} | 54 const GURL& source_url) {} |
54 virtual void OnReportConsoleMessage(int source_identifier, | 55 virtual void OnReportConsoleMessage(int source_identifier, |
55 int message_level, | 56 int message_level, |
56 const base::string16& message, | 57 const base::string16& message, |
57 int line_number, | 58 int line_number, |
58 const GURL& source_url) {} | 59 const GURL& source_url) {} |
59 // These should return false if the message is not handled by this | 60 // These should return false if the message is not handled by this |
(...skipping 30 matching lines...) Expand all Loading... | |
90 void ReleaseProcessReference(int process_id); | 91 void ReleaseProcessReference(int process_id); |
91 bool HasProcessToRun() const { return !process_refs_.empty(); } | 92 bool HasProcessToRun() const { return !process_refs_.empty(); } |
92 | 93 |
93 int embedded_worker_id() const { return embedded_worker_id_; } | 94 int embedded_worker_id() const { return embedded_worker_id_; } |
94 Status status() const { return status_; } | 95 Status status() const { return status_; } |
95 int process_id() const { return process_id_; } | 96 int process_id() const { return process_id_; } |
96 int thread_id() const { return thread_id_; } | 97 int thread_id() const { return thread_id_; } |
97 int worker_devtools_agent_route_id() const { | 98 int worker_devtools_agent_route_id() const { |
98 return worker_devtools_agent_route_id_; | 99 return worker_devtools_agent_route_id_; |
99 } | 100 } |
101 void set_worker_devtools_agent_route_id(int route_id) { | |
102 worker_devtools_agent_route_id_ = route_id; | |
103 } | |
100 | 104 |
101 void AddListener(Listener* listener); | 105 void AddListener(Listener* listener); |
102 void RemoveListener(Listener* listener); | 106 void RemoveListener(Listener* listener); |
103 | 107 |
104 private: | 108 private: |
109 class DevToolsManagerBridge : public Listener, | |
kinuko
2014/05/08 11:17:31
nit: Could be a private inner class (or could be i
horo
2014/05/08 14:17:46
Done.
| |
110 public base::RefCounted<DevToolsManagerBridge> { | |
kinuko
2014/05/08 11:17:31
Does this need to be ref-counted?
horo
2014/05/08 14:17:46
EmbeddedWorkerInstance could be deleted while the
kinuko
2014/05/08 16:34:57
This doesn't necessarily mean we need ref-counting
| |
111 public: | |
112 DevToolsManagerBridge(int embedded_worker_id, | |
113 const base::FilePath& storage_partition_path); | |
114 void RegisterToDevToolsManager( | |
115 int process_id, | |
116 const base::Callback<void(int worker_devtools_agent_route_id, | |
117 bool pause_on_start)>& callback); | |
118 void set_scope(const GURL& scope) { scope_ = scope; } | |
119 | |
120 private: | |
121 friend class base::RefCounted<DevToolsManagerBridge>; | |
122 | |
123 virtual ~DevToolsManagerBridge(); | |
124 | |
125 // EmbeddedWorkerInstance::Listener overrides. | |
126 virtual void OnStarted() OVERRIDE; | |
127 virtual void OnStopped() OVERRIDE; | |
128 virtual void OnScriptLoaded() OVERRIDE; | |
129 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
130 | |
131 void RegisteredCallback( | |
132 const base::Callback<void(int worker_devtools_agent_route_id, | |
133 bool pause_on_start)>& callback, | |
134 int worker_devtools_agent_route_id, | |
135 bool pause_on_start); | |
136 | |
137 const int embedded_worker_id_; | |
138 const base::FilePath storage_partition_path_; | |
139 GURL scope_; | |
140 int worker_process_id_; | |
141 int worker_devtools_agent_route_id_; | |
142 }; | |
143 | |
105 typedef ObserverList<Listener> ListenerList; | 144 typedef ObserverList<Listener> ListenerList; |
106 | 145 |
107 friend class EmbeddedWorkerRegistry; | 146 friend class EmbeddedWorkerRegistry; |
108 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop); | 147 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop); |
109 | 148 |
110 typedef std::map<int, int> ProcessRefMap; | 149 typedef std::map<int, int> ProcessRefMap; |
111 | 150 |
112 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker(). | 151 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker(). |
113 // This instance holds a ref of |registry|. | 152 // This instance holds a ref of |registry|. |
114 EmbeddedWorkerInstance(EmbeddedWorkerRegistry* registry, | 153 EmbeddedWorkerInstance(EmbeddedWorkerRegistry* registry, |
115 int embedded_worker_id); | 154 int embedded_worker_id, |
155 const base::FilePath& storage_partition_path); | |
116 | 156 |
117 // Called back from EmbeddedWorkerRegistry after Start() passes control to the | 157 // Called back from EmbeddedWorkerRegistry after Start() passes control to the |
118 // UI thread to acquire a reference to the process. | 158 // UI thread to acquire a reference to the process. |
119 void RecordProcessId(int process_id, | 159 void WorkerProcessAllocated( |
120 ServiceWorkerStatusCode status, | 160 int process_id, |
121 int worker_devtools_agent_route_id); | 161 const base::Callback<void(int worker_devtools_agent_route_id, |
162 bool pause_on_start)>& callback); | |
163 void WorkerProcessAllocationFailed(); | |
122 | 164 |
123 // Called back from Registry when the worker instance has ack'ed that | 165 // Called back from Registry when the worker instance has ack'ed that |
124 // it finished loading the script. | 166 // it finished loading the script. |
125 void OnScriptLoaded(); | 167 void OnScriptLoaded(); |
126 | 168 |
127 // Called back from Registry when the worker instance has ack'ed that | 169 // Called back from Registry when the worker instance has ack'ed that |
128 // it failed to load the script. | 170 // it failed to load the script. |
129 void OnScriptLoadFailed(); | 171 void OnScriptLoadFailed(); |
130 | 172 |
131 // Called back from Registry when the worker instance has ack'ed that | 173 // Called back from Registry when the worker instance has ack'ed that |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 | 207 |
166 scoped_refptr<EmbeddedWorkerRegistry> registry_; | 208 scoped_refptr<EmbeddedWorkerRegistry> registry_; |
167 const int embedded_worker_id_; | 209 const int embedded_worker_id_; |
168 Status status_; | 210 Status status_; |
169 | 211 |
170 // Current running information. -1 indicates the worker is not running. | 212 // Current running information. -1 indicates the worker is not running. |
171 int process_id_; | 213 int process_id_; |
172 int thread_id_; | 214 int thread_id_; |
173 int worker_devtools_agent_route_id_; | 215 int worker_devtools_agent_route_id_; |
174 | 216 |
217 scoped_refptr<DevToolsManagerBridge> dev_tools_manager_bridge_; | |
218 | |
175 ProcessRefMap process_refs_; | 219 ProcessRefMap process_refs_; |
176 ListenerList listener_list_; | 220 ListenerList listener_list_; |
177 | 221 |
178 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); | 222 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); |
179 }; | 223 }; |
180 | 224 |
181 } // namespace content | 225 } // namespace content |
182 | 226 |
183 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ | 227 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ |
OLD | NEW |