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_SERVICE_SERVICE_PROCESS_H_
6 #define CHROME_SERVICE_SERVICE_PROCESS_H_
10 #include "base/gtest_prod_util.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/threading/sequenced_worker_pool.h"
14 #include "base/threading/thread.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "chrome/service/cloud_print/cloud_print_proxy.h"
18 class ServiceProcessPrefs
;
19 class ServiceIPCServer
;
21 class ServiceURLRequestContextGetter
;
22 class ServiceProcessState
;
25 class NetworkChangeNotifier
;
30 // The ServiceProcess does not inherit from ChildProcess because this
31 // process can live independently of the browser process.
32 // ServiceProcess Design Notes
33 // https://sites.google.com/a/chromium.org/dev/developers/design-documents/service-processes
34 class ServiceProcess
: public cloud_print::CloudPrintProxy::Client
{
37 virtual ~ServiceProcess();
39 // Initialize the ServiceProcess with the message loop that it should run on.
40 // ServiceProcess takes ownership of |state|.
41 bool Initialize(base::MessageLoopForUI
* message_loop
,
42 const CommandLine
& command_line
,
43 ServiceProcessState
* state
);
46 // TODO(sanjeevr): Change various parts of the code such as
47 // net::ProxyService::CreateSystemProxyConfigService to take in
48 // MessageLoopProxy* instead of MessageLoop*. When we have done that, we can
49 // remove the io_thread() and file_thread() accessors and replace them with
50 // io_message_loop_proxy() and file_message_loop_proxy() respectively.
52 // Returns the thread that we perform I/O coordination on (network requests,
53 // communication with renderers, etc.
54 // NOTE: You should ONLY use this to pass to IPC or other objects which must
55 // need a MessageLoop*. If you just want to post a task, use the thread's
56 // message_loop_proxy() as it takes care of checking that a thread is still
57 // alive, race conditions, lifetime differences etc.
58 // If you still must use this, need to check the return value for NULL.
59 base::Thread
* io_thread() const {
60 return io_thread_
.get();
62 // Returns the thread that we perform random file operations on. For code
63 // that wants to do I/O operations (not network requests or even file: URL
64 // requests), this is the thread to use to avoid blocking the UI thread.
65 base::Thread
* file_thread() const {
66 return file_thread_
.get();
69 // A global event object that is signalled when the main thread's message
70 // loop exits. This gives background threads a way to observe the main
71 // thread shutting down.
72 base::WaitableEvent
* shutdown_event() {
73 return &shutdown_event_
;
76 // Shutdown the service process. This is likely triggered by a IPC message.
79 void SetUpdateAvailable() {
80 update_available_
= true;
82 bool update_available() const { return update_available_
; }
84 // Called by the IPC server when a client disconnects. A return value of
85 // true indicates that the IPC server should continue listening for new
87 bool HandleClientDisconnect();
89 cloud_print::CloudPrintProxy
* GetCloudPrintProxy();
91 // CloudPrintProxy::Client implementation.
92 virtual void OnCloudPrintProxyEnabled(bool persist_state
) OVERRIDE
;
93 virtual void OnCloudPrintProxyDisabled(bool persist_state
) OVERRIDE
;
95 ServiceURLRequestContextGetter
* GetServiceURLRequestContextGetter();
98 friend class TestServiceProcess
;
100 // Schedule a call to ShutdownIfNeeded.
101 void ScheduleShutdownCheck();
103 // Shuts down the process if no services are enabled and no clients are
105 void ShutdownIfNeeded();
107 // Schedule a call to CloudPrintPolicyCheckIfNeeded.
108 void ScheduleCloudPrintPolicyCheck();
110 // Launch the browser for a policy check if we're not connected.
111 void CloudPrintPolicyCheckIfNeeded();
113 // Called exactly ONCE per process instance for each service that gets
114 // enabled in this process.
115 void OnServiceEnabled();
117 // Called exactly ONCE per process instance for each service that gets
118 // disabled in this process (note that shutdown != disabled).
119 void OnServiceDisabled();
121 // Terminate forces the service process to quit.
124 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
125 scoped_ptr
<base::Thread
> io_thread_
;
126 scoped_ptr
<base::Thread
> file_thread_
;
127 scoped_refptr
<base::SequencedWorkerPool
> blocking_pool_
;
128 scoped_ptr
<cloud_print::CloudPrintProxy
> cloud_print_proxy_
;
129 scoped_ptr
<ServiceProcessPrefs
> service_prefs_
;
130 scoped_ptr
<ServiceIPCServer
> ipc_server_
;
131 scoped_ptr
<ServiceProcessState
> service_process_state_
;
133 // An event that will be signalled when we shutdown.
134 base::WaitableEvent shutdown_event_
;
136 // Pointer to the main message loop that host this object.
137 base::MessageLoop
* main_message_loop_
;
139 // Count of currently enabled services in this process.
140 int enabled_services_
;
142 // Speficies whether a product update is available.
143 bool update_available_
;
145 scoped_refptr
<ServiceURLRequestContextGetter
> request_context_getter_
;
147 DISALLOW_COPY_AND_ASSIGN(ServiceProcess
);
150 extern ServiceProcess
* g_service_process
;
152 #endif // CHROME_SERVICE_SERVICE_PROCESS_H_