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