| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/public/browser/web_contents_media_capture_id.h" | 5 #include "content/public/browser/web_contents_media_capture_id.h" |
| 6 | 6 |
| 7 #include <tuple> | 7 #include <tuple> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_piece.h" | 10 #include "base/strings/string_piece.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 | 12 |
| 13 namespace content { | 13 namespace content { |
| 14 const char kWebContentsCaptureScheme[] = "web-contents-media-stream://"; | 14 const char kWebContentsCaptureScheme[] = "web-contents-media-stream://"; |
| 15 static char kEnableThrottlingFlag[] = "?throttling=auto"; | 15 static char kEnableThrottlingFlag[] = "throttling=auto"; |
| 16 static char kMuteSourceFlag[] = "mute_source=true"; |
| 17 static char kOptionStart[] = "?"; |
| 18 static char kOptionSeparator[] = "&"; |
| 16 | 19 |
| 17 bool WebContentsMediaCaptureId::operator<( | 20 bool WebContentsMediaCaptureId::operator<( |
| 18 const WebContentsMediaCaptureId& other) const { | 21 const WebContentsMediaCaptureId& other) const { |
| 19 return std::tie(render_process_id, main_render_frame_id, | 22 return std::tie(render_process_id, main_render_frame_id, |
| 20 enable_auto_throttling) < | 23 enable_auto_throttling, mute_source) < |
| 21 std::tie(other.render_process_id, other.main_render_frame_id, | 24 std::tie(other.render_process_id, other.main_render_frame_id, |
| 22 other.enable_auto_throttling); | 25 other.enable_auto_throttling, other.mute_source); |
| 23 } | 26 } |
| 24 | 27 |
| 25 bool WebContentsMediaCaptureId::operator==( | 28 bool WebContentsMediaCaptureId::operator==( |
| 26 const WebContentsMediaCaptureId& other) const { | 29 const WebContentsMediaCaptureId& other) const { |
| 27 return std::tie(render_process_id, main_render_frame_id, | 30 return std::tie(render_process_id, main_render_frame_id, |
| 28 enable_auto_throttling) == | 31 enable_auto_throttling, mute_source) == |
| 29 std::tie(other.render_process_id, other.main_render_frame_id, | 32 std::tie(other.render_process_id, other.main_render_frame_id, |
| 30 other.enable_auto_throttling); | 33 other.enable_auto_throttling, other.mute_source); |
| 31 } | 34 } |
| 32 | 35 |
| 33 bool WebContentsMediaCaptureId::is_null() const { | 36 bool WebContentsMediaCaptureId::is_null() const { |
| 34 return (render_process_id < 0) || (main_render_frame_id < 0); | 37 return (render_process_id < 0) || (main_render_frame_id < 0); |
| 35 } | 38 } |
| 36 | 39 |
| 37 std::string WebContentsMediaCaptureId::ToString() const { | 40 std::string WebContentsMediaCaptureId::ToString() const { |
| 38 std::string s = kWebContentsCaptureScheme; | 41 std::string s = kWebContentsCaptureScheme; |
| 39 s.append(base::Int64ToString(render_process_id)); | 42 s.append(base::Int64ToString(render_process_id)); |
| 40 s.append(":"); | 43 s.append(":"); |
| 41 s.append(base::Int64ToString(main_render_frame_id)); | 44 s.append(base::Int64ToString(main_render_frame_id)); |
| 42 | 45 |
| 43 if (enable_auto_throttling) | 46 char* connector = kOptionStart; |
| 47 if (enable_auto_throttling) { |
| 48 s.append(connector); |
| 44 s.append(kEnableThrottlingFlag); | 49 s.append(kEnableThrottlingFlag); |
| 50 connector = kOptionSeparator; |
| 51 } |
| 52 |
| 53 if (mute_source) { |
| 54 s.append(connector); |
| 55 s.append(kMuteSourceFlag); |
| 56 connector = kOptionSeparator; |
| 57 } |
| 45 | 58 |
| 46 return s; | 59 return s; |
| 47 } | 60 } |
| 48 | 61 |
| 49 // static | 62 // static |
| 50 WebContentsMediaCaptureId WebContentsMediaCaptureId::Parse( | 63 WebContentsMediaCaptureId WebContentsMediaCaptureId::Parse( |
| 51 const std::string& str) { | 64 const std::string& str) { |
| 52 int render_process_id; | 65 int render_process_id; |
| 53 int main_render_frame_id; | 66 int main_render_frame_id; |
| 54 if (!ExtractTabCaptureTarget(str, &render_process_id, &main_render_frame_id)) | 67 if (!ExtractTabCaptureTarget(str, &render_process_id, &main_render_frame_id)) |
| 55 return WebContentsMediaCaptureId(); | 68 return WebContentsMediaCaptureId(); |
| 56 | 69 |
| 70 bool auto_throttling, mute_source; |
| 71 ExtractOptions(str, &auto_throttling, &mute_source); |
| 57 return WebContentsMediaCaptureId(render_process_id, main_render_frame_id, | 72 return WebContentsMediaCaptureId(render_process_id, main_render_frame_id, |
| 58 IsAutoThrottlingOptionSet(str)); | 73 auto_throttling, mute_source); |
| 59 } | 74 } |
| 60 | 75 |
| 61 // static | 76 // static |
| 62 bool WebContentsMediaCaptureId::IsWebContentsDeviceId( | 77 bool WebContentsMediaCaptureId::IsWebContentsDeviceId( |
| 63 const std::string& device_id) { | 78 const std::string& device_id) { |
| 64 int ignored; | 79 int ignored; |
| 65 return ExtractTabCaptureTarget(device_id, &ignored, &ignored); | 80 return ExtractTabCaptureTarget(device_id, &ignored, &ignored); |
| 66 } | 81 } |
| 67 | 82 |
| 68 // static | 83 // static |
| (...skipping 17 matching lines...) Expand all Loading... |
| 86 if (end_pos == std::string::npos) | 101 if (end_pos == std::string::npos) |
| 87 end_pos = device_id.length(); | 102 end_pos = device_id.length(); |
| 88 const base::StringPiece component2(device_id.data() + sep_pos + 1, | 103 const base::StringPiece component2(device_id.data() + sep_pos + 1, |
| 89 end_pos - sep_pos - 1); | 104 end_pos - sep_pos - 1); |
| 90 | 105 |
| 91 return (base::StringToInt(component1, render_process_id) && | 106 return (base::StringToInt(component1, render_process_id) && |
| 92 base::StringToInt(component2, main_render_frame_id)); | 107 base::StringToInt(component2, main_render_frame_id)); |
| 93 } | 108 } |
| 94 | 109 |
| 95 // static | 110 // static |
| 96 bool WebContentsMediaCaptureId::IsAutoThrottlingOptionSet( | 111 bool WebContentsMediaCaptureId::ExtractOptions(const std::string& device_id, |
| 97 const std::string& device_id) { | 112 bool* auto_throttling, |
| 113 bool* mute_source) { |
| 98 if (!IsWebContentsDeviceId(device_id)) | 114 if (!IsWebContentsDeviceId(device_id)) |
| 99 return false; | 115 return false; |
| 100 | 116 |
| 101 // Find the option part of the string and just do a naive string compare since | 117 // 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 | 118 // there are no other options in the |device_id| to account for (at the time |
| 103 // of this writing). | 119 // of this writing). |
| 104 const size_t option_pos = device_id.find('?'); | 120 size_t option_pos = device_id.find(kOptionStart); |
| 105 if (option_pos == std::string::npos) | 121 if (option_pos == std::string::npos) { |
| 106 return false; | 122 if (auto_throttling) |
| 107 const base::StringPiece component(device_id.data() + option_pos, | 123 *auto_throttling = false; |
| 108 device_id.length() - option_pos); | 124 if (mute_source) |
| 109 return component.compare(kEnableThrottlingFlag) == 0; | 125 *mute_source = false; |
| 126 return true; |
| 127 } |
| 128 |
| 129 size_t option_pos_end; |
| 130 while (option_pos < device_id.length()) { |
| 131 option_pos_end = device_id.find(kOptionSeparator, option_pos + 1); |
| 132 if (option_pos_end == std::string::npos) |
| 133 option_pos_end = device_id.length(); |
| 134 const base::StringPiece component(device_id.data() + option_pos + 1, |
| 135 option_pos_end - option_pos - 1); |
| 136 |
| 137 if (auto_throttling && component.compare(kEnableThrottlingFlag) == 0) |
| 138 *auto_throttling = true; |
| 139 if (mute_source && component.compare(kMuteSourceFlag) == 0) |
| 140 *mute_source = true; |
| 141 |
| 142 option_pos = option_pos_end; |
| 143 } |
| 144 return true; |
| 110 } | 145 } |
| 111 | 146 |
| 112 } // namespace content | 147 } // namespace content |
| OLD | NEW |