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