OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/public/browser/web_contents_media_capture_id.h" |
| 6 |
| 7 #include <tuple> |
| 8 |
| 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_piece.h" |
| 11 #include "base/strings/string_util.h" |
| 12 |
| 13 namespace content { |
| 14 const char kWebContentsCaptureScheme[] = "web-contents-media-stream://"; |
| 15 static char kEnableThrottlingFlag[] = "?throttling=auto"; |
| 16 |
| 17 bool WebContentsMediaCaptureId::operator<( |
| 18 const WebContentsMediaCaptureId& other) const { |
| 19 return std::tie(render_process_id, main_render_frame_id, |
| 20 enable_auto_throttling) < |
| 21 std::tie(other.render_process_id, other.main_render_frame_id, |
| 22 other.enable_auto_throttling); |
| 23 } |
| 24 |
| 25 bool WebContentsMediaCaptureId::operator==( |
| 26 const WebContentsMediaCaptureId& other) const { |
| 27 return std::tie(render_process_id, main_render_frame_id, |
| 28 enable_auto_throttling) == |
| 29 std::tie(other.render_process_id, other.main_render_frame_id, |
| 30 other.enable_auto_throttling); |
| 31 } |
| 32 |
| 33 bool WebContentsMediaCaptureId::is_null() const { |
| 34 return (render_process_id < 0) || (main_render_frame_id < 0); |
| 35 } |
| 36 |
| 37 std::string WebContentsMediaCaptureId::ToString() const { |
| 38 std::string s = kWebContentsCaptureScheme; |
| 39 s.append(base::Int64ToString(render_process_id)); |
| 40 s.append(":"); |
| 41 s.append(base::Int64ToString(main_render_frame_id)); |
| 42 |
| 43 if (enable_auto_throttling) |
| 44 s.append(kEnableThrottlingFlag); |
| 45 |
| 46 return s; |
| 47 } |
| 48 |
| 49 // static |
| 50 WebContentsMediaCaptureId WebContentsMediaCaptureId::Parse( |
| 51 const std::string& str) { |
| 52 int render_process_id; |
| 53 int main_render_frame_id; |
| 54 if (!ExtractTabCaptureTarget(str, &render_process_id, &main_render_frame_id)) |
| 55 return WebContentsMediaCaptureId(); |
| 56 |
| 57 return WebContentsMediaCaptureId(render_process_id, main_render_frame_id, |
| 58 IsAutoThrottlingOptionSet(str)); |
| 59 } |
| 60 |
| 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 |
| 112 } // namespace content |
OLD | NEW |