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_PEER_CHROMEOS_H_
6#define DEVICE_NFC_NFC_PEER_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 "dbus/object_path.h"
14#include "device/nfc/nfc_ndef_record.h"
15#include "device/nfc/nfc_peer.h"
16
17namespace chromeos {
18
19// The NfcPeerChromeOS class implements NfcPeer for the Chrome OS platform.
20class NfcPeerChromeOS : public device::NfcPeer,
21                        public NfcRecordClient::Observer {
22 public:
23  // NfcPeer overrides.
24  virtual void AddObserver(device::NfcPeer::Observer* observer) OVERRIDE;
25  virtual void RemoveObserver(device::NfcPeer::Observer* observer) OVERRIDE;
26  virtual std::string GetIdentifier() const OVERRIDE;
27  virtual const device::NfcNdefMessage& GetNdefMessage() const OVERRIDE;
28  virtual void PushNdef(const device::NfcNdefMessage& message,
29                        const base::Closure& callback,
30                        const ErrorCallback& error_callback) OVERRIDE;
31  virtual void StartHandover(HandoverType handover_type,
32                             const base::Closure& callback,
33                             const ErrorCallback& error_callback) OVERRIDE;
34
35 private:
36  friend class NfcAdapterChromeOS;
37
38  // Mapping from D-Bus object paths to NfcNdefRecord objects.
39  typedef std::map<dbus::ObjectPath, device::NfcNdefRecord*> NdefRecordMap;
40
41  explicit NfcPeerChromeOS(const dbus::ObjectPath& object_path);
42  virtual ~NfcPeerChromeOS();
43
44  // NfcRecordClient::Observer overrides.
45  virtual void RecordAdded(const dbus::ObjectPath& object_path) OVERRIDE;
46  virtual void RecordRemoved(const dbus::ObjectPath& object_path) OVERRIDE;
47  virtual void RecordPropertiesReceived(
48      const dbus::ObjectPath& object_path) OVERRIDE;
49
50  // Called by dbus:: on completion of the D-Bus method call to push an NDEF.
51  void OnPushNdef(const base::Closure& callback);
52  void OnPushNdefError(const ErrorCallback& error_callback,
53                       const std::string& error_name,
54                       const std::string& error_message);
55
56  // Creates a record object for the record with object path |object_path| and
57  // notifies the observers, if a record object did not already exist for it.
58  void AddRecord(const dbus::ObjectPath& object_path);
59
60  // Object path of the peer that we are currently tracking.
61  dbus::ObjectPath object_path_;
62
63  // A map containing the NDEF records that were received from the peer.
64  NdefRecordMap records_;
65
66  // Message instance that contains pointers to all created records.
67  device::NfcNdefMessage message_;
68
69  // List of observers interested in event notifications from us.
70  ObserverList<device::NfcPeer::Observer> observers_;
71
72  // Note: This should remain the last member so it'll be destroyed and
73  // invalidate its weak pointers before any other members are destroyed.
74  base::WeakPtrFactory<NfcPeerChromeOS> weak_ptr_factory_;
75
76  DISALLOW_COPY_AND_ASSIGN(NfcPeerChromeOS);
77};
78
79}  // namespace chromeos
80
81#endif  // DEVICE_NFC_NFC_PEER_CHROMEOS_H_
82