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 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 15 matching lines...) Expand all Loading... |
26 namespace content { | 26 namespace content { |
27 | 27 |
28 class EmbeddedWorkerRegistry; | 28 class EmbeddedWorkerRegistry; |
29 struct ServiceWorkerFetchRequest; | 29 struct ServiceWorkerFetchRequest; |
30 | 30 |
31 // This gives an interface to control one EmbeddedWorker instance, which | 31 // This gives an interface to control one EmbeddedWorker instance, which |
32 // may be 'in-waiting' or running in one of the child processes added by | 32 // may be 'in-waiting' or running in one of the child processes added by |
33 // AddProcessReference(). | 33 // AddProcessReference(). |
34 class CONTENT_EXPORT EmbeddedWorkerInstance { | 34 class CONTENT_EXPORT EmbeddedWorkerInstance { |
35 public: | 35 public: |
| 36 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; |
36 enum Status { | 37 enum Status { |
37 STOPPED, | 38 STOPPED, |
38 STARTING, | 39 STARTING, |
39 RUNNING, | 40 RUNNING, |
40 STOPPING, | 41 STOPPING, |
41 }; | 42 }; |
42 | 43 |
43 class Listener { | 44 class Listener { |
44 public: | 45 public: |
45 virtual ~Listener() {} | 46 virtual ~Listener() {} |
46 virtual void OnStarted() = 0; | 47 virtual void OnStarted() = 0; |
47 virtual void OnStopped() = 0; | 48 virtual void OnStopped() = 0; |
48 virtual void OnReportException(const base::string16& error_message, | 49 virtual void OnReportException(const base::string16& error_message, |
49 int line_number, | 50 int line_number, |
50 int column_number, | 51 int column_number, |
51 const GURL& source_url) {} | 52 const GURL& source_url) {} |
52 virtual void OnReportConsoleMessage(int source_identifier, | 53 virtual void OnReportConsoleMessage(int source_identifier, |
53 int message_level, | 54 int message_level, |
54 const base::string16& message, | 55 const base::string16& message, |
55 int line_number, | 56 int line_number, |
56 const GURL& source_url) {} | 57 const GURL& source_url) {} |
57 // These should return false if the message is not handled by this | 58 // These should return false if the message is not handled by this |
58 // listener. (TODO(kinuko): consider using IPC::Listener interface) | 59 // listener. (TODO(kinuko): consider using IPC::Listener interface) |
59 // TODO(kinuko): Deprecate OnReplyReceived. | 60 // TODO(kinuko): Deprecate OnReplyReceived. |
60 virtual bool OnMessageReceived(const IPC::Message& message) = 0; | 61 virtual bool OnMessageReceived(const IPC::Message& message) = 0; |
61 }; | 62 }; |
62 | 63 |
63 ~EmbeddedWorkerInstance(); | 64 ~EmbeddedWorkerInstance(); |
64 | 65 |
65 // Starts the worker. It is invalid to call this when the worker is | 66 // Starts the worker. It is invalid to call this when the worker is not in |
66 // not in STOPPED status. | 67 // STOPPED status. |callback| is invoked when the worker's process is created |
67 ServiceWorkerStatusCode Start(int64 service_worker_version_id, | 68 // if necessary and the IPC to evaluate the worker's script is sent. |
68 const GURL& scope, | 69 // Observer::OnStarted() is run when the worker is actually started. |
69 const GURL& script_url); | 70 void Start(int64 service_worker_version_id, |
| 71 const GURL& scope, |
| 72 const GURL& script_url, |
| 73 const std::vector<int>& possible_process_ids, |
| 74 const StatusCallback& callback); |
70 | 75 |
71 // Stops the worker. It is invalid to call this when the worker is | 76 // Stops the worker. It is invalid to call this when the worker is |
72 // not in STARTING or RUNNING status. | 77 // not in STARTING or RUNNING status. |
73 // This returns false if stopping a worker fails immediately, e.g. when | 78 // This returns false if stopping a worker fails immediately, e.g. when |
74 // IPC couldn't be sent to the worker. | 79 // IPC couldn't be sent to the worker. |
75 ServiceWorkerStatusCode Stop(); | 80 ServiceWorkerStatusCode Stop(); |
76 | 81 |
77 // Sends |message| to the embedded worker running in the child process. | 82 // Sends |message| to the embedded worker running in the child process. |
78 // It is invalid to call this while the worker is not in RUNNING status. | 83 // It is invalid to call this while the worker is not in RUNNING status. |
79 ServiceWorkerStatusCode SendMessage(const IPC::Message& message); | 84 ServiceWorkerStatusCode SendMessage(const IPC::Message& message); |
(...skipping 18 matching lines...) Expand all Loading... |
98 friend class EmbeddedWorkerRegistry; | 103 friend class EmbeddedWorkerRegistry; |
99 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop); | 104 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop); |
100 | 105 |
101 typedef std::map<int, int> ProcessRefMap; | 106 typedef std::map<int, int> ProcessRefMap; |
102 | 107 |
103 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker(). | 108 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker(). |
104 // This instance holds a ref of |registry|. | 109 // This instance holds a ref of |registry|. |
105 EmbeddedWorkerInstance(EmbeddedWorkerRegistry* registry, | 110 EmbeddedWorkerInstance(EmbeddedWorkerRegistry* registry, |
106 int embedded_worker_id); | 111 int embedded_worker_id); |
107 | 112 |
| 113 // Called back from EmbeddedWorkerRegistry after Start() passes control to the |
| 114 // UI thread to acquire a reference to the process. |
| 115 void RecordProcessId(int process_id, ServiceWorkerStatusCode status); |
| 116 |
108 // Called back from Registry when the worker instance has ack'ed that | 117 // Called back from Registry when the worker instance has ack'ed that |
109 // its WorkerGlobalScope is actually started on |thread_id| in the | 118 // its WorkerGlobalScope is actually started on |thread_id| in the |
110 // child process. | 119 // child process. |
111 // This will change the internal status from STARTING to RUNNING. | 120 // This will change the internal status from STARTING to RUNNING. |
112 void OnStarted(int thread_id); | 121 void OnStarted(int thread_id); |
113 | 122 |
114 // Called back from Registry when the worker instance has ack'ed that | 123 // Called back from Registry when the worker instance has ack'ed that |
115 // its WorkerGlobalScope is actually stopped in the child process. | 124 // its WorkerGlobalScope is actually stopped in the child process. |
116 // This will change the internal status from STARTING or RUNNING to | 125 // This will change the internal status from STARTING or RUNNING to |
117 // STOPPED. | 126 // STOPPED. |
(...skipping 10 matching lines...) Expand all Loading... |
128 int column_number, | 137 int column_number, |
129 const GURL& source_url); | 138 const GURL& source_url); |
130 | 139 |
131 // Called back from Registry when the worker instance reports to the console. | 140 // Called back from Registry when the worker instance reports to the console. |
132 void OnReportConsoleMessage(int source_identifier, | 141 void OnReportConsoleMessage(int source_identifier, |
133 int message_level, | 142 int message_level, |
134 const base::string16& message, | 143 const base::string16& message, |
135 int line_number, | 144 int line_number, |
136 const GURL& source_url); | 145 const GURL& source_url); |
137 | 146 |
138 // Chooses a process to start this worker and populate process_id_. | 147 // Chooses a list of processes to try to start this worker in, ordered by how |
139 // Returns false when no process is available. | 148 // many clients are currently in those processes. |
140 bool ChooseProcess(); | 149 std::vector<int> SortProcesses( |
| 150 const std::vector<int>& possible_process_ids) const; |
141 | 151 |
142 scoped_refptr<EmbeddedWorkerRegistry> registry_; | 152 scoped_refptr<EmbeddedWorkerRegistry> registry_; |
143 const int embedded_worker_id_; | 153 const int embedded_worker_id_; |
144 Status status_; | 154 Status status_; |
145 | 155 |
146 // Current running information. -1 indicates the worker is not running. | 156 // Current running information. -1 indicates the worker is not running. |
147 int process_id_; | 157 int process_id_; |
148 int thread_id_; | 158 int thread_id_; |
149 | 159 |
150 ProcessRefMap process_refs_; | 160 ProcessRefMap process_refs_; |
151 ListenerList listener_list_; | 161 ListenerList listener_list_; |
152 | 162 |
153 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); | 163 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); |
154 }; | 164 }; |
155 | 165 |
156 } // namespace content | 166 } // namespace content |
157 | 167 |
158 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ | 168 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ |
OLD | NEW |