1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/renderer/media/media_stream_constraints_util.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
10 #include "third_party/WebKit/public/platform/WebString.h"
16 // Convert a string ("true", "false") to a boolean.
17 bool ConvertStringToBoolean(const std::string
& string
, bool* value
) {
18 static const char kValueTrue
[] = "true";
19 static const char kValueFalse
[] = "false";
21 *value
= (string
== kValueTrue
);
22 return *value
|| (string
== kValueFalse
);
27 bool GetConstraintValueAsBoolean(const blink::WebMediaConstraints
& constraints
,
28 const std::string
& name
,
30 return GetMandatoryConstraintValueAsBoolean(constraints
, name
, value
) ||
31 GetOptionalConstraintValueAsBoolean(constraints
, name
, value
);
34 bool GetConstraintValueAsInteger(const blink::WebMediaConstraints
& constraints
,
35 const std::string
& name
,
37 return GetMandatoryConstraintValueAsInteger(constraints
, name
, value
) ||
38 GetOptionalConstraintValueAsInteger(constraints
, name
, value
);
41 bool GetConstraintValueAsDouble(const blink::WebMediaConstraints
& constraints
,
42 const std::string
& name
,
44 return GetMandatoryConstraintValueAsDouble(constraints
, name
, value
) ||
45 GetOptionalConstraintValueAsDouble(constraints
, name
, value
);
48 bool GetConstraintValueAsString(const blink::WebMediaConstraints
& constraints
,
49 const std::string
& name
,
51 blink::WebString value_str
;
52 base::string16 name_16
= base::UTF8ToUTF16(name
);
53 if (!constraints
.getMandatoryConstraintValue(name_16
, value_str
) &&
54 !constraints
.getOptionalConstraintValue(name_16
, value_str
)) {
58 *value
= value_str
.utf8();
62 bool GetMandatoryConstraintValueAsBoolean(
63 const blink::WebMediaConstraints
& constraints
,
64 const std::string
& name
,
66 blink::WebString value_str
;
67 if (!constraints
.getMandatoryConstraintValue(base::UTF8ToUTF16(name
),
72 return ConvertStringToBoolean(value_str
.utf8(), value
);
75 bool GetMandatoryConstraintValueAsInteger(
76 const blink::WebMediaConstraints
& constraints
,
77 const std::string
& name
,
79 blink::WebString value_str
;
80 if (!constraints
.getMandatoryConstraintValue(base::UTF8ToUTF16(name
),
85 return base::StringToInt(value_str
.utf8(), value
);
88 bool GetMandatoryConstraintValueAsDouble(
89 const blink::WebMediaConstraints
& constraints
,
90 const std::string
& name
,
92 blink::WebString value_str
;
93 if (!constraints
.getMandatoryConstraintValue(base::UTF8ToUTF16(name
),
97 return base::StringToDouble(value_str
.utf8(), value
);
100 bool GetOptionalConstraintValueAsBoolean(
101 const blink::WebMediaConstraints
& constraints
,
102 const std::string
& name
,
104 blink::WebString value_str
;
105 if (!constraints
.getOptionalConstraintValue(base::UTF8ToUTF16(name
),
110 return ConvertStringToBoolean(value_str
.utf8(), value
);
113 bool GetOptionalConstraintValueAsInteger(
114 const blink::WebMediaConstraints
& constraints
,
115 const std::string
& name
,
117 blink::WebString value_str
;
118 if (!constraints
.getOptionalConstraintValue(base::UTF8ToUTF16(name
),
123 return base::StringToInt(value_str
.utf8(), value
);
126 bool GetOptionalConstraintValueAsDouble(
127 const blink::WebMediaConstraints
& constraints
,
128 const std::string
& name
,
130 blink::WebString value_str
;
131 if (!constraints
.getOptionalConstraintValue(base::UTF8ToUTF16(name
),
136 return base::StringToDouble(value_str
.utf8(), value
);
139 } // namespace content