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.
5 #include "chrome/browser/media/router/media_source_helper.h"
7 #include "base/strings/string_util.h"
8 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/media/router/media_source.h"
12 namespace media_router
{
14 // Prefixes used to format and detect various protocols' media source URNs.
15 // See: https://www.ietf.org/rfc/rfc3406.txt
16 const char kTabMediaUrnPrefix
[] = "urn:x-org.chromium.media:source:tab";
17 const char kDesktopMediaUrn
[] = "urn:x-org.chromium.media:source:desktop";
18 const char kCastUrnPrefix
[] = "urn:x-com.google.cast:application:";
20 MediaSource
MediaSourceForTab(int tab_id
) {
21 return MediaSource(base::StringPrintf("%s:%d", kTabMediaUrnPrefix
, tab_id
));
24 MediaSource
MediaSourceForDesktop() {
25 return MediaSource(std::string(kDesktopMediaUrn
));
28 MediaSource
MediaSourceForCastApp(const std::string
& app_id
) {
29 return MediaSource(kCastUrnPrefix
+ app_id
);
32 MediaSource
MediaSourceForPresentationUrl(const std::string
& presentation_url
) {
33 return MediaSource(presentation_url
);
36 bool IsMirroringMediaSource(const MediaSource
& source
) {
37 return base::StartsWith(source
.id(), kDesktopMediaUrn
,
38 base::CompareCase::SENSITIVE
) ||
39 base::StartsWith(source
.id(), kTabMediaUrnPrefix
,
40 base::CompareCase::SENSITIVE
);
43 bool IsValidMediaSource(const MediaSource
& source
) {
44 if (IsMirroringMediaSource(source
) ||
45 base::StartsWith(source
.id(), kCastUrnPrefix
,
46 base::CompareCase::SENSITIVE
)) {
49 GURL
url(source
.id());
50 return url
.is_valid() && url
.SchemeIsHTTPOrHTTPS();
53 std::string
PresentationUrlFromMediaSource(const MediaSource
& source
) {
54 return IsValidPresentationUrl(source
.id()) ? source
.id() : "";
57 bool IsValidPresentationUrl(const std::string
& url
) {
59 return gurl
.is_valid() && gurl
.SchemeIsHTTPOrHTTPS();
62 } // namespace media_router