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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
// Copyright 2015 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 COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_
#define COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_
#include <string>
#include <vector>
#include "base/files/scoped_file.h"
#include "base/gtest_prod_util.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/observer_list.h"
#include "base/values.h"
#include "components/arc/common/arc_bridge.mojom.h"
namespace base {
class CommandLine;
} // namespace base
namespace arc {
class ArcBridgeBootstrap;
// The Chrome-side service that handles ARC instances and ARC bridge creation.
// This service handles the lifetime of ARC instances and sets up the
// communication channel (the ARC bridge) used to send and receive messages.
class ArcBridgeService : public ArcBridgeHost {
public:
// The possible states of the bridge. In the normal flow, the state changes
// in the following sequence:
//
// STOPPED
// PrerequisitesChanged() ->
// CONNECTING
// OnConnectionEstablished() ->
// READY
//
// The ArcBridgeBootstrap state machine can be thought of being substates of
// ArcBridgeService's CONNECTING state.
//
// *
// StopInstance() ->
// STOPPING
// OnStopped() ->
// STOPPED
enum class State {
// ARC is not currently running.
STOPPED,
// The request to connect has been sent.
CONNECTING,
// The instance has started, and the bridge is fully established.
CONNECTED,
// The ARC instance has finished initializing and is now ready for user
// interaction.
READY,
// The ARC instance has started shutting down.
STOPPING,
};
// Notifies life cycle events of ArcBridgeService.
class Observer {
public:
// Called whenever the state of the bridge has changed.
virtual void OnStateChanged(State state) {}
// Called whenever ARC's availability has changed for this system.
virtual void OnAvailableChanged(bool available) {}
// Called whenever the ARC app interface state changes.
virtual void OnAppInstanceReady() {}
virtual void OnAppInstanceClosed() {}
// Called whenever the ARC audio interface state changes.
virtual void OnAudioInstanceReady() {}
virtual void OnAudioInstanceClosed() {}
// Called whenever the ARC auth interface state changes.
virtual void OnAuthInstanceReady() {}
virtual void OnAuthInstanceClosed() {}
// Called whenever ARC Bluetooth instance is ready.
virtual void OnBluetoothInstanceReady() {}
virtual void OnBluetoothInstanceClosed() {}
// Called whenever the ARC clipboard interface state changes.
virtual void OnClipboardInstanceReady() {}
virtual void OnClipboardInstanceClosed() {}
// Called whenever the ARC crash collector interface state changes.
virtual void OnCrashCollectorInstanceReady() {}
virtual void OnCrashCollectorInstanceClosed() {}
// Called whenever the ARC IME interface state changes.
virtual void OnImeInstanceReady() {}
virtual void OnImeInstanceClosed() {}
// Called whenever the ARC input interface state changes.
virtual void OnInputInstanceReady() {}
virtual void OnInputInstanceClosed() {}
// Called whenever the ARC intent helper interface state changes.
virtual void OnIntentHelperInstanceReady() {}
virtual void OnIntentHelperInstanceClosed() {}
// Called whenever the ARC notification interface state changes.
virtual void OnNotificationsInstanceReady() {}
virtual void OnNotificationsInstanceClosed() {}
// Called whenever the ARC net interface state changes.
virtual void OnNetInstanceReady() {}
virtual void OnNetInstanceClosed() {}
// Called whenever the ARC policy interface state changes.
virtual void OnPolicyInstanceReady() {}
virtual void OnPolicyInstanceClosed() {}
// Called whenever the ARC power interface state changes.
virtual void OnPowerInstanceReady() {}
virtual void OnPowerInstanceClosed() {}
// Called whenever the ARC process interface state changes.
virtual void OnProcessInstanceReady() {}
virtual void OnProcessInstanceClosed() {}
// Called whenever the ARC video interface state changes.
virtual void OnVideoInstanceReady() {}
virtual void OnVideoInstanceClosed() {}
protected:
virtual ~Observer() {}
};
~ArcBridgeService() override;
// Gets the global instance of the ARC Bridge Service. This can only be
// called on the thread that this class was created on.
static ArcBridgeService* Get();
// Return true if ARC has been enabled through a commandline
// switch.
static bool GetEnabled(const base::CommandLine* command_line);
// SetDetectedAvailability() should be called once CheckArcAvailability() on
// the session_manager is called. This can only be called on the thread that
// this class was created on.
virtual void SetDetectedAvailability(bool availability) = 0;
// HandleStartup() should be called upon profile startup. This will only
// launch an instance if the instance service is available and it is enabled.
// This can only be called on the thread that this class was created on.
virtual void HandleStartup() = 0;
// Shutdown() should be called when the browser is shutting down. This can
// only be called on the thread that this class was created on.
virtual void Shutdown() = 0;
// Adds or removes observers. This can only be called on the thread that this
// class was created on.
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
// Gets the Mojo interface for all the instance services. This will return
// nullptr if that particular service is not ready yet. Use an Observer if
// you want to be notified when this is ready. This can only be called on the
// thread that this class was created on.
AppInstance* app_instance() { return app_ptr_.get(); }
AudioInstance* audio_instance() { return audio_ptr_.get(); }
AuthInstance* auth_instance() { return auth_ptr_.get(); }
BluetoothInstance* bluetooth_instance() { return bluetooth_ptr_.get(); }
ClipboardInstance* clipboard_instance() { return clipboard_ptr_.get(); }
CrashCollectorInstance* crash_collector_instance() {
return crash_collector_ptr_.get();
}
ImeInstance* ime_instance() { return ime_ptr_.get(); }
InputInstance* input_instance() { return input_ptr_.get(); }
IntentHelperInstance* intent_helper_instance() {
return intent_helper_ptr_.get();
}
NetInstance* net_instance() { return net_ptr_.get(); }
NotificationsInstance* notifications_instance() {
return notifications_ptr_.get();
}
PolicyInstance* policy_instance() { return policy_ptr_.get(); }
PowerInstance* power_instance() { return power_ptr_.get(); }
ProcessInstance* process_instance() { return process_ptr_.get(); }
VideoInstance* video_instance() { return video_ptr_.get(); }
int32_t app_version() const { return app_ptr_.version(); }
int32_t audio_version() const { return audio_ptr_.version(); }
int32_t bluetooth_version() const { return bluetooth_ptr_.version(); }
int32_t auth_version() const { return auth_ptr_.version(); }
int32_t clipboard_version() const { return clipboard_ptr_.version(); }
int32_t crash_collector_version() const {
return crash_collector_ptr_.version();
}
int32_t ime_version() const { return ime_ptr_.version(); }
int32_t input_version() const { return input_ptr_.version(); }
int32_t intent_helper_version() const { return intent_helper_ptr_.version(); }
int32_t net_version() const { return net_ptr_.version(); }
int32_t notifications_version() const { return notifications_ptr_.version(); }
int32_t policy_version() const { return policy_ptr_.version(); }
int32_t power_version() const { return power_ptr_.version(); }
int32_t process_version() const { return process_ptr_.version(); }
int32_t video_version() const { return video_ptr_.version(); }
// ArcHost:
void OnAppInstanceReady(AppInstancePtr app_ptr) override;
void OnAudioInstanceReady(AudioInstancePtr audio_ptr) override;
void OnAuthInstanceReady(AuthInstancePtr auth_ptr) override;
void OnBluetoothInstanceReady(BluetoothInstancePtr bluetooth_ptr) override;
void OnClipboardInstanceReady(ClipboardInstancePtr clipboard_ptr) override;
void OnCrashCollectorInstanceReady(
CrashCollectorInstancePtr crash_collector_ptr) override;
void OnImeInstanceReady(ImeInstancePtr ime_ptr) override;
void OnInputInstanceReady(InputInstancePtr input_ptr) override;
void OnIntentHelperInstanceReady(
IntentHelperInstancePtr intent_helper_ptr) override;
void OnNetInstanceReady(NetInstancePtr net_ptr) override;
void OnNotificationsInstanceReady(
NotificationsInstancePtr notifications_ptr) override;
void OnPolicyInstanceReady(PolicyInstancePtr policy_ptr) override;
void OnPowerInstanceReady(PowerInstancePtr power_ptr) override;
void OnProcessInstanceReady(ProcessInstancePtr process_ptr) override;
void OnVideoInstanceReady(VideoInstancePtr video_ptr) override;
// Gets the current state of the bridge service.
State state() const { return state_; }
// Gets if ARC is available in this system.
bool available() const { return available_; }
protected:
ArcBridgeService();
// Changes the current state and notifies all observers.
void SetState(State state);
// Changes the current availability and notifies all observers.
void SetAvailable(bool availability);
base::ObserverList<Observer>& observer_list() { return observer_list_; }
bool CalledOnValidThread();
// Closes all Mojo channels.
void CloseAllChannels();
private:
friend class ArcBridgeTest;
FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Basic);
FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Prerequisites);
FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, ShutdownMidStartup);
FRIEND_TEST_ALL_PREFIXES(ArcBridgeTest, Restart);
// Called when one of the individual channels is closed.
void CloseAppChannel();
void CloseAudioChannel();
void CloseAuthChannel();
void CloseBluetoothChannel();
void CloseClipboardChannel();
void CloseCrashCollectorChannel();
void CloseImeChannel();
void CloseInputChannel();
void CloseIntentHelperChannel();
void CloseNetChannel();
void CloseNotificationsChannel();
void ClosePolicyChannel();
void ClosePowerChannel();
void CloseProcessChannel();
void CloseVideoChannel();
// Callbacks for QueryVersion.
void OnAppVersionReady(int32_t version);
void OnAudioVersionReady(int32_t version);
void OnAuthVersionReady(int32_t version);
void OnBluetoothVersionReady(int32_t version);
void OnClipboardVersionReady(int32_t version);
void OnCrashCollectorVersionReady(int32_t version);
void OnImeVersionReady(int32_t version);
void OnInputVersionReady(int32_t version);
void OnIntentHelperVersionReady(int32_t version);
void OnNetVersionReady(int32_t version);
void OnNotificationsVersionReady(int32_t version);
void OnPolicyVersionReady(int32_t version);
void OnPowerVersionReady(int32_t version);
void OnProcessVersionReady(int32_t version);
void OnVideoVersionReady(int32_t version);
// Mojo interfaces.
AppInstancePtr app_ptr_;
AudioInstancePtr audio_ptr_;
AuthInstancePtr auth_ptr_;
BluetoothInstancePtr bluetooth_ptr_;
ClipboardInstancePtr clipboard_ptr_;
CrashCollectorInstancePtr crash_collector_ptr_;
ImeInstancePtr ime_ptr_;
InputInstancePtr input_ptr_;
IntentHelperInstancePtr intent_helper_ptr_;
NetInstancePtr net_ptr_;
NotificationsInstancePtr notifications_ptr_;
PolicyInstancePtr policy_ptr_;
PowerInstancePtr power_ptr_;
ProcessInstancePtr process_ptr_;
VideoInstancePtr video_ptr_;
// Temporary Mojo interfaces. After a Mojo interface pointer has been
// received from the other endpoint, we still need to asynchronously query
// its version. While that is going on, we should still return nullptr on
// the xxx_instance() functions.
// To keep the xxx_instance() functions being trivial, store the instance
// pointer in a temporary variable to avoid losing its reference.
AppInstancePtr temporary_app_ptr_;
AudioInstancePtr temporary_audio_ptr_;
AuthInstancePtr temporary_auth_ptr_;
BluetoothInstancePtr temporary_bluetooth_ptr_;
ClipboardInstancePtr temporary_clipboard_ptr_;
CrashCollectorInstancePtr temporary_crash_collector_ptr_;
ImeInstancePtr temporary_ime_ptr_;
InputInstancePtr temporary_input_ptr_;
IntentHelperInstancePtr temporary_intent_helper_ptr_;
NetInstancePtr temporary_net_ptr_;
NotificationsInstancePtr temporary_notifications_ptr_;
PolicyInstancePtr temporary_policy_ptr_;
PowerInstancePtr temporary_power_ptr_;
ProcessInstancePtr temporary_process_ptr_;
VideoInstancePtr temporary_video_ptr_;
base::ObserverList<Observer> observer_list_;
base::ThreadChecker thread_checker_;
// If the ARC instance service is available.
bool available_;
// The current state of the bridge.
ArcBridgeService::State state_;
// WeakPtrFactory to use callbacks.
base::WeakPtrFactory<ArcBridgeService> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ArcBridgeService);
};
} // namespace arc
#endif // COMPONENTS_ARC_ARC_BRIDGE_SERVICE_H_
|