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

Side by Side Diff: third_party/WebKit/Source/modules/mediastream/MediaConstraintsImpl.cpp

Issue 1318393002: Refactor "track options" to be a dictionary. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Another review comment set Created 5 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 | Annotate | Revision Log
OLDNEW
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
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 for (const auto& iter : mandatoryConstraintsHashMap)
57 mandatoryConstraintsVector.append(WebMediaConstraint(iter.key, iter.valu e));
58 mandatory.assign(mandatoryConstraintsVector);
59 return true;
60 }
61
62 static bool parseOptionalConstraintsVectorElement(const Dictionary& constraint, Vector<WebMediaConstraint>& optionalConstraintsVector)
63 {
64 Vector<String> localNames;
65 bool ok = constraint.getPropertyNames(localNames);
66 if (!ok)
67 return false;
68 if (localNames.size() != 1)
69 return false;
70 const String& key = localNames[0];
71 String value;
72 ok = DictionaryHelper::get(constraint, key, value);
73 if (!ok)
74 return false;
75 optionalConstraintsVector.append(WebMediaConstraint(key, value));
76 return true;
77 }
78
47 static bool parse(const Dictionary& constraintsDictionary, WebVector<WebMediaCon straint>& optional, WebVector<WebMediaConstraint>& mandatory) 79 static bool parse(const Dictionary& constraintsDictionary, WebVector<WebMediaCon straint>& optional, WebVector<WebMediaConstraint>& mandatory)
48 { 80 {
49 if (constraintsDictionary.isUndefinedOrNull()) 81 if (constraintsDictionary.isUndefinedOrNull())
50 return true; 82 return true;
51 83
52 Vector<String> names; 84 Vector<String> names;
53 constraintsDictionary.getPropertyNames(names); 85 bool ok = constraintsDictionary.getPropertyNames(names);
86 if (!ok)
87 return false;
54 88
55 String mandatoryName("mandatory"); 89 String mandatoryName("mandatory");
56 String optionalName("optional"); 90 String optionalName("optional");
57 91
58 for (Vector<String>::iterator it = names.begin(); it != names.end(); ++it) { 92 for (Vector<String>::iterator it = names.begin(); it != names.end(); ++it) {
59 if (*it != mandatoryName && *it != optionalName) 93 if (*it != mandatoryName && *it != optionalName)
60 return false; 94 return false;
61 } 95 }
62 96
63 Vector<WebMediaConstraint> mandatoryConstraintsVector;
64 if (names.contains(mandatoryName)) { 97 if (names.contains(mandatoryName)) {
65 Dictionary mandatoryConstraintsDictionary; 98 Dictionary mandatoryConstraintsDictionary;
66 bool ok = constraintsDictionary.get(mandatoryName, mandatoryConstraintsD ictionary); 99 bool ok = constraintsDictionary.get(mandatoryName, mandatoryConstraintsD ictionary);
67 if (!ok || mandatoryConstraintsDictionary.isUndefinedOrNull()) 100 if (!ok || mandatoryConstraintsDictionary.isUndefinedOrNull())
68 return false; 101 return false;
69 102 ok = parseMandatoryConstraintsDictionary(mandatoryConstraintsDictionary, mandatory);
70 HashMap<String, String> mandatoryConstraintsHashMap;
71 ok = mandatoryConstraintsDictionary.getOwnPropertiesAsStringHashMap(mand atoryConstraintsHashMap);
72 if (!ok) 103 if (!ok)
73 return false; 104 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 } 105 }
79 106
80 Vector<WebMediaConstraint> optionalConstraintsVector; 107 Vector<WebMediaConstraint> optionalConstraintsVector;
81 if (names.contains(optionalName)) { 108 if (names.contains(optionalName)) {
82 ArrayValue optionalConstraints; 109 ArrayValue optionalConstraints;
83 bool ok = DictionaryHelper::get(constraintsDictionary, optionalName, opt ionalConstraints); 110 bool ok = DictionaryHelper::get(constraintsDictionary, optionalName, opt ionalConstraints);
84 if (!ok || optionalConstraints.isUndefinedOrNull()) 111 if (!ok || optionalConstraints.isUndefinedOrNull())
85 return false; 112 return false;
86 113
87 size_t numberOfConstraints; 114 size_t numberOfConstraints;
88 ok = optionalConstraints.length(numberOfConstraints); 115 ok = optionalConstraints.length(numberOfConstraints);
89 if (!ok) 116 if (!ok)
90 return false; 117 return false;
91 118
92 for (size_t i = 0; i < numberOfConstraints; ++i) { 119 for (size_t i = 0; i < numberOfConstraints; ++i) {
93 Dictionary constraint; 120 Dictionary constraint;
94 ok = optionalConstraints.get(i, constraint); 121 ok = optionalConstraints.get(i, constraint);
95 if (!ok || constraint.isUndefinedOrNull()) 122 if (!ok || constraint.isUndefinedOrNull())
96 return false; 123 return false;
97 Vector<String> localNames; 124 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) 125 if (!ok)
105 return false; 126 return false;
106 optionalConstraintsVector.append(WebMediaConstraint(key, value));
107 } 127 }
128 optional.assign(optionalConstraintsVector);
108 } 129 }
109 130
110 optional.assign(optionalConstraintsVector);
111 mandatory.assign(mandatoryConstraintsVector);
112 return true; 131 return true;
113 } 132 }
114 133
134 static bool parse(const MediaTrackConstraintSet& constraintsIn, WebVector<WebMed iaConstraint>& optional, WebVector<WebMediaConstraint>& mandatory)
135 {
136 Vector<WebMediaConstraint> mandatoryConstraintsVector;
137 if (constraintsIn.hasMandatory()) {
138 bool ok = parseMandatoryConstraintsDictionary(constraintsIn.mandatory(), mandatory);
139 if (!ok)
140 return false;
141 }
142
143 Vector<WebMediaConstraint> optionalConstraintsVector;
144 if (constraintsIn.hasOptional()) {
145 const Vector<Dictionary>& optionalConstraints = constraintsIn.optional() ;
146
147 for (const auto& constraint : optionalConstraints) {
148 if (constraint.isUndefinedOrNull())
149 return false;
150 bool ok = parseOptionalConstraintsVectorElement(constraint, optional ConstraintsVector);
151 if (!ok)
152 return false;
153 }
154 optional.assign(optionalConstraintsVector);
155 }
156 return true;
157 }
158
115 159
116 WebMediaConstraints create(const Dictionary& constraintsDictionary, ExceptionSta te& exceptionState) 160 WebMediaConstraints create(const Dictionary& constraintsDictionary, ExceptionSta te& exceptionState)
117 { 161 {
118 WebVector<WebMediaConstraint> optional; 162 WebVector<WebMediaConstraint> optional;
119 WebVector<WebMediaConstraint> mandatory; 163 WebVector<WebMediaConstraint> mandatory;
120 if (!parse(constraintsDictionary, optional, mandatory)) { 164 if (!parse(constraintsDictionary, optional, mandatory)) {
121 exceptionState.throwTypeError("Malformed constraints object."); 165 exceptionState.throwTypeError("Malformed constraints object.");
122 return WebMediaConstraints(); 166 return WebMediaConstraints();
123 } 167 }
124 168
125 WebMediaConstraints constraints; 169 WebMediaConstraints constraints;
126 constraints.initialize(optional, mandatory); 170 constraints.initialize(optional, mandatory);
127 return constraints; 171 return constraints;
128 } 172 }
129 173
174 WebMediaConstraints create(const MediaTrackConstraintSet& constraintsIn, Excepti onState& exceptionState)
175 {
176 WebVector<WebMediaConstraint> optional;
177 WebVector<WebMediaConstraint> mandatory;
178 if (!parse(constraintsIn, optional, mandatory)) {
179 exceptionState.throwTypeError("Malformed constraints object.");
180 return WebMediaConstraints();
181 }
182 WebMediaConstraints constraints;
183 constraints.initialize(optional, mandatory);
184 return constraints;
185 }
186
130 WebMediaConstraints create() 187 WebMediaConstraints create()
131 { 188 {
132 WebMediaConstraints constraints; 189 WebMediaConstraints constraints;
133 constraints.initialize(); 190 constraints.initialize();
134 return constraints; 191 return constraints;
135 } 192 }
136 193
137 } // namespace MediaConstraintsImpl 194 } // namespace MediaConstraintsImpl
138 } // namespace blink 195 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698