Chromium Code Reviews| 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 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.
| |
| 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 // Find the option part of the string and just do a naive string compare since | |
| 49 // there are no other options in the |device_id| to account for (at the time | |
| 50 // of this writing). | |
| 51 size_t option_pos = device_id.find(kOptionStart); | |
| 52 if (option_pos == std::string::npos) { | |
| 53 if (auto_throttling) | |
| 54 *auto_throttling = false; | |
| 55 if (disable_local_echo) | |
| 56 *disable_local_echo = false; | |
| 57 return true; | |
| 58 } | |
| 59 | |
| 60 size_t option_pos_end; | |
| 61 while (option_pos < device_id.length()) { | |
| 62 option_pos_end = device_id.find(kOptionSeparator, option_pos + 1); | |
| 63 if (option_pos_end == std::string::npos) | |
| 64 option_pos_end = device_id.length(); | |
| 65 const base::StringPiece component(device_id.data() + option_pos + 1, | |
| 66 option_pos_end - option_pos - 1); | |
| 67 | |
| 68 if (auto_throttling && component.compare(kEnableThrottlingFlag) == 0) | |
| 69 *auto_throttling = true; | |
| 70 if (disable_local_echo && component.compare(kMuteSourceFlag) == 0) | |
| 71 *disable_local_echo = true; | |
| 72 | |
| 73 option_pos = option_pos_end; | |
| 74 } | |
| 75 return true; | |
| 76 } | |
| 77 | |
| 78 } // namespace | |
| 79 | |
| 13 namespace content { | 80 namespace content { |
| 14 const char kWebContentsCaptureScheme[] = "web-contents-media-stream://"; | |
| 15 static char kEnableThrottlingFlag[] = "?throttling=auto"; | |
| 16 | 81 |
| 17 bool WebContentsMediaCaptureId::operator<( | 82 bool WebContentsMediaCaptureId::operator<( |
| 18 const WebContentsMediaCaptureId& other) const { | 83 const WebContentsMediaCaptureId& other) const { |
| 19 return std::tie(render_process_id, main_render_frame_id, | 84 return std::tie(render_process_id, main_render_frame_id, |
| 20 enable_auto_throttling) < | 85 enable_auto_throttling, disable_local_echo) < |
| 21 std::tie(other.render_process_id, other.main_render_frame_id, | 86 std::tie(other.render_process_id, other.main_render_frame_id, |
| 22 other.enable_auto_throttling); | 87 other.enable_auto_throttling, other.disable_local_echo); |
| 23 } | 88 } |
| 24 | 89 |
| 25 bool WebContentsMediaCaptureId::operator==( | 90 bool WebContentsMediaCaptureId::operator==( |
| 26 const WebContentsMediaCaptureId& other) const { | 91 const WebContentsMediaCaptureId& other) const { |
| 27 return std::tie(render_process_id, main_render_frame_id, | 92 return std::tie(render_process_id, main_render_frame_id, |
| 28 enable_auto_throttling) == | 93 enable_auto_throttling, disable_local_echo) == |
| 29 std::tie(other.render_process_id, other.main_render_frame_id, | 94 std::tie(other.render_process_id, other.main_render_frame_id, |
| 30 other.enable_auto_throttling); | 95 other.enable_auto_throttling, other.disable_local_echo); |
| 31 } | 96 } |
| 32 | 97 |
| 33 bool WebContentsMediaCaptureId::is_null() const { | 98 bool WebContentsMediaCaptureId::is_null() const { |
| 34 return (render_process_id < 0) || (main_render_frame_id < 0); | 99 return (render_process_id < 0) || (main_render_frame_id < 0); |
| 35 } | 100 } |
| 36 | 101 |
| 37 std::string WebContentsMediaCaptureId::ToString() const { | 102 std::string WebContentsMediaCaptureId::ToString() const { |
| 38 std::string s = kWebContentsCaptureScheme; | 103 std::string s = kWebContentsCaptureScheme; |
| 39 s.append(base::Int64ToString(render_process_id)); | 104 s.append(base::Int64ToString(render_process_id)); |
| 40 s.append(":"); | 105 s.append(":"); |
| 41 s.append(base::Int64ToString(main_render_frame_id)); | 106 s.append(base::Int64ToString(main_render_frame_id)); |
| 42 | 107 |
| 43 if (enable_auto_throttling) | 108 char connector = kOptionStart; |
| 109 if (enable_auto_throttling) { | |
| 110 s += connector; | |
| 44 s.append(kEnableThrottlingFlag); | 111 s.append(kEnableThrottlingFlag); |
| 112 connector = kOptionSeparator; | |
| 113 } | |
| 114 | |
| 115 if (disable_local_echo) { | |
| 116 s += connector; | |
| 117 s.append(kMuteSourceFlag); | |
| 118 connector = kOptionSeparator; | |
| 119 } | |
| 45 | 120 |
| 46 return s; | 121 return s; |
| 47 } | 122 } |
| 48 | 123 |
| 49 // static | 124 // static |
| 50 WebContentsMediaCaptureId WebContentsMediaCaptureId::Parse( | 125 bool WebContentsMediaCaptureId::Parse(const std::string& str, |
| 51 const std::string& str) { | 126 WebContentsMediaCaptureId* output_id) { |
| 52 int render_process_id; | 127 int render_process_id; |
| 53 int main_render_frame_id; | 128 int main_render_frame_id; |
| 54 if (!ExtractTabCaptureTarget(str, &render_process_id, &main_render_frame_id)) | 129 if (!ExtractTabCaptureTarget(str, &render_process_id, &main_render_frame_id)) |
| 55 return WebContentsMediaCaptureId(); | 130 return false; |
| 56 | 131 |
| 57 return WebContentsMediaCaptureId(render_process_id, main_render_frame_id, | 132 bool auto_throttling, disable_local_echo; |
| 58 IsAutoThrottlingOptionSet(str)); | 133 if (!ExtractOptions(str, &auto_throttling, &disable_local_echo)) |
| 134 return false; | |
| 135 | |
| 136 if (output_id) { | |
| 137 output_id->render_process_id = render_process_id; | |
| 138 output_id->main_render_frame_id = main_render_frame_id; | |
| 139 output_id->enable_auto_throttling = auto_throttling; | |
| 140 output_id->disable_local_echo = disable_local_echo; | |
| 141 } | |
| 142 | |
| 143 return true; | |
| 59 } | 144 } |
| 60 | 145 |
| 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 | 146 |
| 112 } // namespace content | 147 } // namespace content |
| OLD | NEW |