| OLD | NEW | 
 | (Empty) | 
|    1 // Copyright (c) 2012 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 CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H_ |  | 
|    6 #define CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H_ |  | 
|    7  |  | 
|    8 #include <string> |  | 
|    9 #include <vector> |  | 
|   10  |  | 
|   11 #include "base/basictypes.h" |  | 
|   12 #include "base/memory/scoped_ptr.h" |  | 
|   13 #include "base/memory/scoped_vector.h" |  | 
|   14 #include "base/memory/weak_ptr.h" |  | 
|   15 #include "base/string16.h" |  | 
|   16 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" |  | 
|   17 #include "chromeos/dbus/bluetooth_agent_service_provider.h" |  | 
|   18 #include "chromeos/dbus/bluetooth_device_client.h" |  | 
|   19 #include "dbus/object_path.h" |  | 
|   20  |  | 
|   21 namespace chromeos { |  | 
|   22  |  | 
|   23 class BluetoothAdapterChromeOs; |  | 
|   24 class BluetoothServiceRecord; |  | 
|   25  |  | 
|   26 struct BluetoothOutOfBandPairingData; |  | 
|   27  |  | 
|   28 // The BluetoothDeviceChromeOs class is an implementation of BluetoothDevice |  | 
|   29 // for Chrome OS platform. |  | 
|   30 class BluetoothDeviceChromeOs : public BluetoothDevice, |  | 
|   31                                 public BluetoothDeviceClient::Observer, |  | 
|   32                                 public BluetoothAgentServiceProvider::Delegate { |  | 
|   33  public: |  | 
|   34   virtual ~BluetoothDeviceChromeOs(); |  | 
|   35  |  | 
|   36   // BluetoothDevice override |  | 
|   37   virtual bool IsPaired() const OVERRIDE; |  | 
|   38   virtual const ServiceList& GetServices() const OVERRIDE; |  | 
|   39   virtual void GetServiceRecords( |  | 
|   40       const ServiceRecordsCallback& callback, |  | 
|   41       const ErrorCallback& error_callback) OVERRIDE; |  | 
|   42   virtual bool ProvidesServiceWithUUID(const std::string& uuid) const OVERRIDE; |  | 
|   43   virtual void ProvidesServiceWithName( |  | 
|   44       const std::string& name, |  | 
|   45       const ProvidesServiceCallback& callback) OVERRIDE; |  | 
|   46   virtual bool ExpectingPinCode() const OVERRIDE; |  | 
|   47   virtual bool ExpectingPasskey() const OVERRIDE; |  | 
|   48   virtual bool ExpectingConfirmation() const OVERRIDE; |  | 
|   49   virtual void Connect( |  | 
|   50       BluetoothDevice::PairingDelegate* pairing_delegate, |  | 
|   51       const base::Closure& callback, |  | 
|   52       const ErrorCallback& error_callback) OVERRIDE; |  | 
|   53   virtual void SetPinCode(const std::string& pincode) OVERRIDE; |  | 
|   54   virtual void SetPasskey(uint32 passkey) OVERRIDE; |  | 
|   55   virtual void ConfirmPairing() OVERRIDE; |  | 
|   56   virtual void RejectPairing() OVERRIDE; |  | 
|   57   virtual void CancelPairing() OVERRIDE; |  | 
|   58   virtual void Disconnect( |  | 
|   59       const base::Closure& callback, |  | 
|   60       const ErrorCallback& error_callback) OVERRIDE; |  | 
|   61   virtual void Forget(const ErrorCallback& error_callback) OVERRIDE; |  | 
|   62   virtual void ConnectToService( |  | 
|   63       const std::string& service_uuid, |  | 
|   64       const SocketCallback& callback) OVERRIDE; |  | 
|   65   virtual void SetOutOfBandPairingData( |  | 
|   66       const chromeos::BluetoothOutOfBandPairingData& data, |  | 
|   67       const base::Closure& callback, |  | 
|   68       const ErrorCallback& error_callback) OVERRIDE; |  | 
|   69   virtual void ClearOutOfBandPairingData( |  | 
|   70       const base::Closure& callback, |  | 
|   71       const ErrorCallback& error_callback) OVERRIDE; |  | 
|   72  |  | 
|   73  private: |  | 
|   74   friend class BluetoothAdapterChromeOs; |  | 
|   75   friend class MockBluetoothDevice; |  | 
|   76  |  | 
|   77   explicit BluetoothDeviceChromeOs(BluetoothAdapterChromeOs* adapter); |  | 
|   78  |  | 
|   79   // Sets the dbus object path for the device to |object_path|, indicating |  | 
|   80   // that the device has gone from being discovered to paired or bonded. |  | 
|   81   void SetObjectPath(const dbus::ObjectPath& object_path); |  | 
|   82  |  | 
|   83   // Removes the dbus object path from the device, indicating that the |  | 
|   84   // device is no longer paired or bonded, but perhaps still visible. |  | 
|   85   void RemoveObjectPath(); |  | 
|   86  |  | 
|   87   // Sets whether the device is visible to the owning adapter to |visible|. |  | 
|   88   void SetVisible(bool visible) { visible_ = visible; } |  | 
|   89  |  | 
|   90   // Updates device information from the properties in |properties|, device |  | 
|   91   // state properties such as |paired_| and |connected_| are ignored unless |  | 
|   92   // |update_state| is true. |  | 
|   93   void Update(const BluetoothDeviceClient::Properties* properties, |  | 
|   94               bool update_state); |  | 
|   95  |  | 
|   96   // Called by BluetoothAdapterClient when a call to CreateDevice() or |  | 
|   97   // CreatePairedDevice() succeeds, provides the new object path for the remote |  | 
|   98   // device in |device_path|. |callback| and |error_callback| are the callbacks |  | 
|   99   // provided to Connect(). |  | 
|  100   void ConnectCallback(const base::Closure& callback, |  | 
|  101                        const ErrorCallback& error_callback, |  | 
|  102                        const dbus::ObjectPath& device_path); |  | 
|  103  |  | 
|  104   // Called by BluetoothAdapterClient when a call to CreateDevice() or |  | 
|  105   // CreatePairedDevice() fails with the error named |error_name| and |  | 
|  106   // optional message |error_message|, |error_callback| is the callback |  | 
|  107   // provided to Connect(). |  | 
|  108   void ConnectErrorCallback(const ErrorCallback& error_callback, |  | 
|  109                             const std::string& error_name, |  | 
|  110                             const std::string& error_message); |  | 
|  111  |  | 
|  112   // Called by BluetoothAdapterClient when a call to DiscoverServices() |  | 
|  113   // completes.  |callback| and |error_callback| are the callbacks provided to |  | 
|  114   // GetServiceRecords. |  | 
|  115   void CollectServiceRecordsCallback( |  | 
|  116       const ServiceRecordsCallback& callback, |  | 
|  117       const ErrorCallback& error_callback, |  | 
|  118       const dbus::ObjectPath& device_path, |  | 
|  119       const BluetoothDeviceClient::ServiceMap& service_map, |  | 
|  120       bool success); |  | 
|  121  |  | 
|  122   // Called by BluetoothProperty when the call to Set() for the Trusted |  | 
|  123   // property completes. |success| indicates whether or not the request |  | 
|  124   // succeeded, |callback| and |error_callback| are the callbacks provided to |  | 
|  125   // Connect(). |  | 
|  126   void OnSetTrusted(const base::Closure& callback, |  | 
|  127                     const ErrorCallback& error_callback, |  | 
|  128                     bool success); |  | 
|  129  |  | 
|  130   // Connect application-level protocols of the device to the system, called |  | 
|  131   // on a successful connection or to reconnect to a device that is already |  | 
|  132   // paired or previously connected. |error_callback| is called on failure. |  | 
|  133   // Otherwise, |callback| is called when the request is complete. |  | 
|  134   void ConnectApplications(const base::Closure& callback, |  | 
|  135                            const ErrorCallback& error_callback); |  | 
|  136  |  | 
|  137   // Called by IntrospectableClient when a call to Introspect() completes. |  | 
|  138   // |success| indicates whether or not the request succeeded, |callback| and |  | 
|  139   // |error_callback| are the callbacks provided to ConnectApplications(), |  | 
|  140   // |service_name| and |device_path| specify the remote object being |  | 
|  141   // introspected and |xml_data| contains the XML-formatted protocol data. |  | 
|  142   void OnIntrospect(const base::Closure& callback, |  | 
|  143                     const ErrorCallback& error_callback, |  | 
|  144                     const std::string& service_name, |  | 
|  145                     const dbus::ObjectPath& device_path, |  | 
|  146                     const std::string& xml_data, bool success); |  | 
|  147  |  | 
|  148   // Called by BluetoothInputClient when the call to Connect() succeeds. |  | 
|  149   // |error_callback| is the callback provided to ConnectApplications(), |  | 
|  150   // |interface_name| specifies the interface being connected and |  | 
|  151   // |device_path| the remote object path. |  | 
|  152   void OnConnect(const base::Closure& callback, |  | 
|  153                  const std::string& interface_name, |  | 
|  154                  const dbus::ObjectPath& device_path); |  | 
|  155  |  | 
|  156   // Called by BluetoothInputClient when the call to Connect() fails. |  | 
|  157   // |error_callback| is the callback provided to ConnectApplications(), |  | 
|  158   // |interface_name| specifies the interface being connected, |  | 
|  159   // |device_path| the remote object path, |  | 
|  160   // |error_name| the error name and |error_message| the optional message. |  | 
|  161   void OnConnectError(const ErrorCallback& error_callback, |  | 
|  162                       const std::string& interface_name, |  | 
|  163                       const dbus::ObjectPath& device_path, |  | 
|  164                       const std::string& error_name, |  | 
|  165                       const std::string& error_message); |  | 
|  166  |  | 
|  167   // Called by BluetoothDeviceClient when a call to Disconnect() completes, |  | 
|  168   // |success| indicates whether or not the request succeeded, |callback| and |  | 
|  169   // |error_callback| are the callbacks provided to Disconnect() and |  | 
|  170   // |device_path| is the device disconnected. |  | 
|  171   void DisconnectCallback(const base::Closure& callback, |  | 
|  172                           const ErrorCallback& error_callback, |  | 
|  173                           const dbus::ObjectPath& device_path, bool success); |  | 
|  174  |  | 
|  175   // Called by BluetoothAdapterClient when a call to RemoveDevice() |  | 
|  176   // completes, |success| indicates whether or not the request succeeded, |  | 
|  177   // |error_callback| is the callback provided to Forget() and |adapter_path| is |  | 
|  178   // the d-bus object path of the adapter that performed the removal. |  | 
|  179   void ForgetCallback(const ErrorCallback& error_callback, |  | 
|  180                       const dbus::ObjectPath& adapter_path, bool success); |  | 
|  181  |  | 
|  182   // Called if the call to GetServiceRecords from ProvidesServiceWithName fails. |  | 
|  183   void SearchServicesForNameErrorCallback( |  | 
|  184       const ProvidesServiceCallback& callback); |  | 
|  185  |  | 
|  186   // Called by GetServiceRecords with the list of BluetoothServiceRecords to |  | 
|  187   // search for |name|.  |callback| is the callback from |  | 
|  188   // ProvidesServiceWithName. |  | 
|  189   void SearchServicesForNameCallback( |  | 
|  190       const std::string& name, |  | 
|  191       const ProvidesServiceCallback& callback, |  | 
|  192       const ServiceRecordList& list); |  | 
|  193  |  | 
|  194   // Called if the call to GetServiceRecords from Connect fails. |  | 
|  195   void GetServiceRecordsForConnectErrorCallback( |  | 
|  196       const SocketCallback& callback); |  | 
|  197  |  | 
|  198   // Called by GetServiceRecords with the list of BluetoothServiceRecords. |  | 
|  199   // Connections are attempted to each service in the list matching |  | 
|  200   // |service_uuid|, and the socket from the first successful connection is |  | 
|  201   // passed to |callback|. |  | 
|  202   void GetServiceRecordsForConnectCallback( |  | 
|  203       const std::string& service_uuid, |  | 
|  204       const SocketCallback& callback, |  | 
|  205       const ServiceRecordList& list); |  | 
|  206  |  | 
|  207   // Called by BlueoothDeviceClient in response to the AddRemoteData and |  | 
|  208   // RemoveRemoteData method calls. |  | 
|  209   void OnRemoteDataCallback(const base::Closure& callback, |  | 
|  210                             const ErrorCallback& error_callback, |  | 
|  211                             bool success); |  | 
|  212  |  | 
|  213   // BluetoothDeviceClient::Observer override. |  | 
|  214   // |  | 
|  215   // Called when the device with object path |object_path| is about |  | 
|  216   // to be disconnected, giving a chance for application layers to |  | 
|  217   // shut down cleanly. |  | 
|  218   virtual void DisconnectRequested( |  | 
|  219       const dbus::ObjectPath& object_path) OVERRIDE; |  | 
|  220  |  | 
|  221   // BluetoothAgentServiceProvider::Delegate override. |  | 
|  222   // |  | 
|  223   // This method will be called when the agent is unregistered from the |  | 
|  224   // Bluetooth daemon, generally at the end of a pairing request. It may be |  | 
|  225   // used to perform cleanup tasks. |  | 
|  226   virtual void Release() OVERRIDE; |  | 
|  227  |  | 
|  228   // BluetoothAgentServiceProvider::Delegate override. |  | 
|  229   // |  | 
|  230   // This method will be called when the Bluetooth daemon requires a |  | 
|  231   // PIN Code for authentication of the device with object path |device_path|, |  | 
|  232   // the agent should obtain the code from the user and call |callback| |  | 
|  233   // to provide it, or indicate rejection or cancellation of the request. |  | 
|  234   // |  | 
|  235   // PIN Codes are generally required for Bluetooth 2.0 and earlier devices |  | 
|  236   // for which there is no automatic pairing or special handling. |  | 
|  237   virtual void RequestPinCode(const dbus::ObjectPath& device_path, |  | 
|  238                               const PinCodeCallback& callback) OVERRIDE; |  | 
|  239  |  | 
|  240   // BluetoothAgentServiceProvider::Delegate override. |  | 
|  241   // |  | 
|  242   // This method will be called when the Bluetooth daemon requires a |  | 
|  243   // Passkey for authentication of the device with object path |device_path|, |  | 
|  244   // the agent should obtain the passkey from the user (a numeric in the |  | 
|  245   // range 0-999999) and call |callback| to provide it, or indicate |  | 
|  246   // rejection or cancellation of the request. |  | 
|  247   // |  | 
|  248   // Passkeys are generally required for Bluetooth 2.1 and later devices |  | 
|  249   // which cannot provide input or display on their own, and don't accept |  | 
|  250   // passkey-less pairing. |  | 
|  251   virtual void RequestPasskey(const dbus::ObjectPath& device_path, |  | 
|  252                               const PasskeyCallback& callback) OVERRIDE; |  | 
|  253  |  | 
|  254   // BluetoothAgentServiceProvider::Delegate override. |  | 
|  255   // |  | 
|  256   // This method will be called when the Bluetooth daemon requires that the |  | 
|  257   // user enter the PIN code |pincode| into the device with object path |  | 
|  258   // |device_path| so that it may be authenticated. The Cancel() method |  | 
|  259   // will be called to dismiss the display once pairing is complete or |  | 
|  260   // cancelled. |  | 
|  261   // |  | 
|  262   // This is used for Bluetooth 2.0 and earlier keyboard devices, the |  | 
|  263   // |pincode| will always be a six-digit numeric in the range 000000-999999 |  | 
|  264   // for compatibilty with later specifications. |  | 
|  265   virtual void DisplayPinCode(const dbus::ObjectPath& device_path, |  | 
|  266                               const std::string& pincode) OVERRIDE; |  | 
|  267  |  | 
|  268   // BluetoothAgentServiceProvider::Delegate override. |  | 
|  269   // |  | 
|  270   // This method will be called when the Bluetooth daemon requires that the |  | 
|  271   // user enter the Passkey |passkey| into the device with object path |  | 
|  272   // |device_path| so that it may be authenticated. The Cancel() method |  | 
|  273   // will be called to dismiss the display once pairing is complete or |  | 
|  274   // cancelled. |  | 
|  275   // |  | 
|  276   // This is used for Bluetooth 2.1 and later devices that support input |  | 
|  277   // but not display, such as keyboards. The Passkey is a numeric in the |  | 
|  278   // range 0-999999 and should be always presented zero-padded to six |  | 
|  279   // digits. |  | 
|  280   virtual void DisplayPasskey(const dbus::ObjectPath& device_path, |  | 
|  281                               uint32 passkey) OVERRIDE; |  | 
|  282  |  | 
|  283   // BluetoothAgentServiceProvider::Delegate override. |  | 
|  284   // |  | 
|  285   // This method will be called when the Bluetooth daemon requires that the |  | 
|  286   // user confirm that the Passkey |passkey| is displayed on the screen |  | 
|  287   // of the device with object path |object_path| so that it may be |  | 
|  288   // authentication. The agent should display to the user and ask for |  | 
|  289   // confirmation, then call |callback| to provide their response (success, |  | 
|  290   // rejected or cancelled). |  | 
|  291   // |  | 
|  292   // This is used for Bluetooth 2.1 and later devices that support display, |  | 
|  293   // such as other computers or phones. The Passkey is a numeric in the |  | 
|  294   // range 0-999999 and should be always present zero-padded to six |  | 
|  295   // digits. |  | 
|  296   virtual void RequestConfirmation( |  | 
|  297       const dbus::ObjectPath& device_path, |  | 
|  298       uint32 passkey, |  | 
|  299       const ConfirmationCallback& callback) OVERRIDE; |  | 
|  300  |  | 
|  301   // BluetoothAgentServiceProvider::Delegate override. |  | 
|  302   // |  | 
|  303   // This method will be called when the Bluetooth daemon requires that the |  | 
|  304   // user confirm that the device with object path |object_path| is |  | 
|  305   // authorized to connect to the service with UUID |uuid|. The agent should |  | 
|  306   // confirm with the user and call |callback| to provide their response |  | 
|  307   // (success, rejected or cancelled). |  | 
|  308   virtual void Authorize(const dbus::ObjectPath& device_path, |  | 
|  309                          const std::string& uuid, |  | 
|  310                          const ConfirmationCallback& callback) OVERRIDE; |  | 
|  311  |  | 
|  312   // BluetoothAgentServiceProvider::Delegate override. |  | 
|  313   // |  | 
|  314   // This method will be called when the Bluetooth daemon requires that the |  | 
|  315   // user confirm that the device adapter may switch to mode |mode|. The |  | 
|  316   // agent should confirm with the user and call |callback| to provide |  | 
|  317   // their response (success, rejected or cancelled). |  | 
|  318   virtual void ConfirmModeChange(Mode mode, |  | 
|  319                                  const ConfirmationCallback& callback) OVERRIDE; |  | 
|  320  |  | 
|  321   // BluetoothAgentServiceProvider::Delegate override. |  | 
|  322   // |  | 
|  323   // This method will be called by the Bluetooth daemon to indicate that |  | 
|  324   // the request failed before a reply was returned from the device. |  | 
|  325   virtual void Cancel() OVERRIDE; |  | 
|  326  |  | 
|  327   // Creates a new BluetoothDeviceChromeOs object bound to the adapter |  | 
|  328   // |adapter|. |  | 
|  329   static BluetoothDeviceChromeOs* Create(BluetoothAdapterChromeOs* adapter); |  | 
|  330  |  | 
|  331   // The adapter that owns this device instance. |  | 
|  332   BluetoothAdapterChromeOs* adapter_; |  | 
|  333  |  | 
|  334   // The dbus object path of the device, will be empty if the device has only |  | 
|  335   // been discovered and not yet paired with. |  | 
|  336   dbus::ObjectPath object_path_; |  | 
|  337  |  | 
|  338   // The services (identified by UUIDs) that this device provides. |  | 
|  339   std::vector<std::string> service_uuids_; |  | 
|  340  |  | 
|  341   // During pairing this is set to an object that we don't own, but on which |  | 
|  342   // we can make method calls to request, display or confirm PIN Codes and |  | 
|  343   // Passkeys. Generally it is the object that owns this one. |  | 
|  344   BluetoothDevice::PairingDelegate* pairing_delegate_; |  | 
|  345  |  | 
|  346   // During pairing this is set to an instance of a D-Bus agent object |  | 
|  347   // intialized with our own class as its delegate. |  | 
|  348   scoped_ptr<BluetoothAgentServiceProvider> agent_; |  | 
|  349  |  | 
|  350   // During pairing these callbacks are set to those provided by method calls |  | 
|  351   // made on us by |agent_| and are called by our own method calls such as |  | 
|  352   // SetPinCode() and SetPasskey(). |  | 
|  353   PinCodeCallback pincode_callback_; |  | 
|  354   PasskeyCallback passkey_callback_; |  | 
|  355   ConfirmationCallback confirmation_callback_; |  | 
|  356  |  | 
|  357   // Used to keep track of pending application connection requests. |  | 
|  358   int connecting_applications_counter_; |  | 
|  359  |  | 
|  360   // Note: This should remain the last member so it'll be destroyed and |  | 
|  361   // invalidate its weak pointers before any other members are destroyed. |  | 
|  362   base::WeakPtrFactory<BluetoothDeviceChromeOs> weak_ptr_factory_; |  | 
|  363  |  | 
|  364   DISALLOW_COPY_AND_ASSIGN(BluetoothDeviceChromeOs); |  | 
|  365 }; |  | 
|  366  |  | 
|  367 }  // namespace chromeos |  | 
|  368  |  | 
|  369 #endif  // CHROME_BROWSER_CHROMEOS_BLUETOOTH_BLUETOOTH_DEVICE_CHROMEOS_H_ |  | 
| OLD | NEW |