Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: Source/modules/webmidi/MIDIAccess.cpp

Issue 649683006: Web MIDI: add blink APIs to notify device connection events (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: address a comment at #21 Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Source/modules/webmidi/MIDIAccess.h ('k') | Source/modules/webmidi/MIDIAccessInitializer.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
49 49
50 MIDIAccess::MIDIAccess(PassOwnPtr<MIDIAccessor> accessor, bool sysexEnabled, con st Vector<MIDIAccessInitializer::PortDescriptor>& ports, ExecutionContext* execu tionContext) 50 MIDIAccess::MIDIAccess(PassOwnPtr<MIDIAccessor> accessor, bool sysexEnabled, con st Vector<MIDIAccessInitializer::PortDescriptor>& ports, ExecutionContext* execu tionContext)
51 : ActiveDOMObject(executionContext) 51 : ActiveDOMObject(executionContext)
52 , m_accessor(accessor) 52 , m_accessor(accessor)
53 , m_sysexEnabled(sysexEnabled) 53 , m_sysexEnabled(sysexEnabled)
54 { 54 {
55 m_accessor->setClient(this); 55 m_accessor->setClient(this);
56 for (size_t i = 0; i < ports.size(); ++i) { 56 for (size_t i = 0; i < ports.size(); ++i) {
57 const MIDIAccessInitializer::PortDescriptor& port = ports[i]; 57 const MIDIAccessInitializer::PortDescriptor& port = ports[i];
58 if (port.type == MIDIPort::MIDIPortTypeInput) { 58 if (port.type == MIDIPort::MIDIPortTypeInput) {
59 m_inputs.append(MIDIInput::create(this, port.id, port.manufacturer, port.name, port.version)); 59 m_inputs.append(MIDIInput::create(this, port.id, port.manufacturer, port.name, port.version, port.isActive));
60 } else { 60 } else {
61 m_outputs.append(MIDIOutput::create(this, m_outputs.size(), port.id, port.manufacturer, port.name, port.version)); 61 m_outputs.append(MIDIOutput::create(this, m_outputs.size(), port.id, port.manufacturer, port.name, port.version, port.isActive));
62 } 62 }
63 } 63 }
64 } 64 }
65 65
66 MIDIAccess::~MIDIAccess() 66 MIDIAccess::~MIDIAccess()
67 { 67 {
68 } 68 }
69 69
70 MIDIInputMap* MIDIAccess::inputs() const 70 MIDIInputMap* MIDIAccess::inputs() const
71 { 71 {
72 HeapHashMap<String, Member<MIDIInput> > inputs; 72 HeapHashMap<String, Member<MIDIInput> > inputs;
73 size_t inactiveCount = 0;
73 for (size_t i = 0; i < m_inputs.size(); ++i) { 74 for (size_t i = 0; i < m_inputs.size(); ++i) {
74 MIDIInput* input = m_inputs[i]; 75 MIDIInput* input = m_inputs[i];
75 inputs.add(input->id(), input); 76 if (input->isActive())
77 inputs.add(input->id(), input);
78 else
79 inactiveCount++;
76 } 80 }
77 if (inputs.size() != m_inputs.size()) { 81 if ((inputs.size() + inactiveCount) != m_inputs.size()) {
78 // There is id duplication that violates the spec. 82 // There is id duplication that violates the spec.
79 inputs.clear(); 83 inputs.clear();
80 } 84 }
81 return new MIDIInputMap(inputs); 85 return new MIDIInputMap(inputs);
82 } 86 }
83 87
84 MIDIOutputMap* MIDIAccess::outputs() const 88 MIDIOutputMap* MIDIAccess::outputs() const
85 { 89 {
86 HeapHashMap<String, Member<MIDIOutput> > outputs; 90 HeapHashMap<String, Member<MIDIOutput> > outputs;
91 size_t inactiveCount = 0;
87 for (size_t i = 0; i < m_outputs.size(); ++i) { 92 for (size_t i = 0; i < m_outputs.size(); ++i) {
88 MIDIOutput* output = m_outputs[i]; 93 MIDIOutput* output = m_outputs[i];
89 outputs.add(output->id(), output); 94 if (output->isActive())
95 outputs.add(output->id(), output);
96 else
97 inactiveCount++;
90 } 98 }
91 if (outputs.size() != m_outputs.size()) { 99 if ((outputs.size() + inactiveCount) != m_outputs.size()) {
92 // There is id duplication that violates the spec. 100 // There is id duplication that violates the spec.
93 outputs.clear(); 101 outputs.clear();
94 } 102 }
95 return new MIDIOutputMap(outputs); 103 return new MIDIOutputMap(outputs);
96 } 104 }
97 105
98 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, c onst String& name, const String& version) 106 void MIDIAccess::didAddInputPort(const String& id, const String& manufacturer, c onst String& name, const String& version, bool isActive)
99 { 107 {
100 ASSERT(isMainThread()); 108 ASSERT(isMainThread());
101 m_inputs.append(MIDIInput::create(this, id, manufacturer, name, version)); 109 m_inputs.append(MIDIInput::create(this, id, manufacturer, name, version, isA ctive));
102 } 110 }
103 111
104 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version) 112 void MIDIAccess::didAddOutputPort(const String& id, const String& manufacturer, const String& name, const String& version, bool isActive)
105 { 113 {
106 ASSERT(isMainThread()); 114 ASSERT(isMainThread());
107 unsigned portIndex = m_outputs.size(); 115 unsigned portIndex = m_outputs.size();
108 m_outputs.append(MIDIOutput::create(this, portIndex, id, manufacturer, name, version)); 116 m_outputs.append(MIDIOutput::create(this, portIndex, id, manufacturer, name, version, isActive));
117 }
118
119 void MIDIAccess::didSetInputPortState(unsigned portIndex, bool isActive)
120 {
121 ASSERT(isMainThread());
122 if (portIndex < m_inputs.size())
123 m_inputs[portIndex]->setActiveState(isActive);
124 }
125
126 void MIDIAccess::didSetOutputPortState(unsigned portIndex, bool isActive)
127 {
128 ASSERT(isMainThread());
129 if (portIndex < m_outputs.size())
130 m_outputs[portIndex]->setActiveState(isActive);
109 } 131 }
110 132
111 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* dat a, size_t length, double timeStamp) 133 void MIDIAccess::didReceiveMIDIData(unsigned portIndex, const unsigned char* dat a, size_t length, double timeStamp)
112 { 134 {
113 ASSERT(isMainThread()); 135 ASSERT(isMainThread());
114 if (portIndex >= m_inputs.size()) 136 if (portIndex >= m_inputs.size())
115 return; 137 return;
116 138
117 // Convert from time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime() 139 // Convert from time in seconds which is based on the time coordinate system of monotonicallyIncreasingTime()
118 // into time in milliseconds (a DOMHighResTimeStamp) according to the same t ime coordinate system as performance.now(). 140 // into time in milliseconds (a DOMHighResTimeStamp) according to the same t ime coordinate system as performance.now().
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 } 175 }
154 176
155 void MIDIAccess::trace(Visitor* visitor) 177 void MIDIAccess::trace(Visitor* visitor)
156 { 178 {
157 visitor->trace(m_inputs); 179 visitor->trace(m_inputs);
158 visitor->trace(m_outputs); 180 visitor->trace(m_outputs);
159 EventTargetWithInlineData::trace(visitor); 181 EventTargetWithInlineData::trace(visitor);
160 } 182 }
161 183
162 } // namespace blink 184 } // namespace blink
OLDNEW
« no previous file with comments | « Source/modules/webmidi/MIDIAccess.h ('k') | Source/modules/webmidi/MIDIAccessInitializer.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698