Upstream version 5.34.104.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
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/logging.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/observer_list.h"
16 #include "content/common/content_export.h"
17 #include "content/common/service_worker/service_worker_status_code.h"
18
19 class GURL;
20
21 namespace IPC {
22 class Message;
23 }
24
25 namespace content {
26
27 class EmbeddedWorkerRegistry;
28 struct ServiceWorkerFetchRequest;
29
30 // This gives an interface to control one EmbeddedWorker instance, which
31 // may be 'in-waiting' or running in one of the child processes added by
32 // AddProcessReference().
33 class CONTENT_EXPORT EmbeddedWorkerInstance {
34  public:
35   enum Status {
36     STOPPED,
37     STARTING,
38     RUNNING,
39     STOPPING,
40   };
41
42   class Observer {
43    public:
44     virtual ~Observer() {}
45     virtual void OnStarted() = 0;
46     virtual void OnStopped() = 0;
47     virtual void OnMessageReceived(int request_id,
48                                    const IPC::Message& message) = 0;
49   };
50
51   ~EmbeddedWorkerInstance();
52
53   // Starts the worker. It is invalid to call this when the worker is
54   // not in STOPPED status.
55   ServiceWorkerStatusCode Start(int64 service_worker_version_id,
56                                 const GURL& script_url);
57
58   // Stops the worker. It is invalid to call this when the worker is
59   // not in STARTING or RUNNING status.
60   // This returns false if stopping a worker fails immediately, e.g. when
61   // IPC couldn't be sent to the worker.
62   ServiceWorkerStatusCode Stop();
63
64   // Sends |message| to the embedded worker running in the child process.
65   // It is invalid to call this while the worker is not in RUNNING status.
66   // |request_id| can be optionally used to establish 2-way request-response
67   // messaging (e.g. the receiver can send back a response using the same
68   // request_id).
69   ServiceWorkerStatusCode SendMessage(
70       int request_id, const IPC::Message& message);
71
72   // Add or remove |process_id| to the internal process set where this
73   // worker can be started.
74   void AddProcessReference(int process_id);
75   void ReleaseProcessReference(int process_id);
76
77   int embedded_worker_id() const { return embedded_worker_id_; }
78   Status status() const { return status_; }
79   int process_id() const { return process_id_; }
80   int thread_id() const { return thread_id_; }
81
82   void AddObserver(Observer* observer);
83   void RemoveObserver(Observer* observer);
84
85  private:
86   friend class EmbeddedWorkerRegistry;
87   FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop);
88
89   typedef std::map<int, int> ProcessRefMap;
90
91   // Constructor is called via EmbeddedWorkerRegistry::CreateWorker().
92   // This instance holds a ref of |registry|.
93   EmbeddedWorkerInstance(EmbeddedWorkerRegistry* registry,
94                          int embedded_worker_id);
95
96   // Called back from Registry when the worker instance has ack'ed that
97   // its WorkerGlobalScope is actually started on |thread_id| in the
98   // child process.
99   // This will change the internal status from STARTING to RUNNING.
100   void OnStarted(int thread_id);
101
102   // Called back from Registry when the worker instance has ack'ed that
103   // its WorkerGlobalScope is actually stopped in the child process.
104   // This will change the internal status from STARTING or RUNNING to
105   // STOPPED.
106   void OnStopped();
107
108   // Called back from Registry when the worker instance sends message
109   // to the browser (i.e. EmbeddedWorker observers).
110   void OnMessageReceived(int request_id, const IPC::Message& message);
111
112   // Chooses a process to start this worker and populate process_id_.
113   // Returns false when no process is available.
114   bool ChooseProcess();
115
116   scoped_refptr<EmbeddedWorkerRegistry> registry_;
117   const int embedded_worker_id_;
118   Status status_;
119
120   // Current running information. -1 indicates the worker is not running.
121   int process_id_;
122   int thread_id_;
123
124   ProcessRefMap process_refs_;
125   ObserverList<Observer> observer_list_;
126
127   DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance);
128 };
129
130 }  // namespace content
131
132 #endif  // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_