Chromium Code Reviews| 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 constexpr char kTabRemotingUrnFormat[] = | 23 constexpr char kTabRemotingUrnFormat[] = |
| 24 "urn:x-org.chromium.media:source:tab_content_remoting:%d"; | 24 "urn:x-org.chromium.media:source:tab_content_remoting:%d"; |
| 25 | 25 constexpr char kCastPresentationUrlDomain[] = "google.com"; |
| 26 constexpr char kCastPresentationUrlPath[] = "/cast"; | |
| 26 } // namespace | 27 } // namespace |
| 27 | 28 |
| 28 MediaSource MediaSourceForTab(int tab_id) { | 29 MediaSource MediaSourceForTab(int tab_id) { |
| 29 return MediaSource(base::StringPrintf(kTabMediaUrnFormat, tab_id)); | 30 return MediaSource(base::StringPrintf(kTabMediaUrnFormat, tab_id)); |
| 30 } | 31 } |
| 31 | 32 |
| 32 MediaSource MediaSourceForTabContentRemoting(content::WebContents* contents) { | 33 MediaSource MediaSourceForTabContentRemoting(content::WebContents* contents) { |
| 33 DCHECK(contents); | 34 DCHECK(contents); |
| 34 return MediaSource(base::StringPrintf(kTabRemotingUrnFormat, | 35 return MediaSource(base::StringPrintf(kTabRemotingUrnFormat, |
| 35 SessionTabHelper::IdForTab(contents))); | 36 SessionTabHelper::IdForTab(contents))); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 52 int tab_id; | 53 int tab_id; |
| 53 return sscanf(source.id().c_str(), kTabMediaUrnFormat, &tab_id) == 1 && | 54 return sscanf(source.id().c_str(), kTabMediaUrnFormat, &tab_id) == 1 && |
| 54 tab_id > 0; | 55 tab_id > 0; |
| 55 } | 56 } |
| 56 | 57 |
| 57 bool IsMirroringMediaSource(const MediaSource& source) { | 58 bool IsMirroringMediaSource(const MediaSource& source) { |
| 58 return IsDesktopMirroringMediaSource(source) || | 59 return IsDesktopMirroringMediaSource(source) || |
| 59 IsTabMirroringMediaSource(source); | 60 IsTabMirroringMediaSource(source); |
| 60 } | 61 } |
| 61 | 62 |
| 63 bool CanConnectToMediaSource(const MediaSource& source) { | |
| 64 // compare host, port, scheme, and path prefix for source.url() | |
| 65 if (!source.url().SchemeIs(url::kHttpsScheme) || | |
| 66 !source.url().DomainIs(kCastPresentationUrlDomain) || | |
| 67 (!source.url().has_path() || | |
| 68 source.url().path() != kCastPresentationUrlPath)) | |
| 69 return false; | |
| 70 | |
| 71 return true; | |
|
takumif
2016/12/06 21:00:07
Nit: Isn't this equivalent to
return source.url()
| |
| 72 } | |
| 73 | |
| 62 int TabIdFromMediaSource(const MediaSource& source) { | 74 int TabIdFromMediaSource(const MediaSource& source) { |
| 63 int tab_id; | 75 int tab_id; |
| 64 if (sscanf(source.id().c_str(), kTabMediaUrnFormat, &tab_id) == 1) | 76 if (sscanf(source.id().c_str(), kTabMediaUrnFormat, &tab_id) == 1) |
| 65 return tab_id; | 77 return tab_id; |
| 66 else if (sscanf(source.id().c_str(), kTabRemotingUrnFormat, &tab_id) == 1) | 78 else if (sscanf(source.id().c_str(), kTabRemotingUrnFormat, &tab_id) == 1) |
| 67 return tab_id; | 79 return tab_id; |
| 68 else | 80 else |
| 69 return -1; | 81 return -1; |
| 70 } | 82 } |
| 71 | 83 |
| 72 bool IsValidMediaSource(const MediaSource& source) { | 84 bool IsValidMediaSource(const MediaSource& source) { |
| 73 return TabIdFromMediaSource(source) > 0 || | 85 return TabIdFromMediaSource(source) > 0 || |
| 74 IsDesktopMirroringMediaSource(source) || | 86 IsDesktopMirroringMediaSource(source) || |
| 75 IsValidPresentationUrl(GURL(source.id())); | 87 IsValidPresentationUrl(GURL(source.id())); |
| 76 } | 88 } |
| 77 | 89 |
| 78 bool IsValidPresentationUrl(const GURL& url) { | 90 bool IsValidPresentationUrl(const GURL& url) { |
| 79 return url.is_valid() && url.SchemeIsHTTPOrHTTPS(); | 91 return url.is_valid() && url.SchemeIsHTTPOrHTTPS(); |
| 80 } | 92 } |
| 81 | 93 |
| 82 } // namespace media_router | 94 } // namespace media_router |
| OLD | NEW |