Index: content/public/browser/web_contents_media_capture_id.cc |
diff --git a/content/public/browser/web_contents_media_capture_id.cc b/content/public/browser/web_contents_media_capture_id.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..33525b1b40e8745796539ad57284c5a741206764 |
--- /dev/null |
+++ b/content/public/browser/web_contents_media_capture_id.cc |
@@ -0,0 +1,111 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
mark a. foltz
2016/01/04 20:11:41
s/2012/2016/
|
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/public/browser/web_contents_media_capture_id.h" |
+ |
+#include <tuple> |
+ |
+#include "base/basictypes.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_piece.h" |
+#include "base/strings/string_util.h" |
+ |
+namespace content { |
+const char kEnableFlag[] = "?throttling=auto"; |
+ |
+bool WebContentsMediaCaptureId::operator<( |
+ const WebContentsMediaCaptureId& other) const { |
+ return std::tie(render_process_id, main_render_frame_id, |
+ enable_auto_throttling) < |
+ std::tie(other.render_process_id, other.main_render_frame_id, |
+ other.enable_auto_throttling); |
+} |
+ |
+bool WebContentsMediaCaptureId::operator==( |
+ const WebContentsMediaCaptureId& other) const { |
+ return std::tie(render_process_id, main_render_frame_id, |
+ enable_auto_throttling) == |
+ std::tie(other.render_process_id, other.main_render_frame_id, |
+ other.enable_auto_throttling); |
+} |
+ |
+bool WebContentsMediaCaptureId::is_null() const { |
+ return (render_process_id < 0) || (main_render_frame_id < 0); |
mark a. foltz
2016/01/04 20:11:41
What about kInvalidId?
|
+} |
+ |
+std::string WebContentsMediaCaptureId::ToString() const { |
+ std::string s = kWebContentsCaptureScheme; |
+ s.append("://"); |
+ s.append(base::Int64ToString(render_process_id)); |
+ s.append(":"); |
+ s.append(base::Int64ToString(main_render_frame_id)); |
+ |
+ if (enable_auto_throttling) |
mark a. foltz
2016/01/04 20:11:41
This doesn't have anything to do with the target o
|
+ s.append(kEnableFlag); |
+ |
+ return s; |
+} |
+ |
+WebContentsMediaCaptureId WebContentsMediaCaptureId::Parse( |
+ const std::string& str) { |
+ int render_process_id; |
+ int main_render_frame_id; |
+ if (!ExtractTabCaptureTarget(str, &render_process_id, &main_render_frame_id)) |
+ return WebContentsMediaCaptureId(); |
+ |
+ return WebContentsMediaCaptureId(render_process_id, main_render_frame_id, |
+ IsAutoThrottlingOptionSet(str)); |
+} |
+ |
+bool WebContentsMediaCaptureId::IsWebContentsDeviceId( |
+ const std::string& device_id) { |
+ int ignored; |
+ return ExtractTabCaptureTarget(device_id, &ignored, &ignored); |
+} |
+ |
+bool WebContentsMediaCaptureId::ExtractTabCaptureTarget( |
mark a. foltz
2016/01/04 20:11:41
How is a client supposed to come up with the (rend
|
+ const std::string& device_id_param, |
+ int* render_process_id, |
+ int* main_render_frame_id) { |
+ const std::string device_scheme = |
+ std::string(kWebContentsCaptureScheme) + "://"; |
+ if (!base::StartsWith(device_id_param, device_scheme, |
+ base::CompareCase::SENSITIVE)) |
+ return false; |
+ |
+ const std::string device_id = |
+ device_id_param.substr(device_scheme.size() - 1); |
+ |
+ const size_t sep_pos = device_id.find(':'); |
+ if (sep_pos == std::string::npos) |
+ return false; |
+ |
+ const base::StringPiece component1(device_id.data(), sep_pos); |
+ size_t end_pos = device_id.find('?'); |
+ if (end_pos == std::string::npos) |
+ end_pos = device_id.length(); |
+ const base::StringPiece component2(device_id.data() + sep_pos + 1, |
+ end_pos - sep_pos - 1); |
+ |
+ return (base::StringToInt(component1, render_process_id) && |
+ base::StringToInt(component2, main_render_frame_id)); |
+} |
+ |
+bool WebContentsMediaCaptureId::IsAutoThrottlingOptionSet( |
+ const std::string& device_id) { |
+ if (!IsWebContentsDeviceId(device_id)) |
+ return false; |
+ |
+ // Find the option part of the string and just do a naive string compare since |
+ // there are no other options in the |device_id| to account for (at the time |
+ // of this writing). |
+ const size_t option_pos = device_id.find('?'); |
+ if (option_pos == std::string::npos) |
+ return false; |
+ const base::StringPiece component(device_id.data() + option_pos, |
+ device_id.length() - option_pos); |
+ return component.compare(kEnableFlag) == 0; |
+} |
+ |
+} // namespace content |