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 #include "device/nfc/nfc_adapter.h" |
| 6 |
| 7 #include "base/lazy_instance.h" |
| 8 #include "base/memory/weak_ptr.h" |
| 9 #include "base/stl_util.h" |
| 10 #include "device/nfc/nfc_peer.h" |
| 11 #include "device/nfc/nfc_tag.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 // Shared default adapter instance. We don't want to keep this class around if |
| 16 // nobody is using it, so use a WeakPtr and create the object when needed. |
| 17 // Since Google C++ Style (and clang's static analyzer) forbids us from having |
| 18 // exit-time destructors, we use a leaky lazy instance for it. |
| 19 base::LazyInstance<base::WeakPtr<device::NfcAdapter> >::Leaky default_adapter = |
| 20 LAZY_INSTANCE_INITIALIZER; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 namespace device { |
| 25 |
| 26 NfcAdapter::NfcAdapter() { |
| 27 } |
| 28 |
| 29 NfcAdapter::~NfcAdapter() { |
| 30 STLDeleteValues(&peers_); |
| 31 STLDeleteValues(&tags_); |
| 32 } |
| 33 |
| 34 // static |
| 35 bool NfcAdapter::IsNfcAvailable() { |
| 36 // TODO(armansito): Return true on supported platforms here. |
| 37 return false; |
| 38 } |
| 39 |
| 40 // static |
| 41 scoped_refptr<NfcAdapter> NfcAdapter::GetAdapter() { |
| 42 if (!default_adapter.Get().get()) |
| 43 // TODO(armansito): Create platform-specific implementation instances here. |
| 44 return scoped_refptr<NfcAdapter>(); // Return a NULL pointer. |
| 45 return scoped_refptr<NfcAdapter>(default_adapter.Get().get()); |
| 46 } |
| 47 |
| 48 void NfcAdapter::GetPeers(PeerList* peer_list) const { |
| 49 peer_list->clear(); |
| 50 for (PeersMap::const_iterator iter = peers_.begin(); |
| 51 iter != peers_.end(); ++iter) { |
| 52 peer_list->push_back(iter->second); |
| 53 } |
| 54 } |
| 55 |
| 56 void NfcAdapter::GetTags(TagList* tag_list) const { |
| 57 tag_list->clear(); |
| 58 for (TagsMap::const_iterator iter = tags_.begin(); |
| 59 iter != tags_.end(); ++iter) { |
| 60 tag_list->push_back(iter->second); |
| 61 } |
| 62 } |
| 63 |
| 64 NfcPeer* NfcAdapter::GetPeer(const std::string& identifier) const { |
| 65 PeersMap::const_iterator iter = peers_.find(identifier); |
| 66 if (iter != peers_.end()) |
| 67 return iter->second; |
| 68 return NULL; |
| 69 } |
| 70 |
| 71 NfcTag* NfcAdapter::GetTag(const std::string& identifier) const { |
| 72 TagsMap::const_iterator iter = tags_.find(identifier); |
| 73 if (iter != tags_.end()) |
| 74 return iter->second; |
| 75 return NULL; |
| 76 } |
| 77 |
| 78 } // namespace device |
OLD | NEW |