OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
Avi (use Gerrit)
2016/01/08 17:11:39
No (c), and it's 2016.
GeorgeZ
2016/01/08 19:39:03
Done.
| |
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 = | |
77 device_id_param.substr(device_scheme.size() - 1); | |
78 | |
79 const size_t sep_pos = device_id.find(':'); | |
80 if (sep_pos == std::string::npos) | |
81 return false; | |
82 | |
83 const base::StringPiece component1(device_id.data(), sep_pos); | |
84 size_t end_pos = device_id.find('?'); | |
85 if (end_pos == std::string::npos) | |
86 end_pos = device_id.length(); | |
87 const base::StringPiece component2(device_id.data() + sep_pos + 1, | |
88 end_pos - sep_pos - 1); | |
89 | |
90 return (base::StringToInt(component1, render_process_id) && | |
91 base::StringToInt(component2, main_render_frame_id)); | |
92 } | |
93 | |
94 bool WebContentsMediaCaptureId::IsAutoThrottlingOptionSet( | |
95 const std::string& device_id) { | |
96 if (!IsWebContentsDeviceId(device_id)) | |
97 return false; | |
98 | |
99 // Find the option part of the string and just do a naive string compare since | |
100 // there are no other options in the |device_id| to account for (at the time | |
101 // of this writing). | |
102 const size_t option_pos = device_id.find('?'); | |
103 if (option_pos == std::string::npos) | |
104 return false; | |
105 const base::StringPiece component(device_id.data() + option_pos, | |
106 device_id.length() - option_pos); | |
107 return component.compare(kEnableFlag) == 0; | |
108 } | |
109 | |
110 } // namespace content | |
OLD | NEW |