1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
// Copyright 2019 The Chromium Authors
// 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_HID_HID_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_HID_HID_H_
#include "services/device/public/mojom/hid.mojom-blink-forward.h"
#include "third_party/blink/public/mojom/hid/hid.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"
#include "third_party/blink/renderer/core/dom/events/event_target.h"
#include "third_party/blink/renderer/modules/hid/hid_device.h"
#include "third_party/blink/renderer/modules/modules_export.h"
#include "third_party/blink/renderer/platform/bindings/script_wrappable.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_hash_map.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_hash_set.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_associated_receiver.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_remote.h"
#include "third_party/blink/renderer/platform/mojo/heap_mojo_wrapper_mode.h"
#include "third_party/blink/renderer/platform/scheduler/public/frame_or_worker_scheduler.h"
#include "third_party/blink/renderer/platform/supplementable.h"
namespace blink {
class ExecutionContext;
class HIDDeviceFilter;
class HIDDeviceRequestOptions;
class NavigatorBase;
class ScriptState;
class MODULES_EXPORT HID : public EventTarget,
public Supplement<NavigatorBase>,
public device::mojom::blink::HidManagerClient,
public HIDDevice::ServiceInterface {
DEFINE_WRAPPERTYPEINFO();
public:
static const char kSupplementName[];
// Web-exposed getter for navigator.hid
static HID* hid(NavigatorBase&);
explicit HID(NavigatorBase&);
~HID() override;
// EventTarget:
ExecutionContext* GetExecutionContext() const override;
const AtomicString& InterfaceName() const override;
// device::mojom::HidManagerClient:
void DeviceAdded(device::mojom::blink::HidDeviceInfoPtr device_info) override;
void DeviceRemoved(
device::mojom::blink::HidDeviceInfoPtr device_info) override;
void DeviceChanged(
device::mojom::blink::HidDeviceInfoPtr device_info) override;
// Web-exposed interfaces on hid object:
DEFINE_ATTRIBUTE_EVENT_LISTENER(connect, kConnect)
DEFINE_ATTRIBUTE_EVENT_LISTENER(disconnect, kDisconnect)
ScriptPromise<IDLSequence<HIDDevice>> getDevices(ScriptState*,
ExceptionState&);
ScriptPromise<IDLSequence<HIDDevice>>
requestDevice(ScriptState*, const HIDDeviceRequestOptions*, ExceptionState&);
// HIDDevice::ServiceInterface:
void Connect(
const String& device_guid,
mojo::PendingRemote<device::mojom::blink::HidConnectionClient>
connection_client,
device::mojom::blink::HidManager::ConnectCallback callback) override;
void Forget(device::mojom::blink::HidDeviceInfoPtr device_info,
mojom::blink::HidService::ForgetCallback callback) override;
// Converts a HID device `filter` into the equivalent Mojo type and returns
// it. CheckDeviceFilterValidity must be called first.
static mojom::blink::HidDeviceFilterPtr ConvertDeviceFilter(
const HIDDeviceFilter& filter);
// Checks the validity of the given HIDDeviceFilter. Returns null string when
// filter is valid or an error message when the filter is invalid.
static String CheckDeviceFilterValidity(const HIDDeviceFilter& filter);
void Trace(Visitor*) const override;
protected:
// EventTarget:
void AddedEventListener(const AtomicString& event_type,
RegisteredEventListener&) override;
private:
// Returns the HIDDevice matching |info| from |device_cache_|. If the device
// is not in the cache, a new device is created and added to the cache.
HIDDevice* GetOrCreateDevice(device::mojom::blink::HidDeviceInfoPtr info);
// Opens a connection to HidService, or does nothing if the connection is
// already open.
void EnsureServiceConnection();
// Closes the connection to HidService and resolves any pending promises.
void CloseServiceConnection();
using HIDDeviceResolver = ScriptPromiseResolver<IDLSequence<HIDDevice>>;
void FinishGetDevices(HIDDeviceResolver*,
Vector<device::mojom::blink::HidDeviceInfoPtr>);
void FinishRequestDevice(HIDDeviceResolver*,
Vector<device::mojom::blink::HidDeviceInfoPtr>);
HeapMojoRemote<mojom::blink::HidService> service_;
HeapMojoAssociatedReceiver<device::mojom::blink::HidManagerClient, HID>
receiver_;
HeapHashSet<Member<HIDDeviceResolver>> get_devices_promises_;
HeapHashSet<Member<HIDDeviceResolver>> request_device_promises_;
HeapHashMap<String, WeakMember<HIDDevice>> device_cache_;
std::optional<FrameOrWorkerScheduler::SchedulingAffectingFeatureHandle>
feature_handle_for_scheduler_;
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_HID_HID_H_
|