OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "chrome/browser/media/router/media_source_helper.h" | |
6 | |
7 #include <stdio.h> | |
8 | |
9 #include "base/strings/string_util.h" | |
10 #include "base/strings/stringprintf.h" | |
11 #include "chrome/browser/media/router/media_source.h" | |
12 #include "chrome/browser/sessions/session_tab_helper.h" | |
13 #include "url/gurl.h" | |
14 | |
15 namespace media_router { | |
16 | |
17 namespace { | |
18 | |
19 // Prefixes used to format and detect various protocols' media source URNs. | |
20 // See: https://www.ietf.org/rfc/rfc3406.txt | |
21 constexpr char kTabMediaUrnFormat[] = "urn:x-org.chromium.media:source:tab:%d"; | |
22 constexpr char kDesktopMediaUrn[] = "urn:x-org.chromium.media:source:desktop"; | |
23 constexpr char kTabRemotingUrnFormat[] = | |
24 "urn:x-org.chromium.media:source:tab_content_remoting:%d"; | |
25 constexpr char kCastPresentationUrlDomain[] = "google.com"; | |
26 constexpr char kCastPresentationUrlPath[] = "/cast"; | |
27 | |
28 // This value must be the same as |chrome.cast.AUTO_JOIN_PRESENTATION_ID| in the | |
29 // component extension. | |
30 constexpr char kAutoJoinPresentationId[] = "auto-join"; | |
31 | |
32 } // namespace | |
33 | |
34 MediaSource MediaSourceForTab(int tab_id) { | |
35 return MediaSource(base::StringPrintf(kTabMediaUrnFormat, tab_id)); | |
36 } | |
37 | |
38 MediaSource MediaSourceForTabContentRemoting(content::WebContents* contents) { | |
39 DCHECK(contents); | |
40 return MediaSource(base::StringPrintf(kTabRemotingUrnFormat, | |
41 SessionTabHelper::IdForTab(contents))); | |
42 } | |
43 | |
44 MediaSource MediaSourceForDesktop() { | |
45 return MediaSource(std::string(kDesktopMediaUrn)); | |
46 } | |
47 | |
48 MediaSource MediaSourceForPresentationUrl(const GURL& presentation_url) { | |
49 return MediaSource(presentation_url); | |
50 } | |
51 | |
52 bool IsDesktopMirroringMediaSource(const MediaSource& source) { | |
53 return base::StartsWith(source.id(), kDesktopMediaUrn, | |
54 base::CompareCase::SENSITIVE); | |
55 } | |
56 | |
57 bool IsTabMirroringMediaSource(const MediaSource& source) { | |
58 int tab_id; | |
59 return sscanf(source.id().c_str(), kTabMediaUrnFormat, &tab_id) == 1 && | |
60 tab_id > 0; | |
61 } | |
62 | |
63 bool IsMirroringMediaSource(const MediaSource& source) { | |
64 return IsDesktopMirroringMediaSource(source) || | |
65 IsTabMirroringMediaSource(source); | |
66 } | |
67 | |
68 bool CanConnectToMediaSource(const MediaSource& source) { | |
69 // Compare host, port, scheme, and path prefix for source.url(). | |
70 return source.url().SchemeIs(url::kHttpsScheme) && | |
71 source.url().DomainIs(kCastPresentationUrlDomain) && | |
72 source.url().has_path() && | |
73 source.url().path() == kCastPresentationUrlPath; | |
74 } | |
75 | |
76 int TabIdFromMediaSource(const MediaSource& source) { | |
77 int tab_id; | |
78 if (sscanf(source.id().c_str(), kTabMediaUrnFormat, &tab_id) == 1) | |
79 return tab_id; | |
80 else if (sscanf(source.id().c_str(), kTabRemotingUrnFormat, &tab_id) == 1) | |
81 return tab_id; | |
82 else | |
83 return -1; | |
84 } | |
85 | |
86 bool IsValidMediaSource(const MediaSource& source) { | |
87 return TabIdFromMediaSource(source) > 0 || | |
88 IsDesktopMirroringMediaSource(source) || | |
89 IsValidPresentationUrl(GURL(source.id())); | |
90 } | |
91 | |
92 bool IsValidPresentationUrl(const GURL& url) { | |
93 return url.is_valid() && url.SchemeIsHTTPOrHTTPS(); | |
94 } | |
95 | |
96 bool IsAutoJoinPresentationId(const std::string& presentation_id) { | |
97 return presentation_id == kAutoJoinPresentationId; | |
98 } | |
99 | |
100 } // namespace media_router | |
OLD | NEW |