OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/media/router/media_source_helper.h" | 5 #include "chrome/browser/media/router/media_source_helper.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 | 8 |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
11 #include "chrome/browser/media/router/media_source.h" | 11 #include "chrome/browser/media/router/media_source.h" |
12 #include "chrome/browser/sessions/session_tab_helper.h" | 12 #include "chrome/browser/sessions/session_tab_helper.h" |
13 #include "url/gurl.h" | 13 #include "url/gurl.h" |
14 | 14 |
15 namespace media_router { | 15 namespace media_router { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // Prefixes used to format and detect various protocols' media source URNs. | 19 // Prefixes used to format and detect various protocols' media source URNs. |
20 // See: https://www.ietf.org/rfc/rfc3406.txt | 20 // See: https://www.ietf.org/rfc/rfc3406.txt |
21 constexpr char kTabMediaUrnFormat[] = "urn:x-org.chromium.media:source:tab:%d"; | 21 constexpr char kTabMediaUrnFormat[] = "urn:x-org.chromium.media:source:tab:%d"; |
22 constexpr char kDesktopMediaUrn[] = "urn:x-org.chromium.media:source:desktop"; | 22 constexpr char kDesktopMediaUrn[] = "urn:x-org.chromium.media:source:desktop"; |
23 // TODO(mfoltz): Is kCastUrnPrefix still used? | |
imcheng
2016/10/07 00:38:36
Seems not. I only see it and MediaSourceForCastApp
mark a. foltz
2016/10/07 23:14:10
Removed, and updated unittests.
| |
23 constexpr char kCastUrnPrefix[] = "urn:x-com.google.cast:application:"; | 24 constexpr char kCastUrnPrefix[] = "urn:x-com.google.cast:application:"; |
24 constexpr char kTabRemotingUrnFormat[] = | 25 constexpr char kTabRemotingUrnFormat[] = |
25 "urn:x-org.chromium.media:source:tab_content_remoting:%d"; | 26 "urn:x-org.chromium.media:source:tab_content_remoting:%d"; |
26 | 27 |
27 } // namespace | 28 } // namespace |
28 | 29 |
29 MediaSource MediaSourceForTab(int tab_id) { | 30 MediaSource MediaSourceForTab(int tab_id) { |
30 return MediaSource(base::StringPrintf(kTabMediaUrnFormat, tab_id)); | 31 return MediaSource(base::StringPrintf(kTabMediaUrnFormat, tab_id)); |
31 } | 32 } |
32 | 33 |
33 MediaSource MediaSourceForTabContentRemoting(content::WebContents* contents) { | 34 MediaSource MediaSourceForTabContentRemoting(content::WebContents* contents) { |
34 DCHECK(contents); | 35 DCHECK(contents); |
35 return MediaSource(base::StringPrintf(kTabRemotingUrnFormat, | 36 return MediaSource(base::StringPrintf(kTabRemotingUrnFormat, |
36 SessionTabHelper::IdForTab(contents))); | 37 SessionTabHelper::IdForTab(contents))); |
37 } | 38 } |
38 | 39 |
39 MediaSource MediaSourceForDesktop() { | 40 MediaSource MediaSourceForDesktop() { |
40 return MediaSource(std::string(kDesktopMediaUrn)); | 41 return MediaSource(std::string(kDesktopMediaUrn)); |
41 } | 42 } |
42 | 43 |
43 MediaSource MediaSourceForCastApp(const std::string& app_id) { | 44 MediaSource MediaSourceForCastApp(const std::string& app_id) { |
44 return MediaSource(kCastUrnPrefix + app_id); | 45 return MediaSource(kCastUrnPrefix + app_id); |
45 } | 46 } |
46 | 47 |
47 MediaSource MediaSourceForPresentationUrl(const std::string& presentation_url) { | 48 MediaSource MediaSourceForPresentationUrl(const GURL& presentation_url) { |
48 return MediaSource(presentation_url); | 49 return MediaSource(presentation_url); |
49 } | 50 } |
50 | 51 |
51 bool IsDesktopMirroringMediaSource(const MediaSource& source) { | 52 bool IsDesktopMirroringMediaSource(const MediaSource& source) { |
52 return base::StartsWith(source.id(), kDesktopMediaUrn, | 53 return base::StartsWith(source.id(), kDesktopMediaUrn, |
53 base::CompareCase::SENSITIVE); | 54 base::CompareCase::SENSITIVE); |
54 } | 55 } |
55 | 56 |
56 bool IsTabMirroringMediaSource(const MediaSource& source) { | 57 bool IsTabMirroringMediaSource(const MediaSource& source) { |
57 int tab_id; | 58 int tab_id; |
(...skipping 10 matching lines...) Expand all Loading... | |
68 int tab_id; | 69 int tab_id; |
69 if (sscanf(source.id().c_str(), kTabMediaUrnFormat, &tab_id) == 1) | 70 if (sscanf(source.id().c_str(), kTabMediaUrnFormat, &tab_id) == 1) |
70 return tab_id; | 71 return tab_id; |
71 else if (sscanf(source.id().c_str(), kTabRemotingUrnFormat, &tab_id) == 1) | 72 else if (sscanf(source.id().c_str(), kTabRemotingUrnFormat, &tab_id) == 1) |
72 return tab_id; | 73 return tab_id; |
73 else | 74 else |
74 return -1; | 75 return -1; |
75 } | 76 } |
76 | 77 |
77 bool IsValidMediaSource(const MediaSource& source) { | 78 bool IsValidMediaSource(const MediaSource& source) { |
78 return (TabIdFromMediaSource(source) > 0 || | 79 return TabIdFromMediaSource(source) > 0 || |
79 IsDesktopMirroringMediaSource(source) || | 80 IsDesktopMirroringMediaSource(source) || |
80 base::StartsWith(source.id(), kCastUrnPrefix, | 81 base::StartsWith(source.id(), kCastUrnPrefix, |
81 base::CompareCase::SENSITIVE) || | 82 base::CompareCase::SENSITIVE) || |
82 IsValidPresentationUrl(source.id())); | 83 IsValidPresentationUrl(GURL(source.id())); |
83 } | 84 } |
84 | 85 |
85 std::string PresentationUrlFromMediaSource(const MediaSource& source) { | 86 bool IsValidPresentationUrl(const GURL& url) { |
86 return IsValidPresentationUrl(source.id()) ? source.id() : ""; | 87 return url.is_valid() && url.SchemeIsHTTPOrHTTPS(); |
87 } | |
88 | |
89 bool IsValidPresentationUrl(const std::string& url) { | |
90 GURL gurl(url); | |
91 return gurl.is_valid() && gurl.SchemeIsHTTPOrHTTPS(); | |
92 } | 88 } |
93 | 89 |
94 } // namespace media_router | 90 } // namespace media_router |
OLD | NEW |