Chromium Code Reviews| Index: content/public/browser/web_contents_media_capture_id.cc |
| diff --git a/content/public/browser/web_contents_media_capture_id.cc b/content/public/browser/web_contents_media_capture_id.cc |
| index d9b107b922241e12f4ba636042e62134c20a04d1..e40a2aa5dfa6bcd0918269c3767b264e236fa394 100644 |
| --- a/content/public/browser/web_contents_media_capture_id.cc |
| +++ b/content/public/browser/web_contents_media_capture_id.cc |
| @@ -10,24 +10,89 @@ |
| #include "base/strings/string_piece.h" |
| #include "base/strings/string_util.h" |
| +namespace { |
| +constexpr char kWebContentsCaptureScheme[] = "web-contents-media-stream://"; |
| +constexpr char kEnableThrottlingFlag[] = "throttling=auto"; |
| +constexpr char kMuteSourceFlag[] = "disable_local_echo=true"; |
|
miu
2016/09/16 21:22:48
nit: It's fine for the C++ variables to be named |
qiangchen
2016/09/16 22:02:24
Done.
|
| +constexpr char kOptionStart = '?'; |
| +constexpr char kOptionSeparator = '&'; |
| + |
| +bool ExtractTabCaptureTarget(const std::string& device_id_param, |
| + int* render_process_id, |
| + int* main_render_frame_id) { |
| + const std::string device_scheme = kWebContentsCaptureScheme; |
| + if (!base::StartsWith(device_id_param, device_scheme, |
| + base::CompareCase::SENSITIVE)) |
| + return false; |
| + |
| + const std::string device_id = device_id_param.substr(device_scheme.size()); |
| + |
| + const size_t sep_pos = device_id.find(':'); |
| + if (sep_pos == std::string::npos) |
| + return false; |
| + |
| + const base::StringPiece component1(device_id.data(), sep_pos); |
| + size_t end_pos = device_id.find('?'); |
| + if (end_pos == std::string::npos) |
| + end_pos = device_id.length(); |
| + const base::StringPiece component2(device_id.data() + sep_pos + 1, |
| + end_pos - sep_pos - 1); |
| + |
| + return (base::StringToInt(component1, render_process_id) && |
| + base::StringToInt(component2, main_render_frame_id)); |
| +} |
| + |
| +bool ExtractOptions(const std::string& device_id, |
| + bool* auto_throttling, |
| + bool* disable_local_echo) { |
| + // Find the option part of the string and just do a naive string compare since |
| + // there are no other options in the |device_id| to account for (at the time |
| + // of this writing). |
| + size_t option_pos = device_id.find(kOptionStart); |
| + if (option_pos == std::string::npos) { |
| + if (auto_throttling) |
| + *auto_throttling = false; |
| + if (disable_local_echo) |
| + *disable_local_echo = false; |
| + return true; |
| + } |
| + |
| + size_t option_pos_end; |
| + while (option_pos < device_id.length()) { |
| + option_pos_end = device_id.find(kOptionSeparator, option_pos + 1); |
| + if (option_pos_end == std::string::npos) |
| + option_pos_end = device_id.length(); |
| + const base::StringPiece component(device_id.data() + option_pos + 1, |
| + option_pos_end - option_pos - 1); |
| + |
| + if (auto_throttling && component.compare(kEnableThrottlingFlag) == 0) |
| + *auto_throttling = true; |
| + if (disable_local_echo && component.compare(kMuteSourceFlag) == 0) |
| + *disable_local_echo = true; |
| + |
| + option_pos = option_pos_end; |
| + } |
| + return true; |
| +} |
| + |
| +} // namespace |
| + |
| namespace content { |
| -const char kWebContentsCaptureScheme[] = "web-contents-media-stream://"; |
| -static char kEnableThrottlingFlag[] = "?throttling=auto"; |
| bool WebContentsMediaCaptureId::operator<( |
| const WebContentsMediaCaptureId& other) const { |
| return std::tie(render_process_id, main_render_frame_id, |
| - enable_auto_throttling) < |
| + enable_auto_throttling, disable_local_echo) < |
| std::tie(other.render_process_id, other.main_render_frame_id, |
| - other.enable_auto_throttling); |
| + other.enable_auto_throttling, other.disable_local_echo); |
| } |
| bool WebContentsMediaCaptureId::operator==( |
| const WebContentsMediaCaptureId& other) const { |
| return std::tie(render_process_id, main_render_frame_id, |
| - enable_auto_throttling) == |
| + enable_auto_throttling, disable_local_echo) == |
| std::tie(other.render_process_id, other.main_render_frame_id, |
| - other.enable_auto_throttling); |
| + other.enable_auto_throttling, other.disable_local_echo); |
| } |
| bool WebContentsMediaCaptureId::is_null() const { |
| @@ -40,73 +105,43 @@ std::string WebContentsMediaCaptureId::ToString() const { |
| s.append(":"); |
| s.append(base::Int64ToString(main_render_frame_id)); |
| - if (enable_auto_throttling) |
| + char connector = kOptionStart; |
| + if (enable_auto_throttling) { |
| + s += connector; |
| s.append(kEnableThrottlingFlag); |
| + connector = kOptionSeparator; |
| + } |
| + |
| + if (disable_local_echo) { |
| + s += connector; |
| + s.append(kMuteSourceFlag); |
| + connector = kOptionSeparator; |
| + } |
| return s; |
| } |
| // static |
| -WebContentsMediaCaptureId WebContentsMediaCaptureId::Parse( |
| - const std::string& str) { |
| +bool WebContentsMediaCaptureId::Parse(const std::string& str, |
| + WebContentsMediaCaptureId* output_id) { |
| int render_process_id; |
| int main_render_frame_id; |
| if (!ExtractTabCaptureTarget(str, &render_process_id, &main_render_frame_id)) |
| - return WebContentsMediaCaptureId(); |
| - |
| - return WebContentsMediaCaptureId(render_process_id, main_render_frame_id, |
| - IsAutoThrottlingOptionSet(str)); |
| -} |
| - |
| -// static |
| -bool WebContentsMediaCaptureId::IsWebContentsDeviceId( |
| - const std::string& device_id) { |
| - int ignored; |
| - return ExtractTabCaptureTarget(device_id, &ignored, &ignored); |
| -} |
| - |
| -// static |
| -bool WebContentsMediaCaptureId::ExtractTabCaptureTarget( |
| - const std::string& device_id_param, |
| - int* render_process_id, |
| - int* main_render_frame_id) { |
| - const std::string device_scheme = kWebContentsCaptureScheme; |
| - if (!base::StartsWith(device_id_param, device_scheme, |
| - base::CompareCase::SENSITIVE)) |
| return false; |
| - const std::string device_id = device_id_param.substr(device_scheme.size()); |
| - |
| - const size_t sep_pos = device_id.find(':'); |
| - if (sep_pos == std::string::npos) |
| + bool auto_throttling, disable_local_echo; |
| + if (!ExtractOptions(str, &auto_throttling, &disable_local_echo)) |
| return false; |
| - const base::StringPiece component1(device_id.data(), sep_pos); |
| - size_t end_pos = device_id.find('?'); |
| - if (end_pos == std::string::npos) |
| - end_pos = device_id.length(); |
| - const base::StringPiece component2(device_id.data() + sep_pos + 1, |
| - end_pos - sep_pos - 1); |
| + if (output_id) { |
| + output_id->render_process_id = render_process_id; |
| + output_id->main_render_frame_id = main_render_frame_id; |
| + output_id->enable_auto_throttling = auto_throttling; |
| + output_id->disable_local_echo = disable_local_echo; |
| + } |
| - return (base::StringToInt(component1, render_process_id) && |
| - base::StringToInt(component2, main_render_frame_id)); |
| + return true; |
| } |
| -// static |
| -bool WebContentsMediaCaptureId::IsAutoThrottlingOptionSet( |
| - const std::string& device_id) { |
| - if (!IsWebContentsDeviceId(device_id)) |
| - return false; |
| - |
| - // Find the option part of the string and just do a naive string compare since |
| - // there are no other options in the |device_id| to account for (at the time |
| - // of this writing). |
| - const size_t option_pos = device_id.find('?'); |
| - if (option_pos == std::string::npos) |
| - return false; |
| - const base::StringPiece component(device_id.data() + option_pos, |
| - device_id.length() - option_pos); |
| - return component.compare(kEnableThrottlingFlag) == 0; |
| -} |
| } // namespace content |