OLD | NEW |
---|---|
(Empty) | |
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 | |
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"; | |
Sergey Ulanov
2016/01/13 19:06:11
Rename this to kEnableThrottlingFlag please.
Sergey Ulanov
2016/01/13 19:06:11
Make this static.
GeorgeZ
2016/01/13 22:29:02
Done.
GeorgeZ
2016/01/13 22:29:02
Done.
| |
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 // static | |
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 // static | |
62 bool WebContentsMediaCaptureId::IsWebContentsDeviceId( | |
63 const std::string& device_id) { | |
64 int ignored; | |
65 return ExtractTabCaptureTarget(device_id, &ignored, &ignored); | |
66 } | |
67 | |
68 // static | |
69 bool WebContentsMediaCaptureId::ExtractTabCaptureTarget( | |
70 const std::string& device_id_param, | |
71 int* render_process_id, | |
72 int* main_render_frame_id) { | |
73 const std::string device_scheme = | |
74 std::string(kWebContentsCaptureScheme) + "://"; | |
75 if (!base::StartsWith(device_id_param, device_scheme, | |
76 base::CompareCase::SENSITIVE)) | |
77 return false; | |
78 | |
79 const std::string device_id = device_id_param.substr(device_scheme.size()); | |
80 | |
81 const size_t sep_pos = device_id.find(':'); | |
82 if (sep_pos == std::string::npos) | |
83 return false; | |
84 | |
85 const base::StringPiece component1(device_id.data(), sep_pos); | |
86 size_t end_pos = device_id.find('?'); | |
87 if (end_pos == std::string::npos) | |
88 end_pos = device_id.length(); | |
89 const base::StringPiece component2(device_id.data() + sep_pos + 1, | |
90 end_pos - sep_pos - 1); | |
91 | |
92 return (base::StringToInt(component1, render_process_id) && | |
93 base::StringToInt(component2, main_render_frame_id)); | |
94 } | |
95 | |
96 // static | |
97 bool WebContentsMediaCaptureId::IsAutoThrottlingOptionSet( | |
98 const std::string& device_id) { | |
99 if (!IsWebContentsDeviceId(device_id)) | |
100 return false; | |
101 | |
102 // Find the option part of the string and just do a naive string compare since | |
103 // there are no other options in the |device_id| to account for (at the time | |
104 // of this writing). | |
105 const size_t option_pos = device_id.find('?'); | |
106 if (option_pos == std::string::npos) | |
107 return false; | |
108 const base::StringPiece component(device_id.data() + option_pos, | |
109 device_id.length() - option_pos); | |
110 return component.compare(kEnableFlag) == 0; | |
111 } | |
112 | |
113 } // namespace content | |
OLD | NEW |