10f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 20f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 30f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// found in the LICENSE file. 40f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 50f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#ifndef CHROMEOS_DBUS_FAKE_NFC_TAG_CLIENT_H_ 60f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#define CHROMEOS_DBUS_FAKE_NFC_TAG_CLIENT_H_ 70f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/observer_list.h" 90f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chromeos/chromeos_export.h" 100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chromeos/dbus/nfc_client_helpers.h" 110f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#include "chromeos/dbus/nfc_tag_client.h" 120f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 130f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)namespace chromeos { 140f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 150f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// FakeNfcTagClient simulates the behavior of the NFC tag objects 160f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)// and is used both in test cases in place of a mock and on the Linux desktop. 170f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)class CHROMEOS_EXPORT FakeNfcTagClient : public NfcTagClient { 180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) public: 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // The fake tag object path. 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static const char kTagPath[]; 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // The default simulation timeout interval. 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) static const int kDefaultSimulationTimeoutMilliseconds; 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 250f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) struct Properties : public NfcTagClient::Properties { 260f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) explicit Properties(const PropertyChangedCallback& callback); 270f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) virtual ~Properties(); 280f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 290f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // dbus::PropertySet overrides. 300f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) virtual void Get(dbus::PropertyBase* property, 310f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) dbus::PropertySet::GetCallback callback) OVERRIDE; 320f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) virtual void GetAll() OVERRIDE; 330f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) virtual void Set(dbus::PropertyBase* property, 340f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) dbus::PropertySet::SetCallback callback) OVERRIDE; 350f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) }; 360f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 370f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) FakeNfcTagClient(); 380f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) virtual ~FakeNfcTagClient(); 390f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 400f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) // NfcTagClient overrides. 410f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) virtual void Init(dbus::Bus* bus) OVERRIDE; 420f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) virtual void AddObserver(Observer* observer) OVERRIDE; 430f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) virtual void RemoveObserver(Observer* observer) OVERRIDE; 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual std::vector<dbus::ObjectPath> GetTagsForAdapter( 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const dbus::ObjectPath& adapter_path) OVERRIDE; 460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) virtual Properties* GetProperties( 470f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const dbus::ObjectPath& object_path) OVERRIDE; 480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) virtual void Write( 490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const dbus::ObjectPath& object_path, 50a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) const base::DictionaryValue& attributes, 510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const base::Closure& callback, 520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) const nfc_client_helpers::ErrorCallback& error_callback) OVERRIDE; 530f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Simulates the appearance of a tag. The fake tag will show up after 555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // exactly |visibility_delay| milliseconds. |visibility_delay| must have a 565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // non-negative value. The side-effects of this method 575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // occur asynchronously, i.e. even with an argument of 0, the pairing will not 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // take place until after this method has returned. 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void BeginPairingSimulation(int visibility_delay); 605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // If tag pairing was previously started, simulates the disappearance of 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // the tag. Any tag object presented and their records will disappear 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // after this call. Delayed events that were set up by a previous call to 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // BeginPairing() will be canceled through a call to EndPairing(). 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void EndPairingSimulation(); 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Enables or disables automatic unpairing. When enabled, a pairing 685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // simulation will end |simulation_timeout| milliseconds after the tag has 695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // been exposed. This is enabled by default and the timeout is set to 705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // |kDefaultSimulationTimeoutMilliseconds|. |simulation_timeout| must be 715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // non-negative. 725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void EnableSimulationTimeout(int simulation_timeout); 735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void DisableSimulationTimeout(); 745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Tells the FakeNfcDeviceClient to add the records in |record_paths| to its 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // list of records exposed for |kDevicePath|. This method will immediately 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // assign the records and trigger a property changed signal, only if the 785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // tag is currently visible. 795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void SetRecords(const std::vector<dbus::ObjectPath>& record_paths); 805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Tells the FakeNfcDeviceClient to clear the list of records exposed for 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // |kDevicePath|. This method takes effect immediately and triggers a 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // property changed signal. 845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void ClearRecords(); 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Returns true, if a pairing simulation is currently going on. 875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool tag_visible() const { return tag_visible_; } 885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 890f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) private: 905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Property changed callback passed when we create Properties* structures. 915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void OnPropertyChanged(const dbus::ObjectPath& object_path, 925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const std::string& property_name); 935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Makes the fake tag visible if it is not already visible. 955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void MakeTagVisible(); 965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Called when the simulation timeout expires. 985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void HandleSimulationTimeout(); 995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // List of observers interested in event notifications from us. 1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) ObserverList<Observer> observers_; 1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Fake properties that are returned for the emulated tag. 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) scoped_ptr<Properties> properties_; 1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // If true, a pairing simulation was begun using BeginPairing() and no call 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // to EndPairing() has been made. 1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool pairing_started_; 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // If true, observers have been notified that a tag has been created and 1115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // the tag properties are accesible. 1125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool tag_visible_; 1135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // If non-negative, the tag will disappear this many milliseconds after 1155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // its records have been exposed. 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int simulation_timeout_; 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1180f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(FakeNfcTagClient); 1190f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}; 1200f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1210f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)} // namespace chromeos 1220f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles) 1230f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)#endif // CHROMEOS_DBUS_FAKE_NFC_TAG_CLIENT_H_ 124