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
;
20 class ServiceURLRequestContextGetter
;
21 class ServiceProcessState
;
28 class NetworkChangeNotifier
;
31 // The ServiceProcess does not inherit from ChildProcess because this
32 // process can live independently of the browser process.
33 // ServiceProcess Design Notes
34 // https://sites.google.com/a/chromium.org/dev/developers/design-documents/service-processes
35 class ServiceProcess
: public cloud_print::CloudPrintProxy::Client
{
38 virtual ~ServiceProcess();
40 // Initialize the ServiceProcess with the message loop that it should run on.
41 // ServiceProcess takes ownership of |state|.
42 bool Initialize(base::MessageLoopForUI
* message_loop
,
43 const base::CommandLine
& command_line
,
44 ServiceProcessState
* state
);
47 // TODO(sanjeevr): Change various parts of the code such as
48 // net::ProxyService::CreateSystemProxyConfigService to take in
49 // MessageLoopProxy* instead of MessageLoop*. When we have done that, we can
50 // remove the io_thread() and file_thread() accessors and replace them with
51 // io_message_loop_proxy() and file_message_loop_proxy() respectively.
53 // Returns the thread that we perform I/O coordination on (network requests,
54 // communication with renderers, etc.
55 // NOTE: You should ONLY use this to pass to IPC or other objects which must
56 // need a MessageLoop*. If you just want to post a task, use the thread's
57 // message_loop_proxy() as it takes care of checking that a thread is still
58 // alive, race conditions, lifetime differences etc.
59 // If you still must use this, need to check the return value for NULL.
60 base::Thread
* io_thread() const {
61 return io_thread_
.get();
63 // Returns the thread that we perform random file operations on. For code
64 // that wants to do I/O operations (not network requests or even file: URL
65 // requests), this is the thread to use to avoid blocking the UI thread.
66 base::Thread
* file_thread() const {
67 return file_thread_
.get();
70 // A global event object that is signalled when the main thread's message
71 // loop exits. This gives background threads a way to observe the main
72 // thread shutting down.
73 base::WaitableEvent
* shutdown_event() {
74 return &shutdown_event_
;
77 // Shutdown the service process. This is likely triggered by a IPC message.
80 void SetUpdateAvailable() {
81 update_available_
= true;
83 bool update_available() const { return update_available_
; }
85 // Called by the IPC server when a client disconnects. A return value of
86 // true indicates that the IPC server should continue listening for new
88 bool HandleClientDisconnect();
90 cloud_print::CloudPrintProxy
* GetCloudPrintProxy();
92 // CloudPrintProxy::Client implementation.
93 virtual void OnCloudPrintProxyEnabled(bool persist_state
) OVERRIDE
;
94 virtual void OnCloudPrintProxyDisabled(bool persist_state
) OVERRIDE
;
96 ServiceURLRequestContextGetter
* GetServiceURLRequestContextGetter();
99 friend class TestServiceProcess
;
101 // Schedule a call to ShutdownIfNeeded.
102 void ScheduleShutdownCheck();
104 // Shuts down the process if no services are enabled and no clients are
106 void ShutdownIfNeeded();
108 // Schedule a call to CloudPrintPolicyCheckIfNeeded.
109 void ScheduleCloudPrintPolicyCheck();
111 // Launch the browser for a policy check if we're not connected.
112 void CloudPrintPolicyCheckIfNeeded();
114 // Called exactly ONCE per process instance for each service that gets
115 // enabled in this process.
116 void OnServiceEnabled();
118 // Called exactly ONCE per process instance for each service that gets
119 // disabled in this process (note that shutdown != disabled).
120 void OnServiceDisabled();
122 // Terminate forces the service process to quit.
125 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
126 scoped_ptr
<base::Thread
> io_thread_
;
127 scoped_ptr
<base::Thread
> file_thread_
;
128 scoped_refptr
<base::SequencedWorkerPool
> blocking_pool_
;
129 scoped_ptr
<cloud_print::CloudPrintProxy
> cloud_print_proxy_
;
130 scoped_ptr
<ServiceProcessPrefs
> service_prefs_
;
131 scoped_ptr
<ServiceIPCServer
> ipc_server_
;
132 scoped_ptr
<ServiceProcessState
> service_process_state_
;
134 // An event that will be signalled when we shutdown.
135 base::WaitableEvent shutdown_event_
;
137 // Pointer to the main message loop that host this object.
138 base::MessageLoop
* main_message_loop_
;
140 // Count of currently enabled services in this process.
141 int enabled_services_
;
143 // Speficies whether a product update is available.
144 bool update_available_
;
146 scoped_refptr
<ServiceURLRequestContextGetter
> request_context_getter_
;
148 DISALLOW_COPY_AND_ASSIGN(ServiceProcess
);
151 extern ServiceProcess
* g_service_process
;
153 #endif // CHROME_SERVICE_SERVICE_PROCESS_H_