| 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/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
| 13 #include "base/sys_info.h" | 14 #include "base/sys_info.h" |
| 14 #include "base/task_runner_util.h" | 15 #include "base/task_runner_util.h" |
| 15 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "base/time/time.h" |
| 16 #include "chromeos/chromeos_switches.h" | 18 #include "chromeos/chromeos_switches.h" |
| 17 #include "chromeos/dbus/dbus_method_call_status.h" | 19 #include "chromeos/dbus/dbus_method_call_status.h" |
| 18 #include "chromeos/dbus/dbus_thread_manager.h" | 20 #include "chromeos/dbus/dbus_thread_manager.h" |
| 19 #include "chromeos/dbus/session_manager_client.h" | 21 #include "chromeos/dbus/session_manager_client.h" |
| 20 #include "components/prefs/pref_registry_simple.h" | 22 #include "components/prefs/pref_registry_simple.h" |
| 21 #include "components/prefs/pref_service.h" | 23 #include "components/prefs/pref_service.h" |
| 22 | 24 |
| 23 namespace arc { | 25 namespace arc { |
| 24 | 26 |
| 27 namespace { |
| 28 constexpr int64_t kReconnectDelayInSeconds = 5; |
| 29 } // namespace |
| 30 |
| 25 ArcBridgeServiceImpl::ArcBridgeServiceImpl( | 31 ArcBridgeServiceImpl::ArcBridgeServiceImpl( |
| 26 std::unique_ptr<ArcBridgeBootstrap> bootstrap) | 32 std::unique_ptr<ArcBridgeBootstrap> bootstrap) |
| 27 : bootstrap_(std::move(bootstrap)), | 33 : bootstrap_(std::move(bootstrap)), |
| 28 binding_(this), | 34 binding_(this), |
| 29 session_started_(false), | 35 session_started_(false), |
| 30 weak_factory_(this) { | 36 weak_factory_(this) { |
| 31 bootstrap_->set_delegate(this); | 37 bootstrap_->set_delegate(this); |
| 32 } | 38 } |
| 33 | 39 |
| 34 ArcBridgeServiceImpl::~ArcBridgeServiceImpl() { | 40 ArcBridgeServiceImpl::~ArcBridgeServiceImpl() {} |
| 35 } | |
| 36 | 41 |
| 37 void ArcBridgeServiceImpl::HandleStartup() { | 42 void ArcBridgeServiceImpl::HandleStartup() { |
| 38 DCHECK(CalledOnValidThread()); | 43 DCHECK(CalledOnValidThread()); |
| 44 if (session_started_) |
| 45 return; |
| 46 VLOG(1) << "Session started"; |
| 39 session_started_ = true; | 47 session_started_ = true; |
| 40 PrerequisitesChanged(); | 48 PrerequisitesChanged(); |
| 41 } | 49 } |
| 42 | 50 |
| 43 void ArcBridgeServiceImpl::Shutdown() { | 51 void ArcBridgeServiceImpl::Shutdown() { |
| 44 DCHECK(CalledOnValidThread()); | 52 DCHECK(CalledOnValidThread()); |
| 53 if (!session_started_) |
| 54 return; |
| 55 VLOG(1) << "Session ended"; |
| 45 session_started_ = false; | 56 session_started_ = false; |
| 46 PrerequisitesChanged(); | 57 PrerequisitesChanged(); |
| 47 } | 58 } |
| 48 | 59 |
| 60 void ArcBridgeServiceImpl::DisableReconnectDelayForTesting() { |
| 61 use_delay_before_reconnecting_ = false; |
| 62 } |
| 63 |
| 49 void ArcBridgeServiceImpl::PrerequisitesChanged() { | 64 void ArcBridgeServiceImpl::PrerequisitesChanged() { |
| 50 DCHECK(CalledOnValidThread()); | 65 DCHECK(CalledOnValidThread()); |
| 66 VLOG(1) << "Prerequisites changed. " |
| 67 << "state=" << static_cast<uint32_t>(state()) |
| 68 << ", available=" << available() |
| 69 << ", session_started=" << session_started_; |
| 51 if (state() == State::STOPPED) { | 70 if (state() == State::STOPPED) { |
| 52 if (!available() || !session_started_) | 71 if (!available() || !session_started_) |
| 53 return; | 72 return; |
| 73 VLOG(0) << "Prerequisites met, starting ARC"; |
| 54 SetState(State::CONNECTING); | 74 SetState(State::CONNECTING); |
| 55 bootstrap_->Start(); | 75 bootstrap_->Start(); |
| 56 } else { | 76 } else { |
| 57 if (available() && session_started_) | 77 if (available() && session_started_) |
| 58 return; | 78 return; |
| 79 VLOG(0) << "Prerequisites stopped being met, stopping ARC"; |
| 59 StopInstance(); | 80 StopInstance(); |
| 60 } | 81 } |
| 61 } | 82 } |
| 62 | 83 |
| 63 void ArcBridgeServiceImpl::StopInstance() { | 84 void ArcBridgeServiceImpl::StopInstance() { |
| 64 DCHECK(CalledOnValidThread()); | 85 DCHECK(CalledOnValidThread()); |
| 65 if (state() == State::STOPPED || state() == State::STOPPING) { | 86 if (state() == State::STOPPED || state() == State::STOPPING) { |
| 66 VLOG(1) << "StopInstance() called when ARC is not running"; | 87 VLOG(1) << "StopInstance() called when ARC is not running"; |
| 67 return; | 88 return; |
| 68 } | 89 } |
| 69 | 90 |
| 91 VLOG(1) << "Stopping ARC"; |
| 70 SetState(State::STOPPING); | 92 SetState(State::STOPPING); |
| 71 instance_ptr_.reset(); | 93 instance_ptr_.reset(); |
| 72 if (binding_.is_bound()) | 94 if (binding_.is_bound()) |
| 73 binding_.Close(); | 95 binding_.Close(); |
| 74 bootstrap_->Stop(); | 96 bootstrap_->Stop(); |
| 75 } | 97 } |
| 76 | 98 |
| 77 void ArcBridgeServiceImpl::SetDetectedAvailability(bool arc_available) { | 99 void ArcBridgeServiceImpl::SetDetectedAvailability(bool arc_available) { |
| 78 DCHECK(CalledOnValidThread()); | 100 DCHECK(CalledOnValidThread()); |
| 79 if (available() == arc_available) | 101 if (available() == arc_available) |
| 80 return; | 102 return; |
| 103 VLOG(1) << "ARC available: " << arc_available; |
| 81 SetAvailable(arc_available); | 104 SetAvailable(arc_available); |
| 82 PrerequisitesChanged(); | 105 PrerequisitesChanged(); |
| 83 } | 106 } |
| 84 | 107 |
| 85 void ArcBridgeServiceImpl::OnConnectionEstablished( | 108 void ArcBridgeServiceImpl::OnConnectionEstablished( |
| 86 mojom::ArcBridgeInstancePtr instance) { | 109 mojom::ArcBridgeInstancePtr instance) { |
| 87 DCHECK(CalledOnValidThread()); | 110 DCHECK(CalledOnValidThread()); |
| 88 if (state() != State::CONNECTING) { | 111 if (state() != State::CONNECTING) { |
| 89 VLOG(1) << "StopInstance() called while connecting"; | 112 VLOG(1) << "StopInstance() called while connecting"; |
| 90 return; | 113 return; |
| 91 } | 114 } |
| 92 | 115 |
| 93 instance_ptr_ = std::move(instance); | 116 instance_ptr_ = std::move(instance); |
| 94 instance_ptr_.set_connection_error_handler(base::Bind( | 117 instance_ptr_.set_connection_error_handler(base::Bind( |
| 95 &ArcBridgeServiceImpl::OnChannelClosed, weak_factory_.GetWeakPtr())); | 118 &ArcBridgeServiceImpl::OnChannelClosed, weak_factory_.GetWeakPtr())); |
| 96 | 119 |
| 97 instance_ptr_->Init(binding_.CreateInterfacePtrAndBind()); | 120 instance_ptr_->Init(binding_.CreateInterfacePtrAndBind()); |
| 98 | 121 |
| 122 VLOG(0) << "ARC ready"; |
| 99 SetState(State::READY); | 123 SetState(State::READY); |
| 100 } | 124 } |
| 101 | 125 |
| 102 void ArcBridgeServiceImpl::OnStopped() { | 126 void ArcBridgeServiceImpl::OnStopped() { |
| 103 DCHECK(CalledOnValidThread()); | 127 DCHECK(CalledOnValidThread()); |
| 104 SetState(State::STOPPED); | 128 SetState(State::STOPPED); |
| 129 VLOG(0) << "ARC stopped"; |
| 105 if (reconnect_) { | 130 if (reconnect_) { |
| 106 // There was a previous invocation and it crashed for some reason. Try | 131 // There was a previous invocation and it crashed for some reason. Try |
| 107 // starting the container again. | 132 // starting the container again. |
| 108 reconnect_ = false; | 133 reconnect_ = false; |
| 109 PrerequisitesChanged(); | 134 VLOG(0) << "ARC reconnecting"; |
| 135 if (use_delay_before_reconnecting_) { |
| 136 // Instead of immediately trying to restart the container, give it some |
| 137 // time to finish tearing down in case it is still in the process of |
| 138 // stopping. |
| 139 base::MessageLoop::current()->task_runner()->PostDelayedTask( |
| 140 FROM_HERE, base::Bind(&ArcBridgeServiceImpl::PrerequisitesChanged, |
| 141 weak_factory_.GetWeakPtr()), |
| 142 base::TimeDelta::FromSeconds(kReconnectDelayInSeconds)); |
| 143 } else { |
| 144 // Restart immediately. |
| 145 PrerequisitesChanged(); |
| 146 } |
| 110 } | 147 } |
| 111 } | 148 } |
| 112 | 149 |
| 113 void ArcBridgeServiceImpl::OnChannelClosed() { | 150 void ArcBridgeServiceImpl::OnChannelClosed() { |
| 114 DCHECK(CalledOnValidThread()); | 151 DCHECK(CalledOnValidThread()); |
| 115 if (state() == State::STOPPED || state() == State::STOPPING) { | 152 if (state() == State::STOPPED || state() == State::STOPPING) { |
| 116 // This will happen when the instance is shut down. Ignore that case. | 153 // This will happen when the instance is shut down. Ignore that case. |
| 117 return; | 154 return; |
| 118 } | 155 } |
| 119 VLOG(1) << "Mojo connection lost"; | 156 VLOG(1) << "Mojo connection lost"; |
| 120 CloseAllChannels(); | 157 CloseAllChannels(); |
| 121 reconnect_ = true; | 158 reconnect_ = true; |
| 122 StopInstance(); | 159 StopInstance(); |
| 123 } | 160 } |
| 124 | 161 |
| 125 } // namespace arc | 162 } // namespace arc |
| OLD | NEW |