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 64 // NfcTagClient::Observer overrides. 65 virtual void TagAdded(const dbus::ObjectPath& object_path) OVERRIDE; 66 virtual void TagRemoved(const dbus::ObjectPath& object_path) OVERRIDE; 67 68 // Set the tracked adapter to the one in |object_path|, this object will 69 // subsequently operate on that adapter until it is removed. 70 void SetAdapter(const dbus::ObjectPath& object_path); 71 72 // Remove the currently tracked adapter. IsPresent() will return false after 73 // this is called. 74 void RemoveAdapter(); 75 76 // Announce to observers a change in the adapter state. 77 void PoweredChanged(bool powered); 78 void PollingChanged(bool polling); 79 void PresentChanged(bool present); 80 81 // Called by dbus:: on completion of the powered property change. 82 void OnSetPowered(const base::Closure& callback, 83 const ErrorCallback& error_callback, 84 bool success); 85 86 // Called by dbus:: on completion of the D-Bus method call to start polling. 87 void OnStartPolling(const base::Closure& callback); 88 void OnStartPollingError(const ErrorCallback& error_callback, 89 const std::string& error_name, 90 const std::string& error_message); 91 92 // Called by dbus:: on completion of the D-Bus method call to stop polling. 93 void OnStopPolling(const base::Closure& callback); 94 void OnStopPollingError(const ErrorCallback& error_callback, 95 const std::string& error_name, 96 const std::string& error_message); 97 98 // Object path of the adapter that we are currently tracking. 99 dbus::ObjectPath object_path_; 100 101 // List of observers interested in event notifications from us. 102 ObserverList<device::NfcAdapter::Observer> observers_; 103 104 // Note: This should remain the last member so it'll be destroyed and 105 // invalidate its weak pointers before any other members are destroyed. 106 base::WeakPtrFactory<NfcAdapterChromeOS> weak_ptr_factory_; 107 108 DISALLOW_COPY_AND_ASSIGN(NfcAdapterChromeOS); 109 }; 110 111 } // namespace chromeos 112 113 #endif // DEVICE_NFC_NFC_ADAPTER_CHROMEOS_H_ 114