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