Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / media_stream_constraints_util.cc
blob7846582457a5ea344fbca0521e17209c00052f52
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"
12 namespace content {
14 namespace {
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);
25 } // namespace
27 bool GetConstraintValueAsBoolean(const blink::WebMediaConstraints& constraints,
28 const std::string& name,
29 bool* value) {
30 return GetMandatoryConstraintValueAsBoolean(constraints, name, value) ||
31 GetOptionalConstraintValueAsBoolean(constraints, name, value);
34 bool GetConstraintValueAsInteger(const blink::WebMediaConstraints& constraints,
35 const std::string& name,
36 int* value) {
37 return GetMandatoryConstraintValueAsInteger(constraints, name, value) ||
38 GetOptionalConstraintValueAsInteger(constraints, name, value);
41 bool GetConstraintValueAsDouble(const blink::WebMediaConstraints& constraints,
42 const std::string& name,
43 double* value) {
44 return GetMandatoryConstraintValueAsDouble(constraints, name, value) ||
45 GetOptionalConstraintValueAsDouble(constraints, name, value);
48 bool GetConstraintValueAsString(const blink::WebMediaConstraints& constraints,
49 const std::string& name,
50 std::string* value) {
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)) {
55 return false;
58 *value = value_str.utf8();
59 return true;
62 bool GetMandatoryConstraintValueAsBoolean(
63 const blink::WebMediaConstraints& constraints,
64 const std::string& name,
65 bool* value) {
66 blink::WebString value_str;
67 if (!constraints.getMandatoryConstraintValue(base::UTF8ToUTF16(name),
68 value_str)) {
69 return false;
72 return ConvertStringToBoolean(value_str.utf8(), value);
75 bool GetMandatoryConstraintValueAsInteger(
76 const blink::WebMediaConstraints& constraints,
77 const std::string& name,
78 int* value) {
79 blink::WebString value_str;
80 if (!constraints.getMandatoryConstraintValue(base::UTF8ToUTF16(name),
81 value_str)) {
82 return false;
85 return base::StringToInt(value_str.utf8(), value);
88 bool GetMandatoryConstraintValueAsDouble(
89 const blink::WebMediaConstraints& constraints,
90 const std::string& name,
91 double* value) {
92 blink::WebString value_str;
93 if (!constraints.getMandatoryConstraintValue(base::UTF8ToUTF16(name),
94 value_str)) {
95 return false;
97 return base::StringToDouble(value_str.utf8(), value);
100 bool GetOptionalConstraintValueAsBoolean(
101 const blink::WebMediaConstraints& constraints,
102 const std::string& name,
103 bool* value) {
104 blink::WebString value_str;
105 if (!constraints.getOptionalConstraintValue(base::UTF8ToUTF16(name),
106 value_str)) {
107 return false;
110 return ConvertStringToBoolean(value_str.utf8(), value);
113 bool GetOptionalConstraintValueAsInteger(
114 const blink::WebMediaConstraints& constraints,
115 const std::string& name,
116 int* value) {
117 blink::WebString value_str;
118 if (!constraints.getOptionalConstraintValue(base::UTF8ToUTF16(name),
119 value_str)) {
120 return false;
123 return base::StringToInt(value_str.utf8(), value);
126 bool GetOptionalConstraintValueAsDouble(
127 const blink::WebMediaConstraints& constraints,
128 const std::string& name,
129 double* value) {
130 blink::WebString value_str;
131 if (!constraints.getOptionalConstraintValue(base::UTF8ToUTF16(name),
132 value_str)) {
133 return false;
136 return base::StringToDouble(value_str.utf8(), value);
139 } // namespace content