| 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 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 ~EmbeddedWorkerInstance(); | 69 ~EmbeddedWorkerInstance(); |
| 70 | 70 |
| 71 // Starts the worker. It is invalid to call this when the worker is not in | 71 // Starts the worker. It is invalid to call this when the worker is not in |
| 72 // STOPPED status. |callback| is invoked when the worker's process is created | 72 // STOPPED status. |callback| is invoked when the worker's process is created |
| 73 // if necessary and the IPC to evaluate the worker's script is sent. | 73 // if necessary and the IPC to evaluate the worker's script is sent. |
| 74 // Observer::OnStarted() is run when the worker is actually started. | 74 // Observer::OnStarted() is run when the worker is actually started. |
| 75 void Start(int64 service_worker_version_id, | 75 void Start(int64 service_worker_version_id, |
| 76 const GURL& scope, | 76 const GURL& scope, |
| 77 const GURL& script_url, | 77 const GURL& script_url, |
| 78 bool pause_after_download, | 78 bool pause_after_download, |
| 79 const std::vector<int>& possible_process_ids, | |
| 80 const StatusCallback& callback); | 79 const StatusCallback& callback); |
| 81 | 80 |
| 82 // Stops the worker. It is invalid to call this when the worker is | 81 // Stops the worker. It is invalid to call this when the worker is |
| 83 // not in STARTING or RUNNING status. | 82 // not in STARTING or RUNNING status. |
| 84 // This returns false if stopping a worker fails immediately, e.g. when | 83 // This returns false if stopping a worker fails immediately, e.g. when |
| 85 // IPC couldn't be sent to the worker. | 84 // IPC couldn't be sent to the worker. |
| 86 ServiceWorkerStatusCode Stop(); | 85 ServiceWorkerStatusCode Stop(); |
| 87 | 86 |
| 88 // Sends |message| to the embedded worker running in the child process. | 87 // Sends |message| to the embedded worker running in the child process. |
| 89 // It is invalid to call this while the worker is not in RUNNING status. | 88 // It is invalid to call this while the worker is not in RUNNING status. |
| 90 ServiceWorkerStatusCode SendMessage(const IPC::Message& message); | 89 ServiceWorkerStatusCode SendMessage(const IPC::Message& message); |
| 91 | 90 |
| 92 void ResumeAfterDownload(); | 91 void ResumeAfterDownload(); |
| 93 | 92 |
| 94 // Add or remove |process_id| to the internal process set where this | |
| 95 // worker can be started. | |
| 96 void AddProcessReference(int process_id); | |
| 97 void ReleaseProcessReference(int process_id); | |
| 98 bool HasProcessToRun() const { return !process_refs_.empty(); } | |
| 99 | |
| 100 int embedded_worker_id() const { return embedded_worker_id_; } | 93 int embedded_worker_id() const { return embedded_worker_id_; } |
| 101 Status status() const { return status_; } | 94 Status status() const { return status_; } |
| 102 int process_id() const { return process_id_; } | 95 int process_id() const { return process_id_; } |
| 103 int thread_id() const { return thread_id_; } | 96 int thread_id() const { return thread_id_; } |
| 104 int worker_devtools_agent_route_id() const { | 97 int worker_devtools_agent_route_id() const { |
| 105 return worker_devtools_agent_route_id_; | 98 return worker_devtools_agent_route_id_; |
| 106 } | 99 } |
| 107 | 100 |
| 108 void AddListener(Listener* listener); | 101 void AddListener(Listener* listener); |
| 109 void RemoveListener(Listener* listener); | 102 void RemoveListener(Listener* listener); |
| 110 | 103 |
| 111 private: | 104 private: |
| 112 typedef ObserverList<Listener> ListenerList; | 105 typedef ObserverList<Listener> ListenerList; |
| 113 | 106 |
| 114 friend class EmbeddedWorkerRegistry; | 107 friend class EmbeddedWorkerRegistry; |
| 115 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop); | 108 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop); |
| 116 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, SortProcesses); | |
| 117 | |
| 118 typedef std::map<int, int> ProcessRefMap; | |
| 119 | 109 |
| 120 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker(). | 110 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker(). |
| 121 // This instance holds a ref of |registry|. | 111 // This instance holds a ref of |registry|. |
| 122 EmbeddedWorkerInstance(base::WeakPtr<ServiceWorkerContextCore> context, | 112 EmbeddedWorkerInstance(base::WeakPtr<ServiceWorkerContextCore> context, |
| 123 int embedded_worker_id); | 113 int embedded_worker_id); |
| 124 | 114 |
| 125 // Called back from ServiceWorkerProcessManager after Start() passes control | 115 // Called back from ServiceWorkerProcessManager after Start() passes control |
| 126 // to the UI thread to acquire a reference to the process. | 116 // to the UI thread to acquire a reference to the process. |
| 127 static void RunProcessAllocated( | 117 static void RunProcessAllocated( |
| 128 base::WeakPtr<EmbeddedWorkerInstance> instance, | 118 base::WeakPtr<EmbeddedWorkerInstance> instance, |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 int column_number, | 165 int column_number, |
| 176 const GURL& source_url); | 166 const GURL& source_url); |
| 177 | 167 |
| 178 // Called back from Registry when the worker instance reports to the console. | 168 // Called back from Registry when the worker instance reports to the console. |
| 179 void OnReportConsoleMessage(int source_identifier, | 169 void OnReportConsoleMessage(int source_identifier, |
| 180 int message_level, | 170 int message_level, |
| 181 const base::string16& message, | 171 const base::string16& message, |
| 182 int line_number, | 172 int line_number, |
| 183 const GURL& source_url); | 173 const GURL& source_url); |
| 184 | 174 |
| 185 // Chooses a list of processes to try to start this worker in, ordered by how | |
| 186 // many clients are currently in those processes. | |
| 187 std::vector<int> SortProcesses( | |
| 188 const std::vector<int>& possible_process_ids) const; | |
| 189 | |
| 190 base::WeakPtr<ServiceWorkerContextCore> context_; | 175 base::WeakPtr<ServiceWorkerContextCore> context_; |
| 191 scoped_refptr<EmbeddedWorkerRegistry> registry_; | 176 scoped_refptr<EmbeddedWorkerRegistry> registry_; |
| 192 const int embedded_worker_id_; | 177 const int embedded_worker_id_; |
| 193 Status status_; | 178 Status status_; |
| 194 | 179 |
| 195 // Current running information. -1 indicates the worker is not running. | 180 // Current running information. -1 indicates the worker is not running. |
| 196 int process_id_; | 181 int process_id_; |
| 197 int thread_id_; | 182 int thread_id_; |
| 198 int worker_devtools_agent_route_id_; | 183 int worker_devtools_agent_route_id_; |
| 199 | 184 |
| 200 ProcessRefMap process_refs_; | |
| 201 ListenerList listener_list_; | 185 ListenerList listener_list_; |
| 202 | 186 |
| 203 base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_; | 187 base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_; |
| 204 | 188 |
| 205 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); | 189 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); |
| 206 }; | 190 }; |
| 207 | 191 |
| 208 } // namespace content | 192 } // namespace content |
| 209 | 193 |
| 210 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ | 194 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ |
| OLD | NEW |