Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / embedded_worker_instance.h
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.
4
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_
7
8 #include <map>
9 #include <vector>
10
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"
22 #include "url/gurl.h"
23
24 struct EmbeddedWorkerMsg_StartWorker_Params;
25
26 namespace IPC {
27 class Message;
28 }
29
30 namespace content {
31
32 class EmbeddedWorkerRegistry;
33 class ServiceWorkerContextCore;
34 struct ServiceWorkerFetchRequest;
35
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 {
40  public:
41   typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback;
42   enum Status {
43     STOPPED,
44     STARTING,
45     RUNNING,
46     STOPPING,
47   };
48
49   class Listener {
50    public:
51     virtual ~Listener() {}
52     virtual void OnStarted() {}
53     virtual void OnStopped() {}
54     virtual void OnPausedAfterDownload() {}
55     virtual void OnReportException(const base::string16& error_message,
56                                    int line_number,
57                                    int column_number,
58                                    const GURL& source_url) {}
59     virtual void OnReportConsoleMessage(int source_identifier,
60                                         int message_level,
61                                         const base::string16& message,
62                                         int line_number,
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;
68   };
69
70   ~EmbeddedWorkerInstance();
71
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,
77              const GURL& scope,
78              const GURL& script_url,
79              bool pause_after_download,
80              const StatusCallback& callback);
81
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();
87
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);
91
92   void ResumeAfterDownload();
93
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_;
100   }
101
102   void AddListener(Listener* listener);
103   void RemoveListener(Listener* listener);
104
105  private:
106   typedef ObserverList<Listener> ListenerList;
107
108   friend class EmbeddedWorkerRegistry;
109   FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop);
110
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);
115
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,
124       int process_id);
125   void ProcessAllocated(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
126                         const StatusCallback& callback,
127                         int process_id,
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);
135
136   // Called back from Registry when the worker instance has ack'ed that
137   // it is ready for inspection.
138   void OnReadyForInspection();
139
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);
143
144   // Called back from Registry when the worker instance has ack'ed that
145   // it failed to load the script.
146   void OnScriptLoadFailed();
147
148   // Called back from Registry when the worker instance has ack'ed that
149   // it finished evaluating the script.
150   void OnScriptEvaluated(bool success);
151
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.
155   void OnStarted();
156
157   void OnPausedAfterDownload();
158
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
162   // STOPPED.
163   void OnStopped();
164
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);
169
170   // Called back from Registry when the worker instance reports the exception.
171   void OnReportException(const base::string16& error_message,
172                          int line_number,
173                          int column_number,
174                          const GURL& source_url);
175
176   // Called back from Registry when the worker instance reports to the console.
177   void OnReportConsoleMessage(int source_identifier,
178                               int message_level,
179                               const base::string16& message,
180                               int line_number,
181                               const GURL& source_url);
182
183   base::WeakPtr<ServiceWorkerContextCore> context_;
184   scoped_refptr<EmbeddedWorkerRegistry> registry_;
185   const int embedded_worker_id_;
186   Status status_;
187
188   // Current running information. -1 indicates the worker is not running.
189   int process_id_;
190   int thread_id_;
191   int worker_devtools_agent_route_id_;
192
193   StatusCallback start_callback_;
194
195   ListenerList listener_list_;
196
197   base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_;
198
199   DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance);
200 };
201
202 }  // namespace content
203
204 #endif  // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_