• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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"
13 
14 namespace chromeos {
15 
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 {
19  public:
20   // The fake device object path.
21   static const char kDevicePath[];
22 
23   // The default simulation timeout interval.
24   static const int kDefaultSimulationTimeoutMilliseconds;
25 
26   // Properties structure that provides fake behavior for D-Bus calls.
27   struct Properties : public NfcDeviceClient::Properties {
28     explicit Properties(const PropertyChangedCallback& callback);
29     virtual ~Properties();
30 
31     // dbus::PropertySet overrides.
32     virtual void Get(dbus::PropertyBase* property,
33                      dbus::PropertySet::GetCallback callback) OVERRIDE;
34     virtual void GetAll() OVERRIDE;
35     virtual void Set(dbus::PropertyBase* property,
36                      dbus::PropertySet::SetCallback callback) OVERRIDE;
37   };
38 
39   FakeNfcDeviceClient();
40   virtual ~FakeNfcDeviceClient();
41 
42   // NfcDeviceClient overrides.
43   virtual void Init(dbus::Bus* bus) OVERRIDE;
44   virtual void AddObserver(Observer* observer) OVERRIDE;
45   virtual void RemoveObserver(Observer* observer) OVERRIDE;
46   virtual std::vector<dbus::ObjectPath> GetDevicesForAdapter(
47       const dbus::ObjectPath& adapter_path) OVERRIDE;
48   virtual Properties* GetProperties(
49       const dbus::ObjectPath& object_path) OVERRIDE;
50   virtual void Push(
51       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.
device_visible()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   ObserverList<Observer> observers_;
112 
113   // Fake properties that are returned for the emulated device.
114   scoped_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_
134