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 kEnableFlag[] = "?throttling=auto"; |
| 15 |
| 16 bool WebContentsMediaCaptureId::operator<( |
| 17 const WebContentsMediaCaptureId& other) const { |
| 18 return std::tie(render_process_id, main_render_frame_id, |
| 19 enable_auto_throttling) < |
| 20 std::tie(other.render_process_id, other.main_render_frame_id, |
| 21 other.enable_auto_throttling); |
| 22 } |
| 23 |
| 24 bool WebContentsMediaCaptureId::operator==( |
| 25 const WebContentsMediaCaptureId& other) const { |
| 26 return std::tie(render_process_id, main_render_frame_id, |
| 27 enable_auto_throttling) == |
| 28 std::tie(other.render_process_id, other.main_render_frame_id, |
| 29 other.enable_auto_throttling); |
| 30 } |
| 31 |
| 32 bool WebContentsMediaCaptureId::is_null() const { |
| 33 return (render_process_id < 0) || (main_render_frame_id < 0); |
| 34 } |
| 35 |
| 36 std::string WebContentsMediaCaptureId::ToString() const { |
| 37 std::string s = kWebContentsCaptureScheme; |
| 38 s.append("://"); |
| 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(kEnableFlag); |
| 45 |
| 46 return s; |
| 47 } |
| 48 |
| 49 WebContentsMediaCaptureId WebContentsMediaCaptureId::Parse( |
| 50 const std::string& str) { |
| 51 int render_process_id; |
| 52 int main_render_frame_id; |
| 53 if (!ExtractTabCaptureTarget(str, &render_process_id, &main_render_frame_id)) |
| 54 return WebContentsMediaCaptureId(); |
| 55 |
| 56 return WebContentsMediaCaptureId(render_process_id, main_render_frame_id, |
| 57 IsAutoThrottlingOptionSet(str)); |
| 58 } |
| 59 |
| 60 bool WebContentsMediaCaptureId::IsWebContentsDeviceId( |
| 61 const std::string& device_id) { |
| 62 int ignored; |
| 63 return ExtractTabCaptureTarget(device_id, &ignored, &ignored); |
| 64 } |
| 65 |
| 66 bool WebContentsMediaCaptureId::ExtractTabCaptureTarget( |
| 67 const std::string& device_id_param, |
| 68 int* render_process_id, |
| 69 int* main_render_frame_id) { |
| 70 const std::string device_scheme = |
| 71 std::string(kWebContentsCaptureScheme) + "://"; |
| 72 if (!base::StartsWith(device_id_param, device_scheme, |
| 73 base::CompareCase::SENSITIVE)) |
| 74 return false; |
| 75 |
| 76 const std::string device_id = device_id_param.substr(device_scheme.size()); |
| 77 |
| 78 const size_t sep_pos = device_id.find(':'); |
| 79 if (sep_pos == std::string::npos) |
| 80 return false; |
| 81 |
| 82 const base::StringPiece component1(device_id.data(), sep_pos); |
| 83 size_t end_pos = device_id.find('?'); |
| 84 if (end_pos == std::string::npos) |
| 85 end_pos = device_id.length(); |
| 86 const base::StringPiece component2(device_id.data() + sep_pos + 1, |
| 87 end_pos - sep_pos - 1); |
| 88 |
| 89 return (base::StringToInt(component1, render_process_id) && |
| 90 base::StringToInt(component2, main_render_frame_id)); |
| 91 } |
| 92 |
| 93 bool WebContentsMediaCaptureId::IsAutoThrottlingOptionSet( |
| 94 const std::string& device_id) { |
| 95 if (!IsWebContentsDeviceId(device_id)) |
| 96 return false; |
| 97 |
| 98 // Find the option part of the string and just do a naive string compare since |
| 99 // there are no other options in the |device_id| to account for (at the time |
| 100 // of this writing). |
| 101 const size_t option_pos = device_id.find('?'); |
| 102 if (option_pos == std::string::npos) |
| 103 return false; |
| 104 const base::StringPiece component(device_id.data() + option_pos, |
| 105 device_id.length() - option_pos); |
| 106 return component.compare(kEnableFlag) == 0; |
| 107 } |
| 108 |
| 109 } // namespace content |
OLD | NEW |