Vitaly Buka | 7ce499f | 2015-06-09 08:04:11 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 "buffet/privet/ap_manager_client.h" |
| 6 | |
| 7 | namespace privetd { |
| 8 | |
| 9 | using org::chromium::apmanager::ConfigProxy; |
| 10 | using org::chromium::apmanager::ManagerProxy; |
| 11 | using org::chromium::apmanager::ServiceProxy; |
| 12 | |
| 13 | ApManagerClient::ApManagerClient(const scoped_refptr<dbus::Bus>& bus) |
| 14 | : bus_(bus) { |
| 15 | } |
| 16 | |
| 17 | ApManagerClient::~ApManagerClient() { |
| 18 | Stop(); |
| 19 | } |
| 20 | |
| 21 | void ApManagerClient::Start(const std::string& ssid) { |
| 22 | if (service_path_.IsValid()) { |
| 23 | return; |
| 24 | } |
| 25 | |
| 26 | ssid_ = ssid; |
| 27 | |
| 28 | object_manager_proxy_.reset( |
| 29 | new org::chromium::apmanager::ObjectManagerProxy{bus_}); |
| 30 | object_manager_proxy_->SetManagerAddedCallback(base::Bind( |
| 31 | &ApManagerClient::OnManagerAdded, weak_ptr_factory_.GetWeakPtr())); |
| 32 | object_manager_proxy_->SetServiceAddedCallback(base::Bind( |
| 33 | &ApManagerClient::OnServiceAdded, weak_ptr_factory_.GetWeakPtr())); |
| 34 | |
| 35 | object_manager_proxy_->SetServiceRemovedCallback(base::Bind( |
| 36 | &ApManagerClient::OnServiceRemoved, weak_ptr_factory_.GetWeakPtr())); |
| 37 | object_manager_proxy_->SetManagerRemovedCallback(base::Bind( |
| 38 | &ApManagerClient::OnManagerRemoved, weak_ptr_factory_.GetWeakPtr())); |
| 39 | } |
| 40 | |
| 41 | void ApManagerClient::Stop() { |
| 42 | if (manager_proxy_ && service_path_.IsValid()) { |
| 43 | RemoveService(service_path_); |
| 44 | } |
| 45 | service_path_ = dbus::ObjectPath(); |
| 46 | service_proxy_ = nullptr; |
| 47 | manager_proxy_ = nullptr; |
| 48 | object_manager_proxy_.reset(); |
| 49 | ssid_.clear(); |
| 50 | } |
| 51 | |
| 52 | void ApManagerClient::RemoveService(const dbus::ObjectPath& object_path) { |
| 53 | CHECK(object_path.IsValid()); |
| 54 | chromeos::ErrorPtr error; |
| 55 | if (!manager_proxy_->RemoveService(object_path, &error)) { |
| 56 | LOG(ERROR) << "RemoveService failed: " << error->GetMessage(); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void ApManagerClient::OnManagerAdded(ManagerProxy* manager_proxy) { |
| 61 | VLOG(1) << "manager added: " << manager_proxy->GetObjectPath().value(); |
| 62 | manager_proxy_ = manager_proxy; |
| 63 | |
| 64 | if (service_path_.IsValid()) |
| 65 | return; |
| 66 | |
| 67 | chromeos::ErrorPtr error; |
| 68 | if (!manager_proxy_->CreateService(&service_path_, &error)) { |
| 69 | LOG(ERROR) << "CreateService failed: " << error->GetMessage(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void ApManagerClient::OnServiceAdded(ServiceProxy* service_proxy) { |
| 74 | VLOG(1) << "service added: " << service_proxy->GetObjectPath().value(); |
| 75 | if (service_proxy->GetObjectPath() != service_path_) { |
| 76 | RemoveService(service_proxy->GetObjectPath()); |
| 77 | return; |
| 78 | } |
| 79 | service_proxy_ = service_proxy; |
| 80 | |
| 81 | ConfigProxy* config_proxy = |
| 82 | object_manager_proxy_->GetConfigProxy(service_proxy->config()); |
| 83 | ConfigProxy::PropertySet* properties = config_proxy->GetProperties(); |
| 84 | properties->ssid.Set(ssid_, base::Bind(&ApManagerClient::OnSsidSet, |
| 85 | weak_ptr_factory_.GetWeakPtr())); |
| 86 | } |
| 87 | |
| 88 | void ApManagerClient::OnSsidSet(bool success) { |
| 89 | if (!success || !service_proxy_) { |
| 90 | LOG(ERROR) << "Failed to set ssid."; |
| 91 | return; |
| 92 | } |
| 93 | VLOG(1) << "SSID is set: " << ssid_; |
| 94 | |
| 95 | chromeos::ErrorPtr error; |
| 96 | if (!service_proxy_->Start(&error)) { |
| 97 | LOG(ERROR) << "Service start failed: " << error->GetMessage(); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void ApManagerClient::OnServiceRemoved(const dbus::ObjectPath& object_path) { |
| 102 | VLOG(1) << "service removed: " << object_path.value(); |
| 103 | if (object_path != service_path_) |
| 104 | return; |
| 105 | service_path_ = dbus::ObjectPath(); |
| 106 | service_proxy_ = nullptr; |
| 107 | } |
| 108 | |
| 109 | void ApManagerClient::OnManagerRemoved(const dbus::ObjectPath& object_path) { |
| 110 | VLOG(1) << "manager removed: " << object_path.value(); |
| 111 | manager_proxy_ = nullptr; |
| 112 | Stop(); |
| 113 | } |
| 114 | |
| 115 | } // namespace privetd |