Add some instrumentation for jank in URLRequest::Start.
[chromium-blink-merge.git] / chromeos / dbus / nfc_manager_client.cc
blob53813f3650eb2d92e578cf1aaf6d8aa9b067f0b5
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 "chromeos/dbus/nfc_manager_client.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/memory/weak_ptr.h"
9 #include "base/observer_list.h"
10 #include "dbus/bus.h"
11 #include "third_party/cros_system_api/dbus/service_constants.h"
13 namespace chromeos {
15 NfcManagerClient::Properties::Properties(
16 dbus::ObjectProxy* object_proxy,
17 const PropertyChangedCallback& callback)
18 : NfcPropertySet(object_proxy,
19 nfc_manager::kNfcManagerInterface,
20 callback) {
21 RegisterProperty(nfc_manager::kAdaptersProperty, &adapters);
24 NfcManagerClient::Properties::~Properties() {
28 // The NfcManagerClient implementation used in production.
29 class NfcManagerClientImpl : public NfcManagerClient {
30 public:
31 NfcManagerClientImpl()
32 : object_proxy_(NULL),
33 weak_ptr_factory_(this) {
36 ~NfcManagerClientImpl() override {}
38 // NfcManagerClient override.
39 void AddObserver(Observer* observer) override {
40 DCHECK(observer);
41 observers_.AddObserver(observer);
44 // NfcManagerClient override.
45 void RemoveObserver(Observer* observer) override {
46 DCHECK(observer);
47 observers_.RemoveObserver(observer);
50 // NfcManagerClient override.
51 Properties* GetProperties() override { return properties_.get(); }
53 protected:
54 // DBusClient override.
55 void Init(dbus::Bus* bus) override {
56 VLOG(1) << "Creating NfcManagerClientImpl";
58 // Create the object proxy.
59 object_proxy_ = bus->GetObjectProxy(
60 nfc_manager::kNfcManagerServiceName,
61 dbus::ObjectPath(nfc_manager::kNfcManagerServicePath));
63 // Set up the signal handlers.
64 object_proxy_->ConnectToSignal(
65 nfc_manager::kNfcManagerInterface,
66 nfc_manager::kAdapterAddedSignal,
67 base::Bind(&NfcManagerClientImpl::AdapterAddedReceived,
68 weak_ptr_factory_.GetWeakPtr()),
69 base::Bind(&NfcManagerClientImpl::AdapterAddedConnected,
70 weak_ptr_factory_.GetWeakPtr()));
72 object_proxy_->ConnectToSignal(
73 nfc_manager::kNfcManagerInterface,
74 nfc_manager::kAdapterRemovedSignal,
75 base::Bind(&NfcManagerClientImpl::AdapterRemovedReceived,
76 weak_ptr_factory_.GetWeakPtr()),
77 base::Bind(&NfcManagerClientImpl::AdapterRemovedConnected,
78 weak_ptr_factory_.GetWeakPtr()));
80 // Create the properties structure.
81 properties_.reset(new Properties(
82 object_proxy_,
83 base::Bind(&NfcManagerClientImpl::OnPropertyChanged,
84 weak_ptr_factory_.GetWeakPtr())));
86 properties_->ConnectSignals();
87 properties_->GetAll();
90 private:
91 // NFC manager signal handlers.
92 void OnPropertyChanged(const std::string& property_name) {
93 VLOG(1) << "NFC Manager property changed: " << property_name;
94 FOR_EACH_OBSERVER(Observer, observers_,
95 ManagerPropertyChanged(property_name));
98 // Called by dbus:: when an "AdapterAdded" signal is received..
99 void AdapterAddedReceived(dbus::Signal* signal) {
100 DCHECK(signal);
101 dbus::MessageReader reader(signal);
102 dbus::ObjectPath object_path;
103 if (!reader.PopObjectPath(&object_path)) {
104 LOG(WARNING) << "AdapterAdded signal has incorrect parameters: "
105 << signal->ToString();
106 return;
108 VLOG(1) << "Adapter added: " << object_path.value();
109 FOR_EACH_OBSERVER(Observer, observers_, AdapterAdded(object_path));
112 // Called by dbus:: when the "AdapterAdded" signal is initially connected.
113 void AdapterAddedConnected(const std::string& interface_name,
114 const std::string& signal_name,
115 bool success) {
116 LOG_IF(WARNING, !success) << "Failed to connect to AdapterAdded signal.";
119 // Called by dbus:: when an "AdapterRemoved" signal is received..
120 void AdapterRemovedReceived(dbus::Signal* signal) {
121 DCHECK(signal);
122 dbus::MessageReader reader(signal);
123 dbus::ObjectPath object_path;
124 if (!reader.PopObjectPath(&object_path)) {
125 LOG(WARNING) << "AdapterRemoved signal has incorrect parameters: "
126 << signal->ToString();
127 return;
129 VLOG(1) << "Adapter removed: " << object_path.value();
130 FOR_EACH_OBSERVER(Observer, observers_, AdapterRemoved(object_path));
133 // Called by dbus:: when the "AdapterAdded" signal is initially connected.
134 void AdapterRemovedConnected(const std::string& interface_name,
135 const std::string& signal_name,
136 bool success) {
137 LOG_IF(WARNING, !success) << "Failed to connect to AdapterRemoved signal.";
140 // D-Bus proxy for neard Manager interface.
141 dbus::ObjectProxy* object_proxy_;
143 // Properties for neard Manager interface.
144 scoped_ptr<Properties> properties_;
146 // List of observers interested in event notifications.
147 ObserverList<NfcManagerClient::Observer> observers_;
149 // Weak pointer factory for generating 'this' pointers that might live longer
150 // than we do.
151 // Note: This should remain the last member so it'll be destroyed and
152 // invalidate its weak pointers before any other members are destroyed.
153 base::WeakPtrFactory<NfcManagerClientImpl> weak_ptr_factory_;
155 DISALLOW_COPY_AND_ASSIGN(NfcManagerClientImpl);
158 NfcManagerClient::NfcManagerClient() {
161 NfcManagerClient::~NfcManagerClient() {
164 // static
165 NfcManagerClient* NfcManagerClient::Create() {
166 return new NfcManagerClientImpl();
169 } // namespace chromeos