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) | |
58 mandatoryConstraintsVector.append(WebMediaConstraint(iter->key, iter->va lue)); | |
Peter Beverloo
2015/10/08 09:50:25
nit: Why don't use add them to |mandatory| immedia
hta - Chromium
2015/10/09 13:02:28
It seems that mandatory is a WebVector, and I can'
| |
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 constraint.getPropertyNames(localNames); | |
Peter Beverloo
2015/10/08 09:50:25
Would it make sense to check for the return value
hta - Chromium
2015/10/09 13:02:28
Done.
| |
67 if (localNames.size() != 1) | |
68 return false; | |
69 String key = localNames[0]; | |
70 String value; | |
71 bool ok = DictionaryHelper::get(constraint, key, value); | |
72 if (!ok) | |
73 return false; | |
74 optionalConstraintsVector.append(WebMediaConstraint(key, value)); | |
75 return true; | |
76 } | |
77 | |
47 static bool parse(const Dictionary& constraintsDictionary, WebVector<WebMediaCon straint>& optional, WebVector<WebMediaConstraint>& mandatory) | 78 static bool parse(const Dictionary& constraintsDictionary, WebVector<WebMediaCon straint>& optional, WebVector<WebMediaConstraint>& mandatory) |
48 { | 79 { |
49 if (constraintsDictionary.isUndefinedOrNull()) | 80 if (constraintsDictionary.isUndefinedOrNull()) |
50 return true; | 81 return true; |
51 | 82 |
52 Vector<String> names; | 83 Vector<String> names; |
53 constraintsDictionary.getPropertyNames(names); | 84 constraintsDictionary.getPropertyNames(names); |
54 | 85 |
55 String mandatoryName("mandatory"); | 86 String mandatoryName("mandatory"); |
56 String optionalName("optional"); | 87 String optionalName("optional"); |
57 | 88 |
58 for (Vector<String>::iterator it = names.begin(); it != names.end(); ++it) { | 89 for (Vector<String>::iterator it = names.begin(); it != names.end(); ++it) { |
59 if (*it != mandatoryName && *it != optionalName) | 90 if (*it != mandatoryName && *it != optionalName) |
60 return false; | 91 return false; |
61 } | 92 } |
62 | 93 |
63 Vector<WebMediaConstraint> mandatoryConstraintsVector; | |
64 if (names.contains(mandatoryName)) { | 94 if (names.contains(mandatoryName)) { |
65 Dictionary mandatoryConstraintsDictionary; | 95 Dictionary mandatoryConstraintsDictionary; |
66 bool ok = constraintsDictionary.get(mandatoryName, mandatoryConstraintsD ictionary); | 96 bool ok = constraintsDictionary.get(mandatoryName, mandatoryConstraintsD ictionary); |
67 if (!ok || mandatoryConstraintsDictionary.isUndefinedOrNull()) | 97 if (!ok || mandatoryConstraintsDictionary.isUndefinedOrNull()) |
68 return false; | 98 return false; |
69 | 99 ok = parseMandatoryConstraintsDictionary(mandatoryConstraintsDictionary, mandatory); |
70 HashMap<String, String> mandatoryConstraintsHashMap; | |
71 ok = mandatoryConstraintsDictionary.getOwnPropertiesAsStringHashMap(mand atoryConstraintsHashMap); | |
72 if (!ok) | 100 if (!ok) |
73 return false; | 101 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 } | 102 } |
79 | 103 |
80 Vector<WebMediaConstraint> optionalConstraintsVector; | 104 Vector<WebMediaConstraint> optionalConstraintsVector; |
81 if (names.contains(optionalName)) { | 105 if (names.contains(optionalName)) { |
82 ArrayValue optionalConstraints; | 106 ArrayValue optionalConstraints; |
83 bool ok = DictionaryHelper::get(constraintsDictionary, optionalName, opt ionalConstraints); | 107 bool ok = DictionaryHelper::get(constraintsDictionary, optionalName, opt ionalConstraints); |
84 if (!ok || optionalConstraints.isUndefinedOrNull()) | 108 if (!ok || optionalConstraints.isUndefinedOrNull()) |
85 return false; | 109 return false; |
86 | 110 |
87 size_t numberOfConstraints; | 111 size_t numberOfConstraints; |
88 ok = optionalConstraints.length(numberOfConstraints); | 112 ok = optionalConstraints.length(numberOfConstraints); |
89 if (!ok) | 113 if (!ok) |
90 return false; | 114 return false; |
91 | 115 |
92 for (size_t i = 0; i < numberOfConstraints; ++i) { | 116 for (size_t i = 0; i < numberOfConstraints; ++i) { |
93 Dictionary constraint; | 117 Dictionary constraint; |
94 ok = optionalConstraints.get(i, constraint); | 118 ok = optionalConstraints.get(i, constraint); |
95 if (!ok || constraint.isUndefinedOrNull()) | 119 if (!ok || constraint.isUndefinedOrNull()) |
96 return false; | 120 return false; |
97 Vector<String> localNames; | 121 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) | 122 if (!ok) |
105 return false; | 123 return false; |
106 optionalConstraintsVector.append(WebMediaConstraint(key, value)); | |
107 } | 124 } |
125 optional.assign(optionalConstraintsVector); | |
108 } | 126 } |
109 | 127 |
110 optional.assign(optionalConstraintsVector); | |
111 mandatory.assign(mandatoryConstraintsVector); | |
112 return true; | 128 return true; |
113 } | 129 } |
114 | 130 |
131 static bool parse(const MediaTrackConstraintSet& constraintsIn, WebVector<WebMed iaConstraint>& optional, WebVector<WebMediaConstraint>& mandatory) | |
Peter Beverloo
2015/10/08 09:50:25
I'm still not a huge fan of the overloading, but i
hta - Chromium
2015/10/09 13:02:28
I thought Blink only believed in JS-level tests...
| |
132 { | |
133 Vector<WebMediaConstraint> mandatoryConstraintsVector; | |
134 if (constraintsIn.hasMandatory()) { | |
135 bool ok = parseMandatoryConstraintsDictionary(constraintsIn.mandatory(), mandatory); | |
136 if (!ok) | |
137 return false; | |
138 } | |
139 | |
140 Vector<WebMediaConstraint> optionalConstraintsVector; | |
141 if (constraintsIn.hasOptional()) { | |
142 const Vector<Dictionary> optionalConstraints = constraintsIn.optional(); | |
Peter Beverloo
2015/10/08 09:50:25
const T&, otherwise you're making a copy.
hta - Chromium
2015/10/09 13:02:28
Done.
| |
143 | |
144 for (size_t i = 0; i < optionalConstraints.size(); ++i) { | |
145 Dictionary constraint = optionalConstraints[i]; | |
146 if (constraint.isUndefinedOrNull()) | |
147 return false; | |
148 bool ok = parseOptionalConstraintsVectorElement(constraint, optional ConstraintsVector); | |
149 if (!ok) | |
150 return false; | |
151 } | |
152 optional.assign(optionalConstraintsVector); | |
153 } | |
154 return true; | |
155 } | |
156 | |
115 | 157 |
116 WebMediaConstraints create(const Dictionary& constraintsDictionary, ExceptionSta te& exceptionState) | 158 WebMediaConstraints create(const Dictionary& constraintsDictionary, ExceptionSta te& exceptionState) |
117 { | 159 { |
118 WebVector<WebMediaConstraint> optional; | 160 WebVector<WebMediaConstraint> optional; |
119 WebVector<WebMediaConstraint> mandatory; | 161 WebVector<WebMediaConstraint> mandatory; |
120 if (!parse(constraintsDictionary, optional, mandatory)) { | 162 if (!parse(constraintsDictionary, optional, mandatory)) { |
121 exceptionState.throwTypeError("Malformed constraints object."); | 163 exceptionState.throwTypeError("Malformed constraints object."); |
122 return WebMediaConstraints(); | 164 return WebMediaConstraints(); |
123 } | 165 } |
124 | 166 |
125 WebMediaConstraints constraints; | 167 WebMediaConstraints constraints; |
126 constraints.initialize(optional, mandatory); | 168 constraints.initialize(optional, mandatory); |
127 return constraints; | 169 return constraints; |
128 } | 170 } |
129 | 171 |
172 WebMediaConstraints create(const MediaTrackConstraintSet& constraintsIn, Excepti onState& exceptionState) | |
173 { | |
174 WebVector<WebMediaConstraint> optional; | |
175 WebVector<WebMediaConstraint> mandatory; | |
176 if (!parse(constraintsIn, optional, mandatory)) { | |
177 exceptionState.throwTypeError("Malformed constraints object."); | |
178 return WebMediaConstraints(); | |
179 } | |
180 WebMediaConstraints constraints; | |
181 constraints.initialize(optional, mandatory); | |
182 return constraints; | |
183 } | |
184 | |
130 WebMediaConstraints create() | 185 WebMediaConstraints create() |
131 { | 186 { |
132 WebMediaConstraints constraints; | 187 WebMediaConstraints constraints; |
133 constraints.initialize(); | 188 constraints.initialize(); |
134 return constraints; | 189 return constraints; |
135 } | 190 } |
136 | 191 |
137 } // namespace MediaConstraintsImpl | 192 } // namespace MediaConstraintsImpl |
138 } // namespace blink | 193 } // namespace blink |
OLD | NEW |