| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/public/browser/web_contents_media_capture_id.h" | 5 #include "content/public/browser/web_contents_media_capture_id.h" |
| 6 | 6 |
| 7 #include <tuple> | 7 #include <tuple> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 | 12 |
| 13 namespace { |
| 14 constexpr char kWebContentsCaptureScheme[] = "web-contents-media-stream://"; |
| 15 constexpr char kEnableThrottlingFlag[] = "throttling=auto"; |
| 16 constexpr char kDisableLocalEchoFlag[] = "local_echo=false"; |
| 17 constexpr char kOptionStart = '?'; |
| 18 constexpr char kOptionSeparator = '&'; |
| 19 |
| 20 bool ExtractTabCaptureTarget(const std::string& device_id_param, |
| 21 int* render_process_id, |
| 22 int* main_render_frame_id) { |
| 23 const std::string device_scheme = kWebContentsCaptureScheme; |
| 24 if (!base::StartsWith(device_id_param, device_scheme, |
| 25 base::CompareCase::SENSITIVE)) |
| 26 return false; |
| 27 |
| 28 const std::string device_id = device_id_param.substr(device_scheme.size()); |
| 29 |
| 30 const size_t sep_pos = device_id.find(':'); |
| 31 if (sep_pos == std::string::npos) |
| 32 return false; |
| 33 |
| 34 const base::StringPiece component1(device_id.data(), sep_pos); |
| 35 size_t end_pos = device_id.find('?'); |
| 36 if (end_pos == std::string::npos) |
| 37 end_pos = device_id.length(); |
| 38 const base::StringPiece component2(device_id.data() + sep_pos + 1, |
| 39 end_pos - sep_pos - 1); |
| 40 |
| 41 return (base::StringToInt(component1, render_process_id) && |
| 42 base::StringToInt(component2, main_render_frame_id)); |
| 43 } |
| 44 |
| 45 bool ExtractOptions(const std::string& device_id, |
| 46 bool* auto_throttling, |
| 47 bool* disable_local_echo) { |
| 48 DCHECK(auto_throttling); |
| 49 DCHECK(disable_local_echo); |
| 50 |
| 51 *auto_throttling = false; |
| 52 *disable_local_echo = false; |
| 53 |
| 54 // Find the option part of the string and just do a naive string compare since |
| 55 // there are no other options in the |device_id| to account for (at the time |
| 56 // of this writing). |
| 57 size_t option_pos = device_id.find(kOptionStart); |
| 58 if (option_pos == std::string::npos) |
| 59 return true; |
| 60 |
| 61 size_t option_pos_end; |
| 62 while (option_pos < device_id.length()) { |
| 63 option_pos_end = device_id.find(kOptionSeparator, option_pos + 1); |
| 64 if (option_pos_end == std::string::npos) |
| 65 option_pos_end = device_id.length(); |
| 66 const base::StringPiece component(device_id.data() + option_pos + 1, |
| 67 option_pos_end - option_pos - 1); |
| 68 |
| 69 if (component.compare(kEnableThrottlingFlag) == 0) |
| 70 *auto_throttling = true; |
| 71 else if (component.compare(kDisableLocalEchoFlag) == 0) |
| 72 *disable_local_echo = true; |
| 73 else // Some unknown parameter is specified, and thus this ID is invalid. |
| 74 return false; |
| 75 |
| 76 option_pos = option_pos_end; |
| 77 } |
| 78 return true; |
| 79 } |
| 80 |
| 81 } // namespace |
| 82 |
| 13 namespace content { | 83 namespace content { |
| 14 const char kWebContentsCaptureScheme[] = "web-contents-media-stream://"; | |
| 15 static char kEnableThrottlingFlag[] = "?throttling=auto"; | |
| 16 | 84 |
| 17 bool WebContentsMediaCaptureId::operator<( | 85 bool WebContentsMediaCaptureId::operator<( |
| 18 const WebContentsMediaCaptureId& other) const { | 86 const WebContentsMediaCaptureId& other) const { |
| 19 return std::tie(render_process_id, main_render_frame_id, | 87 return std::tie(render_process_id, main_render_frame_id, |
| 20 enable_auto_throttling) < | 88 enable_auto_throttling, disable_local_echo) < |
| 21 std::tie(other.render_process_id, other.main_render_frame_id, | 89 std::tie(other.render_process_id, other.main_render_frame_id, |
| 22 other.enable_auto_throttling); | 90 other.enable_auto_throttling, other.disable_local_echo); |
| 23 } | 91 } |
| 24 | 92 |
| 25 bool WebContentsMediaCaptureId::operator==( | 93 bool WebContentsMediaCaptureId::operator==( |
| 26 const WebContentsMediaCaptureId& other) const { | 94 const WebContentsMediaCaptureId& other) const { |
| 27 return std::tie(render_process_id, main_render_frame_id, | 95 return std::tie(render_process_id, main_render_frame_id, |
| 28 enable_auto_throttling) == | 96 enable_auto_throttling, disable_local_echo) == |
| 29 std::tie(other.render_process_id, other.main_render_frame_id, | 97 std::tie(other.render_process_id, other.main_render_frame_id, |
| 30 other.enable_auto_throttling); | 98 other.enable_auto_throttling, other.disable_local_echo); |
| 31 } | 99 } |
| 32 | 100 |
| 33 bool WebContentsMediaCaptureId::is_null() const { | 101 bool WebContentsMediaCaptureId::is_null() const { |
| 34 return (render_process_id < 0) || (main_render_frame_id < 0); | 102 return (render_process_id < 0) || (main_render_frame_id < 0); |
| 35 } | 103 } |
| 36 | 104 |
| 37 std::string WebContentsMediaCaptureId::ToString() const { | 105 std::string WebContentsMediaCaptureId::ToString() const { |
| 38 std::string s = kWebContentsCaptureScheme; | 106 std::string s = kWebContentsCaptureScheme; |
| 39 s.append(base::Int64ToString(render_process_id)); | 107 s.append(base::Int64ToString(render_process_id)); |
| 40 s.append(":"); | 108 s.append(":"); |
| 41 s.append(base::Int64ToString(main_render_frame_id)); | 109 s.append(base::Int64ToString(main_render_frame_id)); |
| 42 | 110 |
| 43 if (enable_auto_throttling) | 111 char connector = kOptionStart; |
| 112 if (enable_auto_throttling) { |
| 113 s += connector; |
| 44 s.append(kEnableThrottlingFlag); | 114 s.append(kEnableThrottlingFlag); |
| 115 connector = kOptionSeparator; |
| 116 } |
| 117 |
| 118 if (disable_local_echo) { |
| 119 s += connector; |
| 120 s.append(kDisableLocalEchoFlag); |
| 121 connector = kOptionSeparator; |
| 122 } |
| 45 | 123 |
| 46 return s; | 124 return s; |
| 47 } | 125 } |
| 48 | 126 |
| 49 // static | 127 // static |
| 50 WebContentsMediaCaptureId WebContentsMediaCaptureId::Parse( | 128 bool WebContentsMediaCaptureId::Parse(const std::string& str, |
| 51 const std::string& str) { | 129 WebContentsMediaCaptureId* output_id) { |
| 52 int render_process_id; | 130 int render_process_id; |
| 53 int main_render_frame_id; | 131 int main_render_frame_id; |
| 54 if (!ExtractTabCaptureTarget(str, &render_process_id, &main_render_frame_id)) | 132 if (!ExtractTabCaptureTarget(str, &render_process_id, &main_render_frame_id)) |
| 55 return WebContentsMediaCaptureId(); | 133 return false; |
| 56 | 134 |
| 57 return WebContentsMediaCaptureId(render_process_id, main_render_frame_id, | 135 bool auto_throttling, disable_local_echo; |
| 58 IsAutoThrottlingOptionSet(str)); | 136 if (!ExtractOptions(str, &auto_throttling, &disable_local_echo)) |
| 137 return false; |
| 138 |
| 139 if (output_id) { |
| 140 output_id->render_process_id = render_process_id; |
| 141 output_id->main_render_frame_id = main_render_frame_id; |
| 142 output_id->enable_auto_throttling = auto_throttling; |
| 143 output_id->disable_local_echo = disable_local_echo; |
| 144 } |
| 145 |
| 146 return true; |
| 59 } | 147 } |
| 60 | 148 |
| 61 // static | |
| 62 bool WebContentsMediaCaptureId::IsWebContentsDeviceId( | |
| 63 const std::string& device_id) { | |
| 64 int ignored; | |
| 65 return ExtractTabCaptureTarget(device_id, &ignored, &ignored); | |
| 66 } | |
| 67 | |
| 68 // static | |
| 69 bool WebContentsMediaCaptureId::ExtractTabCaptureTarget( | |
| 70 const std::string& device_id_param, | |
| 71 int* render_process_id, | |
| 72 int* main_render_frame_id) { | |
| 73 const std::string device_scheme = kWebContentsCaptureScheme; | |
| 74 if (!base::StartsWith(device_id_param, device_scheme, | |
| 75 base::CompareCase::SENSITIVE)) | |
| 76 return false; | |
| 77 | |
| 78 const std::string device_id = device_id_param.substr(device_scheme.size()); | |
| 79 | |
| 80 const size_t sep_pos = device_id.find(':'); | |
| 81 if (sep_pos == std::string::npos) | |
| 82 return false; | |
| 83 | |
| 84 const base::StringPiece component1(device_id.data(), sep_pos); | |
| 85 size_t end_pos = device_id.find('?'); | |
| 86 if (end_pos == std::string::npos) | |
| 87 end_pos = device_id.length(); | |
| 88 const base::StringPiece component2(device_id.data() + sep_pos + 1, | |
| 89 end_pos - sep_pos - 1); | |
| 90 | |
| 91 return (base::StringToInt(component1, render_process_id) && | |
| 92 base::StringToInt(component2, main_render_frame_id)); | |
| 93 } | |
| 94 | |
| 95 // static | |
| 96 bool WebContentsMediaCaptureId::IsAutoThrottlingOptionSet( | |
| 97 const std::string& device_id) { | |
| 98 if (!IsWebContentsDeviceId(device_id)) | |
| 99 return false; | |
| 100 | |
| 101 // Find the option part of the string and just do a naive string compare since | |
| 102 // there are no other options in the |device_id| to account for (at the time | |
| 103 // of this writing). | |
| 104 const size_t option_pos = device_id.find('?'); | |
| 105 if (option_pos == std::string::npos) | |
| 106 return false; | |
| 107 const base::StringPiece component(device_id.data() + option_pos, | |
| 108 device_id.length() - option_pos); | |
| 109 return component.compare(kEnableThrottlingFlag) == 0; | |
| 110 } | |
| 111 | 149 |
| 112 } // namespace content | 150 } // namespace content |
| OLD | NEW |