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"
17 #include "chrome/service/service_ipc_server.h"
19 class ServiceProcessPrefs
;
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 ServiceIPCServer::Client
,
36 public cloud_print::CloudPrintProxy::Client
{
39 ~ServiceProcess() override
;
41 // Initialize the ServiceProcess with the message loop that it should run on.
42 // ServiceProcess takes ownership of |state|.
43 bool Initialize(base::MessageLoopForUI
* message_loop
,
44 const base::CommandLine
& command_line
,
45 ServiceProcessState
* state
);
49 // Returns the SingleThreadTaskRunner for the service process io thread (used
50 // for e.g. network requests and IPC). Returns null before Initialize is
51 // called and after Teardown is called.
52 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner() {
53 return io_thread_
? io_thread_
->task_runner() : nullptr;
56 // Returns the SingleThreadTaskRunner for the service process file thread.
57 // Used to do I/O operations (not network requests or even file: URL requests)
58 // to avoid blocking the main thread. Returns null before Initialize is
59 // called and after Teardown is called.
60 scoped_refptr
<base::SingleThreadTaskRunner
> file_task_runner() {
61 return file_thread_
? file_thread_
->task_runner() : nullptr;
64 // A global event object that is signalled when the main thread's message
65 // loop exits. This gives background threads a way to observe the main
66 // thread shutting down.
67 base::WaitableEvent
* GetShutdownEventForTesting() {
68 return &shutdown_event_
;
71 // Shuts down the service process.
74 // ServiceIPCServer::Client implementation.
75 void OnShutdown() override
;
76 void OnUpdateAvailable() override
;
77 bool OnIPCClientDisconnect() override
;
79 cloud_print::CloudPrintProxy
* GetCloudPrintProxy();
81 // CloudPrintProxy::Client implementation.
82 void OnCloudPrintProxyEnabled(bool persist_state
) override
;
83 void OnCloudPrintProxyDisabled(bool persist_state
) override
;
85 ServiceURLRequestContextGetter
* GetServiceURLRequestContextGetter();
88 friend class TestServiceProcess
;
90 // Schedule a call to ShutdownIfNeeded.
91 void ScheduleShutdownCheck();
93 // Shuts down the process if no services are enabled and no IPC client is
95 void ShutdownIfNeeded();
97 // Called exactly ONCE per process instance for each service that gets
98 // enabled in this process.
99 void OnServiceEnabled();
101 // Called exactly ONCE per process instance for each service that gets
102 // disabled in this process (note that shutdown != disabled).
103 void OnServiceDisabled();
105 // Terminate forces the service process to quit.
108 scoped_ptr
<net::NetworkChangeNotifier
> network_change_notifier_
;
109 scoped_ptr
<base::Thread
> io_thread_
;
110 scoped_ptr
<base::Thread
> file_thread_
;
111 scoped_refptr
<base::SequencedWorkerPool
> blocking_pool_
;
112 scoped_ptr
<cloud_print::CloudPrintProxy
> cloud_print_proxy_
;
113 scoped_ptr
<ServiceProcessPrefs
> service_prefs_
;
114 scoped_ptr
<ServiceIPCServer
> ipc_server_
;
115 scoped_ptr
<ServiceProcessState
> service_process_state_
;
117 // An event that will be signalled when we shutdown.
118 base::WaitableEvent shutdown_event_
;
120 // Pointer to the main message loop that host this object.
121 base::MessageLoop
* main_message_loop_
;
123 // Count of currently enabled services in this process.
124 int enabled_services_
;
126 // Speficies whether a product update is available.
127 bool update_available_
;
129 scoped_refptr
<ServiceURLRequestContextGetter
> request_context_getter_
;
131 DISALLOW_COPY_AND_ASSIGN(ServiceProcess
);
134 extern ServiceProcess
* g_service_process
;
136 #endif // CHROME_SERVICE_SERVICE_PROCESS_H_