bluetooth.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2014 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. #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_BLUETOOTH_H_
  5. #define THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_BLUETOOTH_H_
  6. #include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-blink-forward.h"
  7. #include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
  8. #include "third_party/blink/renderer/core/page/page_visibility_observer.h"
  9. #include "third_party/blink/renderer/modules/bluetooth/bluetooth_advertising_event.h"
  10. #include "third_party/blink/renderer/modules/bluetooth/bluetooth_device.h"
  11. #include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
  12. #include "third_party/blink/renderer/platform/heap/handle.h"
  13. #include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_receiver_set.h"
  14. #include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
  15. #include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
  16. namespace blink {
  17. class BluetoothLEScanOptions;
  18. class ExceptionState;
  19. class RequestDeviceOptions;
  20. class ScriptPromise;
  21. class ScriptState;
  22. class Bluetooth final : public EventTargetWithInlineData,
  23. public PageVisibilityObserver,
  24. public mojom::blink::WebBluetoothAdvertisementClient {
  25. DEFINE_WRAPPERTYPEINFO();
  26. public:
  27. explicit Bluetooth(LocalDOMWindow*);
  28. ~Bluetooth() override;
  29. // IDL exposed interface:
  30. ScriptPromise getAvailability(ScriptState*, ExceptionState&);
  31. ScriptPromise getDevices(ScriptState*, ExceptionState&);
  32. ScriptPromise requestDevice(ScriptState*,
  33. const RequestDeviceOptions*,
  34. ExceptionState&);
  35. ScriptPromise requestLEScan(ScriptState*,
  36. const BluetoothLEScanOptions*,
  37. ExceptionState&);
  38. mojom::blink::WebBluetoothService* Service() { return service_.get(); }
  39. // WebBluetoothAdvertisementClient
  40. void AdvertisingEvent(mojom::blink::WebBluetoothAdvertisingEventPtr) override;
  41. // EventTarget
  42. const AtomicString& InterfaceName() const override;
  43. ExecutionContext* GetExecutionContext() const override;
  44. // GC
  45. void Trace(Visitor*) const override;
  46. DEFINE_ATTRIBUTE_EVENT_LISTENER(advertisementreceived, kAdvertisementreceived)
  47. // PageVisibilityObserver
  48. void PageVisibilityChanged() override;
  49. void CancelScan(mojo::ReceiverId);
  50. bool IsScanActive(mojo::ReceiverId) const;
  51. BluetoothAdvertisingEvent* CreateBluetoothAdvertisingEvent(
  52. mojom::blink::WebBluetoothAdvertisingEventPtr advertising_event);
  53. private:
  54. BluetoothDevice* GetBluetoothDeviceRepresentingDevice(
  55. mojom::blink::WebBluetoothDevicePtr,
  56. ExecutionContext*);
  57. void GetDevicesCallback(ScriptPromiseResolver*,
  58. Vector<mojom::blink::WebBluetoothDevicePtr>);
  59. void RequestDeviceCallback(ScriptPromiseResolver*,
  60. mojom::blink::WebBluetoothResult,
  61. mojom::blink::WebBluetoothDevicePtr);
  62. void RequestScanningCallback(
  63. ScriptPromiseResolver*,
  64. mojo::ReceiverId,
  65. mojom::blink::WebBluetoothRequestLEScanOptionsPtr,
  66. mojom::blink::WebBluetoothResult);
  67. void EnsureServiceConnection(ExecutionContext*);
  68. // Map of device ids to BluetoothDevice objects.
  69. // Ensures only one BluetoothDevice instance represents each
  70. // Bluetooth device inside a single global object.
  71. HeapHashMap<String, Member<BluetoothDevice>> device_instance_map_;
  72. Member<LocalDOMWindow> window_;
  73. HeapMojoAssociatedReceiverSet<mojom::blink::WebBluetoothAdvertisementClient,
  74. Bluetooth>
  75. client_receivers_;
  76. // HeapMojoRemote objects are associated with a ContextLifecycleNotifier and
  77. // cleaned up automatically when it is destroyed.
  78. HeapMojoRemote<mojom::blink::WebBluetoothService> service_;
  79. };
  80. } // namespace blink
  81. #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_BLUETOOTH_H_