Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser/media/capture/web_contents_capture_util.h" | 5 #include "content/public/browser/web_contents_media_capture_id.h" |
| 6 | |
| 7 #include <tuple> | |
| 6 | 8 |
| 7 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 8 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 9 #include "base/strings/string_piece.h" | 11 #include "base/strings/string_piece.h" |
| 10 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 11 | 13 |
| 12 namespace content { | 14 namespace content { |
| 15 const char kEnableFlag[] = "?throttling=auto"; | |
| 13 | 16 |
| 14 bool WebContentsCaptureUtil::IsWebContentsDeviceId( | 17 bool WebContentsMediaCaptureId::operator<( |
|
miu
2015/12/14 20:21:08
Please account for |enable_auto_throttling| here.
GeorgeZ
2015/12/14 21:50:27
Done.
| |
| 18 const WebContentsMediaCaptureId& other) const { | |
| 19 return std::tie(render_process_id, main_render_frame_id) < | |
| 20 std::tie(other.render_process_id, other.main_render_frame_id); | |
| 21 } | |
| 22 | |
| 23 bool WebContentsMediaCaptureId::operator==( | |
|
miu
2015/12/14 20:21:08
And need to account for |enable_auto_throttling| h
GeorgeZ
2015/12/14 21:50:27
Done.
| |
| 24 const WebContentsMediaCaptureId& other) const { | |
| 25 return std::tie(render_process_id, main_render_frame_id) == | |
| 26 std::tie(other.render_process_id, other.main_render_frame_id); | |
| 27 } | |
| 28 | |
| 29 bool WebContentsMediaCaptureId::is_null() const { | |
| 30 return (render_process_id < 0) || (main_render_frame_id < 0); | |
| 31 } | |
| 32 | |
| 33 std::string WebContentsMediaCaptureId::ToString() const { | |
| 34 std::string s = kTabPrefix; | |
| 35 s.append("://"); | |
| 36 s.append(base::Int64ToString(render_process_id)); | |
| 37 s.append(":"); | |
| 38 s.append(base::Int64ToString(main_render_frame_id)); | |
| 39 | |
| 40 if (enable_auto_throttling) | |
| 41 s.append(kEnableFlag); | |
| 42 | |
| 43 return s; | |
| 44 } | |
| 45 | |
| 46 WebContentsMediaCaptureId WebContentsMediaCaptureId::Parse( | |
| 47 const std::string& str) { | |
| 48 int render_process_id; | |
| 49 int main_render_frame_id; | |
| 50 if (ExtractTabCaptureTarget(str, &render_process_id, &main_render_frame_id)) | |
| 51 return WebContentsMediaCaptureId(render_process_id, main_render_frame_id); | |
|
miu
2015/12/14 20:21:08
Please account for |enable_auto_throttling| here t
GeorgeZ
2015/12/14 21:50:27
Done.
| |
| 52 else | |
| 53 return WebContentsMediaCaptureId(); | |
| 54 } | |
| 55 | |
| 56 bool WebContentsMediaCaptureId::IsWebContentsDeviceId( | |
| 15 const std::string& device_id) { | 57 const std::string& device_id) { |
| 16 int ignored; | 58 int ignored; |
| 17 return ExtractTabCaptureTarget(device_id, &ignored, &ignored); | 59 return ExtractTabCaptureTarget(device_id, &ignored, &ignored); |
| 18 } | 60 } |
| 19 | 61 |
| 20 bool WebContentsCaptureUtil::ExtractTabCaptureTarget( | 62 bool WebContentsMediaCaptureId::ExtractTabCaptureTarget( |
| 21 const std::string& device_id_param, | 63 const std::string& device_id_param, |
| 22 int* render_process_id, | 64 int* render_process_id, |
| 23 int* main_render_frame_id) { | 65 int* main_render_frame_id) { |
| 24 static const char kDeviceScheme[] = "web-contents-media-stream://"; | 66 if (!base::StartsWith(device_id_param, kTabPrefix, |
| 25 if (!base::StartsWith(device_id_param, kDeviceScheme, | |
| 26 base::CompareCase::SENSITIVE)) | 67 base::CompareCase::SENSITIVE)) |
| 27 return false; | 68 return false; |
| 28 | 69 |
| 29 const std::string device_id = device_id_param.substr( | 70 const std::string device_id = |
| 30 arraysize(kDeviceScheme) - 1); | 71 device_id_param.substr(arraysize(kTabPrefix) - 1); |
|
miu
2015/12/14 20:21:08
With the change from kDeviceScheme to kTabPrefix,
GeorgeZ
2015/12/14 21:50:27
Good catch
| |
| 31 | 72 |
| 32 const size_t sep_pos = device_id.find(':'); | 73 const size_t sep_pos = device_id.find(':'); |
| 33 if (sep_pos == std::string::npos) | 74 if (sep_pos == std::string::npos) |
| 34 return false; | 75 return false; |
| 35 | 76 |
| 36 const base::StringPiece component1(device_id.data(), sep_pos); | 77 const base::StringPiece component1(device_id.data(), sep_pos); |
| 37 size_t end_pos = device_id.find('?'); | 78 size_t end_pos = device_id.find('?'); |
| 38 if (end_pos == std::string::npos) | 79 if (end_pos == std::string::npos) |
| 39 end_pos = device_id.length(); | 80 end_pos = device_id.length(); |
| 40 const base::StringPiece component2(device_id.data() + sep_pos + 1, | 81 const base::StringPiece component2(device_id.data() + sep_pos + 1, |
| 41 end_pos - sep_pos - 1); | 82 end_pos - sep_pos - 1); |
| 42 | 83 |
| 43 return (base::StringToInt(component1, render_process_id) && | 84 return (base::StringToInt(component1, render_process_id) && |
| 44 base::StringToInt(component2, main_render_frame_id)); | 85 base::StringToInt(component2, main_render_frame_id)); |
| 45 } | 86 } |
| 46 | 87 |
| 47 bool WebContentsCaptureUtil::IsAutoThrottlingOptionSet( | 88 bool WebContentsMediaCaptureId::IsAutoThrottlingOptionSet( |
| 48 const std::string& device_id) { | 89 const std::string& device_id) { |
| 49 if (!IsWebContentsDeviceId(device_id)) | 90 if (!IsWebContentsDeviceId(device_id)) |
| 50 return false; | 91 return false; |
| 51 | 92 |
| 52 // Find the option part of the string and just do a naive string compare since | 93 // Find the option part of the string and just do a naive string compare since |
| 53 // there are no other options in the |device_id| to account for (at the time | 94 // there are no other options in the |device_id| to account for (at the time |
| 54 // of this writing). | 95 // of this writing). |
| 55 const size_t option_pos = device_id.find('?'); | 96 const size_t option_pos = device_id.find('?'); |
| 56 if (option_pos == std::string::npos) | 97 if (option_pos == std::string::npos) |
| 57 return false; | 98 return false; |
| 58 const base::StringPiece component(device_id.data() + option_pos, | 99 const base::StringPiece component(device_id.data() + option_pos, |
| 59 device_id.length() - option_pos); | 100 device_id.length() - option_pos); |
| 60 static const char kEnableFlag[] = "?throttling=auto"; | |
| 61 return component.compare(kEnableFlag) == 0; | 101 return component.compare(kEnableFlag) == 0; |
| 62 } | 102 } |
| 63 | 103 |
| 64 } // namespace content | 104 } // namespace content |
| OLD | NEW |