| 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 "base/strings/string_number_conversions.h" |
| 7 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 8 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
| 9 #include "chrome/browser/media/router/media_source.h" | 10 #include "chrome/browser/media/router/media_source.h" |
| 11 #include "chrome/browser/sessions/session_tab_helper.h" |
| 12 #include "chrome/browser/ui/browser.h" |
| 13 #include "chrome/browser/ui/browser_list.h" |
| 14 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 10 #include "url/gurl.h" | 15 #include "url/gurl.h" |
| 11 | 16 |
| 12 namespace media_router { | 17 namespace media_router { |
| 13 | 18 |
| 19 namespace { |
| 20 |
| 14 // Prefixes used to format and detect various protocols' media source URNs. | 21 // Prefixes used to format and detect various protocols' media source URNs. |
| 15 // See: https://www.ietf.org/rfc/rfc3406.txt | 22 // See: https://www.ietf.org/rfc/rfc3406.txt |
| 16 const char kTabMediaUrnPrefix[] = "urn:x-org.chromium.media:source:tab"; | 23 const char kTabMediaUrnPrefix[] = "urn:x-org.chromium.media:source:tab:"; |
| 17 const char kDesktopMediaUrn[] = "urn:x-org.chromium.media:source:desktop"; | 24 const char kDesktopMediaUrn[] = "urn:x-org.chromium.media:source:desktop"; |
| 18 const char kCastUrnPrefix[] = "urn:x-com.google.cast:application:"; | 25 const char kCastUrnPrefix[] = "urn:x-com.google.cast:application:"; |
| 19 | 26 |
| 27 constexpr size_t kTabMediaUrnPrefixLength = sizeof(kTabMediaUrnPrefix) - 1; |
| 28 |
| 29 } // namespace |
| 30 |
| 20 MediaSource MediaSourceForTab(int tab_id) { | 31 MediaSource MediaSourceForTab(int tab_id) { |
| 21 return MediaSource(base::StringPrintf("%s:%d", kTabMediaUrnPrefix, tab_id)); | 32 return MediaSource(base::StringPrintf("%s%d", kTabMediaUrnPrefix, tab_id)); |
| 22 } | 33 } |
| 23 | 34 |
| 24 MediaSource MediaSourceForDesktop() { | 35 MediaSource MediaSourceForDesktop() { |
| 25 return MediaSource(std::string(kDesktopMediaUrn)); | 36 return MediaSource(std::string(kDesktopMediaUrn)); |
| 26 } | 37 } |
| 27 | 38 |
| 28 MediaSource MediaSourceForCastApp(const std::string& app_id) { | 39 MediaSource MediaSourceForCastApp(const std::string& app_id) { |
| 29 return MediaSource(kCastUrnPrefix + app_id); | 40 return MediaSource(kCastUrnPrefix + app_id); |
| 30 } | 41 } |
| 31 | 42 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 51 bool IsValidMediaSource(const MediaSource& source) { | 62 bool IsValidMediaSource(const MediaSource& source) { |
| 52 if (IsMirroringMediaSource(source) || | 63 if (IsMirroringMediaSource(source) || |
| 53 base::StartsWith(source.id(), kCastUrnPrefix, | 64 base::StartsWith(source.id(), kCastUrnPrefix, |
| 54 base::CompareCase::SENSITIVE)) { | 65 base::CompareCase::SENSITIVE)) { |
| 55 return true; | 66 return true; |
| 56 } | 67 } |
| 57 GURL url(source.id()); | 68 GURL url(source.id()); |
| 58 return url.is_valid() && url.SchemeIsHTTPOrHTTPS(); | 69 return url.is_valid() && url.SchemeIsHTTPOrHTTPS(); |
| 59 } | 70 } |
| 60 | 71 |
| 72 content::WebContents* WebContentsFromMediaSource(const MediaSource& source) { |
| 73 if (!IsTabMirroringMediaSource(source)) |
| 74 return nullptr; |
| 75 |
| 76 // Extract SessionTabHelper ID from the URN. |
| 77 int tab_id = 0; |
| 78 if (!base::StringToInt( |
| 79 base::StringPiece(source.id().begin() + kTabMediaUrnPrefixLength, |
| 80 source.id().end()), |
| 81 &tab_id)) { |
| 82 return nullptr; |
| 83 } |
| 84 |
| 85 // Look-up the WebContents instance associated with the |tab_id|. |
| 86 // |
| 87 // TODO(miu): This is broken for WebContentses that are not associated with |
| 88 // Browser tabs (such as the offscreen presentations provided by the |
| 89 // tabCapture API). |
| 90 for (const Browser* target_browser : *BrowserList::GetInstance()) { |
| 91 TabStripModel* const target_tab_strip = target_browser->tab_strip_model(); |
| 92 for (int i = 0; i < target_tab_strip->count(); ++i) { |
| 93 content::WebContents* const contents = |
| 94 target_tab_strip->GetWebContentsAt(i); |
| 95 if (SessionTabHelper::IdForTab(contents) == tab_id) |
| 96 return contents; |
| 97 } |
| 98 } |
| 99 |
| 100 return nullptr; |
| 101 } |
| 102 |
| 61 std::string PresentationUrlFromMediaSource(const MediaSource& source) { | 103 std::string PresentationUrlFromMediaSource(const MediaSource& source) { |
| 62 return IsValidPresentationUrl(source.id()) ? source.id() : ""; | 104 return IsValidPresentationUrl(source.id()) ? source.id() : ""; |
| 63 } | 105 } |
| 64 | 106 |
| 65 bool IsValidPresentationUrl(const std::string& url) { | 107 bool IsValidPresentationUrl(const std::string& url) { |
| 66 GURL gurl(url); | 108 GURL gurl(url); |
| 67 return gurl.is_valid() && gurl.SchemeIsHTTPOrHTTPS(); | 109 return gurl.is_valid() && gurl.SchemeIsHTTPOrHTTPS(); |
| 68 } | 110 } |
| 69 | 111 |
| 70 } // namespace media_router | 112 } // namespace media_router |
| OLD | NEW |