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