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 #ifndef CHROMEOS_DBUS_FAKE_NFC_DEVICE_CLIENT_H_
6 #define CHROMEOS_DBUS_FAKE_NFC_DEVICE_CLIENT_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/observer_list.h"
10 #include "chromeos/chromeos_export.h"
11 #include "chromeos/dbus/nfc_client_helpers.h"
12 #include "chromeos/dbus/nfc_device_client.h"
16 // FakeNfcDeviceClient simulates the behavior of the NFC device objects
17 // and is used both in test cases in place of a mock and on the Linux desktop.
18 class CHROMEOS_EXPORT FakeNfcDeviceClient
: public NfcDeviceClient
{
20 // The fake device object path.
21 static const char kDevicePath
[];
23 // The default simulation timeout interval.
24 static const int kDefaultSimulationTimeoutMilliseconds
;
26 // Properties structure that provides fake behavior for D-Bus calls.
27 struct Properties
: public NfcDeviceClient::Properties
{
28 explicit Properties(const PropertyChangedCallback
& callback
);
29 ~Properties() override
;
31 // dbus::PropertySet overrides.
32 void Get(dbus::PropertyBase
* property
,
33 dbus::PropertySet::GetCallback callback
) override
;
34 void GetAll() override
;
35 void Set(dbus::PropertyBase
* property
,
36 dbus::PropertySet::SetCallback callback
) override
;
39 FakeNfcDeviceClient();
40 ~FakeNfcDeviceClient() override
;
42 // NfcDeviceClient overrides.
43 void Init(dbus::Bus
* bus
) override
;
44 void AddObserver(Observer
* observer
) override
;
45 void RemoveObserver(Observer
* observer
) override
;
46 std::vector
<dbus::ObjectPath
> GetDevicesForAdapter(
47 const dbus::ObjectPath
& adapter_path
) override
;
48 Properties
* GetProperties(const dbus::ObjectPath
& object_path
) override
;
49 void Push(const dbus::ObjectPath
& object_path
,
50 const base::DictionaryValue
& attributes
,
51 const base::Closure
& callback
,
52 const nfc_client_helpers::ErrorCallback
& error_callback
) override
;
54 // Simulates the appearance of a device. The fake device will show up after
55 // exactly |visibility_delay| milliseconds, and will simulate pushing a single
56 // record to the local fake adapter after exactly |record_push_delay|
57 // milliseconds after the the device appears. |visibility_delay| must have a
58 // non-negative value. |record_push_delay| CAN be negative: if it has a
59 // negative value, the record push step will not be simulated. The
60 // side-effects of this method occur asynchronously, i.e. even with arguments
61 // with value 0, the pairing won't take place until after this method has
63 void BeginPairingSimulation(int visibility_delay
, int record_push_delay
);
65 // If device pairing was previously started, simulates the disappearance of
66 // the device. Any device objects presented and their records will disappear
67 // after this call. Delayed events that were set up by a previous call to
68 // BeginPairing() will be canceled through a call to EndPairing().
69 void EndPairingSimulation();
71 // Enables or disables automatic unpairing. When enabled, a pairing
72 // simulation will end |simulation_timeout| milliseconds after records have
73 // been exposed (or after the tag has been exposed, if |record_push_delay| was
74 // given as a negative value to BeginPairingSimulation) This is enabled by
75 // default and the timeout is set to |kDefaultSimulationTimeoutMilliseconds|.
76 void EnableSimulationTimeout(int simulation_timeout
);
77 void DisableSimulationTimeout();
79 // Tells the FakeNfcDeviceClient to add the records in |record_paths| to its
80 // list of records exposed for |kDevicePath|. This method will immediately
81 // assign the records and trigger a property changed signal, only if the
82 // device is currently visible.
83 void SetRecords(const std::vector
<dbus::ObjectPath
>& record_paths
);
85 // Tells the FakeNfcDeviceClient to clear the list of records exposed for
86 // |kDevicePath|. This method takes effect immediately and triggers a
87 // property changed signal.
90 // Returns true, if a pairing simulation is currently going on.
91 bool device_visible() const { return device_visible_
; }
94 // Property changed callback passed when we create Properties* structures.
95 void OnPropertyChanged(const dbus::ObjectPath
& object_path
,
96 const std::string
& property_name
);
98 // Makes the fake device visible (if it is not already so) and simulates a
99 // record push after |record_push_delay| seconds. Posted by BeginPairing().
100 void MakeDeviceVisible(int record_push_delay
);
102 // Makes the fake records visible. Called by MakeDeviceVisible().
103 void MakeRecordsVisible();
105 // Called when the simulation timeout expires.
106 void HandleSimulationTimeout();
108 // List of observers interested in event notifications from us.
109 base::ObserverList
<Observer
> observers_
;
111 // Fake properties that are returned for the emulated device.
112 scoped_ptr
<Properties
> properties_
;
114 // If true, a pairing simulation was started using BeginPairing() and no call
115 // to EndPairing() has been made.
116 bool pairing_started_
;
118 // If true, observers have been notified that a device has been created and
119 // the device properties are accessible.
120 bool device_visible_
;
122 // If non-negative, the device will disappear this many milliseconds after
123 // its records have been exposed.
124 int simulation_timeout_
;
126 DISALLOW_COPY_AND_ASSIGN(FakeNfcDeviceClient
);
129 } // namespace chromeos
131 #endif // CHROMEOS_DBUS_FAKE_NFC_DEVICE_CLIENT_H_