| 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/prefs/pref_registry_simple.h" | 12 #include "base/prefs/pref_registry_simple.h" |
| 13 #include "base/prefs/pref_service.h" | 13 #include "base/prefs/pref_service.h" |
| 14 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
| 15 #include "base/sys_info.h" | 15 #include "base/sys_info.h" |
| 16 #include "base/task_runner_util.h" | 16 #include "base/task_runner_util.h" |
| 17 #include "base/thread_task_runner_handle.h" | 17 #include "base/thread_task_runner_handle.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 "chromeos/dbus/session_manager_client.h" | 21 #include "chromeos/dbus/session_manager_client.h" |
| 22 #include "mojo/public/cpp/bindings/array.h" | |
| 23 #include "third_party/mojo/src/mojo/edk/embedder/embedder.h" | |
| 24 | |
| 25 namespace mojo { | |
| 26 | |
| 27 template <> | |
| 28 struct TypeConverter<arc::AppInfo, arc::AppInfoPtr> { | |
| 29 static arc::AppInfo Convert(const arc::AppInfoPtr& app_info_ptr) { | |
| 30 return *app_info_ptr; | |
| 31 } | |
| 32 }; | |
| 33 | |
| 34 template <> | |
| 35 struct TypeConverter<arc::RunningAppProcessInfo, | |
| 36 arc::RunningAppProcessInfoPtr> { | |
| 37 static arc::RunningAppProcessInfo Convert( | |
| 38 const arc::RunningAppProcessInfoPtr& process_ptr) { | |
| 39 return *process_ptr; | |
| 40 } | |
| 41 }; | |
| 42 | |
| 43 } // namespace mojo | |
| 44 | 22 |
| 45 namespace arc { | 23 namespace arc { |
| 46 | 24 |
| 47 ArcBridgeServiceImpl::ArcBridgeServiceImpl( | 25 ArcBridgeServiceImpl::ArcBridgeServiceImpl( |
| 48 scoped_ptr<ArcBridgeBootstrap> bootstrap) | 26 scoped_ptr<ArcBridgeBootstrap> bootstrap) |
| 49 : bootstrap_(std::move(bootstrap)), | 27 : bootstrap_(std::move(bootstrap)), |
| 50 binding_(this), | 28 binding_(this), |
| 51 session_started_(false), | 29 session_started_(false), |
| 52 weak_factory_(this) { | 30 weak_factory_(this) { |
| 53 bootstrap_->set_delegate(this); | 31 bootstrap_->set_delegate(this); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 DCHECK(CalledOnValidThread()); | 71 DCHECK(CalledOnValidThread()); |
| 94 if (state() == State::STOPPED || state() == State::STOPPING) { | 72 if (state() == State::STOPPED || state() == State::STOPPING) { |
| 95 VLOG(1) << "StopInstance() called when ARC is not running"; | 73 VLOG(1) << "StopInstance() called when ARC is not running"; |
| 96 return; | 74 return; |
| 97 } | 75 } |
| 98 | 76 |
| 99 SetState(State::STOPPING); | 77 SetState(State::STOPPING); |
| 100 bootstrap_->Stop(); | 78 bootstrap_->Stop(); |
| 101 } | 79 } |
| 102 | 80 |
| 103 bool ArcBridgeServiceImpl::RegisterInputDevice(const std::string& name, | |
| 104 const std::string& device_type, | |
| 105 base::ScopedFD fd) { | |
| 106 DCHECK(CalledOnValidThread()); | |
| 107 if (state() != State::READY) { | |
| 108 LOG(ERROR) << "Called RegisterInputDevice when the service is not ready"; | |
| 109 return false; | |
| 110 } | |
| 111 MojoHandle wrapped_handle; | |
| 112 MojoResult wrap_result = mojo::embedder::CreatePlatformHandleWrapper( | |
| 113 mojo::embedder::ScopedPlatformHandle( | |
| 114 mojo::embedder::PlatformHandle(fd.release())), | |
| 115 &wrapped_handle); | |
| 116 if (wrap_result != MOJO_RESULT_OK) { | |
| 117 LOG(WARNING) << "Pipe failed to wrap handles. Closing: " << wrap_result; | |
| 118 return false; | |
| 119 } | |
| 120 instance_ptr_->RegisterInputDevice( | |
| 121 name, device_type, mojo::ScopedHandle(mojo::Handle(wrapped_handle))); | |
| 122 return true; | |
| 123 } | |
| 124 | |
| 125 bool ArcBridgeServiceImpl::SendBroadcast(const std::string& action, | |
| 126 const std::string& package, | |
| 127 const std::string& clazz, | |
| 128 const base::DictionaryValue& extras) { | |
| 129 DCHECK(CalledOnValidThread()); | |
| 130 if (action.empty() || package.empty() || clazz.empty()) { | |
| 131 LOG(ERROR) << "SendBroadcast failed: Invalid parameter"; | |
| 132 return false; | |
| 133 } | |
| 134 std::string extras_json; | |
| 135 bool write_success = base::JSONWriter::Write(extras, &extras_json); | |
| 136 DCHECK(write_success); | |
| 137 if (state() != State::READY) { | |
| 138 LOG(ERROR) << "Called SendBroadcast when the service is not ready"; | |
| 139 return false; | |
| 140 } | |
| 141 instance_ptr_->SendBroadcast(action, package, clazz, extras_json); | |
| 142 return true; | |
| 143 } | |
| 144 | |
| 145 bool ArcBridgeServiceImpl::SendNotificationEventToAndroid( | |
| 146 const std::string& key, ArcNotificationEvent event) { | |
| 147 DCHECK(CalledOnValidThread()); | |
| 148 if (key.empty()) { | |
| 149 LOG(ERROR) << "SendNotificationToAndroid failed: Wrong parameter"; | |
| 150 return false; | |
| 151 } | |
| 152 if (state() != State::READY) { | |
| 153 LOG(ERROR) << "Called SendNotificationEventToAndroid when the service is" | |
| 154 << "not ready"; | |
| 155 return false; | |
| 156 } | |
| 157 instance_ptr_->SendNotificationEventToAndroid(key, event); | |
| 158 return true; | |
| 159 } | |
| 160 | |
| 161 bool ArcBridgeServiceImpl::RefreshAppList() { | |
| 162 DCHECK(CalledOnValidThread()); | |
| 163 if (state() != State::READY) { | |
| 164 LOG(ERROR) << "Called RefreshAppList when the service is not ready"; | |
| 165 return false; | |
| 166 } | |
| 167 instance_ptr_->RefreshAppList(); | |
| 168 return true; | |
| 169 } | |
| 170 | |
| 171 bool ArcBridgeServiceImpl::LaunchApp(const std::string& package, | |
| 172 const std::string& activity) { | |
| 173 DCHECK(CalledOnValidThread()); | |
| 174 if (state() != State::READY) { | |
| 175 LOG(ERROR) << "Called LaunchApp when the service is not ready"; | |
| 176 return false; | |
| 177 } | |
| 178 instance_ptr_->LaunchApp(package, activity); | |
| 179 return true; | |
| 180 } | |
| 181 | |
| 182 bool ArcBridgeServiceImpl::RequestAppIcon(const std::string& package, | |
| 183 const std::string& activity, | |
| 184 ScaleFactor scale_factor) { | |
| 185 DCHECK(CalledOnValidThread()); | |
| 186 if (state() != State::READY) { | |
| 187 LOG(ERROR) << "Called RequestAppIcon when the service is not ready"; | |
| 188 return false; | |
| 189 } | |
| 190 instance_ptr_->RequestAppIcon(package, activity, scale_factor); | |
| 191 return true; | |
| 192 } | |
| 193 | |
| 194 bool ArcBridgeServiceImpl::RequestProcessList() { | |
| 195 DCHECK(CalledOnValidThread()); | |
| 196 if (state() != State::READY) { | |
| 197 LOG_IF(ERROR, base::SysInfo::IsRunningOnChromeOS()) | |
| 198 << "Called RequestProcessList when the service is not ready"; | |
| 199 return false; | |
| 200 } | |
| 201 instance_ptr_->RequestProcessList(); | |
| 202 return true; | |
| 203 } | |
| 204 | |
| 205 void ArcBridgeServiceImpl::OnInstanceBootPhase(InstanceBootPhase phase) { | |
| 206 DCHECK(CalledOnValidThread()); | |
| 207 // The state can be CONNECTED the first time this is called, and will then | |
| 208 // transition to READY after BRIDGE_READY has been passed. | |
| 209 if (state() != State::CONNECTED && state() != State::READY) { | |
| 210 VLOG(1) << "StopInstance() called while connecting"; | |
| 211 return; | |
| 212 } | |
| 213 if (phase == INSTANCE_BOOT_PHASE_BRIDGE_READY) { | |
| 214 SetState(State::READY); | |
| 215 } | |
| 216 FOR_EACH_OBSERVER(Observer, observer_list(), OnInstanceBootPhase(phase)); | |
| 217 } | |
| 218 | |
| 219 void ArcBridgeServiceImpl::OnNotificationPosted(ArcNotificationDataPtr data) { | |
| 220 DCHECK(CalledOnValidThread()); | |
| 221 FOR_EACH_OBSERVER(NotificationObserver, notification_observer_list(), | |
| 222 OnNotificationPostedFromAndroid(*data.get())); | |
| 223 } | |
| 224 | |
| 225 void ArcBridgeServiceImpl::OnNotificationRemoved(const mojo::String& key) { | |
| 226 DCHECK(CalledOnValidThread()); | |
| 227 FOR_EACH_OBSERVER(NotificationObserver, notification_observer_list(), | |
| 228 OnNotificationRemovedFromAndroid(key)); | |
| 229 } | |
| 230 | |
| 231 void ArcBridgeServiceImpl::OnAppListRefreshed( | |
| 232 mojo::Array<arc::AppInfoPtr> apps_ptr) { | |
| 233 DCHECK(CalledOnValidThread()); | |
| 234 std::vector<arc::AppInfo> apps(apps_ptr.To<std::vector<arc::AppInfo>>()); | |
| 235 FOR_EACH_OBSERVER(AppObserver, app_observer_list(), OnAppListRefreshed(apps)); | |
| 236 } | |
| 237 | |
| 238 void ArcBridgeServiceImpl::OnAppIcon(const mojo::String& package, | |
| 239 const mojo::String& activity, | |
| 240 ScaleFactor scale_factor, | |
| 241 mojo::Array<uint8_t> icon_png_data) { | |
| 242 DCHECK(CalledOnValidThread()); | |
| 243 FOR_EACH_OBSERVER( | |
| 244 AppObserver, app_observer_list(), | |
| 245 OnAppIcon(package, activity, scale_factor, icon_png_data.storage())); | |
| 246 } | |
| 247 | |
| 248 void ArcBridgeServiceImpl::OnUpdateProcessList( | |
| 249 mojo::Array<RunningAppProcessInfoPtr> processes_ptr) { | |
| 250 DCHECK(CalledOnValidThread()); | |
| 251 std::vector<RunningAppProcessInfo> processes( | |
| 252 processes_ptr.To<std::vector<RunningAppProcessInfo>>()); | |
| 253 FOR_EACH_OBSERVER( | |
| 254 ProcessObserver, | |
| 255 process_observer_list(), | |
| 256 OnUpdateProcessList(processes)); | |
| 257 } | |
| 258 | |
| 259 void ArcBridgeServiceImpl::OnAcquireDisplayWakeLock(DisplayWakeLockType type) { | |
| 260 DCHECK(CalledOnValidThread()); | |
| 261 // TODO(ejcaruso): Implement. | |
| 262 VLOG(1) << "OnAcquireDisplayWakeLock"; | |
| 263 } | |
| 264 | |
| 265 void ArcBridgeServiceImpl::OnReleaseDisplayWakeLock(DisplayWakeLockType type) { | |
| 266 DCHECK(CalledOnValidThread()); | |
| 267 // TODO(ejcaruso): Implement. | |
| 268 VLOG(1) << "OnReleaseDisplayWakeLock"; | |
| 269 } | |
| 270 | |
| 271 void ArcBridgeServiceImpl::OnArcAvailable(bool arc_available) { | 81 void ArcBridgeServiceImpl::OnArcAvailable(bool arc_available) { |
| 272 DCHECK(CalledOnValidThread()); | 82 DCHECK(CalledOnValidThread()); |
| 273 if (available() == arc_available) | 83 if (available() == arc_available) |
| 274 return; | 84 return; |
| 275 SetAvailable(arc_available); | 85 SetAvailable(arc_available); |
| 276 PrerequisitesChanged(); | 86 PrerequisitesChanged(); |
| 277 } | 87 } |
| 278 | 88 |
| 279 void ArcBridgeServiceImpl::OnConnectionEstablished( | 89 void ArcBridgeServiceImpl::OnConnectionEstablished( |
| 280 ArcBridgeInstancePtr instance) { | 90 ArcBridgeInstancePtr instance) { |
| 281 DCHECK(CalledOnValidThread()); | 91 DCHECK(CalledOnValidThread()); |
| 282 if (state() != State::CONNECTING) { | 92 if (state() != State::CONNECTING) { |
| 283 VLOG(1) << "StopInstance() called while connecting"; | 93 VLOG(1) << "StopInstance() called while connecting"; |
| 284 return; | 94 return; |
| 285 } | 95 } |
| 286 | 96 |
| 287 instance_ptr_ = std::move(instance); | 97 instance_ptr_ = std::move(instance); |
| 288 | 98 |
| 289 ArcBridgeHostPtr host; | 99 ArcBridgeHostPtr host; |
| 290 binding_.Bind(GetProxy(&host)); | 100 binding_.Bind(GetProxy(&host)); |
| 291 instance_ptr_->Init(std::move(host)); | 101 instance_ptr_->Init(std::move(host)); |
| 292 | 102 |
| 293 SetState(State::CONNECTED); | 103 SetState(State::READY); |
| 294 } | 104 } |
| 295 | 105 |
| 296 void ArcBridgeServiceImpl::OnStopped() { | 106 void ArcBridgeServiceImpl::OnStopped() { |
| 297 DCHECK(CalledOnValidThread()); | 107 DCHECK(CalledOnValidThread()); |
| 298 SetState(State::STOPPED); | 108 SetState(State::STOPPED); |
| 299 } | 109 } |
| 300 | 110 |
| 301 } // namespace arc | 111 } // namespace arc |
| OLD | NEW |