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
3 // found in the LICENSE file.
4
5 #include "content/browser/renderer_host/media/web_contents_capture_util.h"
6
7 #include "base/basictypes.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_piece.h"
10 #include "base/strings/string_util.h"
11
12 namespace {
13
14 const char kVirtualDeviceScheme[] = "virtual-media-stream://";
15
16 } // namespace
17
18 namespace content {
19
AppendWebContentsDeviceScheme(const std::string & device_id)20 std::string WebContentsCaptureUtil::AppendWebContentsDeviceScheme(
21 const std::string& device_id) {
22 return kVirtualDeviceScheme + device_id;
23 }
24
StripWebContentsDeviceScheme(const std::string & device_id)25 std::string WebContentsCaptureUtil::StripWebContentsDeviceScheme(
26 const std::string& device_id) {
27 return (IsWebContentsDeviceId(device_id) ?
28 device_id.substr(arraysize(kVirtualDeviceScheme) - 1) :
29 device_id);
30 }
31
IsWebContentsDeviceId(const std::string & device_id)32 bool WebContentsCaptureUtil::IsWebContentsDeviceId(
33 const std::string& device_id) {
34 return StartsWithASCII(device_id, kVirtualDeviceScheme, true);
35 }
36
ExtractTabCaptureTarget(const std::string & device_id_param,int * render_process_id,int * render_view_id)37 bool WebContentsCaptureUtil::ExtractTabCaptureTarget(
38 const std::string& device_id_param,
39 int* render_process_id,
40 int* render_view_id) {
41 if (!IsWebContentsDeviceId(device_id_param))
42 return false;
43
44 const std::string device_id = device_id_param.substr(
45 arraysize(kVirtualDeviceScheme) - 1);
46
47 const size_t sep_pos = device_id.find(':');
48 if (sep_pos == std::string::npos)
49 return false;
50
51 const base::StringPiece component1(device_id.data(), sep_pos);
52 const base::StringPiece component2(device_id.data() + sep_pos + 1,
53 device_id.length() - sep_pos - 1);
54
55 return (base::StringToInt(component1, render_process_id) &&
56 base::StringToInt(component2, render_view_id));
57 }
58
59 } // namespace content
60