OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 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 | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * | 7 * |
8 * 1. Redistributions of source code must retain the above copyright | 8 * 1. 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 * 2. Redistributions in binary form must reproduce the above copyright | 10 * 2. Redistributions in binary form must reproduce the above copyright |
(...skipping 13 matching lines...) Expand all Loading... | |
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 */ | 29 */ |
30 | 30 |
31 #include "config.h" | 31 #include "config.h" |
32 | 32 |
33 #include "modules/mediastream/MediaConstraintsImpl.h" | 33 #include "modules/mediastream/MediaConstraintsImpl.h" |
34 #include "modules/mediastream/MediaTrackConstraintSet.h" | |
34 | 35 |
35 #include "bindings/core/v8/ArrayValue.h" | 36 #include "bindings/core/v8/ArrayValue.h" |
36 #include "bindings/core/v8/Dictionary.h" | 37 #include "bindings/core/v8/Dictionary.h" |
37 #include "bindings/core/v8/ExceptionState.h" | 38 #include "bindings/core/v8/ExceptionState.h" |
38 #include "core/dom/ExceptionCode.h" | 39 #include "core/dom/ExceptionCode.h" |
39 #include "wtf/HashMap.h" | 40 #include "wtf/HashMap.h" |
40 #include "wtf/Vector.h" | 41 #include "wtf/Vector.h" |
41 #include "wtf/text/StringHash.h" | 42 #include "wtf/text/StringHash.h" |
42 | 43 |
43 namespace blink { | 44 namespace blink { |
44 | 45 |
45 namespace MediaConstraintsImpl { | 46 namespace MediaConstraintsImpl { |
46 | 47 |
48 static bool parseMandatoryConstraintsDictionary(const Dictionary& mandatoryConst raintsDictionary, WebVector<WebMediaConstraint>& mandatory) | |
49 { | |
50 Vector<WebMediaConstraint> mandatoryConstraintsVector; | |
51 HashMap<String, String> mandatoryConstraintsHashMap; | |
52 bool ok = mandatoryConstraintsDictionary.getOwnPropertiesAsStringHashMap(man datoryConstraintsHashMap); | |
53 if (!ok) | |
54 return false; | |
55 | |
56 HashMap<String, String>::const_iterator iter = mandatoryConstraintsHashMap.b egin(); | |
57 for (; iter != mandatoryConstraintsHashMap.end(); ++iter) | |
tommi (sloooow) - chröme
2015/10/09 14:49:21
range based for loop? (looks like HashMap support
hta - Chromium
2015/10/09 16:26:28
Guido suggested that a couple of cycles ago, but I
Guido Urdaneta
2015/10/12 08:54:56
Something like this should work:
for (const auto&
tommi (sloooow) - chröme
2015/10/12 09:02:28
Assuming it works the same way as std maps, it'd l
hta - Chromium
2015/10/12 12:24:40
This looked so beautiful, I'm adopting it :-)
| |
58 mandatoryConstraintsVector.append(WebMediaConstraint(iter->key, iter->va lue)); | |
59 mandatory.assign(mandatoryConstraintsVector); | |
60 return true; | |
61 } | |
62 | |
63 static bool parseOptionalConstraintsVectorElement(const Dictionary& constraint, Vector<WebMediaConstraint>& optionalConstraintsVector) | |
64 { | |
65 Vector<String> localNames; | |
66 bool ok = constraint.getPropertyNames(localNames); | |
67 if (!ok) | |
68 return false; | |
69 if (localNames.size() != 1) | |
70 return false; | |
71 String key = localNames[0]; | |
tommi (sloooow) - chröme
2015/10/09 14:49:21
is this copy necessary? (const String&?)
hta - Chromium
2015/10/09 16:26:28
Done.
| |
72 String value; | |
73 ok = DictionaryHelper::get(constraint, key, value); | |
74 if (!ok) | |
75 return false; | |
76 optionalConstraintsVector.append(WebMediaConstraint(key, value)); | |
77 return true; | |
78 } | |
79 | |
47 static bool parse(const Dictionary& constraintsDictionary, WebVector<WebMediaCon straint>& optional, WebVector<WebMediaConstraint>& mandatory) | 80 static bool parse(const Dictionary& constraintsDictionary, WebVector<WebMediaCon straint>& optional, WebVector<WebMediaConstraint>& mandatory) |
48 { | 81 { |
49 if (constraintsDictionary.isUndefinedOrNull()) | 82 if (constraintsDictionary.isUndefinedOrNull()) |
50 return true; | 83 return true; |
51 | 84 |
52 Vector<String> names; | 85 Vector<String> names; |
53 constraintsDictionary.getPropertyNames(names); | 86 bool ok = constraintsDictionary.getPropertyNames(names); |
87 if (!ok) | |
88 return false; | |
54 | 89 |
55 String mandatoryName("mandatory"); | 90 String mandatoryName("mandatory"); |
56 String optionalName("optional"); | 91 String optionalName("optional"); |
57 | 92 |
58 for (Vector<String>::iterator it = names.begin(); it != names.end(); ++it) { | 93 for (Vector<String>::iterator it = names.begin(); it != names.end(); ++it) { |
59 if (*it != mandatoryName && *it != optionalName) | 94 if (*it != mandatoryName && *it != optionalName) |
60 return false; | 95 return false; |
61 } | 96 } |
62 | 97 |
63 Vector<WebMediaConstraint> mandatoryConstraintsVector; | |
64 if (names.contains(mandatoryName)) { | 98 if (names.contains(mandatoryName)) { |
65 Dictionary mandatoryConstraintsDictionary; | 99 Dictionary mandatoryConstraintsDictionary; |
66 bool ok = constraintsDictionary.get(mandatoryName, mandatoryConstraintsD ictionary); | 100 bool ok = constraintsDictionary.get(mandatoryName, mandatoryConstraintsD ictionary); |
67 if (!ok || mandatoryConstraintsDictionary.isUndefinedOrNull()) | 101 if (!ok || mandatoryConstraintsDictionary.isUndefinedOrNull()) |
68 return false; | 102 return false; |
69 | 103 ok = parseMandatoryConstraintsDictionary(mandatoryConstraintsDictionary, mandatory); |
70 HashMap<String, String> mandatoryConstraintsHashMap; | |
71 ok = mandatoryConstraintsDictionary.getOwnPropertiesAsStringHashMap(mand atoryConstraintsHashMap); | |
72 if (!ok) | 104 if (!ok) |
73 return false; | 105 return false; |
74 | |
75 HashMap<String, String>::const_iterator iter = mandatoryConstraintsHashM ap.begin(); | |
76 for (; iter != mandatoryConstraintsHashMap.end(); ++iter) | |
77 mandatoryConstraintsVector.append(WebMediaConstraint(iter->key, iter ->value)); | |
78 } | 106 } |
79 | 107 |
80 Vector<WebMediaConstraint> optionalConstraintsVector; | 108 Vector<WebMediaConstraint> optionalConstraintsVector; |
81 if (names.contains(optionalName)) { | 109 if (names.contains(optionalName)) { |
82 ArrayValue optionalConstraints; | 110 ArrayValue optionalConstraints; |
83 bool ok = DictionaryHelper::get(constraintsDictionary, optionalName, opt ionalConstraints); | 111 bool ok = DictionaryHelper::get(constraintsDictionary, optionalName, opt ionalConstraints); |
84 if (!ok || optionalConstraints.isUndefinedOrNull()) | 112 if (!ok || optionalConstraints.isUndefinedOrNull()) |
85 return false; | 113 return false; |
86 | 114 |
87 size_t numberOfConstraints; | 115 size_t numberOfConstraints; |
88 ok = optionalConstraints.length(numberOfConstraints); | 116 ok = optionalConstraints.length(numberOfConstraints); |
89 if (!ok) | 117 if (!ok) |
90 return false; | 118 return false; |
91 | 119 |
92 for (size_t i = 0; i < numberOfConstraints; ++i) { | 120 for (size_t i = 0; i < numberOfConstraints; ++i) { |
93 Dictionary constraint; | 121 Dictionary constraint; |
94 ok = optionalConstraints.get(i, constraint); | 122 ok = optionalConstraints.get(i, constraint); |
95 if (!ok || constraint.isUndefinedOrNull()) | 123 if (!ok || constraint.isUndefinedOrNull()) |
96 return false; | 124 return false; |
97 Vector<String> localNames; | 125 ok = parseOptionalConstraintsVectorElement(constraint, optionalConst raintsVector); |
98 constraint.getPropertyNames(localNames); | |
99 if (localNames.size() != 1) | |
100 return false; | |
101 String key = localNames[0]; | |
102 String value; | |
103 ok = DictionaryHelper::get(constraint, key, value); | |
104 if (!ok) | 126 if (!ok) |
105 return false; | 127 return false; |
106 optionalConstraintsVector.append(WebMediaConstraint(key, value)); | |
107 } | 128 } |
129 optional.assign(optionalConstraintsVector); | |
108 } | 130 } |
109 | 131 |
110 optional.assign(optionalConstraintsVector); | |
111 mandatory.assign(mandatoryConstraintsVector); | |
112 return true; | 132 return true; |
113 } | 133 } |
114 | 134 |
135 static bool parse(const MediaTrackConstraintSet& constraintsIn, WebVector<WebMed iaConstraint>& optional, WebVector<WebMediaConstraint>& mandatory) | |
136 { | |
137 Vector<WebMediaConstraint> mandatoryConstraintsVector; | |
138 if (constraintsIn.hasMandatory()) { | |
139 bool ok = parseMandatoryConstraintsDictionary(constraintsIn.mandatory(), mandatory); | |
140 if (!ok) | |
141 return false; | |
142 } | |
143 | |
144 Vector<WebMediaConstraint> optionalConstraintsVector; | |
145 if (constraintsIn.hasOptional()) { | |
146 const Vector<Dictionary>& optionalConstraints = constraintsIn.optional() ; | |
147 | |
148 for (size_t i = 0; i < optionalConstraints.size(); ++i) { | |
149 Dictionary constraint = optionalConstraints[i]; | |
tommi (sloooow) - chröme
2015/10/09 14:49:21
can we avoid creating a copy or is this not really
hta - Chromium
2015/10/09 16:26:28
The old code (above) used a getter that returned a
| |
150 if (constraint.isUndefinedOrNull()) | |
151 return false; | |
152 bool ok = parseOptionalConstraintsVectorElement(constraint, optional ConstraintsVector); | |
153 if (!ok) | |
154 return false; | |
155 } | |
156 optional.assign(optionalConstraintsVector); | |
157 } | |
158 return true; | |
159 } | |
160 | |
115 | 161 |
116 WebMediaConstraints create(const Dictionary& constraintsDictionary, ExceptionSta te& exceptionState) | 162 WebMediaConstraints create(const Dictionary& constraintsDictionary, ExceptionSta te& exceptionState) |
117 { | 163 { |
118 WebVector<WebMediaConstraint> optional; | 164 WebVector<WebMediaConstraint> optional; |
119 WebVector<WebMediaConstraint> mandatory; | 165 WebVector<WebMediaConstraint> mandatory; |
120 if (!parse(constraintsDictionary, optional, mandatory)) { | 166 if (!parse(constraintsDictionary, optional, mandatory)) { |
121 exceptionState.throwTypeError("Malformed constraints object."); | 167 exceptionState.throwTypeError("Malformed constraints object."); |
122 return WebMediaConstraints(); | 168 return WebMediaConstraints(); |
123 } | 169 } |
124 | 170 |
125 WebMediaConstraints constraints; | 171 WebMediaConstraints constraints; |
126 constraints.initialize(optional, mandatory); | 172 constraints.initialize(optional, mandatory); |
127 return constraints; | 173 return constraints; |
128 } | 174 } |
129 | 175 |
176 WebMediaConstraints create(const MediaTrackConstraintSet& constraintsIn, Excepti onState& exceptionState) | |
177 { | |
178 WebVector<WebMediaConstraint> optional; | |
179 WebVector<WebMediaConstraint> mandatory; | |
180 if (!parse(constraintsIn, optional, mandatory)) { | |
181 exceptionState.throwTypeError("Malformed constraints object."); | |
182 return WebMediaConstraints(); | |
183 } | |
184 WebMediaConstraints constraints; | |
185 constraints.initialize(optional, mandatory); | |
tommi (sloooow) - chröme
2015/10/09 14:49:21
is the ownership of the vectors given to the const
hta - Chromium
2015/10/09 16:26:28
It's not a start, it's been that way for a long ti
tommi (sloooow) - chröme
2015/10/12 09:02:28
Sorry, my 'to start' remark didn't come out the wa
hta - Chromium
2015/10/12 12:24:40
Acknowledged.
| |
186 return constraints; | |
187 } | |
188 | |
130 WebMediaConstraints create() | 189 WebMediaConstraints create() |
131 { | 190 { |
132 WebMediaConstraints constraints; | 191 WebMediaConstraints constraints; |
133 constraints.initialize(); | 192 constraints.initialize(); |
134 return constraints; | 193 return constraints; |
135 } | 194 } |
136 | 195 |
137 } // namespace MediaConstraintsImpl | 196 } // namespace MediaConstraintsImpl |
138 } // namespace blink | 197 } // namespace blink |
OLD | NEW |