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.
5 #include "device/nfc/nfc_adapter_factory.h"
7 #include "base/lazy_instance.h"
8 #include "base/logging.h"
9 #include "base/memory/weak_ptr.h"
11 #if defined(OS_CHROMEOS)
12 #include "device/nfc/nfc_adapter_chromeos.h"
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
;
29 bool NfcAdapterFactory::IsNfcAvailable() {
30 #if defined(OS_CHROMEOS)
38 void NfcAdapterFactory::GetAdapter(const AdapterCallback
& callback
) {
39 if (!IsNfcAvailable()) {
40 LOG(WARNING
) << "NFC is not available on the current platform.";
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();
50 if (default_adapter
.Get()->IsInitialized())
51 callback
.Run(scoped_refptr
<NfcAdapter
>(default_adapter
.Get().get()));