| 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 #include "device/nfc/nfc_adapter_factory.h" | |
| 6 | |
| 7 #include "base/lazy_instance.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/weak_ptr.h" | |
| 10 #include "build/build_config.h" | |
| 11 | |
| 12 #if defined(OS_CHROMEOS) | |
| 13 #include "device/nfc/nfc_adapter_chromeos.h" | |
| 14 #endif | |
| 15 | |
| 16 namespace device { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 // Shared default adapter instance, we don't want to keep this class around | |
| 21 // if nobody is using it so use a WeakPtr and create the object when needed; | |
| 22 // since Google C++ Style (and clang's static analyzer) forbids us having | |
| 23 // exit-time destructors we use a leaky lazy instance for it. | |
| 24 base::LazyInstance<base::WeakPtr<device::NfcAdapter> >::Leaky | |
| 25 default_adapter = LAZY_INSTANCE_INITIALIZER; | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 // static | |
| 30 bool NfcAdapterFactory::IsNfcAvailable() { | |
| 31 #if defined(OS_CHROMEOS) | |
| 32 return true; | |
| 33 #else | |
| 34 return false; | |
| 35 #endif | |
| 36 } | |
| 37 | |
| 38 // static | |
| 39 void NfcAdapterFactory::GetAdapter(const AdapterCallback& callback) { | |
| 40 if (!IsNfcAvailable()) { | |
| 41 LOG(WARNING) << "NFC is not available on the current platform."; | |
| 42 return; | |
| 43 } | |
| 44 if (!default_adapter.Get().get()) { | |
| 45 #if defined(OS_CHROMEOS) | |
| 46 chromeos::NfcAdapterChromeOS* new_adapter = | |
| 47 new chromeos::NfcAdapterChromeOS(); | |
| 48 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr(); | |
| 49 #endif | |
| 50 } | |
| 51 if (default_adapter.Get()->IsInitialized()) | |
| 52 callback.Run(scoped_refptr<NfcAdapter>(default_adapter.Get().get())); | |
| 53 } | |
| 54 | |
| 55 } // namespace device | |
| OLD | NEW |