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_
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"
26 class EmbeddedWorkerRegistry;
27 struct ServiceWorkerFetchRequest;
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 {
43 virtual ~Observer() {}
44 virtual void OnStarted() = 0;
45 virtual void OnStopped() = 0;
46 virtual void OnMessageReceived(const IPC::Message& message) = 0;
49 ~EmbeddedWorkerInstance();
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);
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.
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);
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);
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_; }
79 void AddObserver(Observer* observer);
80 void RemoveObserver(Observer* observer);
83 friend class EmbeddedWorkerRegistry;
84 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop);
86 typedef std::map<int, int> ProcessRefMap;
88 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker().
89 // This instance holds a ref of |registry|.
90 EmbeddedWorkerInstance(EmbeddedWorkerRegistry* registry,
91 int embedded_worker_id);
93 // Called back from Registry when the worker instance has ack'ed that
94 // its WorkerGlobalScope is actually started on |thread_id| in the
96 // This will change the internal status from STARTING to RUNNING.
97 void OnStarted(int thread_id);
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
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);
109 // Chooses a process to start this worker and populate process_id_.
110 // Returns false when no process is available.
111 bool ChooseProcess();
113 scoped_refptr<EmbeddedWorkerRegistry> registry_;
114 const int embedded_worker_id_;
117 // Current running information. -1 indicates the worker is not running.
121 ProcessRefMap process_refs_;
122 ObserverList<Observer> observer_list_;
124 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance);
127 } // namespace content
129 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_