OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/arc/arc_bridge_service.h" |
| 6 |
| 7 #include "base/files/file_path.h" |
| 8 #include "base/files/file_util.h" |
| 9 #include "base/prefs/pref_registry_simple.h" |
| 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/task_runner_util.h" |
| 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "chromeos/dbus/dbus_method_call_status.h" |
| 14 #include "chromeos/dbus/dbus_thread_manager.h" |
| 15 #include "chromeos/dbus/session_manager_client.h" |
| 16 #include "components/arc/common/arc_host_messages.h" |
| 17 #include "components/arc/common/arc_instance_messages.h" |
| 18 #include "ipc/ipc_channel.h" |
| 19 |
| 20 namespace arc { |
| 21 |
| 22 namespace { |
| 23 |
| 24 const base::FilePath::CharType kArcBridgeSocketPath[] = |
| 25 FILE_PATH_LITERAL("/home/chronos/ArcBridge/bridge.sock"); |
| 26 |
| 27 // Weak pointer. This class is owned by ChromeBrowserMainPartsChromeos. |
| 28 ArcBridgeService* g_arc_bridge_service = nullptr; |
| 29 |
| 30 } // namespace |
| 31 |
| 32 ArcBridgeService::ArcBridgeService( |
| 33 const scoped_refptr<base::SingleThreadTaskRunner>& ipc_task_runner, |
| 34 const scoped_refptr<base::SequencedTaskRunner>& file_task_runner) |
| 35 : origin_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 36 ipc_task_runner_(ipc_task_runner), |
| 37 file_task_runner_(file_task_runner), |
| 38 session_started_(false), |
| 39 available_(false), |
| 40 state_(State::STOPPED), |
| 41 weak_factory_(this) { |
| 42 DCHECK(!g_arc_bridge_service); |
| 43 g_arc_bridge_service = this; |
| 44 } |
| 45 |
| 46 ArcBridgeService::~ArcBridgeService() { |
| 47 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 48 DCHECK(state_ == State::STOPPING || state_ == State::STOPPED); |
| 49 DCHECK(g_arc_bridge_service); |
| 50 g_arc_bridge_service = nullptr; |
| 51 } |
| 52 |
| 53 // static |
| 54 ArcBridgeService* ArcBridgeService::Get() { |
| 55 DCHECK(g_arc_bridge_service); |
| 56 DCHECK(g_arc_bridge_service->origin_task_runner_->RunsTasksOnCurrentThread()); |
| 57 return g_arc_bridge_service; |
| 58 } |
| 59 |
| 60 void ArcBridgeService::DetectAvailability() { |
| 61 chromeos::SessionManagerClient* session_manager_client = |
| 62 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(); |
| 63 session_manager_client->CheckArcAvailability(base::Bind( |
| 64 &ArcBridgeService::OnArcAvailable, weak_factory_.GetWeakPtr())); |
| 65 } |
| 66 |
| 67 void ArcBridgeService::HandleStartup() { |
| 68 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 69 session_started_ = true; |
| 70 PrerequisitesChanged(); |
| 71 } |
| 72 |
| 73 void ArcBridgeService::Shutdown() { |
| 74 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 75 session_started_ = false; |
| 76 PrerequisitesChanged(); |
| 77 } |
| 78 |
| 79 void ArcBridgeService::AddObserver(Observer* observer) { |
| 80 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 81 observer_list_.AddObserver(observer); |
| 82 } |
| 83 |
| 84 void ArcBridgeService::RemoveObserver(Observer* observer) { |
| 85 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 86 observer_list_.RemoveObserver(observer); |
| 87 } |
| 88 |
| 89 void ArcBridgeService::PrerequisitesChanged() { |
| 90 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 91 if (state_ == State::STOPPED) { |
| 92 if (!available_ || !session_started_) |
| 93 return; |
| 94 SocketConnect(base::FilePath(kArcBridgeSocketPath)); |
| 95 } else { |
| 96 if (available_ && session_started_) |
| 97 return; |
| 98 StopInstance(); |
| 99 } |
| 100 } |
| 101 |
| 102 void ArcBridgeService::StopInstance() { |
| 103 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 104 if (state_ == State::STOPPED || state_ == State::STOPPING) { |
| 105 VLOG(1) << "StopInstance() called when ARC is not running"; |
| 106 return; |
| 107 } |
| 108 if (state_ == State::CONNECTED || state_ == State::CONNECTING) { |
| 109 // This was stopped before the D-Bus command to start the instance. Just |
| 110 // close the socket (if it was opened). |
| 111 if (state_ == State::CONNECTED) { |
| 112 ipc_channel_.reset(); |
| 113 } |
| 114 SetState(State::STOPPED); |
| 115 return; |
| 116 } |
| 117 |
| 118 SetState(State::STOPPING); |
| 119 chromeos::SessionManagerClient* session_manager_client = |
| 120 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(); |
| 121 session_manager_client->StopArcInstance(base::Bind( |
| 122 &ArcBridgeService::OnInstanceStopped, weak_factory_.GetWeakPtr())); |
| 123 } |
| 124 |
| 125 bool ArcBridgeService::RegisterInputDevice(const std::string& name, |
| 126 const std::string& device_type, |
| 127 base::ScopedFD fd) { |
| 128 DCHECK(ipc_task_runner_->RunsTasksOnCurrentThread()); |
| 129 if (state_ != State::READY) { |
| 130 LOG(ERROR) << "Called RegisterInputDevice when the service is not ready"; |
| 131 return false; |
| 132 } |
| 133 return ipc_channel_->Send(new ArcInstanceMsg_RegisterInputDevice( |
| 134 name, device_type, base::FileDescriptor(fd.Pass()))); |
| 135 } |
| 136 |
| 137 void ArcBridgeService::SocketConnect(const base::FilePath& socket_path) { |
| 138 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 139 if (state_ != State::STOPPED) { |
| 140 VLOG(1) << "SocketConnect() called when instance is not stopped"; |
| 141 return; |
| 142 } |
| 143 SetState(State::CONNECTING); |
| 144 base::PostTaskAndReplyWithResult( |
| 145 file_task_runner_.get(), FROM_HERE, |
| 146 base::Bind(&base::CreateDirectory, socket_path.DirName()), |
| 147 base::Bind(&ArcBridgeService::SocketConnectAfterEnsureParentDirectory, |
| 148 weak_factory_.GetWeakPtr(), socket_path)); |
| 149 } |
| 150 |
| 151 void ArcBridgeService::SocketConnectAfterEnsureParentDirectory( |
| 152 const base::FilePath& socket_path, |
| 153 bool directory_present) { |
| 154 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 155 if (state_ != State::CONNECTING) { |
| 156 VLOG(1) << "StopInstance() called while connecting"; |
| 157 return; |
| 158 } |
| 159 if (!directory_present) { |
| 160 LOG(ERROR) << "Error creating directory for " << socket_path.value(); |
| 161 StopInstance(); |
| 162 return; |
| 163 } |
| 164 |
| 165 if (!Connect(IPC::ChannelHandle(socket_path.value()), |
| 166 IPC::Channel::MODE_OPEN_NAMED_SERVER)) { |
| 167 LOG(ERROR) << "Error connecting to " << socket_path.value(); |
| 168 StopInstance(); |
| 169 return; |
| 170 } |
| 171 |
| 172 base::PostTaskAndReplyWithResult( |
| 173 file_task_runner_.get(), FROM_HERE, |
| 174 // TODO(lhchavez): Tighten the security around the socket by tying it to |
| 175 // the user the instance will run as. |
| 176 base::Bind(&base::SetPosixFilePermissions, socket_path, 0777), |
| 177 base::Bind(&ArcBridgeService::SocketConnectAfterSetSocketPermissions, |
| 178 weak_factory_.GetWeakPtr(), socket_path)); |
| 179 } |
| 180 |
| 181 bool ArcBridgeService::Connect(const IPC::ChannelHandle& handle, |
| 182 IPC::Channel::Mode mode) { |
| 183 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 184 if (state_ != State::CONNECTING) { |
| 185 VLOG(1) << "StopInstance() called while connecting"; |
| 186 return false; |
| 187 } |
| 188 |
| 189 ipc_channel_ = |
| 190 IPC::ChannelProxy::Create(handle, mode, this, ipc_task_runner_.get()); |
| 191 if (!ipc_channel_) |
| 192 return false; |
| 193 SetState(State::CONNECTED); |
| 194 return true; |
| 195 } |
| 196 |
| 197 void ArcBridgeService::SocketConnectAfterSetSocketPermissions( |
| 198 const base::FilePath& socket_path, |
| 199 bool socket_permissions_success) { |
| 200 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 201 if (state_ != State::CONNECTED) { |
| 202 VLOG(1) << "StopInstance() called while connecting"; |
| 203 return; |
| 204 } |
| 205 |
| 206 if (!socket_permissions_success) { |
| 207 LOG(ERROR) << "Error setting socket permissions for " |
| 208 << socket_path.value(); |
| 209 StopInstance(); |
| 210 return; |
| 211 } |
| 212 |
| 213 SetState(State::STARTING); |
| 214 chromeos::SessionManagerClient* session_manager_client = |
| 215 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(); |
| 216 session_manager_client->StartArcInstance( |
| 217 socket_path.value(), base::Bind(&ArcBridgeService::OnInstanceStarted, |
| 218 weak_factory_.GetWeakPtr())); |
| 219 } |
| 220 |
| 221 void ArcBridgeService::OnInstanceStarted(bool success) { |
| 222 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 223 if (state_ != State::STARTING) { |
| 224 VLOG(1) << "StopInstance() called while connecting"; |
| 225 return; |
| 226 } |
| 227 if (!success) { |
| 228 LOG(ERROR) << "ARC instance unable to start. Shutting down the bridge"; |
| 229 StopInstance(); |
| 230 return; |
| 231 } |
| 232 } |
| 233 |
| 234 void ArcBridgeService::OnInstanceReady() { |
| 235 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 236 if (state_ != State::STARTING) { |
| 237 VLOG(1) << "StopInstance() called while connecting"; |
| 238 return; |
| 239 } |
| 240 SetState(State::READY); |
| 241 } |
| 242 |
| 243 void ArcBridgeService::SetState(State state) { |
| 244 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 245 // DCHECK on enum classes not supported. |
| 246 DCHECK(state_ != state); |
| 247 state_ = state; |
| 248 FOR_EACH_OBSERVER(Observer, observer_list_, OnStateChanged(state_)); |
| 249 } |
| 250 |
| 251 bool ArcBridgeService::OnMessageReceived(const IPC::Message& message) { |
| 252 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 253 bool handled = true; |
| 254 |
| 255 IPC_BEGIN_MESSAGE_MAP(ArcBridgeService, message) |
| 256 IPC_MESSAGE_HANDLER(ArcInstanceHostMsg_InstanceReady, OnInstanceReady) |
| 257 IPC_MESSAGE_UNHANDLED(handled = false) |
| 258 IPC_END_MESSAGE_MAP() |
| 259 |
| 260 if (!handled) |
| 261 LOG(ERROR) << "Invalid message with type = " << message.type(); |
| 262 return handled; |
| 263 } |
| 264 |
| 265 void ArcBridgeService::OnArcAvailable(bool available) { |
| 266 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 267 if (available_ == available) |
| 268 return; |
| 269 available_ = available; |
| 270 FOR_EACH_OBSERVER(Observer, observer_list_, OnAvailableChanged(available_)); |
| 271 PrerequisitesChanged(); |
| 272 } |
| 273 |
| 274 void ArcBridgeService::OnInstanceStopped(bool success) { |
| 275 DCHECK(origin_task_runner_->RunsTasksOnCurrentThread()); |
| 276 // STOPPING is the only valid state for this function. |
| 277 // DCHECK on enum classes not supported. |
| 278 DCHECK(state_ == State::STOPPING); |
| 279 ipc_channel_.reset(); |
| 280 SetState(State::STOPPED); |
| 281 } |
| 282 |
| 283 } // namespace arc |
OLD | NEW |