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. 4 5 #include "content/renderer/media/media_stream_constraints_util.h" 6 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" 11 12 namespace content { 13 14 namespace { 15 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"; 20 21 *value = (string == kValueTrue); 22 return *value || (string == kValueFalse); 23 } 24 25 } // namespace 26 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); 32 } 33 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); 39 } 40 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); 46 } 47 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; 56 } 57 58 *value = value_str.utf8(); 59 return true; 60 } 61 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; 70 } 71 72 return ConvertStringToBoolean(value_str.utf8(), value); 73 } 74 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; 83 } 84 85 return base::StringToInt(value_str.utf8(), value); 86 } 87 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; 96 } 97 return base::StringToDouble(value_str.utf8(), value); 98 } 99 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; 108 } 109 110 return ConvertStringToBoolean(value_str.utf8(), value); 111 } 112 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; 121 } 122 123 return base::StringToInt(value_str.utf8(), value); 124 } 125 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; 134 } 135 136 return base::StringToDouble(value_str.utf8(), value); 137 } 138 139 } // namespace content 140