Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [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 "shill/device_claimer.h" |
| 6 | |
| 7 | #include "shill/device_info.h" |
| 8 | |
| 9 | using std::string; |
| 10 | |
| 11 | namespace shill { |
| 12 | |
| 13 | DeviceClaimer::DeviceClaimer( |
| 14 | const std::string &dbus_service_name, |
| 15 | DeviceInfo *device_info) |
| 16 | : dbus_service_name_(dbus_service_name), |
| 17 | device_info_(device_info) {} |
| 18 | |
| 19 | DeviceClaimer::~DeviceClaimer() { |
| 20 | // Release claimed devices if there is any. |
| 21 | if (DevicesClaimed()) { |
| 22 | for (const auto &device : claimed_device_names_) { |
| 23 | device_info_->RemoveDeviceFromBlackList(device); |
| 24 | } |
| 25 | // Clear claimed device list. |
| 26 | claimed_device_names_.clear(); |
| 27 | } |
| 28 | // Reset DBus name watcher. |
| 29 | dbus_name_watcher_.reset(); |
| 30 | } |
| 31 | |
| 32 | bool DeviceClaimer::StartDBusNameWatcher( |
| 33 | DBusManager *dbus_manager, |
| 34 | const DBusNameWatcher::NameAppearedCallback &name_appeared_callback, |
| 35 | const DBusNameWatcher::NameVanishedCallback &name_vanished_callback) { |
| 36 | if (dbus_name_watcher_) { |
| 37 | LOG(ERROR) << "DBus name watcher already started"; |
| 38 | return false; |
| 39 | } |
| 40 | dbus_name_watcher_.reset( |
| 41 | dbus_manager->CreateNameWatcher(dbus_service_name_, |
| 42 | name_appeared_callback, |
| 43 | name_vanished_callback)); |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | bool DeviceClaimer::Claim(const string &device_name, Error *error) { |
| 48 | // Check if device is claimed already. |
| 49 | if (claimed_device_names_.find(device_name) != claimed_device_names_.end()) { |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame^] | 50 | Error::PopulateAndLog(FROM_HERE, error, Error::kInvalidArguments, |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 51 | "Device " + device_name + |
| 52 | " had already been claimed"); |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | // Add device to the black list. |
| 57 | device_info_->AddDeviceToBlackList(device_name); |
| 58 | |
| 59 | claimed_device_names_.insert(device_name); |
| 60 | return true; |
| 61 | } |
| 62 | |
| 63 | bool DeviceClaimer::Release(const std::string &device_name, |
| 64 | Error *error) { |
| 65 | // Make sure this is a device that have been claimed. |
| 66 | if (claimed_device_names_.find(device_name) == claimed_device_names_.end()) { |
Paul Stewart | 34f424e | 2015-01-16 15:30:20 -0800 | [diff] [blame^] | 67 | Error::PopulateAndLog(FROM_HERE, error, Error::kInvalidArguments, |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 68 | "Device " + device_name + |
| 69 | " have not been claimed"); |
| 70 | return false; |
| 71 | } |
| 72 | |
| 73 | // Remove the device from the black list. |
| 74 | device_info_->RemoveDeviceFromBlackList(device_name); |
| 75 | |
| 76 | // Remove device from the claimed list. |
| 77 | claimed_device_names_.erase(device_name); |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | bool DeviceClaimer::DevicesClaimed() { |
| 82 | return !claimed_device_names_.empty(); |
| 83 | } |
| 84 | |
| 85 | } // namespace shill |