Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
|
mark a. foltz
2016/01/04 20:11:41
s/2012/2016/
| |
| 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/basictypes.h" | |
| 10 #include "base/strings/string_number_conversions.h" | |
| 11 #include "base/strings/string_piece.h" | |
| 12 #include "base/strings/string_util.h" | |
| 13 | |
| 14 namespace content { | |
| 15 const char kEnableFlag[] = "?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); | |
|
mark a. foltz
2016/01/04 20:11:41
What about kInvalidId?
| |
| 35 } | |
| 36 | |
| 37 std::string WebContentsMediaCaptureId::ToString() const { | |
| 38 std::string s = kWebContentsCaptureScheme; | |
| 39 s.append("://"); | |
| 40 s.append(base::Int64ToString(render_process_id)); | |
| 41 s.append(":"); | |
| 42 s.append(base::Int64ToString(main_render_frame_id)); | |
| 43 | |
| 44 if (enable_auto_throttling) | |
|
mark a. foltz
2016/01/04 20:11:41
This doesn't have anything to do with the target o
| |
| 45 s.append(kEnableFlag); | |
| 46 | |
| 47 return s; | |
| 48 } | |
| 49 | |
| 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 bool WebContentsMediaCaptureId::IsWebContentsDeviceId( | |
| 62 const std::string& device_id) { | |
| 63 int ignored; | |
| 64 return ExtractTabCaptureTarget(device_id, &ignored, &ignored); | |
| 65 } | |
| 66 | |
| 67 bool WebContentsMediaCaptureId::ExtractTabCaptureTarget( | |
|
mark a. foltz
2016/01/04 20:11:41
How is a client supposed to come up with the (rend
| |
| 68 const std::string& device_id_param, | |
| 69 int* render_process_id, | |
| 70 int* main_render_frame_id) { | |
| 71 const std::string device_scheme = | |
| 72 std::string(kWebContentsCaptureScheme) + "://"; | |
| 73 if (!base::StartsWith(device_id_param, device_scheme, | |
| 74 base::CompareCase::SENSITIVE)) | |
| 75 return false; | |
| 76 | |
| 77 const std::string device_id = | |
| 78 device_id_param.substr(device_scheme.size() - 1); | |
| 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 bool WebContentsMediaCaptureId::IsAutoThrottlingOptionSet( | |
| 96 const std::string& device_id) { | |
| 97 if (!IsWebContentsDeviceId(device_id)) | |
| 98 return false; | |
| 99 | |
| 100 // Find the option part of the string and just do a naive string compare since | |
| 101 // there are no other options in the |device_id| to account for (at the time | |
| 102 // of this writing). | |
| 103 const size_t option_pos = device_id.find('?'); | |
| 104 if (option_pos == std::string::npos) | |
| 105 return false; | |
| 106 const base::StringPiece component(device_id.data() + option_pos, | |
| 107 device_id.length() - option_pos); | |
| 108 return component.compare(kEnableFlag) == 0; | |
| 109 } | |
| 110 | |
| 111 } // namespace content | |
| OLD | NEW |