blob: d253f7640aeaa370c8d3f3eacd0f6bb8b43c22d4 [file] [log] [blame]
Peter Qiu7e8b8ee2014-11-25 13:55:57 -08001// 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
14namespace shill {
15
16class DeviceInfo;
17
Peter Qiu1d499ed2015-01-30 16:01:27 -080018// 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 Qiu7e8b8ee2014-11-25 13:55:57 -080023class DeviceClaimer {
24 public:
25 DeviceClaimer(
Paul Stewarta794cd62015-06-16 13:13:10 -070026 const std::string& dbus_service_name,
27 DeviceInfo* device_info,
Peter Qiu1d499ed2015-01-30 16:01:27 -080028 bool default_claimer);
Peter Qiu7e8b8ee2014-11-25 13:55:57 -080029 virtual ~DeviceClaimer();
30
31 virtual bool StartDBusNameWatcher(
Paul Stewarta794cd62015-06-16 13:13:10 -070032 DBusManager* dbus_manager,
33 const DBusNameWatcher::NameAppearedCallback& name_appeared_callback,
34 const DBusNameWatcher::NameVanishedCallback& name_vanished_callback);
Peter Qiu7e8b8ee2014-11-25 13:55:57 -080035
Paul Stewarta794cd62015-06-16 13:13:10 -070036 virtual bool Claim(const std::string& device_name, Error* error);
37 virtual bool Release(const std::string& device_name, Error* error);
Peter Qiu7e8b8ee2014-11-25 13:55:57 -080038
Peter Qiu1d499ed2015-01-30 16:01:27 -080039 // Return true if there are devices claimed by this claimer, false
Peter Qiu7e8b8ee2014-11-25 13:55:57 -080040 // otherwise.
41 virtual bool DevicesClaimed();
42
Peter Qiu1d499ed2015-01-30 16:01:27 -080043 // Return true if the specified device is released by this claimer, false
44 // otherwise.
Paul Stewarta794cd62015-06-16 13:13:10 -070045 virtual bool IsDeviceReleased(const std::string& device_name);
Peter Qiu1d499ed2015-01-30 16:01:27 -080046
Paul Stewarta794cd62015-06-16 13:13:10 -070047 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 Qiu7e8b8ee2014-11-25 13:55:57 -080050 dbus_service_owner_ = dbus_service_owner;
51 }
52
Peter Qiu1d499ed2015-01-30 16:01:27 -080053 virtual bool default_claimer() const { return default_claimer_; }
54
Paul Stewarta794cd62015-06-16 13:13:10 -070055 const std::set<std::string>& claimed_device_names() const {
Garret Kellybabfe5a2015-04-14 16:32:33 -040056 return claimed_device_names_;
57 }
58
Peter Qiu7e8b8ee2014-11-25 13:55:57 -080059 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 Qiu1d499ed2015-01-30 16:01:27 -080064 // The name of devices that have been released by this claimer.
65 std::set<std::string> released_device_names_;
Peter Qiu7e8b8ee2014-11-25 13:55:57 -080066 // 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 Stewarta794cd62015-06-16 13:13:10 -070071 DeviceInfo* device_info_;
Peter Qiu7e8b8ee2014-11-25 13:55:57 -080072
Peter Qiu1d499ed2015-01-30 16:01:27 -080073 // 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 Qiu7e8b8ee2014-11-25 13:55:57 -080077 DISALLOW_COPY_AND_ASSIGN(DeviceClaimer);
78};
79
80} // namespace shill
81
82#endif // SHILL_DEVICE_CLAIMER_H_