| 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/memory/weak_ptr.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" |
| 21 #include "url/gurl.h" |
| 20 | 22 |
| 21 class GURL; | 23 struct EmbeddedWorkerMsg_StartWorker_Params; |
| 22 | 24 |
| 23 namespace IPC { | 25 namespace IPC { |
| 24 class Message; | 26 class Message; |
| 25 } | 27 } |
| 26 | 28 |
| 27 namespace content { | 29 namespace content { |
| 28 | 30 |
| 29 class EmbeddedWorkerRegistry; | 31 class EmbeddedWorkerRegistry; |
| 32 class ServiceWorkerContextCore; |
| 30 struct ServiceWorkerFetchRequest; | 33 struct ServiceWorkerFetchRequest; |
| 31 | 34 |
| 32 // This gives an interface to control one EmbeddedWorker instance, which | 35 // 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 | 36 // may be 'in-waiting' or running in one of the child processes added by |
| 34 // AddProcessReference(). | 37 // AddProcessReference(). |
| 35 class CONTENT_EXPORT EmbeddedWorkerInstance { | 38 class CONTENT_EXPORT EmbeddedWorkerInstance { |
| 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, |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 // Add or remove |process_id| to the internal process set where this | 90 // Add or remove |process_id| to the internal process set where this |
| 88 // worker can be started. | 91 // worker can be started. |
| 89 void AddProcessReference(int process_id); | 92 void AddProcessReference(int process_id); |
| 90 void ReleaseProcessReference(int process_id); | 93 void ReleaseProcessReference(int process_id); |
| 91 bool HasProcessToRun() const { return !process_refs_.empty(); } | 94 bool HasProcessToRun() const { return !process_refs_.empty(); } |
| 92 | 95 |
| 93 int embedded_worker_id() const { return embedded_worker_id_; } | 96 int embedded_worker_id() const { return embedded_worker_id_; } |
| 94 Status status() const { return status_; } | 97 Status status() const { return status_; } |
| 95 int process_id() const { return process_id_; } | 98 int process_id() const { return process_id_; } |
| 96 int thread_id() const { return thread_id_; } | 99 int thread_id() const { return thread_id_; } |
| 97 int worker_devtools_agent_route_id() const { | |
| 98 return worker_devtools_agent_route_id_; | |
| 99 } | |
| 100 | 100 |
| 101 void AddListener(Listener* listener); | 101 void AddListener(Listener* listener); |
| 102 void RemoveListener(Listener* listener); | 102 void RemoveListener(Listener* listener); |
| 103 | 103 |
| 104 private: | 104 private: |
| 105 typedef ObserverList<Listener> ListenerList; | 105 typedef ObserverList<Listener> ListenerList; |
| 106 | 106 |
| 107 friend class EmbeddedWorkerRegistry; | 107 friend class EmbeddedWorkerRegistry; |
| 108 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop); | 108 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop); |
| 109 | 109 |
| 110 typedef std::map<int, int> ProcessRefMap; | 110 typedef std::map<int, int> ProcessRefMap; |
| 111 | 111 |
| 112 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker(). | 112 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker(). |
| 113 // This instance holds a ref of |registry|. | 113 // This instance holds a ref of |registry|. |
| 114 EmbeddedWorkerInstance(EmbeddedWorkerRegistry* registry, | 114 EmbeddedWorkerInstance(base::WeakPtr<ServiceWorkerContextCore> context, |
| 115 int embedded_worker_id); | 115 int embedded_worker_id); |
| 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 void SendStartWorker(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params, |
| 133 const StatusCallback& callback, |
| 134 int worker_devtools_agent_route_id, |
| 135 bool pause_on_start); |
| 122 | 136 |
| 123 // Called back from Registry when the worker instance has ack'ed that | 137 // Called back from Registry when the worker instance has ack'ed that |
| 124 // it finished loading the script. | 138 // it finished loading the script. |
| 125 void OnScriptLoaded(); | 139 void OnScriptLoaded(); |
| 126 | 140 |
| 127 // 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 |
| 128 // it failed to load the script. | 142 // it failed to load the script. |
| 129 void OnScriptLoadFailed(); | 143 void OnScriptLoadFailed(); |
| 130 | 144 |
| 131 // 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 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 156 int message_level, | 170 int message_level, |
| 157 const base::string16& message, | 171 const base::string16& message, |
| 158 int line_number, | 172 int line_number, |
| 159 const GURL& source_url); | 173 const GURL& source_url); |
| 160 | 174 |
| 161 // Chooses a list of processes to try to start this worker in, ordered by how | 175 // Chooses a list of processes to try to start this worker in, ordered by how |
| 162 // many clients are currently in those processes. | 176 // many clients are currently in those processes. |
| 163 std::vector<int> SortProcesses( | 177 std::vector<int> SortProcesses( |
| 164 const std::vector<int>& possible_process_ids) const; | 178 const std::vector<int>& possible_process_ids) const; |
| 165 | 179 |
| 180 base::WeakPtr<ServiceWorkerContextCore> context_; |
| 166 scoped_refptr<EmbeddedWorkerRegistry> registry_; | 181 scoped_refptr<EmbeddedWorkerRegistry> registry_; |
| 167 const int embedded_worker_id_; | 182 const int embedded_worker_id_; |
| 168 Status status_; | 183 Status status_; |
| 169 | 184 |
| 170 // Current running information. -1 indicates the worker is not running. | 185 // Current running information. -1 indicates the worker is not running. |
| 171 int process_id_; | 186 int process_id_; |
| 172 int thread_id_; | 187 int thread_id_; |
| 173 int worker_devtools_agent_route_id_; | 188 int worker_devtools_agent_route_id_; |
| 174 | 189 |
| 175 ProcessRefMap process_refs_; | 190 ProcessRefMap process_refs_; |
| 176 ListenerList listener_list_; | 191 ListenerList listener_list_; |
| 177 | 192 |
| 193 base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_; |
| 194 |
| 178 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); | 195 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); |
| 179 }; | 196 }; |
| 180 | 197 |
| 181 } // namespace content | 198 } // namespace content |
| 182 | 199 |
| 183 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ | 200 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ |
| OLD | NEW |