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 ~ServiceProcess() override
;
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 // SingleThreadTaskRunner* instead of MessageLoop*. When we have done that,
50 // we can remove the io_thread() and file_thread() accessors and replace them
51 // with 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 // task_runner() as it takes care of checking that a thread is still alive,
58 // race conditions, lifetime differences etc. If you still must use this,
59 // 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 void OnCloudPrintProxyEnabled(bool persist_state
) override
;
94 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 // Called exactly ONCE per process instance for each service that gets
109 // enabled in this process.
110 void OnServiceEnabled();
112 // Called exactly ONCE per process instance for each service that gets
113 // disabled in this process (note that shutdown != disabled).
114 void OnServiceDisabled();
116 // Terminate forces the service process to quit.
119 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
120 scoped_ptr
<base::Thread
> io_thread_
;
121 scoped_ptr
<base::Thread
> file_thread_
;
122 scoped_refptr
<base::SequencedWorkerPool
> blocking_pool_
;
123 scoped_ptr
<cloud_print::CloudPrintProxy
> cloud_print_proxy_
;
124 scoped_ptr
<ServiceProcessPrefs
> service_prefs_
;
125 scoped_ptr
<ServiceIPCServer
> ipc_server_
;
126 scoped_ptr
<ServiceProcessState
> service_process_state_
;
128 // An event that will be signalled when we shutdown.
129 base::WaitableEvent shutdown_event_
;
131 // Pointer to the main message loop that host this object.
132 base::MessageLoop
* main_message_loop_
;
134 // Count of currently enabled services in this process.
135 int enabled_services_
;
137 // Speficies whether a product update is available.
138 bool update_available_
;
140 scoped_refptr
<ServiceURLRequestContextGetter
> request_context_getter_
;
142 DISALLOW_COPY_AND_ASSIGN(ServiceProcess
);
145 extern ServiceProcess
* g_service_process
;
147 #endif // CHROME_SERVICE_SERVICE_PROCESS_H_