forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_process.h
158 lines (124 loc) · 5.25 KB
/
service_process.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_SERVICE_SERVICE_PROCESS_H_
#define CHROME_SERVICE_SERVICE_PROCESS_H_
#include <memory>
#include <string>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "chrome/service/cloud_print/cloud_print_proxy.h"
#include "chrome/service/service_ipc_server.h"
#include "mojo/edk/embedder/named_platform_handle.h"
#include "mojo/edk/embedder/scoped_platform_handle.h"
#include "mojo/public/cpp/system/message_pipe.h"
class ServiceProcessPrefs;
class ServiceURLRequestContextGetter;
class ServiceProcessState;
namespace base {
class CommandLine;
class SequencedWorkerPool;
class WaitableEvent;
}
namespace mojo {
namespace edk {
class PeerConnection;
class ScopedIPCSupport;
}
}
namespace net {
class NetworkChangeNotifier;
}
// The ServiceProcess does not inherit from ChildProcess because this
// process can live independently of the browser process.
// ServiceProcess Design Notes
// https://sites.google.com/a/chromium.org/dev/developers/design-documents/service-processes
class ServiceProcess : public ServiceIPCServer::Client,
public cloud_print::CloudPrintProxy::Client,
public cloud_print::CloudPrintProxy::Provider {
public:
ServiceProcess();
~ServiceProcess() override;
// Initialize the ServiceProcess with the message loop that it should run on.
// ServiceProcess takes ownership of |state|.
bool Initialize(base::MessageLoopForUI* message_loop,
const base::CommandLine& command_line,
ServiceProcessState* state);
bool Teardown();
// Returns the SingleThreadTaskRunner for the service process io thread (used
// for e.g. network requests and IPC). Returns null before Initialize is
// called and after Teardown is called.
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner() {
return io_thread_ ? io_thread_->task_runner() : nullptr;
}
// Returns the SingleThreadTaskRunner for the service process file thread.
// Used to do I/O operations (not network requests or even file: URL requests)
// to avoid blocking the main thread. Returns null before Initialize is
// called and after Teardown is called.
scoped_refptr<base::SingleThreadTaskRunner> file_task_runner() {
return file_thread_ ? file_thread_->task_runner() : nullptr;
}
// A global event object that is signalled when the main thread's message
// loop exits. This gives background threads a way to observe the main
// thread shutting down.
base::WaitableEvent* GetShutdownEventForTesting() {
return &shutdown_event_;
}
// Shuts down the service process.
void Shutdown();
// ServiceIPCServer::Client implementation.
void OnShutdown() override;
void OnUpdateAvailable() override;
bool OnIPCClientDisconnect() override;
mojo::ScopedMessagePipeHandle CreateChannelMessagePipe() override;
// CloudPrintProxy::Provider implementation.
cloud_print::CloudPrintProxy* GetCloudPrintProxy() override;
// CloudPrintProxy::Client implementation.
void OnCloudPrintProxyEnabled(bool persist_state) override;
void OnCloudPrintProxyDisabled(bool persist_state) override;
ServiceURLRequestContextGetter* GetServiceURLRequestContextGetter();
private:
friend class TestServiceProcess;
// Schedule a call to ShutdownIfNeeded.
void ScheduleShutdownCheck();
// Shuts down the process if no services are enabled and no IPC client is
// connected.
void ShutdownIfNeeded();
// Called exactly ONCE per process instance for each service that gets
// enabled in this process.
void OnServiceEnabled();
// Called exactly ONCE per process instance for each service that gets
// disabled in this process (note that shutdown != disabled).
void OnServiceDisabled();
// Terminate forces the service process to quit.
void Terminate();
std::unique_ptr<net::NetworkChangeNotifier> network_change_notifier_;
std::unique_ptr<base::Thread> io_thread_;
std::unique_ptr<base::Thread> file_thread_;
scoped_refptr<base::SequencedWorkerPool> blocking_pool_;
std::unique_ptr<cloud_print::CloudPrintProxy> cloud_print_proxy_;
std::unique_ptr<ServiceProcessPrefs> service_prefs_;
std::unique_ptr<ServiceIPCServer> ipc_server_;
std::unique_ptr<ServiceProcessState> service_process_state_;
std::unique_ptr<mojo::edk::ScopedIPCSupport> mojo_ipc_support_;
std::unique_ptr<mojo::edk::PeerConnection> peer_connection_;
// An event that will be signalled when we shutdown.
base::WaitableEvent shutdown_event_;
// Pointer to the main message loop that host this object.
base::MessageLoop* main_message_loop_;
// Count of currently enabled services in this process.
int enabled_services_;
// Speficies whether a product update is available.
bool update_available_;
scoped_refptr<ServiceURLRequestContextGetter> request_context_getter_;
#if defined(OS_POSIX)
mojo::edk::ScopedPlatformHandle server_handle_;
#elif defined(OS_WIN)
mojo::edk::NamedPlatformHandle server_handle_;
#endif
DISALLOW_COPY_AND_ASSIGN(ServiceProcess);
};
extern ServiceProcess* g_service_process;
#endif // CHROME_SERVICE_SERVICE_PROCESS_H_