Home | History | Annotate | Download | only in nfc
      1 // Copyright 2014 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_TAG_TECHNOLOGY_CHROMEOS_H_
      6 #define DEVICE_NFC_NFC_TAG_TECHNOLOGY_CHROMEOS_H_
      7 
      8 #include <map>
      9 
     10 #include "base/memory/weak_ptr.h"
     11 #include "base/observer_list.h"
     12 #include "chromeos/dbus/nfc_record_client.h"
     13 #include "device/nfc/nfc_tag_technology.h"
     14 
     15 namespace chromeos {
     16 
     17 class NfcTagChromeOS;
     18 
     19 // The NfcNdefTagTechnologyChromeOS class implements
     20 // device::NfcNdefTagTechnology for the Chrome OS platform. The lifetime of an
     21 // instance of this class must be tied to an instance of NfcTagChromeOS.
     22 // Instances of this class must never outlast the owning NfcTagChromeOS
     23 // instance.
     24 class NfcNdefTagTechnologyChromeOS : public device::NfcNdefTagTechnology,
     25                                      public NfcRecordClient::Observer {
     26  public:
     27   virtual ~NfcNdefTagTechnologyChromeOS();
     28 
     29   // device::NfcNdefTagTechnology overrides.
     30   virtual void AddObserver(device::NfcNdefTagTechnology::Observer* observer)
     31     OVERRIDE;
     32   virtual void RemoveObserver(device::NfcNdefTagTechnology::Observer* observer)
     33     OVERRIDE;
     34   virtual const device::NfcNdefMessage& GetNdefMessage() const OVERRIDE;
     35   virtual void WriteNdef(const device::NfcNdefMessage& message,
     36                          const base::Closure& callback,
     37                          const ErrorCallback& error_callback) OVERRIDE;
     38 
     39   // NfcRecordClient::Observer overrides.
     40   virtual void RecordAdded(const dbus::ObjectPath& object_path) OVERRIDE;
     41   virtual void RecordRemoved(const dbus::ObjectPath& object_path) OVERRIDE;
     42   virtual void RecordPropertiesReceived(
     43       const dbus::ObjectPath& object_path) OVERRIDE;
     44 
     45  private:
     46   friend class NfcTagChromeOS;
     47 
     48   // Mapping from D-Bus object paths to NfcNdefRecord objects.
     49   typedef std::map<dbus::ObjectPath, device::NfcNdefRecord*> NdefRecordMap;
     50 
     51   explicit NfcNdefTagTechnologyChromeOS(NfcTagChromeOS* tag);
     52 
     53   // Called by dbus:: on completion of the D-Bus method call to write an NDEF.
     54   void OnWriteNdefMessage(const base::Closure& callback);
     55   void OnWriteNdefMessageError(const ErrorCallback& error_callback,
     56                                const std::string& error_name,
     57                                const std::string& error_message);
     58 
     59   // Creates a record object for the record with object path |object_path| and
     60   // notifies the observers, if a record object did not already exist for it.
     61   void AddRecord(const dbus::ObjectPath& object_path);
     62 
     63   // A map containing the NDEF records that were received from the tag.
     64   NdefRecordMap records_;
     65 
     66   // Message instance that contains pointers to all created records that are
     67   // in |records_|. This is mainly used as the cached return value for
     68   // GetNdefMessage().
     69   device::NfcNdefMessage message_;
     70 
     71   // List of observers interested in event notifications from us.
     72   ObserverList<device::NfcNdefTagTechnology::Observer> observers_;
     73 
     74   // D-Bus object path of the remote tag or device that this object operates
     75   // on.
     76   dbus::ObjectPath object_path_;
     77 
     78   // Note: This should remain the last member so it'll be destroyed and
     79   // invalidate its weak pointers before any other members are destroyed.
     80   base::WeakPtrFactory<NfcNdefTagTechnologyChromeOS> weak_ptr_factory_;
     81 
     82   DISALLOW_COPY_AND_ASSIGN(NfcNdefTagTechnologyChromeOS);
     83 };
     84 
     85 }  // namespace chromeos
     86 
     87 #endif  // DEVICE_NFC_NFC_TAG_TECHNOLOGY_CHROMEOS_H_
     88