| 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" |
| 11 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "chrome/browser/media/cast_remoting_connector_messaging.h" | 13 #include "chrome/browser/media/cast_remoting_connector_messaging.h" |
| 14 #include "chrome/browser/media/cast_remoting_sender.h" | 14 #include "chrome/browser/media/cast_remoting_sender.h" |
| 15 #include "chrome/browser/media/router/media_router.h" | 15 #include "chrome/browser/media/router/media_router.h" |
| 16 #include "chrome/browser/media/router/media_router_factory.h" | 16 #include "chrome/browser/media/router/media_router_factory.h" |
| 17 #include "chrome/browser/media/router/media_source_helper.h" | 17 #include "chrome/browser/media/router/media_source_helper.h" |
| 18 #include "chrome/browser/media/router/route_message.h" | 18 #include "chrome/browser/media/router/route_message.h" |
| 19 #include "chrome/browser/media/router/route_message_observer.h" | 19 #include "chrome/browser/media/router/route_message_observer.h" |
| 20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/render_frame_host.h" |
| 22 #include "content/public/browser/render_process_host.h" |
| 21 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 22 #include "mojo/public/cpp/bindings/strong_binding.h" | 24 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 23 | 25 |
| 24 using content::BrowserThread; | 26 using content::BrowserThread; |
| 25 using media::mojom::RemotingStartFailReason; | 27 using media::mojom::RemotingStartFailReason; |
| 26 using media::mojom::RemotingStopReason; | 28 using media::mojom::RemotingStopReason; |
| 27 | 29 |
| 28 using Messaging = CastRemotingConnectorMessaging; | 30 using Messaging = CastRemotingConnectorMessaging; |
| 29 | 31 |
| 30 class CastRemotingConnector::FrameRemoterFactory | 32 class CastRemotingConnector::FrameRemoterFactory |
| 31 : public media::mojom::RemoterFactory { | 33 : public media::mojom::RemoterFactory { |
| 32 public: | 34 public: |
| 33 // |render_frame_host| represents the source render frame. | 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. |
| 34 explicit FrameRemoterFactory(content::RenderFrameHost* render_frame_host) | 38 explicit FrameRemoterFactory(content::RenderFrameHost* render_frame_host) |
| 35 : host_(render_frame_host) { | 39 : render_frame_process_id_(render_frame_host->GetProcess()->GetID()), |
| 36 DCHECK(host_); | 40 render_frame_routing_id_(render_frame_host->GetRoutingID()) {} |
| 37 } | |
| 38 ~FrameRemoterFactory() final {} | 41 ~FrameRemoterFactory() final {} |
| 39 | 42 |
| 40 void Create(media::mojom::RemotingSourcePtr source, | 43 void Create(media::mojom::RemotingSourcePtr source, |
| 41 media::mojom::RemoterRequest request) final { | 44 media::mojom::RemoterRequest request) final { |
| 42 CastRemotingConnector::Get(content::WebContents::FromRenderFrameHost(host_)) | 45 auto* const host = content::RenderFrameHost::FromID( |
| 43 ->CreateBridge(std::move(source), std::move(request)); | 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)); |
| 44 } | 54 } |
| 45 | 55 |
| 46 private: | 56 private: |
| 47 content::RenderFrameHost* const host_; | 57 const int render_frame_process_id_; |
| 58 const int render_frame_routing_id_; |
| 48 | 59 |
| 49 DISALLOW_COPY_AND_ASSIGN(FrameRemoterFactory); | 60 DISALLOW_COPY_AND_ASSIGN(FrameRemoterFactory); |
| 50 }; | 61 }; |
| 51 | 62 |
| 52 class CastRemotingConnector::RemotingBridge : public media::mojom::Remoter { | 63 class CastRemotingConnector::RemotingBridge : public media::mojom::Remoter { |
| 53 public: | 64 public: |
| 54 // Constructs a "bridge" to delegate calls between the given |source| and | 65 // Constructs a "bridge" to delegate calls between the given |source| and |
| 55 // |connector|. |connector| must be valid at the time of construction, but is | 66 // |connector|. |connector| must be valid at the time of construction, but is |
| 56 // otherwise a weak pointer that can become invalid during the lifetime of a | 67 // otherwise a weak pointer that can become invalid during the lifetime of a |
| 57 // RemotingBridge. | 68 // RemotingBridge. |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 media_router::MediaSourceForTabContentRemoting(contents).id()); | 172 media_router::MediaSourceForTabContentRemoting(contents).id()); |
| 162 contents->SetUserData(kUserDataKey, connector); | 173 contents->SetUserData(kUserDataKey, connector); |
| 163 } | 174 } |
| 164 return connector; | 175 return connector; |
| 165 } | 176 } |
| 166 | 177 |
| 167 // static | 178 // static |
| 168 void CastRemotingConnector::CreateRemoterFactory( | 179 void CastRemotingConnector::CreateRemoterFactory( |
| 169 content::RenderFrameHost* render_frame_host, | 180 content::RenderFrameHost* render_frame_host, |
| 170 media::mojom::RemoterFactoryRequest request) { | 181 media::mojom::RemoterFactoryRequest request) { |
| 182 DCHECK(render_frame_host); |
| 171 mojo::MakeStrongBinding( | 183 mojo::MakeStrongBinding( |
| 172 base::MakeUnique<FrameRemoterFactory>(render_frame_host), | 184 base::MakeUnique<FrameRemoterFactory>(render_frame_host), |
| 173 std::move(request)); | 185 std::move(request)); |
| 174 } | 186 } |
| 175 | 187 |
| 176 CastRemotingConnector::CastRemotingConnector( | 188 CastRemotingConnector::CastRemotingConnector( |
| 177 media_router::MediaRouter* router, | 189 media_router::MediaRouter* router, |
| 178 const media_router::MediaSource::Id& media_source_id) | 190 const media_router::MediaSource::Id& media_source_id) |
| 179 : media_router::MediaRoutesObserver(router), | 191 : media_router::MediaRoutesObserver(router), |
| 180 media_source_id_(media_source_id), | 192 media_source_id_(media_source_id), |
| (...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 502 media_router::MediaRoutesObserver::router(), route.media_route_id(), | 514 media_router::MediaRoutesObserver::router(), route.media_route_id(), |
| 503 this)); | 515 this)); |
| 504 // TODO(miu): In the future, scan the route ID for sink capabilities | 516 // TODO(miu): In the future, scan the route ID for sink capabilities |
| 505 // properties and pass these to the source in the OnSinkAvailable() | 517 // properties and pass these to the source in the OnSinkAvailable() |
| 506 // notification. | 518 // notification. |
| 507 for (RemotingBridge* notifyee : bridges_) | 519 for (RemotingBridge* notifyee : bridges_) |
| 508 notifyee->OnSinkAvailable(); | 520 notifyee->OnSinkAvailable(); |
| 509 break; | 521 break; |
| 510 } | 522 } |
| 511 } | 523 } |
| OLD | NEW |