Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / service_worker / service_worker_version.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_SERVICE_WORKER_VERSION_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
7
8 #include <vector>
9
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/id_map.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "content/browser/service_worker/embedded_worker_instance.h"
17 #include "content/common/content_export.h"
18 #include "content/common/service_worker/service_worker_status_code.h"
19
20 class GURL;
21
22 namespace content {
23
24 class EmbeddedWorkerRegistry;
25 class ServiceWorkerRegistration;
26 struct ServiceWorkerFetchRequest;
27
28 // This class corresponds to a specific version of a ServiceWorker
29 // script for a given pattern. When a script is upgraded, there may be
30 // more than one ServiceWorkerVersion "running" at a time, but only
31 // one of them is active. This class connects the actual script with a
32 // running worker.
33 // Instances of this class are in one of two install states:
34 // - Pending: The script is in the process of being installed. There
35 //            may be another active script running.
36 // - Active: The script is the only worker handling requests for the
37 //           registration's pattern.
38 //
39 // In addition, a version has a running state (this is a rough
40 // sketch). Since a service worker can be stopped and started at any
41 // time, it will transition among these states multiple times during
42 // its lifetime.
43 // - Stopped: The script is not running
44 // - Starting: A request to fire an event against the version has been
45 //             queued, but the worker is not yet
46 //             loaded/initialized/etc.
47 // - Started: The worker is ready to receive events
48 // - Stopping: The worker is returning to the stopped state.
49 //
50 // The worker can "run" in both the Pending and the Active
51 // install states above. During the Pending state, the worker is only
52 // started in order to fire the 'install' and 'activate'
53 // events. During the Active state, it can receive other events such
54 // as 'fetch'.
55 //
56 // And finally, is_shutdown_ is detects the live-ness of the object
57 // itself. If the object is shut down, then it is in the process of
58 // being deleted from memory. This happens when a version is replaced
59 // as well as at browser shutdown.
60 class CONTENT_EXPORT ServiceWorkerVersion
61     : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>),
62       public EmbeddedWorkerInstance::Observer {
63  public:
64   typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback;
65   typedef base::Callback<void(ServiceWorkerStatusCode,
66                               const IPC::Message& message)> MessageCallback;
67
68   enum Status {
69     STOPPED = EmbeddedWorkerInstance::STOPPED,
70     STARTING = EmbeddedWorkerInstance::STARTING,
71     RUNNING = EmbeddedWorkerInstance::RUNNING,
72     STOPPING = EmbeddedWorkerInstance::STOPPING,
73   };
74
75   ServiceWorkerVersion(
76       ServiceWorkerRegistration* registration,
77       EmbeddedWorkerRegistry* worker_registry,
78       int64 version_id);
79
80   int64 version_id() const { return version_id_; }
81
82   void Shutdown();
83   bool is_shutdown() const { return is_shutdown_; }
84
85   Status status() const {
86     return static_cast<Status>(embedded_worker_->status());
87   }
88
89   // Starts an embedded worker for this version.
90   // This returns OK (success) if the worker is already running.
91   void StartWorker(const StatusCallback& callback);
92
93   // Starts an embedded worker for this version.
94   // This returns OK (success) if the worker is already stopped.
95   void StopWorker(const StatusCallback& callback);
96
97   // Sends an IPC message to the worker.
98   // If the worker is not running this first tries to start it by
99   // calling StartWorker internally.
100   // |callback| can be null if the sender does not need to know if the
101   // message is successfully sent or not.
102   // (If the sender expects the receiver to respond please use
103   // SendMessageAndRegisterCallback instead)
104   void SendMessage(const IPC::Message& message, const StatusCallback& callback);
105
106   // Sends an IPC message to the worker and registers |callback| to
107   // be notified when a response message is received.
108   // The |callback| will be also fired with an error code if the worker
109   // is unexpectedly (being) stopped.
110   // If the worker is not running this first tries to start it by
111   // calling StartWorker internally.
112   void SendMessageAndRegisterCallback(const IPC::Message& message,
113                                       const MessageCallback& callback);
114
115   // Sends install event to the associated embedded worker and asynchronously
116   // calls |callback| when it errors out or it gets response from the worker
117   // to notify install completion.
118   // |active_version_embedded_worker_id| must be a valid positive ID
119   // if there's an active (previous) version running.
120   void DispatchInstallEvent(int active_version_embedded_worker_id,
121                             const StatusCallback& callback);
122
123   // Sends fetch event to the associated embedded worker.
124   // This immediately returns false if the worker is not running
125   // or sending a message to the child process fails.
126   // TODO(kinuko): Make this take callback as well.
127   bool DispatchFetchEvent(const ServiceWorkerFetchRequest& request);
128
129   // These are expected to be called when a renderer process host for the
130   // same-origin as for this ServiceWorkerVersion is created.  The added
131   // processes are used to run an in-renderer embedded worker.
132   void AddProcessToWorker(int process_id);
133   void RemoveProcessToWorker(int process_id);
134
135   EmbeddedWorkerInstance* embedded_worker() { return embedded_worker_.get(); }
136
137   // EmbeddedWorkerInstance::Observer overrides:
138   virtual void OnStarted() OVERRIDE;
139   virtual void OnStopped() OVERRIDE;
140   virtual void OnMessageReceived(int request_id,
141                                  const IPC::Message& message) OVERRIDE;
142
143  private:
144   typedef ServiceWorkerVersion self;
145   friend class base::RefCounted<ServiceWorkerVersion>;
146
147   virtual ~ServiceWorkerVersion();
148
149   const int64 version_id_;
150
151   bool is_shutdown_;
152   scoped_refptr<ServiceWorkerRegistration> registration_;
153   scoped_ptr<EmbeddedWorkerInstance> embedded_worker_;
154
155   // Pending callbacks.
156   std::vector<StatusCallback> start_callbacks_;
157   std::vector<StatusCallback> stop_callbacks_;
158
159   IDMap<MessageCallback, IDMapOwnPointer> message_callbacks_;
160
161   base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_;
162
163   DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion);
164 };
165
166 }  // namespace content
167
168 #endif  // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_