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