Roll src/third_party/WebKit b944946:201fd41 (svn 192400:192407)
[chromium-blink-merge.git] / device / nfc / nfc_adapter_factory.cc
blob1ba533a068614353fb78415c6681b12844ce142a
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"
13 #endif
15 namespace device {
17 namespace {
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;
26 } // namespace
28 // static
29 bool NfcAdapterFactory::IsNfcAvailable() {
30 #if defined(OS_CHROMEOS)
31 return true;
32 #else
33 return false;
34 #endif
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;
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
50 if (default_adapter.Get()->IsInitialized())
51 callback.Run(scoped_refptr<NfcAdapter>(default_adapter.Get().get()));
54 } // namespace device