OLD | NEW |
| (Empty) |
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_H_ | |
6 #define DEVICE_NFC_NFC_ADAPTER_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/callback.h" | |
13 #include "base/macros.h" | |
14 #include "base/memory/ref_counted.h" | |
15 | |
16 namespace device { | |
17 | |
18 class NfcPeer; | |
19 class NfcTag; | |
20 | |
21 // NfcAdapter represents a local NFC adapter which may be used to interact with | |
22 // NFC tags and remote NFC adapters on platforms that support NFC. Through | |
23 // instances of this class, users can obtain important information such as if | |
24 // and/or when an adapter is present, supported NFC technologies, and the | |
25 // adapter's power and polling state. NfcAdapter instances can be used to power | |
26 // up/down the NFC adapter and its Observer interface allows users to get | |
27 // notified when new adapters are added/removed and when remote NFC tags and | |
28 // devices were detected or lost. | |
29 // | |
30 // A system can contain more than one NFC adapter (e.g. external USB adapters) | |
31 // but Chrome will have only one NfcAdapter instance. This instance will do | |
32 // its best to represent all underlying adapters but will only allow | |
33 // interacting with only one at a given time. If the currently represented | |
34 // adapter is removed from the system, the NfcAdapter instance will update to | |
35 // reflect the information from the next available adapter. | |
36 class NfcAdapter : public base::RefCounted<NfcAdapter> { | |
37 public: | |
38 // Interface for observing changes from NFC adapters. | |
39 class Observer { | |
40 public: | |
41 virtual ~Observer() {} | |
42 | |
43 // Called when the presence of the adapter |adapter| changes. When |present| | |
44 // is true, this indicates that the adapter has now become present, while a | |
45 // value of false indicates that the adapter is no longer available on the | |
46 // current system. | |
47 virtual void AdapterPresentChanged(NfcAdapter* adapter, bool present) {} | |
48 | |
49 // Called when the radio power state of the adapter |adapter| changes. If | |
50 // |powered| is true, the adapter radio is turned on, otherwise it's turned | |
51 // off. | |
52 virtual void AdapterPoweredChanged(NfcAdapter* adapter, bool powered) {} | |
53 | |
54 // Called when the "polling" state of the adapter |adapter| changes. If | |
55 // |polling| is true, the adapter is currently polling for remote tags and | |
56 // devices. If false, the adapter isn't polling, either because a poll loop | |
57 // was never started or because a connection with a tag or peer has been | |
58 // established. | |
59 virtual void AdapterPollingChanged(NfcAdapter* adapter, bool polling) {} | |
60 | |
61 // Called when an NFC tag |tag| has been found by the adapter |adapter|. | |
62 // The observer can use this method to take further action on the tag object | |
63 // |tag|, such as reading its records or writing to it. While |tag| will be | |
64 // valid within the context of this call, its life-time cannot be guaranteed | |
65 // once this call returns, as the object may get destroyed if the connection | |
66 // with the tag is lost. Instead of caching the pointer directly, observers | |
67 // should store the tag's assigned unique identifier instead, which can be | |
68 // used to obtain a pointer to the tag, as long as it exists. | |
69 virtual void TagFound(NfcAdapter* adapter, NfcTag* tag) {} | |
70 | |
71 // Called when the NFC tag |tag| is no longer known by the adapter | |
72 // |adapter|. |tag| should not be cached. | |
73 virtual void TagLost(NfcAdapter* adapter, NfcTag* tag) {} | |
74 | |
75 // Called when a remote NFC adapter |peer| has been detected, which is | |
76 // available for peer-to-peer communication over NFC. The observer can use | |
77 // this method to take further action on |peer| such as reading its records | |
78 // or pushing NDEFs to it. While |peer| will be valid within the context of | |
79 // this call, its life-time cannot be guaranteed once this call returns, as | |
80 // the object may get destroyed if the connection with the peer is lost. | |
81 // Instead of caching the pointer directly, observers should store the | |
82 // peer's assigned unique identifier instead, which can be used to obtain a | |
83 // pointer to the peer, as long as it exists. | |
84 virtual void PeerFound(NfcAdapter* adaper, NfcPeer* peer) {} | |
85 | |
86 // Called when the remote NFC adapter |peer| is no longer known by the | |
87 // adapter |adapter|. |peer| should not be cached. | |
88 virtual void PeerLost(NfcAdapter* adapter, NfcPeer* peer) {} | |
89 }; | |
90 | |
91 // The ErrorCallback is used by methods to asynchronously report errors. | |
92 typedef base::Closure ErrorCallback; | |
93 | |
94 // Typedefs for lists of NFC peer and NFC tag objects. | |
95 typedef std::vector<NfcPeer*> PeerList; | |
96 typedef std::vector<NfcTag*> TagList; | |
97 | |
98 // Adds and removes observers for events on this NFC adapter. If monitoring | |
99 // multiple adapters, check the |adapter| parameter of observer methods to | |
100 // determine which adapter is issuing the event. | |
101 virtual void AddObserver(Observer* observer) = 0; | |
102 virtual void RemoveObserver(Observer* observer) = 0; | |
103 | |
104 // Indicates whether an underlying adapter is actually present on the | |
105 // system. An adapter that was previously present can become no longer | |
106 // present, for example, if all physical adapters that can back it up were | |
107 // removed from the system. | |
108 virtual bool IsPresent() const = 0; | |
109 | |
110 // Indicates whether the adapter radio is powered. | |
111 virtual bool IsPowered() const = 0; | |
112 | |
113 // Indicates whether the adapter is polling for remote NFC tags and peers. | |
114 virtual bool IsPolling() const = 0; | |
115 | |
116 // Indicates whether the NfcAdapter instance is initialized and ready to use. | |
117 virtual bool IsInitialized() const = 0; | |
118 | |
119 // Requests a change to the adapter radio power. Setting |powered| to true | |
120 // will turn on the radio and false will turn it off. On success, |callback| | |
121 // will be invoked. On failure, |error_callback| will be invoked, which can | |
122 // happen if the radio power is already in the requested state, or if the | |
123 // underlying adapter is not present. | |
124 virtual void SetPowered(bool powered, | |
125 const base::Closure& callback, | |
126 const ErrorCallback& error_callback) = 0; | |
127 | |
128 // Requests the adapter to begin its poll loop to start looking for remote | |
129 // NFC tags and peers. On success, |callback| will be invoked. On failure, | |
130 // |error_callback| will be invoked. This method can fail for various | |
131 // reasons, including: | |
132 // - The adapter radio is not powered. | |
133 // - The adapter is already polling. | |
134 // - The adapter is busy; it has already established a connection to a | |
135 // remote tag or peer. | |
136 // Bear in mind that this method may be called by multiple users of the same | |
137 // adapter. | |
138 virtual void StartPolling(const base::Closure& callback, | |
139 const ErrorCallback& error_callback) = 0; | |
140 | |
141 // Requests the adapter to stop its current poll loop. On success, |callback| | |
142 // will be invoked. On failure, |error_callback| will be invoked. This method | |
143 // can fail if the adapter is not polling when this method was called. Bear | |
144 // in mind that this method may be called by multiple users of the same | |
145 // adapter and polling may not actually stop if other callers have called | |
146 // StartPolling() in the mean time. | |
147 virtual void StopPolling(const base::Closure& callback, | |
148 const ErrorCallback& error_callback) = 0; | |
149 | |
150 // Returns a list containing all known peers in |peer_list|. If |peer_list| | |
151 // was non-empty at the time of the call, it will be cleared. The contents of | |
152 // |peer_list| at the end of this call are owned by the adapter. | |
153 virtual void GetPeers(PeerList* peer_list) const; | |
154 | |
155 // Returns a list containing all known tags in |tag_list|. If |tag_list| was | |
156 // non-empty at the time of the call, it will be cleared. The contents of | |
157 // |tag_list| at the end of this call are owned by the adapter. | |
158 virtual void GetTags(TagList* tag_list) const; | |
159 | |
160 // Returns a pointer to the peer with the given identifier |identifier| or | |
161 // NULL if no such peer is known. If a non-NULL pointer is returned, the | |
162 // instance that it points to is owned by this adapter. | |
163 virtual NfcPeer* GetPeer(const std::string& identifier) const; | |
164 | |
165 // Returns a pointer to the tag with the given identifier |identifier| or | |
166 // NULL if no such tag is known. If a non-NULL pointer is returned, the | |
167 // instance that it points to is owned by this adapter. | |
168 virtual NfcTag* GetTag(const std::string& identifier) const; | |
169 | |
170 protected: | |
171 friend class base::RefCounted<NfcAdapter>; | |
172 | |
173 // The default constructor does nothing. The destructor deletes all known | |
174 // NfcPeer and NfcTag instances. | |
175 NfcAdapter(); | |
176 virtual ~NfcAdapter(); | |
177 | |
178 // Peers and tags that have been found. The key is the unique identifier | |
179 // assigned to the peer or tag and the value is a pointer to the | |
180 // corresponding NfcPeer or NfcTag object, whose lifetime is managed by the | |
181 // adapter instance. | |
182 typedef std::map<const std::string, NfcPeer*> PeersMap; | |
183 typedef std::map<const std::string, NfcTag*> TagsMap; | |
184 | |
185 // Set the given tag or peer for |identifier|. If a tag or peer for | |
186 // |identifier| already exists, these methods won't do anything. | |
187 void SetTag(const std::string& identifier, NfcTag* tag); | |
188 void SetPeer(const std::string& identifier, NfcPeer* peer); | |
189 | |
190 // Removes the tag or peer for |identifier| and returns the removed object. | |
191 // Returns NULL, if no tag or peer for |identifier| was found. | |
192 NfcTag* RemoveTag(const std::string& identifier); | |
193 NfcPeer* RemovePeer(const std::string& identifier); | |
194 | |
195 // Clear the peer and tag maps. These methods won't delete the tag and peer | |
196 // objects, however after the call to these methods, the peers and tags won't | |
197 // be returned via calls to GetPeers and GetTags. | |
198 void ClearTags(); | |
199 void ClearPeers(); | |
200 | |
201 private: | |
202 // Peers and tags that are managed by this adapter. | |
203 PeersMap peers_; | |
204 TagsMap tags_; | |
205 | |
206 DISALLOW_COPY_AND_ASSIGN(NfcAdapter); | |
207 }; | |
208 | |
209 } // namespace device | |
210 | |
211 #endif // DEVICE_NFC_NFC_ADAPTER_H_ | |
OLD | NEW |