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 | |
Evan Stade | 92b69ca | 2020-08-21 23:55:58 | [diff] [blame] | 5 | #include "components/media_router/common/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 | |
Peter Kasting | 0116752 | 2021-04-28 19:32:08 | [diff] [blame] | 13 | #include "base/strings/string_piece.h" |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 14 | #include "base/strings/string_util.h" |
| 15 | #include "base/strings/stringprintf.h" |
Evan Stade | 92b69ca | 2020-08-21 23:55:58 | [diff] [blame] | 16 | #include "components/media_router/common/media_source.h" |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 17 | #include "url/gurl.h" |
zhaobin | e1ed8473 | 2016-12-20 23:16:34 | [diff] [blame] | 18 | |
kmarshall | d2f3bea | 2015-03-11 23:42:22 | [diff] [blame] | 19 | namespace media_router { |
| 20 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | // Prefixes used to format and detect various protocols' media source URNs. |
| 24 | // See: https://www.ietf.org/rfc/rfc3406.txt |
Jordan Bayles | 8934646 | 2020-05-26 21:36:12 | [diff] [blame] | 25 | constexpr char kAnyTabMediaUrn[] = "urn:x-org.chromium.media:source:tab:*"; |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 26 | constexpr char kTabMediaUrnFormat[] = "urn:x-org.chromium.media:source:tab:%d"; |
John Williams | 4c35344 | 2019-11-13 23:37:00 | [diff] [blame] | 27 | constexpr base::StringPiece kDesktopMediaUrnPrefix = |
| 28 | "urn:x-org.chromium.media:source:desktop:"; |
Yuri Wiitala | 8524538 | 2020-08-25 03:09:39 | [diff] [blame] | 29 | // WARNING: If more desktop URN parameters are added in the future, the parsing |
| 30 | // code will have to be smarter! |
| 31 | constexpr base::StringPiece kDesktopMediaUrnAudioParam = "?with_audio=true"; |
| 32 | constexpr base::StringPiece kUnchosenDesktopMediaUrn = |
John Williams | 4c35344 | 2019-11-13 23:37:00 | [diff] [blame] | 33 | "urn:x-org.chromium.media:source:desktop"; |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 34 | |
| 35 | // List of non-http(s) schemes that are allowed in a Presentation URL. |
| 36 | constexpr std::array<const char* const, 5> kAllowedSchemes{ |
| 37 | {kCastPresentationUrlScheme, kCastDialPresentationUrlScheme, |
| 38 | kDialPresentationUrlScheme, kRemotePlaybackPresentationUrlScheme, "test"}}; |
| 39 | |
| 40 | bool IsSchemeAllowed(const GURL& url) { |
| 41 | return url.SchemeIsHTTPOrHTTPS() || |
| 42 | std::any_of( |
| 43 | kAllowedSchemes.begin(), kAllowedSchemes.end(), |
| 44 | [&url](const char* const scheme) { return url.SchemeIs(scheme); }); |
| 45 | } |
| 46 | |
| 47 | } // namespace |
| 48 | |
| 49 | bool IsLegacyCastPresentationUrl(const GURL& url) { |
| 50 | return base::StartsWith(url.spec(), kLegacyCastPresentationUrlPrefix, |
| 51 | base::CompareCase::INSENSITIVE_ASCII); |
| 52 | } |
| 53 | |
| 54 | bool IsValidPresentationUrl(const GURL& url) { |
| 55 | return url.is_valid() && IsSchemeAllowed(url); |
| 56 | } |
| 57 | |
Clifford Cheng | 5d6c517 | 2020-11-25 02:05:09 | [diff] [blame] | 58 | bool IsValidStandardPresentationSource(const std::string& media_source) { |
| 59 | const GURL source_url(media_source); |
| 60 | return source_url.is_valid() && source_url.SchemeIsHTTPOrHTTPS() && |
| 61 | !base::StartsWith(source_url.spec(), kLegacyCastPresentationUrlPrefix, |
| 62 | base::CompareCase::INSENSITIVE_ASCII); |
| 63 | } |
| 64 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 65 | bool IsAutoJoinPresentationId(const std::string& presentation_id) { |
| 66 | return presentation_id == kAutoJoinPresentationId; |
| 67 | } |
| 68 | |
| 69 | MediaSource::MediaSource() = default; |
| 70 | |
kmarshall | 5fa0263d | 2015-06-03 16:07:08 | [diff] [blame] | 71 | MediaSource::MediaSource(const MediaSource::Id& source_id) : id_(source_id) { |
zhaobin | e1ed8473 | 2016-12-20 23:16:34 | [diff] [blame] | 72 | GURL url(source_id); |
| 73 | if (IsValidPresentationUrl(url)) |
| 74 | url_ = url; |
imcheng | 2ae6fd3 | 2015-04-10 17:59:18 | [diff] [blame] | 75 | } |
| 76 | |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 77 | MediaSource::MediaSource(const GURL& presentation_url) |
zhaobin | 81cb26bf | 2016-12-07 19:42:55 | [diff] [blame] | 78 | : id_(presentation_url.spec()), url_(presentation_url) {} |
mfoltz | 7a2c823b | 2016-10-08 01:35:24 | [diff] [blame] | 79 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 80 | MediaSource::~MediaSource() = default; |
kmarshall | d2f3bea | 2015-03-11 23:42:22 | [diff] [blame] | 81 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 82 | // static |
John Williams | 479d990 | 2020-06-04 17:09:34 | [diff] [blame] | 83 | MediaSource MediaSource::ForLocalFile() { |
| 84 | // TODO(crbug.com/1090878): Use something more sane here. Fixing this |
| 85 | // requires tracking down other places where tab ID 0 is used to indicate |
| 86 | // local file casting. |
| 87 | // |
| 88 | // This probably isn't a source of bugs in practice, because tab IDs are |
| 89 | // generated by SessionIdGenerator, which appears to only produce positive |
| 90 | // values, but that fact isn't clearly documentated, and other parts of |
| 91 | // Chromium don't seem to rely on it, using -1 as the canonical invalid tab |
| 92 | // ID. |
| 93 | return MediaSource(base::StringPrintf(kTabMediaUrnFormat, 0)); |
| 94 | } |
| 95 | |
| 96 | // static |
Jordan Bayles | 8934646 | 2020-05-26 21:36:12 | [diff] [blame] | 97 | MediaSource MediaSource::ForAnyTab() { |
| 98 | return MediaSource(std::string(kAnyTabMediaUrn)); |
| 99 | } |
| 100 | |
| 101 | // static |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 102 | MediaSource MediaSource::ForTab(int tab_id) { |
John Williams | 479d990 | 2020-06-04 17:09:34 | [diff] [blame] | 103 | // Ideally we shouldn't allow -1 as a tab ID, but in unit tests, a tab ID of |
| 104 | // -1 can show up when this function is called from |
| 105 | // CastHandler::StartObservingForSinks() because SessionTabHelper::IdForTab |
| 106 | // can return -1. |
| 107 | DCHECK_GE(tab_id, -1); |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 108 | return MediaSource(base::StringPrintf(kTabMediaUrnFormat, tab_id)); |
kmarshall | d2f3bea | 2015-03-11 23:42:22 | [diff] [blame] | 109 | } |
| 110 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 111 | // static |
Yuri Wiitala | 8524538 | 2020-08-25 03:09:39 | [diff] [blame] | 112 | MediaSource MediaSource::ForDesktop( |
| 113 | const std::string& registered_desktop_stream_id, |
| 114 | bool with_audio) { |
| 115 | DCHECK(!registered_desktop_stream_id.empty()); |
| 116 | std::string id = |
Peter Kasting | 0116752 | 2021-04-28 19:32:08 | [diff] [blame] | 117 | std::string(kDesktopMediaUrnPrefix) + registered_desktop_stream_id; |
Yuri Wiitala | 8524538 | 2020-08-25 03:09:39 | [diff] [blame] | 118 | if (with_audio) { |
Peter Kasting | 0116752 | 2021-04-28 19:32:08 | [diff] [blame] | 119 | id += std::string(kDesktopMediaUrnAudioParam); |
Yuri Wiitala | 8524538 | 2020-08-25 03:09:39 | [diff] [blame] | 120 | } |
| 121 | return MediaSource(id); |
John Williams | 4c35344 | 2019-11-13 23:37:00 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // static |
Yuri Wiitala | 8524538 | 2020-08-25 03:09:39 | [diff] [blame] | 125 | MediaSource MediaSource::ForUnchosenDesktop() { |
Peter Kasting | 0116752 | 2021-04-28 19:32:08 | [diff] [blame] | 126 | return MediaSource(std::string(kUnchosenDesktopMediaUrn)); |
imcheng | 2ae6fd3 | 2015-04-10 17:59:18 | [diff] [blame] | 127 | } |
| 128 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 129 | // static |
| 130 | MediaSource MediaSource::ForPresentationUrl(const GURL& presentation_url) { |
| 131 | return MediaSource(presentation_url); |
Bin Zhao | 3e0c55d | 2018-03-16 23:40:51 | [diff] [blame] | 132 | } |
| 133 | |
Jordan Bayles | 8934646 | 2020-05-26 21:36:12 | [diff] [blame] | 134 | bool MediaSource::IsTabMirroringSource() const { |
John Williams | 479d990 | 2020-06-04 17:09:34 | [diff] [blame] | 135 | return id() == kAnyTabMediaUrn || TabId() > 0; |
Jordan Bayles | 8934646 | 2020-05-26 21:36:12 | [diff] [blame] | 136 | } |
| 137 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 138 | bool MediaSource::IsDesktopMirroringSource() const { |
Yuri Wiitala | 8524538 | 2020-08-25 03:09:39 | [diff] [blame] | 139 | return id() == kUnchosenDesktopMediaUrn || |
John Williams | 4c35344 | 2019-11-13 23:37:00 | [diff] [blame] | 140 | base::StartsWith(id(), kDesktopMediaUrnPrefix, |
| 141 | base::CompareCase::SENSITIVE); |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 142 | } |
| 143 | |
John Williams | 479d990 | 2020-06-04 17:09:34 | [diff] [blame] | 144 | bool MediaSource::IsLocalFileSource() const { |
| 145 | // TODO(crbug.com/1090878): Keep this method is sync with ForLocalFile(). |
| 146 | return TabId() == 0; |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | bool MediaSource::IsCastPresentationUrl() const { |
| 150 | return url_.SchemeIs(kCastPresentationUrlScheme) || |
| 151 | IsLegacyCastPresentationUrl(url_); |
| 152 | } |
| 153 | |
| 154 | int MediaSource::TabId() const { |
John Williams | 479d990 | 2020-06-04 17:09:34 | [diff] [blame] | 155 | int tab_id = -1; |
| 156 | sscanf(id_.c_str(), kTabMediaUrnFormat, &tab_id); |
| 157 | return tab_id; |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 158 | } |
| 159 | |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 160 | absl::optional<std::string> MediaSource::DesktopStreamId() const { |
John Williams | 4c35344 | 2019-11-13 23:37:00 | [diff] [blame] | 161 | if (base::StartsWith(id_, kDesktopMediaUrnPrefix, |
| 162 | base::CompareCase::SENSITIVE)) { |
Yuri Wiitala | 8524538 | 2020-08-25 03:09:39 | [diff] [blame] | 163 | const auto begin = id_.begin() + kDesktopMediaUrnPrefix.size(); |
| 164 | auto end = id_.end(); |
| 165 | if (base::EndsWith(id_, kDesktopMediaUrnAudioParam, |
| 166 | base::CompareCase::SENSITIVE)) { |
| 167 | end -= kDesktopMediaUrnAudioParam.size(); |
| 168 | } |
| 169 | return std::string(begin, end); |
John Williams | 4c35344 | 2019-11-13 23:37:00 | [diff] [blame] | 170 | } |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame] | 171 | return absl::nullopt; |
John Williams | 4c35344 | 2019-11-13 23:37:00 | [diff] [blame] | 172 | } |
| 173 | |
Yuri Wiitala | 8524538 | 2020-08-25 03:09:39 | [diff] [blame] | 174 | bool MediaSource::IsDesktopSourceWithAudio() const { |
| 175 | return base::StartsWith(id_, kDesktopMediaUrnPrefix, |
| 176 | base::CompareCase::SENSITIVE) && |
| 177 | base::EndsWith(id_, kDesktopMediaUrnAudioParam, |
| 178 | base::CompareCase::SENSITIVE); |
| 179 | } |
| 180 | |
John Williams | d48d028c | 2019-05-23 23:59:12 | [diff] [blame] | 181 | bool MediaSource::IsDialSource() const { |
| 182 | return url_.SchemeIs(kCastDialPresentationUrlScheme); |
| 183 | } |
| 184 | |
| 185 | std::string MediaSource::AppNameFromDialSource() const { |
| 186 | return IsDialSource() ? url_.path() : ""; |
| 187 | } |
imcheng | ab1bf635 | 2017-02-16 03:30:07 | [diff] [blame] | 188 | |
Takumi Fujimoto | 97945157 | 2020-06-08 23:04:58 | [diff] [blame] | 189 | std::string MediaSource::TruncateForLogging(size_t max_length) const { |
| 190 | const std::string origin = url_.GetOrigin().spec(); |
| 191 | if (!origin.empty()) |
| 192 | return origin.substr(0, max_length); |
| 193 | // TODO(takumif): Keep the query string by redacting PII. The query string may |
| 194 | // contain info useful for debugging such as the required capabilities. |
| 195 | const size_t query_start_index = id_.find("?"); |
| 196 | const size_t length = |
| 197 | query_start_index == std::string::npos ? max_length : query_start_index; |
| 198 | return id_.substr(0, length); |
| 199 | } |
| 200 | |
kmarshall | d2f3bea | 2015-03-11 23:42:22 | [diff] [blame] | 201 | } // namespace media_router |