bluetooth: Chooser Context Implementation

This change implements the methods in BluetoothChooserContext and
creates a unit test for the class.

Design Doc:
https://docs.google.com/document/d/1h3uAVXJARHrNWaNACUPiQhLt7XI-fFFQoARSs1WgMDM/edit?usp=sharing

Bug: 589228
Change-Id: I1c564177063f9459081c45ddb4c0bb757fabd667
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1978819
Commit-Queue: Ovidio de Jesús Ruiz-Henríquez <[email protected]>
Reviewed-by: Reilly Grant <[email protected]>
Reviewed-by: Balazs Engedy <[email protected]>
Cr-Commit-Position: refs/heads/master@{#731730}
diff --git a/chrome/browser/bluetooth/bluetooth_chooser_context.h b/chrome/browser/bluetooth/bluetooth_chooser_context.h
index 700204d1..a04ab4be 100644
--- a/chrome/browser/bluetooth/bluetooth_chooser_context.h
+++ b/chrome/browser/bluetooth/bluetooth_chooser_context.h
@@ -5,12 +5,16 @@
 #ifndef CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_
 #define CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_
 
+#include <map>
 #include <string>
 
 #include "base/containers/flat_set.h"
 #include "chrome/browser/permissions/chooser_context_base.h"
+#include "device/bluetooth/bluetooth_adapter.h"
+#include "device/bluetooth/bluetooth_device.h"
 #include "device/bluetooth/public/cpp/bluetooth_uuid.h"
 #include "third_party/blink/public/common/bluetooth/web_bluetooth_device_id.h"
+#include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-forward.h"
 
 namespace base {
 class Value;
@@ -37,22 +41,27 @@
 
   // Helper methods for converting between a WebBluetoothDeviceId and a
   // Bluetooth device address string for a given origin pair.
-  const blink::WebBluetoothDeviceId GetWebBluetoothDeviceId(
+  blink::WebBluetoothDeviceId GetWebBluetoothDeviceId(
       const url::Origin& requesting_origin,
       const url::Origin& embedding_origin,
       const std::string& device_address);
-  const std::string GetDeviceAddress(
+  std::string GetDeviceAddress(const url::Origin& requesting_origin,
+                               const url::Origin& embedding_origin,
+                               const blink::WebBluetoothDeviceId& device_id);
+
+  // Bluetooth scanning specific interface for generating WebBluetoothDeviceIds
+  // for scanned devices.
+  blink::WebBluetoothDeviceId AddScannedDevice(
       const url::Origin& requesting_origin,
       const url::Origin& embedding_origin,
-      const blink::WebBluetoothDeviceId& device_id);
+      const std::string& device_address);
 
   // Bluetooth-specific interface for granting and checking permissions.
-  const blink::WebBluetoothDeviceId GrantDevicePermission(
+  blink::WebBluetoothDeviceId GrantServiceAccessPermission(
       const url::Origin& requesting_origin,
       const url::Origin& embedding_origin,
-      const std::string& device_address,
-      base::flat_set<device::BluetoothUUID, device::BluetoothUUIDHash>&
-          services);
+      const device::BluetoothDevice* device,
+      const blink::mojom::WebBluetoothRequestDeviceOptionsPtr& options);
   bool HasDevicePermission(const url::Origin& requesting_origin,
                            const url::Origin& embedding_origin,
                            const blink::WebBluetoothDeviceId& device_id);
@@ -68,13 +77,15 @@
  protected:
   // ChooserContextBase implementation;
   bool IsValidObject(const base::Value& object) override;
-  std::vector<std::unique_ptr<Object>> GetGrantedObjects(
-      const url::Origin& requesting_origin,
-      const url::Origin& embedding_origin) override;
-  std::vector<std::unique_ptr<Object>> GetAllGrantedObjects() override;
-  void RevokeObjectPermission(const url::Origin& requesting_origin,
-                              const url::Origin& embedding_origin,
-                              const base::Value& object) override;
+
+ private:
+  // This map records the generated Web Bluetooth IDs for devices discovered via
+  // the Scanning API. Each requesting/embedding origin pair has its own version
+  // of this map so that IDs cannot be correlated between cross-origin sites.
+  using DeviceAddressToIdMap =
+      std::map<std::string, blink::WebBluetoothDeviceId>;
+  std::map<std::pair<url::Origin, url::Origin>, DeviceAddressToIdMap>
+      scanned_devices_;
 };
 
 #endif  // CHROME_BROWSER_BLUETOOTH_BLUETOOTH_CHOOSER_CONTEXT_H_