| 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> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/json/json_writer.h" | |
| 12 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
| 13 #include "base/sequenced_task_runner.h" | 8 #include "base/sequenced_task_runner.h" |
| 14 #include "base/sys_info.h" | |
| 15 #include "base/task_runner_util.h" | |
| 16 #include "base/threading/thread_task_runner_handle.h" | |
| 17 #include "base/time/time.h" | 9 #include "base/time/time.h" |
| 18 #include "chromeos/chromeos_switches.h" | |
| 19 #include "chromeos/dbus/dbus_method_call_status.h" | |
| 20 #include "chromeos/dbus/dbus_thread_manager.h" | |
| 21 #include "chromeos/dbus/session_manager_client.h" | |
| 22 #include "components/arc/arc_bridge_host_impl.h" | |
| 23 #include "components/prefs/pref_registry_simple.h" | |
| 24 #include "components/prefs/pref_service.h" | |
| 25 | 10 |
| 26 namespace arc { | 11 namespace arc { |
| 27 | 12 |
| 28 extern ArcBridgeService* g_arc_bridge_service; | 13 extern ArcBridgeService* g_arc_bridge_service; |
| 29 | 14 |
| 30 namespace { | 15 namespace { |
| 31 constexpr int64_t kReconnectDelayInSeconds = 5; | 16 constexpr int64_t kReconnectDelayInSeconds = 5; |
| 32 } // namespace | 17 } // namespace |
| 33 | 18 |
| 34 ArcBridgeServiceImpl::ArcBridgeServiceImpl( | 19 ArcBridgeServiceImpl::ArcBridgeServiceImpl() : weak_factory_(this) { |
| 35 std::unique_ptr<ArcBridgeBootstrap> bootstrap) | |
| 36 : bootstrap_(std::move(bootstrap)), | |
| 37 session_started_(false), | |
| 38 weak_factory_(this) { | |
| 39 DCHECK(!g_arc_bridge_service); | 20 DCHECK(!g_arc_bridge_service); |
| 40 g_arc_bridge_service = this; | 21 g_arc_bridge_service = this; |
| 41 bootstrap_->set_delegate(this); | |
| 42 } | 22 } |
| 43 | 23 |
| 44 ArcBridgeServiceImpl::~ArcBridgeServiceImpl() { | 24 ArcBridgeServiceImpl::~ArcBridgeServiceImpl() { |
| 45 DCHECK(g_arc_bridge_service == this); | 25 DCHECK(g_arc_bridge_service == this); |
| 46 g_arc_bridge_service = nullptr; | 26 g_arc_bridge_service = nullptr; |
| 47 } | 27 } |
| 48 | 28 |
| 49 void ArcBridgeServiceImpl::HandleStartup() { | 29 void ArcBridgeServiceImpl::HandleStartup() { |
| 50 DCHECK(CalledOnValidThread()); | 30 DCHECK(CalledOnValidThread()); |
| 51 if (session_started_) | 31 if (session_started_) |
| (...skipping 19 matching lines...) Expand all Loading... |
| 71 void ArcBridgeServiceImpl::PrerequisitesChanged() { | 51 void ArcBridgeServiceImpl::PrerequisitesChanged() { |
| 72 DCHECK(CalledOnValidThread()); | 52 DCHECK(CalledOnValidThread()); |
| 73 VLOG(1) << "Prerequisites changed. " | 53 VLOG(1) << "Prerequisites changed. " |
| 74 << "state=" << static_cast<uint32_t>(state()) | 54 << "state=" << static_cast<uint32_t>(state()) |
| 75 << ", session_started=" << session_started_; | 55 << ", session_started=" << session_started_; |
| 76 if (state() == State::STOPPED) { | 56 if (state() == State::STOPPED) { |
| 77 if (!session_started_) | 57 if (!session_started_) |
| 78 return; | 58 return; |
| 79 VLOG(0) << "Prerequisites met, starting ARC"; | 59 VLOG(0) << "Prerequisites met, starting ARC"; |
| 80 SetStopReason(StopReason::SHUTDOWN); | 60 SetStopReason(StopReason::SHUTDOWN); |
| 61 |
| 81 SetState(State::CONNECTING); | 62 SetState(State::CONNECTING); |
| 63 bootstrap_ = |
| 64 delegate_ ? delegate_->CreateBootstrap() : ArcBridgeBootstrap::Create(); |
| 65 bootstrap_->set_delegate(this); |
| 82 bootstrap_->Start(); | 66 bootstrap_->Start(); |
| 83 } else { | 67 } else { |
| 84 if (session_started_) | 68 if (session_started_) |
| 85 return; | 69 return; |
| 86 VLOG(0) << "Prerequisites stopped being met, stopping ARC"; | 70 VLOG(0) << "Prerequisites stopped being met, stopping ARC"; |
| 87 StopInstance(); | 71 StopInstance(); |
| 88 } | 72 } |
| 89 } | 73 } |
| 90 | 74 |
| 91 void ArcBridgeServiceImpl::StopInstance() { | 75 void ArcBridgeServiceImpl::StopInstance() { |
| 92 DCHECK(CalledOnValidThread()); | 76 DCHECK(CalledOnValidThread()); |
| 93 if (state() == State::STOPPED || state() == State::STOPPING) { | 77 if (state() == State::STOPPED || state() == State::STOPPING) { |
| 94 VLOG(1) << "StopInstance() called when ARC is not running"; | 78 VLOG(1) << "StopInstance() called when ARC is not running"; |
| 95 return; | 79 return; |
| 96 } | 80 } |
| 97 | 81 |
| 98 VLOG(1) << "Stopping ARC"; | |
| 99 SetState(State::STOPPING); | |
| 100 arc_bridge_host_.reset(); | |
| 101 bootstrap_->Stop(); | |
| 102 | |
| 103 // We were explicitly asked to stop, so do not reconnect. | 82 // We were explicitly asked to stop, so do not reconnect. |
| 104 reconnect_ = false; | 83 reconnect_ = false; |
| 84 |
| 85 VLOG(1) << "Stopping ARC"; |
| 86 DCHECK(bootstrap_.get()); |
| 87 SetState(State::STOPPING); |
| 88 |
| 89 // Note: this can call OnStopped() internally as a callback. |
| 90 bootstrap_->Stop(); |
| 105 } | 91 } |
| 106 | 92 |
| 107 void ArcBridgeServiceImpl::OnConnectionEstablished( | 93 void ArcBridgeServiceImpl::OnReady() { |
| 108 mojom::ArcBridgeInstancePtr instance) { | |
| 109 DCHECK(CalledOnValidThread()); | 94 DCHECK(CalledOnValidThread()); |
| 110 if (state() != State::CONNECTING) { | 95 if (state() != State::CONNECTING) { |
| 111 VLOG(1) << "StopInstance() called while connecting"; | 96 VLOG(1) << "StopInstance() called while connecting"; |
| 112 return; | 97 return; |
| 113 } | 98 } |
| 114 | 99 |
| 115 arc_bridge_host_.reset(new ArcBridgeHostImpl(std::move(instance))); | |
| 116 | |
| 117 // The container can be considered to have been successfully launched, so | 100 // The container can be considered to have been successfully launched, so |
| 118 // restart if the connection goes down without being requested. | 101 // restart if the connection goes down without being requested. |
| 119 reconnect_ = true; | 102 reconnect_ = true; |
| 120 VLOG(0) << "ARC ready"; | 103 VLOG(0) << "ARC ready"; |
| 121 SetState(State::READY); | 104 SetState(State::READY); |
| 122 } | 105 } |
| 123 | 106 |
| 124 void ArcBridgeServiceImpl::OnStopped(StopReason stop_reason) { | 107 void ArcBridgeServiceImpl::OnStopped(StopReason stop_reason) { |
| 125 DCHECK(CalledOnValidThread()); | 108 DCHECK(CalledOnValidThread()); |
| 126 arc_bridge_host_.reset(); | 109 VLOG(0) << "ARC stopped: " << static_cast<int>(stop_reason); |
| 110 bootstrap_.reset(); |
| 127 SetStopReason(stop_reason); | 111 SetStopReason(stop_reason); |
| 128 SetState(State::STOPPED); | 112 SetState(State::STOPPED); |
| 129 VLOG(0) << "ARC stopped"; | |
| 130 | 113 |
| 131 if (reconnect_) { | 114 if (reconnect_) { |
| 132 // There was a previous invocation and it crashed for some reason. Try | 115 // There was a previous invocation and it crashed for some reason. Try |
| 133 // starting the container again. | 116 // starting the container again. |
| 134 reconnect_ = false; | 117 reconnect_ = false; |
| 135 VLOG(0) << "ARC reconnecting"; | 118 VLOG(0) << "ARC reconnecting"; |
| 136 if (use_delay_before_reconnecting_) { | 119 if (use_delay_before_reconnecting_) { |
| 137 // Instead of immediately trying to restart the container, give it some | 120 // Instead of immediately trying to restart the container, give it some |
| 138 // time to finish tearing down in case it is still in the process of | 121 // time to finish tearing down in case it is still in the process of |
| 139 // stopping. | 122 // stopping. |
| 140 base::MessageLoop::current()->task_runner()->PostDelayedTask( | 123 base::MessageLoop::current()->task_runner()->PostDelayedTask( |
| 141 FROM_HERE, base::Bind(&ArcBridgeServiceImpl::PrerequisitesChanged, | 124 FROM_HERE, base::Bind(&ArcBridgeServiceImpl::PrerequisitesChanged, |
| 142 weak_factory_.GetWeakPtr()), | 125 weak_factory_.GetWeakPtr()), |
| 143 base::TimeDelta::FromSeconds(kReconnectDelayInSeconds)); | 126 base::TimeDelta::FromSeconds(kReconnectDelayInSeconds)); |
| 144 } else { | 127 } else { |
| 145 // Restart immediately. | 128 // Restart immediately. |
| 146 PrerequisitesChanged(); | 129 PrerequisitesChanged(); |
| 147 } | 130 } |
| 148 } | 131 } |
| 149 } | 132 } |
| 150 | 133 |
| 151 } // namespace arc | 134 } // namespace arc |
| OLD | NEW |