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