// Copyright 2014 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_BLUETOOTH_H_ #define THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_BLUETOOTH_H_ #include "third_party/blink/public/mojom/bluetooth/web_bluetooth.mojom-blink-forward.h" #include "third_party/blink/renderer/bindings/core/v8/script_promise.h" #include "third_party/blink/renderer/core/page/page_visibility_observer.h" #include "third_party/blink/renderer/modules/bluetooth/bluetooth_advertising_event.h" #include "third_party/blink/renderer/modules/bluetooth/bluetooth_device.h" #include "third_party/blink/renderer/platform/bindings/script_wrappable.h" #include "third_party/blink/renderer/platform/heap/handle.h" #include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_receiver_set.h" #include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h" #include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h" namespace blink { class BluetoothLEScanOptions; class ExceptionState; class RequestDeviceOptions; class ScriptPromise; class ScriptState; class Bluetooth final : public EventTargetWithInlineData, public PageVisibilityObserver, public mojom::blink::WebBluetoothAdvertisementClient { DEFINE_WRAPPERTYPEINFO(); public: explicit Bluetooth(LocalDOMWindow*); ~Bluetooth() override; // IDL exposed interface: ScriptPromise getAvailability(ScriptState*, ExceptionState&); ScriptPromise getDevices(ScriptState*, ExceptionState&); ScriptPromise requestDevice(ScriptState*, const RequestDeviceOptions*, ExceptionState&); ScriptPromise requestLEScan(ScriptState*, const BluetoothLEScanOptions*, ExceptionState&); mojom::blink::WebBluetoothService* Service() { return service_.get(); } // WebBluetoothAdvertisementClient void AdvertisingEvent(mojom::blink::WebBluetoothAdvertisingEventPtr) override; // EventTarget const AtomicString& InterfaceName() const override; ExecutionContext* GetExecutionContext() const override; // GC void Trace(Visitor*) const override; DEFINE_ATTRIBUTE_EVENT_LISTENER(advertisementreceived, kAdvertisementreceived) // PageVisibilityObserver void PageVisibilityChanged() override; void CancelScan(mojo::ReceiverId); bool IsScanActive(mojo::ReceiverId) const; BluetoothAdvertisingEvent* CreateBluetoothAdvertisingEvent( mojom::blink::WebBluetoothAdvertisingEventPtr advertising_event); private: BluetoothDevice* GetBluetoothDeviceRepresentingDevice( mojom::blink::WebBluetoothDevicePtr, ExecutionContext*); void GetDevicesCallback(ScriptPromiseResolver*, Vector); void RequestDeviceCallback(ScriptPromiseResolver*, mojom::blink::WebBluetoothResult, mojom::blink::WebBluetoothDevicePtr); void RequestScanningCallback( ScriptPromiseResolver*, mojo::ReceiverId, mojom::blink::WebBluetoothRequestLEScanOptionsPtr, mojom::blink::WebBluetoothResult); void EnsureServiceConnection(ExecutionContext*); // Map of device ids to BluetoothDevice objects. // Ensures only one BluetoothDevice instance represents each // Bluetooth device inside a single global object. HeapHashMap> device_instance_map_; Member window_; HeapMojoAssociatedReceiverSet client_receivers_; // HeapMojoRemote objects are associated with a ContextLifecycleNotifier and // cleaned up automatically when it is destroyed. HeapMojoRemote service_; }; } // namespace blink #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_BLUETOOTH_BLUETOOTH_H_