OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/arc/arc_bridge_service_impl.h" | 5 #include "components/arc/arc_bridge_service_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
14 #include "base/sys_info.h" | 14 #include "base/sys_info.h" |
15 #include "base/task_runner_util.h" | 15 #include "base/task_runner_util.h" |
16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
18 #include "chromeos/chromeos_switches.h" | 18 #include "chromeos/chromeos_switches.h" |
19 #include "chromeos/dbus/dbus_method_call_status.h" | 19 #include "chromeos/dbus/dbus_method_call_status.h" |
20 #include "chromeos/dbus/dbus_thread_manager.h" | 20 #include "chromeos/dbus/dbus_thread_manager.h" |
21 #include "components/arc/arc_bridge_host_impl.h" | |
22 #include "components/prefs/pref_registry_simple.h" | 21 #include "components/prefs/pref_registry_simple.h" |
23 #include "components/prefs/pref_service.h" | 22 #include "components/prefs/pref_service.h" |
24 | 23 |
25 namespace arc { | 24 namespace arc { |
26 | 25 |
27 extern ArcBridgeService* g_arc_bridge_service; | 26 extern ArcBridgeService* g_arc_bridge_service; |
28 | 27 |
29 namespace { | 28 namespace { |
30 constexpr int64_t kReconnectDelayInSeconds = 5; | 29 constexpr int64_t kReconnectDelayInSeconds = 5; |
31 } // namespace | 30 } // namespace |
32 | 31 |
33 ArcBridgeServiceImpl::ArcBridgeServiceImpl() | 32 ArcBridgeServiceImpl::ArcBridgeServiceImpl() |
34 : session_started_(false), | 33 : session_started_(false), |
35 factory_(base::Bind(ArcBridgeBootstrap::Create)), | 34 factory_(base::Bind(ArcBridgeBootstrap::Create)), |
36 weak_factory_(this) { | 35 weak_factory_(this) { |
37 DCHECK(!g_arc_bridge_service); | 36 DCHECK(!g_arc_bridge_service); |
38 g_arc_bridge_service = this; | 37 g_arc_bridge_service = this; |
39 } | 38 } |
40 | 39 |
41 ArcBridgeServiceImpl::~ArcBridgeServiceImpl() { | 40 ArcBridgeServiceImpl::~ArcBridgeServiceImpl() { |
41 if (bootstrap_) | |
42 bootstrap_->RemoveObserver(this); | |
43 | |
42 DCHECK(g_arc_bridge_service == this); | 44 DCHECK(g_arc_bridge_service == this); |
43 g_arc_bridge_service = nullptr; | 45 g_arc_bridge_service = nullptr; |
44 } | 46 } |
45 | 47 |
46 void ArcBridgeServiceImpl::HandleStartup() { | 48 void ArcBridgeServiceImpl::HandleStartup() { |
47 DCHECK(CalledOnValidThread()); | 49 DCHECK(CalledOnValidThread()); |
48 if (session_started_) | 50 if (session_started_) |
49 return; | 51 return; |
50 VLOG(1) << "Session started"; | 52 VLOG(1) << "Session started"; |
51 session_started_ = true; | 53 session_started_ = true; |
(...skipping 25 matching lines...) Expand all Loading... | |
77 VLOG(1) << "Prerequisites changed. " | 79 VLOG(1) << "Prerequisites changed. " |
78 << "state=" << static_cast<uint32_t>(state()) | 80 << "state=" << static_cast<uint32_t>(state()) |
79 << ", session_started=" << session_started_; | 81 << ", session_started=" << session_started_; |
80 if (state() == State::STOPPED) { | 82 if (state() == State::STOPPED) { |
81 if (!session_started_) | 83 if (!session_started_) |
82 return; | 84 return; |
83 VLOG(0) << "Prerequisites met, starting ARC"; | 85 VLOG(0) << "Prerequisites met, starting ARC"; |
84 SetStopReason(StopReason::SHUTDOWN); | 86 SetStopReason(StopReason::SHUTDOWN); |
85 | 87 |
86 SetState(State::CONNECTING); | 88 SetState(State::CONNECTING); |
87 bootstrap_ = factory_.Run(); | 89 bootstrap_ = factory_.Run(); |
Luis Héctor Chávez
2016/10/03 18:05:32
Should you unregister the previous bootstrap here
hidehiko
2016/10/04 05:34:45
Added RemoveObserver, which is much safer.
| |
88 bootstrap_->set_delegate(this); | 90 bootstrap_->AddObserver(this); |
89 bootstrap_->Start(); | 91 bootstrap_->Start(); |
90 } else { | 92 } else { |
91 if (session_started_) | 93 if (session_started_) |
92 return; | 94 return; |
93 VLOG(0) << "Prerequisites stopped being met, stopping ARC"; | 95 VLOG(0) << "Prerequisites stopped being met, stopping ARC"; |
94 StopInstance(); | 96 StopInstance(); |
95 } | 97 } |
96 } | 98 } |
97 | 99 |
98 void ArcBridgeServiceImpl::StopInstance() { | 100 void ArcBridgeServiceImpl::StopInstance() { |
99 DCHECK(CalledOnValidThread()); | 101 DCHECK(CalledOnValidThread()); |
100 if (state() == State::STOPPED || state() == State::STOPPING) { | 102 if (state() == State::STOPPED || state() == State::STOPPING) { |
101 VLOG(1) << "StopInstance() called when ARC is not running"; | 103 VLOG(1) << "StopInstance() called when ARC is not running"; |
102 return; | 104 return; |
103 } | 105 } |
104 | 106 |
105 // We were explicitly asked to stop, so do not reconnect. | 107 // We were explicitly asked to stop, so do not reconnect. |
106 reconnect_ = false; | 108 reconnect_ = false; |
107 | 109 |
108 VLOG(1) << "Stopping ARC"; | 110 VLOG(1) << "Stopping ARC"; |
109 DCHECK(bootstrap_.get()); | 111 DCHECK(bootstrap_.get()); |
110 SetState(State::STOPPING); | 112 SetState(State::STOPPING); |
111 arc_bridge_host_.reset(); | |
112 | 113 |
113 // Note: this can call OnStopped() internally as a callback. | 114 // Note: this can call OnStopped() internally as a callback. |
114 bootstrap_->Stop(); | 115 bootstrap_->Stop(); |
115 } | 116 } |
116 | 117 |
117 void ArcBridgeServiceImpl::OnConnectionEstablished( | 118 void ArcBridgeServiceImpl::OnReady() { |
118 mojom::ArcBridgeInstancePtr instance) { | |
119 DCHECK(CalledOnValidThread()); | 119 DCHECK(CalledOnValidThread()); |
120 if (state() != State::CONNECTING) { | 120 if (state() != State::CONNECTING) { |
121 VLOG(1) << "StopInstance() called while connecting"; | 121 VLOG(1) << "StopInstance() called while connecting"; |
122 return; | 122 return; |
123 } | 123 } |
124 | 124 |
125 arc_bridge_host_.reset(new ArcBridgeHostImpl(std::move(instance))); | |
126 | |
127 // The container can be considered to have been successfully launched, so | 125 // The container can be considered to have been successfully launched, so |
128 // restart if the connection goes down without being requested. | 126 // restart if the connection goes down without being requested. |
129 reconnect_ = true; | 127 reconnect_ = true; |
130 VLOG(0) << "ARC ready"; | 128 VLOG(0) << "ARC ready"; |
131 SetState(State::READY); | 129 SetState(State::READY); |
132 } | 130 } |
133 | 131 |
134 void ArcBridgeServiceImpl::OnStopped(StopReason stop_reason) { | 132 void ArcBridgeServiceImpl::OnStopped(StopReason stop_reason) { |
135 DCHECK(CalledOnValidThread()); | 133 DCHECK(CalledOnValidThread()); |
136 VLOG(0) << "ARC stopped: " << stop_reason; | 134 VLOG(0) << "ARC stopped: " << stop_reason; |
137 arc_bridge_host_.reset(); | 135 bootstrap_->RemoveObserver(this); |
138 bootstrap_.reset(); | 136 bootstrap_.reset(); |
139 SetStopReason(stop_reason); | 137 SetStopReason(stop_reason); |
140 SetState(State::STOPPED); | 138 SetState(State::STOPPED); |
141 | 139 |
142 if (reconnect_) { | 140 if (reconnect_) { |
143 // There was a previous invocation and it crashed for some reason. Try | 141 // There was a previous invocation and it crashed for some reason. Try |
144 // starting the container again. | 142 // starting the container again. |
145 reconnect_ = false; | 143 reconnect_ = false; |
146 VLOG(0) << "ARC reconnecting"; | 144 VLOG(0) << "ARC reconnecting"; |
147 if (use_delay_before_reconnecting_) { | 145 if (use_delay_before_reconnecting_) { |
148 // Instead of immediately trying to restart the container, give it some | 146 // Instead of immediately trying to restart the container, give it some |
149 // time to finish tearing down in case it is still in the process of | 147 // time to finish tearing down in case it is still in the process of |
150 // stopping. | 148 // stopping. |
151 base::MessageLoop::current()->task_runner()->PostDelayedTask( | 149 base::MessageLoop::current()->task_runner()->PostDelayedTask( |
152 FROM_HERE, base::Bind(&ArcBridgeServiceImpl::PrerequisitesChanged, | 150 FROM_HERE, base::Bind(&ArcBridgeServiceImpl::PrerequisitesChanged, |
153 weak_factory_.GetWeakPtr()), | 151 weak_factory_.GetWeakPtr()), |
154 base::TimeDelta::FromSeconds(kReconnectDelayInSeconds)); | 152 base::TimeDelta::FromSeconds(kReconnectDelayInSeconds)); |
155 } else { | 153 } else { |
156 // Restart immediately. | 154 // Restart immediately. |
157 PrerequisitesChanged(); | 155 PrerequisitesChanged(); |
158 } | 156 } |
159 } | 157 } |
160 } | 158 } |
161 | 159 |
162 } // namespace arc | 160 } // namespace arc |
OLD | NEW |