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/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/observer_list.h" | 16 #include "base/observer_list.h" |
17 #include "base/strings/string16.h" | 17 #include "base/strings/string16.h" |
18 #include "content/common/content_export.h" | 18 #include "content/common/content_export.h" |
19 #include "content/common/service_worker/service_worker_status_code.h" | 19 #include "content/common/service_worker/service_worker_status_code.h" |
20 #include "url/gurl.h" | |
20 | 21 |
21 class GURL; | 22 struct EmbeddedWorkerMsg_StartWorker_Params; |
22 | 23 |
23 namespace IPC { | 24 namespace IPC { |
24 class Message; | 25 class Message; |
25 } | 26 } |
26 | 27 |
27 namespace content { | 28 namespace content { |
28 | 29 |
29 class EmbeddedWorkerRegistry; | 30 class EmbeddedWorkerRegistry; |
31 class ServiceWorkerContextCore; | |
30 struct ServiceWorkerFetchRequest; | 32 struct ServiceWorkerFetchRequest; |
31 | 33 |
32 // This gives an interface to control one EmbeddedWorker instance, which | 34 // 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 | 35 // may be 'in-waiting' or running in one of the child processes added by |
34 // AddProcessReference(). | 36 // AddProcessReference(). |
35 class CONTENT_EXPORT EmbeddedWorkerInstance { | 37 class CONTENT_EXPORT EmbeddedWorkerInstance |
38 : NON_EXPORTED_BASE(public base::SupportsWeakPtr<EmbeddedWorkerInstance>) { | |
kinuko
2014/05/13 05:45:19
Can we not inherit from SupportsWeakPtr but use We
horo
2014/05/13 09:11:21
Done.
| |
36 public: | 39 public: |
37 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; | 40 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; |
38 enum Status { | 41 enum Status { |
39 STOPPED, | 42 STOPPED, |
40 STARTING, | 43 STARTING, |
41 RUNNING, | 44 RUNNING, |
42 STOPPING, | 45 STOPPING, |
43 }; | 46 }; |
44 | 47 |
45 class Listener { | 48 class Listener { |
(...skipping 16 matching lines...) Expand all Loading... | |
62 virtual bool OnMessageReceived(const IPC::Message& message) = 0; | 65 virtual bool OnMessageReceived(const IPC::Message& message) = 0; |
63 }; | 66 }; |
64 | 67 |
65 ~EmbeddedWorkerInstance(); | 68 ~EmbeddedWorkerInstance(); |
66 | 69 |
67 // Starts the worker. It is invalid to call this when the worker is not in | 70 // Starts the worker. It is invalid to call this when the worker is not in |
68 // STOPPED status. |callback| is invoked when the worker's process is created | 71 // STOPPED status. |callback| is invoked when the worker's process is created |
69 // if necessary and the IPC to evaluate the worker's script is sent. | 72 // if necessary and the IPC to evaluate the worker's script is sent. |
70 // Observer::OnStarted() is run when the worker is actually started. | 73 // Observer::OnStarted() is run when the worker is actually started. |
71 void Start(int64 service_worker_version_id, | 74 void Start(int64 service_worker_version_id, |
72 const GURL& scope, | |
73 const GURL& script_url, | 75 const GURL& script_url, |
74 const std::vector<int>& possible_process_ids, | 76 const std::vector<int>& possible_process_ids, |
75 const StatusCallback& callback); | 77 const StatusCallback& callback); |
76 | 78 |
77 // Stops the worker. It is invalid to call this when the worker is | 79 // Stops the worker. It is invalid to call this when the worker is |
78 // not in STARTING or RUNNING status. | 80 // not in STARTING or RUNNING status. |
79 // This returns false if stopping a worker fails immediately, e.g. when | 81 // This returns false if stopping a worker fails immediately, e.g. when |
80 // IPC couldn't be sent to the worker. | 82 // IPC couldn't be sent to the worker. |
81 ServiceWorkerStatusCode Stop(); | 83 ServiceWorkerStatusCode Stop(); |
82 | 84 |
83 // Sends |message| to the embedded worker running in the child process. | 85 // Sends |message| to the embedded worker running in the child process. |
84 // It is invalid to call this while the worker is not in RUNNING status. | 86 // It is invalid to call this while the worker is not in RUNNING status. |
85 ServiceWorkerStatusCode SendMessage(const IPC::Message& message); | 87 ServiceWorkerStatusCode SendMessage(const IPC::Message& message); |
86 | 88 |
87 // Add or remove |process_id| to the internal process set where this | 89 // Add or remove |process_id| to the internal process set where this |
88 // worker can be started. | 90 // worker can be started. |
89 void AddProcessReference(int process_id); | 91 void AddProcessReference(int process_id); |
90 void ReleaseProcessReference(int process_id); | 92 void ReleaseProcessReference(int process_id); |
91 bool HasProcessToRun() const { return !process_refs_.empty(); } | 93 bool HasProcessToRun() const { return !process_refs_.empty(); } |
92 | 94 |
93 int embedded_worker_id() const { return embedded_worker_id_; } | 95 int embedded_worker_id() const { return embedded_worker_id_; } |
94 Status status() const { return status_; } | 96 Status status() const { return status_; } |
95 int process_id() const { return process_id_; } | 97 int process_id() const { return process_id_; } |
96 int thread_id() const { return thread_id_; } | 98 int thread_id() const { return thread_id_; } |
97 int worker_devtools_agent_route_id() const { | |
98 return worker_devtools_agent_route_id_; | |
99 } | |
100 | 99 |
101 void AddListener(Listener* listener); | 100 void AddListener(Listener* listener); |
102 void RemoveListener(Listener* listener); | 101 void RemoveListener(Listener* listener); |
103 | 102 |
104 private: | 103 private: |
105 typedef ObserverList<Listener> ListenerList; | 104 typedef ObserverList<Listener> ListenerList; |
106 | 105 |
107 friend class EmbeddedWorkerRegistry; | 106 friend class EmbeddedWorkerRegistry; |
108 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop); | 107 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop); |
109 | 108 |
110 typedef std::map<int, int> ProcessRefMap; | 109 typedef std::map<int, int> ProcessRefMap; |
111 | 110 |
112 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker(). | 111 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker(). |
113 // This instance holds a ref of |registry|. | 112 // This instance holds a ref of |registry|. |
114 EmbeddedWorkerInstance(EmbeddedWorkerRegistry* registry, | 113 EmbeddedWorkerInstance(base::WeakPtr<ServiceWorkerContextCore> context, |
115 int embedded_worker_id); | 114 int embedded_worker_id, |
115 const GURL& scope); | |
116 | 116 |
117 // Called back from EmbeddedWorkerRegistry after Start() passes control to the | 117 // Called back from ServiceWorkerProcessManager after Start() passes control |
118 // UI thread to acquire a reference to the process. | 118 // to the UI thread to acquire a reference to the process. |
119 void RecordProcessId(int process_id, | 119 static void RunProcessAllocated( |
120 ServiceWorkerStatusCode status, | 120 base::WeakPtr<EmbeddedWorkerInstance> instance, |
121 int worker_devtools_agent_route_id); | 121 base::WeakPtr<ServiceWorkerContextCore> context, |
122 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | |
123 const EmbeddedWorkerInstance::StatusCallback& callback, | |
124 ServiceWorkerStatusCode status, | |
125 int process_id); | |
126 void ProcessAllocated(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | |
127 const StatusCallback& callback, | |
128 int process_id, | |
129 ServiceWorkerStatusCode status); | |
130 // Called back after ProcessAllocated() passes control to the UI thread to | |
131 // register to WorkerDevToolsManager. | |
132 static void RunSendStartWorker( | |
133 base::WeakPtr<EmbeddedWorkerInstance> instance, | |
134 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | |
135 const EmbeddedWorkerInstance::StatusCallback& callback, | |
136 int worker_devtools_agent_route_id, | |
137 bool pause_on_start); | |
138 void SendStartWorker(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, | |
139 const StatusCallback& callback); | |
122 | 140 |
123 // Called back from Registry when the worker instance has ack'ed that | 141 // Called back from Registry when the worker instance has ack'ed that |
124 // it finished loading the script. | 142 // it finished loading the script. |
125 void OnScriptLoaded(); | 143 void OnScriptLoaded(); |
126 | 144 |
127 // Called back from Registry when the worker instance has ack'ed that | 145 // Called back from Registry when the worker instance has ack'ed that |
128 // it failed to load the script. | 146 // it failed to load the script. |
129 void OnScriptLoadFailed(); | 147 void OnScriptLoadFailed(); |
130 | 148 |
131 // Called back from Registry when the worker instance has ack'ed that | 149 // Called back from Registry when the worker instance has ack'ed that |
(...skipping 24 matching lines...) Expand all Loading... | |
156 int message_level, | 174 int message_level, |
157 const base::string16& message, | 175 const base::string16& message, |
158 int line_number, | 176 int line_number, |
159 const GURL& source_url); | 177 const GURL& source_url); |
160 | 178 |
161 // Chooses a list of processes to try to start this worker in, ordered by how | 179 // Chooses a list of processes to try to start this worker in, ordered by how |
162 // many clients are currently in those processes. | 180 // many clients are currently in those processes. |
163 std::vector<int> SortProcesses( | 181 std::vector<int> SortProcesses( |
164 const std::vector<int>& possible_process_ids) const; | 182 const std::vector<int>& possible_process_ids) const; |
165 | 183 |
184 base::WeakPtr<ServiceWorkerContextCore> context_; | |
166 scoped_refptr<EmbeddedWorkerRegistry> registry_; | 185 scoped_refptr<EmbeddedWorkerRegistry> registry_; |
167 const int embedded_worker_id_; | 186 const int embedded_worker_id_; |
187 const GURL scope_; | |
168 Status status_; | 188 Status status_; |
169 | 189 |
170 // Current running information. -1 indicates the worker is not running. | 190 // Current running information. -1 indicates the worker is not running. |
171 int process_id_; | 191 int process_id_; |
172 int thread_id_; | 192 int thread_id_; |
173 int worker_devtools_agent_route_id_; | 193 int worker_devtools_agent_route_id_; |
174 | 194 |
175 ProcessRefMap process_refs_; | 195 ProcessRefMap process_refs_; |
176 ListenerList listener_list_; | 196 ListenerList listener_list_; |
177 | 197 |
178 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); | 198 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); |
179 }; | 199 }; |
180 | 200 |
181 } // namespace content | 201 } // namespace content |
182 | 202 |
183 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ | 203 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ |
OLD | NEW |