1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * 3. Neither the name of Google Inc. nor the names of its contributors
15 * may be used to endorse or promote products derived from this
16 * software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
29 */
30
31 #include "config.h"
32
33 #include "modules/mediastream/MediaConstraintsImpl.h"
34
35 #include "bindings/v8/ArrayValue.h"
36 #include "bindings/v8/Dictionary.h"
37 #include "bindings/v8/ExceptionState.h"
38 #include "core/dom/ExceptionCode.h"
39 #include "wtf/HashMap.h"
40 #include "wtf/Vector.h"
41 #include "wtf/text/StringHash.h"
42
43 namespace WebCore {
44 namespace MediaConstraintsImpl {
45
parse(const Dictionary & constraintsDictionary,blink::WebVector<blink::WebMediaConstraint> & optional,blink::WebVector<blink::WebMediaConstraint> & mandatory)46 static bool parse(const Dictionary& constraintsDictionary, blink::WebVector<blink::WebMediaConstraint>& optional, blink::WebVector<blink::WebMediaConstraint>& mandatory)
47 {
48 if (constraintsDictionary.isUndefinedOrNull())
49 return true;
50
51 Vector<String> names;
52 constraintsDictionary.getOwnPropertyNames(names);
53
54 String mandatoryName("mandatory");
55 String optionalName("optional");
56
57 for (Vector<String>::iterator it = names.begin(); it != names.end(); ++it) {
58 if (*it != mandatoryName && *it != optionalName)
59 return false;
60 }
61
62 Vector<blink::WebMediaConstraint> mandatoryConstraintsVector;
63 if (names.contains(mandatoryName)) {
64 Dictionary mandatoryConstraintsDictionary;
65 bool ok = constraintsDictionary.get(mandatoryName, mandatoryConstraintsDictionary);
66 if (!ok || mandatoryConstraintsDictionary.isUndefinedOrNull())
67 return false;
68
69 HashMap<String, String> mandatoryConstraintsHashMap;
70 ok = mandatoryConstraintsDictionary.getOwnPropertiesAsStringHashMap(mandatoryConstraintsHashMap);
71 if (!ok)
72 return false;
73
74 HashMap<String, String>::const_iterator iter = mandatoryConstraintsHashMap.begin();
75 for (; iter != mandatoryConstraintsHashMap.end(); ++iter)
76 mandatoryConstraintsVector.append(blink::WebMediaConstraint(iter->key, iter->value));
77 }
78
79 Vector<blink::WebMediaConstraint> optionalConstraintsVector;
80 if (names.contains(optionalName)) {
81 ArrayValue optionalConstraints;
82 bool ok = constraintsDictionary.get(optionalName, optionalConstraints);
83 if (!ok || optionalConstraints.isUndefinedOrNull())
84 return false;
85
86 size_t numberOfConstraints;
87 ok = optionalConstraints.length(numberOfConstraints);
88 if (!ok)
89 return false;
90
91 for (size_t i = 0; i < numberOfConstraints; ++i) {
92 Dictionary constraint;
93 ok = optionalConstraints.get(i, constraint);
94 if (!ok || constraint.isUndefinedOrNull())
95 return false;
96 Vector<String> localNames;
97 constraint.getOwnPropertyNames(localNames);
98 if (localNames.size() != 1)
99 return false;
100 String key = localNames[0];
101 String value;
102 ok = constraint.get(key, value);
103 if (!ok)
104 return false;
105 optionalConstraintsVector.append(blink::WebMediaConstraint(key, value));
106 }
107 }
108
109 optional.assign(optionalConstraintsVector);
110 mandatory.assign(mandatoryConstraintsVector);
111 return true;
112 }
113
114
create(const Dictionary & constraintsDictionary,ExceptionState & exceptionState)115 blink::WebMediaConstraints create(const Dictionary& constraintsDictionary, ExceptionState& exceptionState)
116 {
117 blink::WebVector<blink::WebMediaConstraint> optional;
118 blink::WebVector<blink::WebMediaConstraint> mandatory;
119 if (!parse(constraintsDictionary, optional, mandatory)) {
120 exceptionState.throwTypeError("Malformed constraints object.");
121 return blink::WebMediaConstraints();
122 }
123
124 blink::WebMediaConstraints constraints;
125 constraints.initialize(optional, mandatory);
126 return constraints;
127 }
128
create()129 blink::WebMediaConstraints create()
130 {
131 blink::WebMediaConstraints constraints;
132 constraints.initialize();
133 return constraints;
134 }
135
136 } // namespace MediaConstraintsImpl
137 } // namespace WebCore
138