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 | #ifndef SHILL_DEVICE_CLAIMER_H_ |
| 6 | #define SHILL_DEVICE_CLAIMER_H_ |
| 7 | |
| 8 | #include <set> |
| 9 | #include <string> |
| 10 | |
| 11 | #include "shill/dbus_manager.h" |
| 12 | #include "shill/error.h" |
| 13 | |
| 14 | namespace shill { |
| 15 | |
| 16 | class DeviceInfo; |
| 17 | |
Peter Qiu | 1d499ed | 2015-01-30 16:01:27 -0800 | [diff] [blame] | 18 | // Provide an abstraction for remote service to claim/release devices |
| 19 | // from/to shill. When the DBus service name is provided, which means that |
| 20 | // the remote service is a DBus endpoint, Shill will perform DBus monitoring |
| 21 | // on that service, and revert all operations performed by that service when |
| 22 | // it disappears. |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 23 | class DeviceClaimer { |
| 24 | public: |
| 25 | DeviceClaimer( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 26 | const std::string& dbus_service_name, |
| 27 | DeviceInfo* device_info, |
Peter Qiu | 1d499ed | 2015-01-30 16:01:27 -0800 | [diff] [blame] | 28 | bool default_claimer); |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 29 | virtual ~DeviceClaimer(); |
| 30 | |
| 31 | virtual bool StartDBusNameWatcher( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 32 | DBusManager* dbus_manager, |
| 33 | const DBusNameWatcher::NameAppearedCallback& name_appeared_callback, |
| 34 | const DBusNameWatcher::NameVanishedCallback& name_vanished_callback); |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 35 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 36 | virtual bool Claim(const std::string& device_name, Error* error); |
| 37 | virtual bool Release(const std::string& device_name, Error* error); |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 38 | |
Peter Qiu | 1d499ed | 2015-01-30 16:01:27 -0800 | [diff] [blame] | 39 | // Return true if there are devices claimed by this claimer, false |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 40 | // otherwise. |
| 41 | virtual bool DevicesClaimed(); |
| 42 | |
Peter Qiu | 1d499ed | 2015-01-30 16:01:27 -0800 | [diff] [blame] | 43 | // Return true if the specified device is released by this claimer, false |
| 44 | // otherwise. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 45 | virtual bool IsDeviceReleased(const std::string& device_name); |
Peter Qiu | 1d499ed | 2015-01-30 16:01:27 -0800 | [diff] [blame] | 46 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 47 | const std::string& name() const { return dbus_service_name_; } |
| 48 | const std::string& owner() const { return dbus_service_owner_; } |
| 49 | void set_owner(const std::string& dbus_service_owner) { |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 50 | dbus_service_owner_ = dbus_service_owner; |
| 51 | } |
| 52 | |
Peter Qiu | 1d499ed | 2015-01-30 16:01:27 -0800 | [diff] [blame] | 53 | virtual bool default_claimer() const { return default_claimer_; } |
| 54 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 55 | const std::set<std::string>& claimed_device_names() const { |
Garret Kelly | babfe5a | 2015-04-14 16:32:33 -0400 | [diff] [blame] | 56 | return claimed_device_names_; |
| 57 | } |
| 58 | |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 59 | private: |
| 60 | // DBus name watcher for monitoring the DBus service of the claimer. |
| 61 | std::unique_ptr<DBusNameWatcher> dbus_name_watcher_; |
| 62 | // The name of devices that have been claimed by this claimer. |
| 63 | std::set<std::string> claimed_device_names_; |
Peter Qiu | 1d499ed | 2015-01-30 16:01:27 -0800 | [diff] [blame] | 64 | // The name of devices that have been released by this claimer. |
| 65 | std::set<std::string> released_device_names_; |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 66 | // DBus service name of the claimer. |
| 67 | std::string dbus_service_name_; |
| 68 | // DBus service owner name of the claimer. |
| 69 | std::string dbus_service_owner_; |
| 70 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 71 | DeviceInfo* device_info_; |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 72 | |
Peter Qiu | 1d499ed | 2015-01-30 16:01:27 -0800 | [diff] [blame] | 73 | // Flag indicating if this is the default claimer. When set to true, this |
| 74 | // claimer will only be deleted when shill terminates. |
| 75 | bool default_claimer_; |
| 76 | |
Peter Qiu | 7e8b8ee | 2014-11-25 13:55:57 -0800 | [diff] [blame] | 77 | DISALLOW_COPY_AND_ASSIGN(DeviceClaimer); |
| 78 | }; |
| 79 | |
| 80 | } // namespace shill |
| 81 | |
| 82 | #endif // SHILL_DEVICE_CLAIMER_H_ |