File: midi_dispatcher.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (153 lines) | stat: -rw-r--r-- 5,560 bytes parent folder | download | duplicates (6)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/modules/webmidi/midi_dispatcher.h"

#include <utility>

#include "base/trace_event/trace_event.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "third_party/blink/public/common/thread_safe_browser_interface_broker_proxy.h"
#include "third_party/blink/public/platform/browser_interface_broker_proxy.h"
#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/public/platform/task_type.h"
#include "third_party/blink/public/platform/web_string.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"

namespace blink {

namespace {
// The maximum number of bytes which we're allowed to send to the browser
// before getting acknowledgement back from the browser that they've been
// successfully sent.
static const size_t kMaxUnacknowledgedBytesSent = 10 * 1024 * 1024;  // 10 MB.
}  // namespace

MIDIDispatcher::MIDIDispatcher(ExecutionContext* execution_context)
    : midi_session_(execution_context),
      receiver_(this, execution_context),
      midi_session_provider_(execution_context) {
  TRACE_EVENT0("midi", "MIDIDispatcher::MIDIDispatcher");
  // See https://bit.ly/2S0zRAS for task types.
  execution_context->GetBrowserInterfaceBroker().GetInterface(
      midi_session_provider_.BindNewPipeAndPassReceiver(
          execution_context->GetTaskRunner(blink::TaskType::kMiscPlatformAPI)));
  midi_session_provider_->StartSession(
      midi_session_.BindNewPipeAndPassReceiver(
          execution_context->GetTaskRunner(blink::TaskType::kMiscPlatformAPI)),
      receiver_.BindNewPipeAndPassRemote(
          execution_context->GetTaskRunner(blink::TaskType::kMiscPlatformAPI)));
}

MIDIDispatcher::~MIDIDispatcher() = default;

void MIDIDispatcher::SendMIDIData(uint32_t port,
                                  base::span<const uint8_t> data,
                                  base::TimeTicks timestamp) {
  if ((kMaxUnacknowledgedBytesSent - unacknowledged_bytes_sent_) <
      data.size()) {
    // TODO(toyoshim): buffer up the data to send at a later time.
    // For now we're just dropping these bytes on the floor.
    return;
  }

  unacknowledged_bytes_sent_ += data.size();
  Vector<uint8_t> v;
  v.AppendSpan(data);
  midi_session_->SendData(port, std::move(v), timestamp);
}

void MIDIDispatcher::AddInputPort(midi::mojom::blink::PortInfoPtr info) {
  DCHECK(client_);
  inputs_.push_back(*info);
  if (initialized_) {
    client_->DidAddInputPort(info->id, info->manufacturer, info->name,
                             info->version, info->state);
  }
}

void MIDIDispatcher::AddOutputPort(midi::mojom::blink::PortInfoPtr info) {
  DCHECK(client_);
  outputs_.push_back(*info);
  if (initialized_) {
    client_->DidAddOutputPort(info->id, info->manufacturer, info->name,
                              info->version, info->state);
  }
}

void MIDIDispatcher::SetInputPortState(uint32_t port,
                                       midi::mojom::blink::PortState state) {
  DCHECK(client_);
  if (inputs_[port].state == state)
    return;
  inputs_[port].state = state;
  if (initialized_)
    client_->DidSetInputPortState(port, state);
}

void MIDIDispatcher::SetOutputPortState(uint32_t port,
                                        midi::mojom::blink::PortState state) {
  DCHECK(client_);
  if (outputs_[port].state == state)
    return;
  outputs_[port].state = state;
  if (initialized_)
    client_->DidSetOutputPortState(port, state);
}

void MIDIDispatcher::SessionStarted(midi::mojom::blink::Result result) {
  TRACE_EVENT0("midi", "MIDIDispatcher::OnSessionStarted");

  // We always have a valid instance in `client_` in the production code, but
  // just in case to be robust for mojo injections and code changes in the
  // future. Other methods protect accesses to `client_` by `initialized_` flag
  // that is set below.
  SECURITY_CHECK(client_);

  DCHECK(!initialized_);
  initialized_ = true;

  if (result == midi::mojom::blink::Result::OK) {
    // Add the accessor's input and output ports.
    for (const auto& info : inputs_) {
      client_->DidAddInputPort(info.id, info.manufacturer, info.name,
                               info.version, info.state);
    }

    for (const auto& info : outputs_) {
      client_->DidAddOutputPort(info.id, info.manufacturer, info.name,
                                info.version, info.state);
    }
  }
  client_->DidStartSession(result);
}

void MIDIDispatcher::AcknowledgeSentData(uint32_t bytes_sent) {
  DCHECK_GE(unacknowledged_bytes_sent_, bytes_sent);
  if (unacknowledged_bytes_sent_ >= bytes_sent)
    unacknowledged_bytes_sent_ -= bytes_sent;
}

void MIDIDispatcher::DataReceived(uint32_t port,
                                  const Vector<uint8_t>& data,
                                  base::TimeTicks timestamp) {
  DCHECK(client_);
  TRACE_EVENT0("midi", "MIDIDispatcher::DataReceived");
  DCHECK(!data.empty());

  if (initialized_)
    client_->DidReceiveMIDIData(port, data, timestamp);
}

void MIDIDispatcher::Trace(Visitor* visitor) const {
  visitor->Trace(client_);
  visitor->Trace(midi_session_);
  visitor->Trace(receiver_);
  visitor->Trace(midi_session_provider_);
}

}  // namespace blink