kmarshall | d2f3bea | 2015-03-11 23:42:22 | [diff] [blame] | 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 | |
takumif | a34e5193 | 2017-04-19 20:38:00 | [diff] [blame] | 5 | #include "chrome/common/media_router/media_source.h" |
kmarshall | d2f3bea | 2015-03-11 23:42:22 | [diff] [blame] | 6 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | #include <array> |
| 9 | #include <cstdio> |
John Williams | e955a2481 | 2019-01-19 01:35:00 | [diff] [blame] | 10 | #include <ostream> |
kmarshall | d2f3bea | 2015-03-11 23:42:22 | [diff] [blame] | 11 | #include <string> |
| 12 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
| 14 | #include "base/strings/stringprintf.h" |
| 15 | #include "chrome/common/media_router/media_source.h" |
| 16 | #include "url/gurl.h" |
zhaobin | e1ed8473 | 2016-12-20 23:16:34 | [diff] [blame] | 17 | |
kmarshall | d2f3bea | 2015-03-11 23:42:22 | [diff] [blame] | 18 | namespace media_router { |
| 19 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | // Prefixes used to format and detect various protocols' media source URNs. |
| 23 | // See: https://www.ietf.org/rfc/rfc3406.txt |
| 24 | constexpr char kTabMediaUrnFormat[] = "urn:x-org.chromium.media:source:tab:%d"; |
John Williams | 4c35344 | 2019-11-13 23:37:00 | [diff] [blame^] | 25 | constexpr base::StringPiece kDesktopMediaUrnPrefix = |
| 26 | "urn:x-org.chromium.media:source:desktop:"; |
| 27 | constexpr base::StringPiece kUnknownDesktopMediaUrn = |
| 28 | "urn:x-org.chromium.media:source:desktop"; |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 29 | constexpr char kTabRemotingUrnFormat[] = |
| 30 | "urn:x-org.chromium.media:source:tab_content_remoting:%d"; |
| 31 | |
| 32 | // List of non-http(s) schemes that are allowed in a Presentation URL. |
| 33 | constexpr std::array<const char* const, 5> kAllowedSchemes{ |
| 34 | {kCastPresentationUrlScheme, kCastDialPresentationUrlScheme, |
| 35 | kDialPresentationUrlScheme, kRemotePlaybackPresentationUrlScheme, "test"}}; |
| 36 | |
| 37 | bool IsSchemeAllowed(const GURL& url) { |
| 38 | return url.SchemeIsHTTPOrHTTPS() || |
| 39 | std::any_of( |
| 40 | kAllowedSchemes.begin(), kAllowedSchemes.end(), |
| 41 | [&url](const char* const scheme) { return url.SchemeIs(scheme); }); |
| 42 | } |
| 43 | |
| 44 | } // namespace |
| 45 | |
| 46 | bool IsLegacyCastPresentationUrl(const GURL& url) { |
| 47 | return base::StartsWith(url.spec(), kLegacyCastPresentationUrlPrefix, |
| 48 | base::CompareCase::INSENSITIVE_ASCII); |
| 49 | } |
| 50 | |
| 51 | bool IsValidPresentationUrl(const GURL& url) { |
| 52 | return url.is_valid() && IsSchemeAllowed(url); |
| 53 | } |
| 54 | |
| 55 | bool IsAutoJoinPresentationId(const std::string& presentation_id) { |
| 56 | return presentation_id == kAutoJoinPresentationId; |
| 57 | } |
| 58 | |
| 59 | MediaSource::MediaSource() = default; |
| 60 | |
kmarshall | 5fa0263d | 2015-06-03 16:07:08 | [diff] [blame] | 61 | MediaSource::MediaSource(const MediaSource::Id& source_id) : id_(source_id) { |
zhaobin | e1ed8473 | 2016-12-20 23:16:34 | [diff] [blame] | 62 | GURL url(source_id); |
| 63 | if (IsValidPresentationUrl(url)) |
| 64 | url_ = url; |
imcheng | 2ae6fd3 | 2015-04-10 17:59:18 | [diff] [blame] | 65 | } |
| 66 | |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 67 | MediaSource::MediaSource(const GURL& presentation_url) |
zhaobin | 81cb26bf | 2016-12-07 19:42:55 | [diff] [blame] | 68 | : id_(presentation_url.spec()), url_(presentation_url) {} |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 69 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 70 | MediaSource::~MediaSource() = default; |
kmarshall | d2f3bea | 2015-03-11 23:42:22 | [diff] [blame] | 71 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 72 | // static |
| 73 | MediaSource MediaSource::ForTab(int tab_id) { |
| 74 | return MediaSource(base::StringPrintf(kTabMediaUrnFormat, tab_id)); |
kmarshall | d2f3bea | 2015-03-11 23:42:22 | [diff] [blame] | 75 | } |
| 76 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 77 | // static |
| 78 | MediaSource MediaSource::ForTabContentRemoting(int tab_id) { |
| 79 | return MediaSource(base::StringPrintf(kTabRemotingUrnFormat, tab_id)); |
zhaobin | 81cb26bf | 2016-12-07 19:42:55 | [diff] [blame] | 80 | } |
| 81 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 82 | // static |
John Williams | 4c35344 | 2019-11-13 23:37:00 | [diff] [blame^] | 83 | MediaSource MediaSource::ForDesktop(const std::string& desktop_media_id) { |
| 84 | return MediaSource(kDesktopMediaUrnPrefix.as_string() + desktop_media_id); |
| 85 | } |
| 86 | |
| 87 | // static |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 88 | MediaSource MediaSource::ForDesktop() { |
John Williams | 4c35344 | 2019-11-13 23:37:00 | [diff] [blame^] | 89 | return MediaSource(kUnknownDesktopMediaUrn.as_string()); |
imcheng | 2ae6fd3 | 2015-04-10 17:59:18 | [diff] [blame] | 90 | } |
| 91 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 92 | // static |
| 93 | MediaSource MediaSource::ForPresentationUrl(const GURL& presentation_url) { |
| 94 | return MediaSource(presentation_url); |
Bin Zhao | 3e0c55d | 2018-03-16 23:40:51 | [diff] [blame] | 95 | } |
| 96 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 97 | bool MediaSource::IsDesktopMirroringSource() const { |
John Williams | 4c35344 | 2019-11-13 23:37:00 | [diff] [blame^] | 98 | return id() == kUnknownDesktopMediaUrn || |
| 99 | base::StartsWith(id(), kDesktopMediaUrnPrefix, |
| 100 | base::CompareCase::SENSITIVE); |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | bool MediaSource::IsTabMirroringSource() const { |
| 104 | int tab_id; |
| 105 | return std::sscanf(id_.c_str(), kTabMediaUrnFormat, &tab_id) == 1 && |
| 106 | tab_id > 0; |
| 107 | } |
| 108 | |
| 109 | bool MediaSource::IsMirroringSource() const { |
| 110 | return IsDesktopMirroringSource() || IsTabMirroringSource(); |
| 111 | } |
| 112 | |
| 113 | bool MediaSource::IsCastPresentationUrl() const { |
| 114 | return url_.SchemeIs(kCastPresentationUrlScheme) || |
| 115 | IsLegacyCastPresentationUrl(url_); |
| 116 | } |
| 117 | |
| 118 | int MediaSource::TabId() const { |
| 119 | int tab_id; |
| 120 | if (sscanf(id_.c_str(), kTabMediaUrnFormat, &tab_id) == 1) |
| 121 | return tab_id; |
| 122 | else if (sscanf(id_.c_str(), kTabRemotingUrnFormat, &tab_id) == 1) |
| 123 | return tab_id; |
| 124 | else |
| 125 | return -1; |
| 126 | } |
| 127 | |
John Williams | 4c35344 | 2019-11-13 23:37:00 | [diff] [blame^] | 128 | base::Optional<std::string> MediaSource::DesktopStreamId() const { |
| 129 | if (base::StartsWith(id_, kDesktopMediaUrnPrefix, |
| 130 | base::CompareCase::SENSITIVE)) { |
| 131 | return std::string(id_.begin() + kDesktopMediaUrnPrefix.size(), id_.end()); |
| 132 | } |
| 133 | return base::nullopt; |
| 134 | } |
| 135 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 136 | bool MediaSource::IsValid() const { |
| 137 | return TabId() > 0 || IsDesktopMirroringSource() || |
| 138 | IsValidPresentationUrl(GURL(id_)); |
| 139 | } |
| 140 | |
| 141 | bool MediaSource::IsDialSource() const { |
| 142 | return url_.SchemeIs(kCastDialPresentationUrlScheme); |
| 143 | } |
| 144 | |
| 145 | std::string MediaSource::AppNameFromDialSource() const { |
| 146 | return IsDialSource() ? url_.path() : ""; |
| 147 | } |
imcheng | ab1bf635 | 2017-02-16 03:30:07 | [diff] [blame] | 148 | |
kmarshall | d2f3bea | 2015-03-11 23:42:22 | [diff] [blame] | 149 | } // namespace media_router |