forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arc_bridge_service_impl.cc
176 lines (153 loc) · 5.14 KB
/
arc_bridge_service_impl.cc
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
// 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.
#include "components/arc/arc_bridge_service_impl.h"
#include <string>
#include <utility>
#include "base/command_line.h"
#include "base/json/json_writer.h"
#include "base/message_loop/message_loop.h"
#include "base/sequenced_task_runner.h"
#include "base/sys_info.h"
#include "base/task_runner_util.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/time/time.h"
#include "chromeos/chromeos_switches.h"
#include "chromeos/dbus/dbus_method_call_status.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/session_manager_client.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
namespace arc {
extern ArcBridgeService* g_arc_bridge_service;
namespace {
constexpr int64_t kReconnectDelayInSeconds = 5;
} // namespace
ArcBridgeServiceImpl::ArcBridgeServiceImpl(
std::unique_ptr<ArcBridgeBootstrap> bootstrap)
: bootstrap_(std::move(bootstrap)),
binding_(this),
session_started_(false),
weak_factory_(this) {
DCHECK(!g_arc_bridge_service);
g_arc_bridge_service = this;
bootstrap_->set_delegate(this);
}
ArcBridgeServiceImpl::~ArcBridgeServiceImpl() {
DCHECK(g_arc_bridge_service == this);
g_arc_bridge_service = nullptr;
}
void ArcBridgeServiceImpl::HandleStartup() {
DCHECK(CalledOnValidThread());
if (session_started_)
return;
VLOG(1) << "Session started";
session_started_ = true;
PrerequisitesChanged();
}
void ArcBridgeServiceImpl::Shutdown() {
DCHECK(CalledOnValidThread());
if (!session_started_)
return;
VLOG(1) << "Session ended";
session_started_ = false;
PrerequisitesChanged();
}
void ArcBridgeServiceImpl::DisableReconnectDelayForTesting() {
use_delay_before_reconnecting_ = false;
}
void ArcBridgeServiceImpl::PrerequisitesChanged() {
DCHECK(CalledOnValidThread());
VLOG(1) << "Prerequisites changed. "
<< "state=" << static_cast<uint32_t>(state())
<< ", available=" << available()
<< ", session_started=" << session_started_;
if (state() == State::STOPPED) {
if (!available() || !session_started_)
return;
VLOG(0) << "Prerequisites met, starting ARC";
SetState(State::CONNECTING);
bootstrap_->Start();
} else {
if (available() && session_started_)
return;
VLOG(0) << "Prerequisites stopped being met, stopping ARC";
StopInstance();
}
}
void ArcBridgeServiceImpl::StopInstance() {
DCHECK(CalledOnValidThread());
if (state() == State::STOPPED || state() == State::STOPPING) {
VLOG(1) << "StopInstance() called when ARC is not running";
return;
}
VLOG(1) << "Stopping ARC";
SetState(State::STOPPING);
instance_ptr_.reset();
if (binding_.is_bound())
binding_.Close();
bootstrap_->Stop();
// We were explicitly asked to stop, so do not reconnect.
reconnect_ = false;
}
void ArcBridgeServiceImpl::SetDetectedAvailability(bool arc_available) {
DCHECK(CalledOnValidThread());
if (available() == arc_available)
return;
VLOG(1) << "ARC available: " << arc_available;
SetAvailable(arc_available);
PrerequisitesChanged();
}
void ArcBridgeServiceImpl::OnConnectionEstablished(
mojom::ArcBridgeInstancePtr instance) {
DCHECK(CalledOnValidThread());
if (state() != State::CONNECTING) {
VLOG(1) << "StopInstance() called while connecting";
return;
}
instance_ptr_ = std::move(instance);
instance_ptr_.set_connection_error_handler(base::Bind(
&ArcBridgeServiceImpl::OnChannelClosed, weak_factory_.GetWeakPtr()));
instance_ptr_->Init(binding_.CreateInterfacePtrAndBind());
// The container can be considered to have been successfully launched, so
// restart if the connection goes down without being requested.
reconnect_ = true;
VLOG(0) << "ARC ready";
SetState(State::READY);
}
void ArcBridgeServiceImpl::OnStopped() {
DCHECK(CalledOnValidThread());
SetState(State::STOPPED);
VLOG(0) << "ARC stopped";
if (reconnect_) {
// There was a previous invocation and it crashed for some reason. Try
// starting the container again.
reconnect_ = false;
VLOG(0) << "ARC reconnecting";
if (use_delay_before_reconnecting_) {
// Instead of immediately trying to restart the container, give it some
// time to finish tearing down in case it is still in the process of
// stopping.
base::MessageLoop::current()->task_runner()->PostDelayedTask(
FROM_HERE, base::Bind(&ArcBridgeServiceImpl::PrerequisitesChanged,
weak_factory_.GetWeakPtr()),
base::TimeDelta::FromSeconds(kReconnectDelayInSeconds));
} else {
// Restart immediately.
PrerequisitesChanged();
}
}
}
void ArcBridgeServiceImpl::OnChannelClosed() {
DCHECK(CalledOnValidThread());
if (state() == State::STOPPED || state() == State::STOPPING) {
// This will happen when the instance is shut down. Ignore that case.
return;
}
VLOG(1) << "Mojo connection lost";
instance_ptr_.reset();
if (binding_.is_bound())
binding_.Close();
CloseAllChannels();
}
} // namespace arc