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