| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/cast_remoting_connector.h" | 5 #include "chrome/browser/media/cast_remoting_connector.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "content/public/browser/render_process_host.h" | 22 #include "content/public/browser/render_process_host.h" |
| 23 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 24 #include "mojo/public/cpp/bindings/strong_binding.h" | 24 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 25 | 25 |
| 26 using content::BrowserThread; | 26 using content::BrowserThread; |
| 27 using media::mojom::RemotingStartFailReason; | 27 using media::mojom::RemotingStartFailReason; |
| 28 using media::mojom::RemotingStopReason; | 28 using media::mojom::RemotingStopReason; |
| 29 | 29 |
| 30 using Messaging = CastRemotingConnectorMessaging; | 30 using Messaging = CastRemotingConnectorMessaging; |
| 31 | 31 |
| 32 class CastRemotingConnector::FrameRemoterFactory | |
| 33 : public media::mojom::RemoterFactory { | |
| 34 public: | |
| 35 // |render_frame_host| represents the source render frame. It could be | |
| 36 // destroyed at any time and so the process/routing IDs are used as a weak | |
| 37 // reference. | |
| 38 explicit FrameRemoterFactory(content::RenderFrameHost* render_frame_host) | |
| 39 : render_frame_process_id_(render_frame_host->GetProcess()->GetID()), | |
| 40 render_frame_routing_id_(render_frame_host->GetRoutingID()) {} | |
| 41 ~FrameRemoterFactory() final {} | |
| 42 | |
| 43 void Create(media::mojom::RemotingSourcePtr source, | |
| 44 media::mojom::RemoterRequest request) final { | |
| 45 auto* const host = content::RenderFrameHost::FromID( | |
| 46 render_frame_process_id_, render_frame_routing_id_); | |
| 47 if (!host) | |
| 48 return; | |
| 49 auto* const contents = content::WebContents::FromRenderFrameHost(host); | |
| 50 if (!contents) | |
| 51 return; | |
| 52 CastRemotingConnector::Get(contents)->CreateBridge(std::move(source), | |
| 53 std::move(request)); | |
| 54 } | |
| 55 | |
| 56 private: | |
| 57 const int render_frame_process_id_; | |
| 58 const int render_frame_routing_id_; | |
| 59 | |
| 60 DISALLOW_COPY_AND_ASSIGN(FrameRemoterFactory); | |
| 61 }; | |
| 62 | |
| 63 class CastRemotingConnector::RemotingBridge : public media::mojom::Remoter { | 32 class CastRemotingConnector::RemotingBridge : public media::mojom::Remoter { |
| 64 public: | 33 public: |
| 65 // Constructs a "bridge" to delegate calls between the given |source| and | 34 // Constructs a "bridge" to delegate calls between the given |source| and |
| 66 // |connector|. |connector| must be valid at the time of construction, but is | 35 // |connector|. |connector| must be valid at the time of construction, but is |
| 67 // otherwise a weak pointer that can become invalid during the lifetime of a | 36 // otherwise a weak pointer that can become invalid during the lifetime of a |
| 68 // RemotingBridge. | 37 // RemotingBridge. |
| 69 RemotingBridge(media::mojom::RemotingSourcePtr source, | 38 RemotingBridge(media::mojom::RemotingSourcePtr source, |
| 70 CastRemotingConnector* connector) | 39 CastRemotingConnector* connector) |
| 71 : source_(std::move(source)), connector_(connector) { | 40 : source_(std::move(source)), connector_(connector) { |
| 72 DCHECK(connector_); | 41 DCHECK(connector_); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 connector = new CastRemotingConnector( | 138 connector = new CastRemotingConnector( |
| 170 media_router::MediaRouterFactory::GetApiForBrowserContext( | 139 media_router::MediaRouterFactory::GetApiForBrowserContext( |
| 171 contents->GetBrowserContext()), | 140 contents->GetBrowserContext()), |
| 172 media_router::MediaSourceForTabContentRemoting(contents).id()); | 141 media_router::MediaSourceForTabContentRemoting(contents).id()); |
| 173 contents->SetUserData(kUserDataKey, connector); | 142 contents->SetUserData(kUserDataKey, connector); |
| 174 } | 143 } |
| 175 return connector; | 144 return connector; |
| 176 } | 145 } |
| 177 | 146 |
| 178 // static | 147 // static |
| 179 void CastRemotingConnector::CreateRemoterFactory( | 148 void CastRemotingConnector::CreateMediaRemoter( |
| 180 content::RenderFrameHost* render_frame_host, | 149 content::RenderFrameHost* host, |
| 181 media::mojom::RemoterFactoryRequest request) { | 150 media::mojom::RemotingSourcePtr source, |
| 182 DCHECK(render_frame_host); | 151 media::mojom::RemoterRequest request) { |
| 183 mojo::MakeStrongBinding( | 152 DCHECK(host); |
| 184 base::MakeUnique<FrameRemoterFactory>(render_frame_host), | 153 auto* const contents = content::WebContents::FromRenderFrameHost(host); |
| 185 std::move(request)); | 154 if (!contents) |
| 155 return; |
| 156 CastRemotingConnector::Get(contents)->CreateBridge(std::move(source), |
| 157 std::move(request)); |
| 186 } | 158 } |
| 187 | 159 |
| 188 CastRemotingConnector::CastRemotingConnector( | 160 CastRemotingConnector::CastRemotingConnector( |
| 189 media_router::MediaRouter* router, | 161 media_router::MediaRouter* router, |
| 190 const media_router::MediaSource::Id& media_source_id) | 162 const media_router::MediaSource::Id& media_source_id) |
| 191 : media_router::MediaRoutesObserver(router), | 163 : media_router::MediaRoutesObserver(router), |
| 192 media_source_id_(media_source_id), | 164 media_source_id_(media_source_id), |
| 193 session_counter_(0), | 165 session_counter_(0), |
| 194 active_bridge_(nullptr), | 166 active_bridge_(nullptr), |
| 195 weak_factory_(this) {} | 167 weak_factory_(this) {} |
| (...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 514 media_router::MediaRoutesObserver::router(), route.media_route_id(), | 486 media_router::MediaRoutesObserver::router(), route.media_route_id(), |
| 515 this)); | 487 this)); |
| 516 // TODO(miu): In the future, scan the route ID for sink capabilities | 488 // TODO(miu): In the future, scan the route ID for sink capabilities |
| 517 // properties and pass these to the source in the OnSinkAvailable() | 489 // properties and pass these to the source in the OnSinkAvailable() |
| 518 // notification. | 490 // notification. |
| 519 for (RemotingBridge* notifyee : bridges_) | 491 for (RemotingBridge* notifyee : bridges_) |
| 520 notifyee->OnSinkAvailable(); | 492 notifyee->OnSinkAvailable(); |
| 521 break; | 493 break; |
| 522 } | 494 } |
| 523 } | 495 } |
| OLD | NEW |