Chromium Code Reviews| 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 |
| 25 ArcBridgeServiceImpl::ArcBridgeServiceImpl( | 27 ArcBridgeServiceImpl::ArcBridgeServiceImpl( |
| 26 std::unique_ptr<ArcBridgeBootstrap> bootstrap) | 28 std::unique_ptr<ArcBridgeBootstrap> bootstrap) |
| 27 : bootstrap_(std::move(bootstrap)), | 29 : bootstrap_(std::move(bootstrap)), |
| 28 binding_(this), | 30 binding_(this), |
| 29 session_started_(false), | 31 session_started_(false), |
| 30 weak_factory_(this) { | 32 weak_factory_(this) { |
| 31 bootstrap_->set_delegate(this); | 33 bootstrap_->set_delegate(this); |
| 32 } | 34 } |
| 33 | 35 |
| 34 ArcBridgeServiceImpl::~ArcBridgeServiceImpl() { | |
| 35 } | |
| 36 | |
| 37 void ArcBridgeServiceImpl::HandleStartup() { | 36 void ArcBridgeServiceImpl::HandleStartup() { |
| 37 VLOG(1) << "Session started"; | |
| 38 DCHECK(CalledOnValidThread()); | 38 DCHECK(CalledOnValidThread()); |
| 39 session_started_ = true; | 39 session_started_ = true; |
| 40 PrerequisitesChanged(); | 40 PrerequisitesChanged(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void ArcBridgeServiceImpl::Shutdown() { | 43 void ArcBridgeServiceImpl::Shutdown() { |
| 44 VLOG(1) << "Session ended"; | |
| 44 DCHECK(CalledOnValidThread()); | 45 DCHECK(CalledOnValidThread()); |
| 45 session_started_ = false; | 46 session_started_ = false; |
| 46 PrerequisitesChanged(); | 47 PrerequisitesChanged(); |
| 47 } | 48 } |
| 48 | 49 |
| 49 void ArcBridgeServiceImpl::PrerequisitesChanged() { | 50 void ArcBridgeServiceImpl::PrerequisitesChanged() { |
| 50 DCHECK(CalledOnValidThread()); | 51 DCHECK(CalledOnValidThread()); |
| 51 if (state() == State::STOPPED) { | 52 if (state() == State::STOPPED) { |
| 52 if (!available() || !session_started_) | 53 if (!available() || !session_started_) |
| 53 return; | 54 return; |
| 55 VLOG(0) << "Prerequisites met, starting ARC"; | |
| 54 SetState(State::CONNECTING); | 56 SetState(State::CONNECTING); |
| 55 bootstrap_->Start(); | 57 bootstrap_->Start(); |
| 56 } else { | 58 } else { |
| 57 if (available() && session_started_) | 59 if (available() && session_started_) |
| 58 return; | 60 return; |
| 61 VLOG(0) << "Prerequisites stopped being met, stopping ARC"; | |
| 59 StopInstance(); | 62 StopInstance(); |
| 60 } | 63 } |
| 61 } | 64 } |
| 62 | 65 |
| 63 void ArcBridgeServiceImpl::StopInstance() { | 66 void ArcBridgeServiceImpl::StopInstance() { |
| 64 DCHECK(CalledOnValidThread()); | 67 DCHECK(CalledOnValidThread()); |
| 65 if (state() == State::STOPPED || state() == State::STOPPING) { | 68 if (state() == State::STOPPED || state() == State::STOPPING) { |
| 66 VLOG(1) << "StopInstance() called when ARC is not running"; | 69 VLOG(1) << "StopInstance() called when ARC is not running"; |
| 67 return; | 70 return; |
| 68 } | 71 } |
| 69 | 72 |
| 73 VLOG(1) << "Stopping ARC"; | |
| 70 SetState(State::STOPPING); | 74 SetState(State::STOPPING); |
| 71 instance_ptr_.reset(); | 75 instance_ptr_.reset(); |
| 72 if (binding_.is_bound()) | 76 if (binding_.is_bound()) |
| 73 binding_.Close(); | 77 binding_.Close(); |
| 74 bootstrap_->Stop(); | 78 bootstrap_->Stop(); |
| 75 } | 79 } |
| 76 | 80 |
| 77 void ArcBridgeServiceImpl::SetDetectedAvailability(bool arc_available) { | 81 void ArcBridgeServiceImpl::SetDetectedAvailability(bool arc_available) { |
| 78 DCHECK(CalledOnValidThread()); | 82 DCHECK(CalledOnValidThread()); |
| 79 if (available() == arc_available) | 83 if (available() == arc_available) |
| 80 return; | 84 return; |
| 85 VLOG(1) << "ARC available: " << arc_available; | |
| 81 SetAvailable(arc_available); | 86 SetAvailable(arc_available); |
| 82 PrerequisitesChanged(); | 87 PrerequisitesChanged(); |
| 83 } | 88 } |
| 84 | 89 |
| 85 void ArcBridgeServiceImpl::OnConnectionEstablished( | 90 void ArcBridgeServiceImpl::OnConnectionEstablished( |
| 86 mojom::ArcBridgeInstancePtr instance) { | 91 mojom::ArcBridgeInstancePtr instance) { |
| 87 DCHECK(CalledOnValidThread()); | 92 DCHECK(CalledOnValidThread()); |
| 88 if (state() != State::CONNECTING) { | 93 if (state() != State::CONNECTING) { |
| 89 VLOG(1) << "StopInstance() called while connecting"; | 94 VLOG(1) << "StopInstance() called while connecting"; |
| 90 return; | 95 return; |
| 91 } | 96 } |
| 92 | 97 |
| 93 instance_ptr_ = std::move(instance); | 98 instance_ptr_ = std::move(instance); |
| 94 instance_ptr_.set_connection_error_handler(base::Bind( | 99 instance_ptr_.set_connection_error_handler(base::Bind( |
| 95 &ArcBridgeServiceImpl::OnChannelClosed, weak_factory_.GetWeakPtr())); | 100 &ArcBridgeServiceImpl::OnChannelClosed, weak_factory_.GetWeakPtr())); |
| 96 | 101 |
| 97 instance_ptr_->Init(binding_.CreateInterfacePtrAndBind()); | 102 instance_ptr_->Init(binding_.CreateInterfacePtrAndBind()); |
| 98 | 103 |
| 104 VLOG(0) << "ARC ready"; | |
| 99 SetState(State::READY); | 105 SetState(State::READY); |
| 100 } | 106 } |
| 101 | 107 |
| 102 void ArcBridgeServiceImpl::OnStopped() { | 108 void ArcBridgeServiceImpl::OnStopped() { |
| 103 DCHECK(CalledOnValidThread()); | 109 DCHECK(CalledOnValidThread()); |
| 104 SetState(State::STOPPED); | 110 SetState(State::STOPPED); |
| 111 VLOG(0) << "ARC stopped"; | |
| 105 if (reconnect_) { | 112 if (reconnect_) { |
| 106 // There was a previous invocation and it crashed for some reason. Try | 113 // There was a previous invocation and it crashed for some reason. Try |
| 107 // starting the container again. | 114 // starting the container again. |
| 115 VLOG(0) << "ARC reconnecting"; | |
| 108 reconnect_ = false; | 116 reconnect_ = false; |
| 109 PrerequisitesChanged(); | 117 base::MessageLoop::current()->task_runner()->PostDelayedTask( |
|
Yusuke Sato
2016/04/29 00:28:41
What's the purpose of this change? Please mention
Luis Héctor Chávez
2016/04/29 20:23:15
Done, and added a comment.
| |
| 118 FROM_HERE, base::Bind(&ArcBridgeServiceImpl::PrerequisitesChanged, | |
| 119 weak_factory_.GetWeakPtr()), | |
| 120 base::TimeDelta::FromSeconds(5)); | |
|
Yusuke Sato
2016/04/29 00:28:41
Can you name the constant?
Luis Héctor Chávez
2016/04/29 20:23:15
Done.
| |
| 110 } | 121 } |
| 111 } | 122 } |
| 112 | 123 |
| 113 void ArcBridgeServiceImpl::OnChannelClosed() { | 124 void ArcBridgeServiceImpl::OnChannelClosed() { |
| 114 DCHECK(CalledOnValidThread()); | 125 DCHECK(CalledOnValidThread()); |
| 115 if (state() == State::STOPPED || state() == State::STOPPING) { | 126 if (state() == State::STOPPED || state() == State::STOPPING) { |
| 116 // This will happen when the instance is shut down. Ignore that case. | 127 // This will happen when the instance is shut down. Ignore that case. |
| 117 return; | 128 return; |
| 118 } | 129 } |
| 119 VLOG(1) << "Mojo connection lost"; | 130 VLOG(1) << "Mojo connection lost"; |
| 120 CloseAllChannels(); | 131 CloseAllChannels(); |
| 121 reconnect_ = true; | 132 reconnect_ = true; |
| 122 StopInstance(); | 133 StopInstance(); |
| 123 } | 134 } |
| 124 | 135 |
| 125 } // namespace arc | 136 } // namespace arc |
| OLD | NEW |