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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/callback_forward.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/logging.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/observer_list.h"
19 #include "base/strings/string16.h"
20 #include "content/common/content_export.h"
21 #include "content/common/service_worker/service_worker_status_code.h"
24 struct EmbeddedWorkerMsg_StartWorker_Params;
32 class EmbeddedWorkerRegistry;
33 class ServiceWorkerContextCore;
34 struct ServiceWorkerFetchRequest;
36 // This gives an interface to control one EmbeddedWorker instance, which
37 // may be 'in-waiting' or running in one of the child processes added by
38 // AddProcessReference().
39 class CONTENT_EXPORT EmbeddedWorkerInstance {
41 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback;
51 virtual ~Listener() {}
52 virtual void OnStarted() {}
53 virtual void OnStopped() {}
54 virtual void OnPausedAfterDownload() {}
55 virtual void OnReportException(const base::string16& error_message,
58 const GURL& source_url) {}
59 virtual void OnReportConsoleMessage(int source_identifier,
61 const base::string16& message,
63 const GURL& source_url) {}
64 // These should return false if the message is not handled by this
65 // listener. (TODO(kinuko): consider using IPC::Listener interface)
66 // TODO(kinuko): Deprecate OnReplyReceived.
67 virtual bool OnMessageReceived(const IPC::Message& message) = 0;
70 ~EmbeddedWorkerInstance();
72 // Starts the worker. It is invalid to call this when the worker is not in
73 // STOPPED status. |callback| is invoked when the worker's process is created
74 // if necessary and the IPC to evaluate the worker's script is sent.
75 // Observer::OnStarted() is run when the worker is actually started.
76 void Start(int64 service_worker_version_id,
78 const GURL& script_url,
79 bool pause_after_download,
80 const StatusCallback& callback);
82 // Stops the worker. It is invalid to call this when the worker is
83 // not in STARTING or RUNNING status.
84 // This returns false if stopping a worker fails immediately, e.g. when
85 // IPC couldn't be sent to the worker.
86 ServiceWorkerStatusCode Stop();
88 // 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.
90 ServiceWorkerStatusCode SendMessage(const IPC::Message& message);
92 void ResumeAfterDownload();
94 int embedded_worker_id() const { return embedded_worker_id_; }
95 Status status() const { return status_; }
96 int process_id() const { return process_id_; }
97 int thread_id() const { return thread_id_; }
98 int worker_devtools_agent_route_id() const {
99 return worker_devtools_agent_route_id_;
102 void AddListener(Listener* listener);
103 void RemoveListener(Listener* listener);
106 typedef ObserverList<Listener> ListenerList;
108 friend class EmbeddedWorkerRegistry;
109 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop);
111 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker().
112 // This instance holds a ref of |registry|.
113 EmbeddedWorkerInstance(base::WeakPtr<ServiceWorkerContextCore> context,
114 int embedded_worker_id);
116 // Called back from ServiceWorkerProcessManager after Start() passes control
117 // to the UI thread to acquire a reference to the process.
118 static void RunProcessAllocated(
119 base::WeakPtr<EmbeddedWorkerInstance> instance,
120 base::WeakPtr<ServiceWorkerContextCore> context,
121 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
122 const EmbeddedWorkerInstance::StatusCallback& callback,
123 ServiceWorkerStatusCode status,
125 void ProcessAllocated(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
126 const StatusCallback& callback,
128 ServiceWorkerStatusCode status);
129 // Called back after ProcessAllocated() passes control to the UI thread to
130 // register to WorkerDevToolsManager.
131 void SendStartWorker(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
132 const StatusCallback& callback,
133 int worker_devtools_agent_route_id,
134 bool wait_for_debugger);
136 // Called back from Registry when the worker instance has ack'ed that
137 // it is ready for inspection.
138 void OnReadyForInspection();
140 // Called back from Registry when the worker instance has ack'ed that
141 // it finished loading the script and has started a worker thread.
142 void OnScriptLoaded(int thread_id);
144 // Called back from Registry when the worker instance has ack'ed that
145 // it failed to load the script.
146 void OnScriptLoadFailed();
148 // Called back from Registry when the worker instance has ack'ed that
149 // it finished evaluating the script.
150 void OnScriptEvaluated(bool success);
152 // Called back from Registry when the worker instance has ack'ed that
153 // its WorkerGlobalScope is actually started and parsed.
154 // This will change the internal status from STARTING to RUNNING.
157 void OnPausedAfterDownload();
159 // Called back from Registry when the worker instance has ack'ed that
160 // its WorkerGlobalScope is actually stopped in the child process.
161 // This will change the internal status from STARTING or RUNNING to
165 // Called back from Registry when the worker instance sends message
166 // to the browser (i.e. EmbeddedWorker observers).
167 // Returns false if the message is not handled.
168 bool OnMessageReceived(const IPC::Message& message);
170 // Called back from Registry when the worker instance reports the exception.
171 void OnReportException(const base::string16& error_message,
174 const GURL& source_url);
176 // Called back from Registry when the worker instance reports to the console.
177 void OnReportConsoleMessage(int source_identifier,
179 const base::string16& message,
181 const GURL& source_url);
183 base::WeakPtr<ServiceWorkerContextCore> context_;
184 scoped_refptr<EmbeddedWorkerRegistry> registry_;
185 const int embedded_worker_id_;
188 // Current running information. -1 indicates the worker is not running.
191 int worker_devtools_agent_route_id_;
193 StatusCallback start_callback_;
195 ListenerList listener_list_;
197 base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_;
199 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance);
202 } // namespace content
204 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_