1 // Copyright 2013 The Chromium 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 DEVICE_NFC_NFC_ADAPTER_CHROMEOS_H_ 6 #define DEVICE_NFC_NFC_ADAPTER_CHROMEOS_H_ 7 8 #include "base/memory/weak_ptr.h" 9 #include "base/observer_list.h" 10 #include "chromeos/dbus/nfc_adapter_client.h" 11 #include "chromeos/dbus/nfc_device_client.h" 12 #include "chromeos/dbus/nfc_tag_client.h" 13 #include "dbus/object_path.h" 14 #include "device/nfc/nfc_adapter.h" 15 16 namespace device { 17 18 class NfcAdapterFactory; 19 20 } // namespace device 21 22 namespace chromeos { 23 24 // The NfcAdapterChromeOS class implements NfcAdapter for the Chrome OS 25 // platform. 26 class NfcAdapterChromeOS : public device::NfcAdapter, 27 public chromeos::NfcAdapterClient::Observer, 28 public chromeos::NfcDeviceClient::Observer, 29 public chromeos::NfcTagClient::Observer { 30 public: 31 // NfcAdapter overrides. 32 virtual void AddObserver(NfcAdapter::Observer* observer) OVERRIDE; 33 virtual void RemoveObserver(NfcAdapter::Observer* observer) OVERRIDE; 34 virtual bool IsPresent() const OVERRIDE; 35 virtual bool IsPowered() const OVERRIDE; 36 virtual bool IsPolling() const OVERRIDE; 37 virtual bool IsInitialized() const OVERRIDE; 38 virtual void SetPowered(bool powered, 39 const base::Closure& callback, 40 const ErrorCallback& error_callback) OVERRIDE; 41 virtual void StartPolling(const base::Closure& callback, 42 const ErrorCallback& error_callback) OVERRIDE; 43 virtual void StopPolling(const base::Closure& callback, 44 const ErrorCallback& error_callback) OVERRIDE; 45 46 private: 47 friend class device::NfcAdapterFactory; 48 friend class NfcChromeOSTest; 49 50 NfcAdapterChromeOS(); 51 virtual ~NfcAdapterChromeOS(); 52 53 // NfcAdapterClient::Observer overrides. 54 virtual void AdapterAdded(const dbus::ObjectPath& object_path) OVERRIDE; 55 virtual void AdapterRemoved(const dbus::ObjectPath& object_path) OVERRIDE; 56 virtual void AdapterPropertyChanged( 57 const dbus::ObjectPath& object_path, 58 const std::string& property_name) OVERRIDE; 59 60 // NfcDeviceClient::Observer overrides. 61 virtual void DeviceAdded(const dbus::ObjectPath& object_path) OVERRIDE; 62 virtual void DeviceRemoved(const dbus::ObjectPath& object_path) OVERRIDE; 63 virtual void DevicePropertyChanged(const dbus::ObjectPath& object_path, 64 const std::string& property_name) OVERRIDE; 65 66 // NfcTagClient::Observer overrides. 67 virtual void TagAdded(const dbus::ObjectPath& object_path) OVERRIDE; 68 virtual void TagRemoved(const dbus::ObjectPath& object_path) OVERRIDE; 69 virtual void TagPropertyChanged(const dbus::ObjectPath& object_path, 70 const std::string& property_name) OVERRIDE; 71 72 // Set the tracked adapter to the one in |object_path|, this object will 73 // subsequently operate on that adapter until it is removed. 74 void SetAdapter(const dbus::ObjectPath& object_path); 75 76 // Remove the currently tracked adapter. IsPresent() will return false after 77 // this is called. 78 void RemoveAdapter(); 79 80 // Announce to observers a change in the adapter state. 81 void PoweredChanged(bool powered); 82 void PollingChanged(bool polling); 83 void PresentChanged(bool present); 84 85 // Called by dbus:: on completion of the powered property change. 86 void OnSetPowered(const base::Closure& callback, 87 const ErrorCallback& error_callback, 88 bool success); 89 90 // Called by dbus:: on completion of the D-Bus method call to start polling. 91 void OnStartPolling(const base::Closure& callback); 92 void OnStartPollingError(const ErrorCallback& error_callback, 93 const std::string& error_name, 94 const std::string& error_message); 95 96 // Called by dbus:: on completion of the D-Bus method call to stop polling. 97 void OnStopPolling(const base::Closure& callback); 98 void OnStopPollingError(const ErrorCallback& error_callback, 99 const std::string& error_name, 100 const std::string& error_message); 101 102 // Object path of the adapter that we are currently tracking. 103 dbus::ObjectPath object_path_; 104 105 // List of observers interested in event notifications from us. 106 ObserverList<device::NfcAdapter::Observer> observers_; 107 108 // Note: This should remain the last member so it'll be destroyed and 109 // invalidate its weak pointers before any other members are destroyed. 110 base::WeakPtrFactory<NfcAdapterChromeOS> weak_ptr_factory_; 111 112 DISALLOW_COPY_AND_ASSIGN(NfcAdapterChromeOS); 113 }; 114 115 } // namespace chromeos 116 117 #endif // DEVICE_NFC_NFC_ADAPTER_CHROMEOS_H_ 118