ExtensionSyncService: listen for relevant changes instead of being explicitly called...
[chromium-blink-merge.git] / chrome / browser / service_process / service_process_control.h
blob2bc6d69b7fee1a7507fe9241230666fbe9ef9513
1 // Copyright (c) 2012 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 CHROME_BROWSER_SERVICE_PROCESS_SERVICE_PROCESS_CONTROL_H_
6 #define CHROME_BROWSER_SERVICE_PROCESS_SERVICE_PROCESS_CONTROL_H_
8 #include <queue>
9 #include <set>
10 #include <string>
11 #include <vector>
13 #include "base/basictypes.h"
14 #include "base/callback.h"
15 #include "base/cancelable_callback.h"
16 #include "base/id_map.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/memory/singleton.h"
19 #include "base/process/process.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 #include "ipc/ipc_channel_proxy.h"
23 #include "ipc/ipc_listener.h"
24 #include "ipc/ipc_sender.h"
26 namespace base {
27 class CommandLine;
30 namespace cloud_print {
31 struct CloudPrintProxyInfo;
32 } // namespace cloud_print
34 // A ServiceProcessControl works as a portal between the service process and
35 // the browser process.
37 // It is used to start and terminate the service process. It is also used
38 // to send and receive IPC messages from the service process.
40 // THREADING
42 // This class is accessed on the UI thread through some UI actions. It then
43 // talks to the IPC channel on the IO thread.
44 class ServiceProcessControl : public IPC::Sender,
45 public IPC::Listener,
46 public content::NotificationObserver {
47 public:
48 enum ServiceProcessEvent {
49 SERVICE_EVENT_INITIALIZE,
50 SERVICE_EVENT_ENABLED_ON_LAUNCH,
51 SERVICE_EVENT_ENABLE,
52 SERVICE_EVENT_DISABLE,
53 SERVICE_EVENT_DISABLE_BY_POLICY,
54 SERVICE_EVENT_LAUNCH,
55 SERVICE_EVENT_LAUNCHED,
56 SERVICE_EVENT_LAUNCH_FAILED,
57 SERVICE_EVENT_CHANNEL_CONNECTED,
58 SERVICE_EVENT_CHANNEL_ERROR,
59 SERVICE_EVENT_INFO_REQUEST,
60 SERVICE_EVENT_INFO_REPLY,
61 SERVICE_EVENT_HISTOGRAMS_REQUEST,
62 SERVICE_EVENT_HISTOGRAMS_REPLY,
63 SERVICE_PRINTERS_REQUEST,
64 SERVICE_PRINTERS_REPLY,
65 SERVICE_EVENT_MAX,
68 typedef IDMap<ServiceProcessControl>::iterator iterator;
69 typedef std::queue<IPC::Message> MessageQueue;
70 typedef base::Callback<void(const cloud_print::CloudPrintProxyInfo&)>
71 CloudPrintProxyInfoCallback;
72 typedef base::Callback<void(const std::vector<std::string>&)>
73 PrintersCallback;
75 // Returns the singleton instance of this class.
76 static ServiceProcessControl* GetInstance();
78 // Return true if this object is connected to the service.
79 // Virtual for testing.
80 virtual bool IsConnected() const;
82 // If no service process is currently running, creates a new service process
83 // and connects to it. If a service process is already running this method
84 // will try to connect to it.
85 // |success_task| is called when we have successfully launched the process
86 // and connected to it.
87 // |failure_task| is called when we failed to connect to the service process.
88 // It is OK to pass the same value for |success_task| and |failure_task|. In
89 // this case, the task is invoked on success or failure.
90 // Note that if we are already connected to service process then
91 // |success_task| can be invoked in the context of the Launch call.
92 // Virtual for testing.
93 virtual void Launch(const base::Closure& success_task,
94 const base::Closure& failure_task);
96 // Disconnect the IPC channel from the service process.
97 // Virtual for testing.
98 virtual void Disconnect();
100 // IPC::Listener implementation.
101 bool OnMessageReceived(const IPC::Message& message) override;
102 void OnChannelConnected(int32 peer_pid) override;
103 void OnChannelError() override;
105 // IPC::Sender implementation
106 bool Send(IPC::Message* message) override;
108 // content::NotificationObserver implementation.
109 void Observe(int type,
110 const content::NotificationSource& source,
111 const content::NotificationDetails& details) override;
113 // Send a shutdown message to the service process. IPC channel will be
114 // destroyed after calling this method.
115 // Return true if the message was sent.
116 // Virtual for testing.
117 virtual bool Shutdown();
119 // Send request for cloud print proxy info (enabled state, email, proxy id).
120 // The callback gets the information when received.
121 // Returns true if request was sent. Callback will be called only in case of
122 // reply from service. The method resets any previous callback.
123 // This call starts service if needed.
124 bool GetCloudPrintProxyInfo(
125 const CloudPrintProxyInfoCallback& cloud_print_status_callback);
127 // Send request for histograms collected in service process.
128 // Returns true if request was sent, and callback will be called in case of
129 // success or timeout. The method resets any previous callback.
130 // Returns false if service is not running or other failure, callback will not
131 // be called in this case.
132 bool GetHistograms(const base::Closure& cloud_print_status_callback,
133 const base::TimeDelta& timeout);
135 // Send request for printers available for cloud print proxy.
136 // The callback gets the information when received.
137 // Returns true if request was sent. Callback will be called only in case of
138 // reply from service. The method resets any previous callback.
139 // This call starts service if needed.
140 bool GetPrinters(const PrintersCallback& enumerate_printers_callback);
142 private:
143 // This class is responsible for launching the service process on the
144 // PROCESS_LAUNCHER thread.
145 class Launcher
146 : public base::RefCountedThreadSafe<ServiceProcessControl::Launcher> {
147 public:
148 explicit Launcher(scoped_ptr<base::CommandLine> cmd_line);
149 // Execute the command line to start the process asynchronously. After the
150 // command is executed |task| is called with the process handle on the UI
151 // thread.
152 void Run(const base::Closure& task);
154 bool launched() const { return launched_; }
156 private:
157 friend class base::RefCountedThreadSafe<ServiceProcessControl::Launcher>;
158 virtual ~Launcher();
160 #if !defined(OS_MACOSX)
161 void DoDetectLaunched();
162 #endif // !OS_MACOSX
164 void DoRun();
165 void Notify();
166 scoped_ptr<base::CommandLine> cmd_line_;
167 base::Closure notify_task_;
168 bool launched_;
169 uint32 retry_count_;
170 base::Process process_;
173 friend class MockServiceProcessControl;
174 friend class CloudPrintProxyPolicyStartupTest;
176 ServiceProcessControl();
177 ~ServiceProcessControl() override;
179 friend struct DefaultSingletonTraits<ServiceProcessControl>;
181 typedef std::vector<base::Closure> TaskList;
183 // Message handlers
184 void OnCloudPrintProxyInfo(
185 const cloud_print::CloudPrintProxyInfo& proxy_info);
186 void OnHistograms(const std::vector<std::string>& pickled_histograms);
187 void OnPrinters(const std::vector<std::string>& printers);
189 // Runs callback provided in |GetHistograms()|.
190 void RunHistogramsCallback();
192 // Helper method to invoke all the callbacks based on success or failure.
193 void RunConnectDoneTasks();
195 // Method called by Launcher when the service process is launched.
196 void OnProcessLaunched();
198 // Used internally to connect to the service process.
199 void ConnectInternal();
201 // Takes ownership of the pointer. Split out for testing.
202 void SetChannel(scoped_ptr<IPC::ChannelProxy> channel);
204 static void RunAllTasksHelper(TaskList* task_list);
206 // IPC channel to the service process.
207 scoped_ptr<IPC::ChannelProxy> channel_;
209 // Service process launcher.
210 scoped_refptr<Launcher> launcher_;
212 // Callbacks that get invoked when the channel is successfully connected.
213 TaskList connect_success_tasks_;
214 // Callbacks that get invoked when there was a connection failure.
215 TaskList connect_failure_tasks_;
217 // Callback that gets invoked when a printers is received from
218 // the cloud print proxy.
219 PrintersCallback printers_callback_;
221 // Callback that gets invoked when a status message is received from
222 // the cloud print proxy.
223 CloudPrintProxyInfoCallback cloud_print_info_callback_;
225 // Callback that gets invoked when a message with histograms is received from
226 // the service process.
227 base::Closure histograms_callback_;
229 content::NotificationRegistrar registrar_;
231 // Callback that gets invoked if service didn't reply in time.
232 base::CancelableClosure histograms_timeout_callback_;
235 #endif // CHROME_BROWSER_SERVICE_PROCESS_SERVICE_PROCESS_CONTROL_H_