OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/media/router/offscreen_presentation_manager.h" | |
6 | |
7 #include <utility> | |
8 | |
9 #include "base/memory/ptr_util.h" | |
10 #include "content/public/browser/render_frame_host.h" | |
11 #include "content/public/browser/render_process_host.h" | |
12 #include "content/public/browser/web_contents.h" | |
13 #include "url/gurl.h" | |
14 | |
15 namespace media_router { | |
16 | |
17 // OffscreenPresentationManager implementation. | |
18 OffscreenPresentationManager::OffscreenPresentationManager() { | |
19 thread_checker_.DetachFromThread(); | |
mark a. foltz
2016/10/08 00:33:41
I don't think this is needed. The thread_checker_
zhaobin
2016/10/12 02:27:32
Done.
| |
20 } | |
21 | |
22 OffscreenPresentationManager::~OffscreenPresentationManager() {} | |
mark a. foltz
2016/10/08 00:33:41
Could there be any still-registered OffscreenPrese
zhaobin
2016/10/12 02:27:32
Done.
| |
23 | |
24 OffscreenPresentationManager::OffscreenPresentation* | |
25 OffscreenPresentationManager::GetOrCreateOffscreenPresentation( | |
26 const std::string& presentation_id, | |
27 const GURL& presentation_url) { | |
28 auto it = offscreen_presentations_.find(presentation_id); | |
29 if (it == offscreen_presentations_.end()) { | |
30 it = offscreen_presentations_ | |
31 .insert(std::make_pair( | |
32 presentation_id, | |
33 new OffscreenPresentation(presentation_id, presentation_url))) | |
34 .first; | |
35 } | |
36 return it->second; | |
37 } | |
38 | |
39 void OffscreenPresentationManager::RegisterOffscreenPresentationController( | |
40 const std::string& presentation_id, | |
41 const GURL& presentation_url, | |
42 const RenderFrameHostId& render_frame_host_id, | |
43 content::PresentationConnectionPtr controller) { | |
44 DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id | |
45 << ", [render_frame_host_id]: " << render_frame_host_id.second; | |
46 DCHECK(thread_checker_.CalledOnValidThread()); | |
47 | |
48 auto presentation = | |
49 GetOrCreateOffscreenPresentation(presentation_id, presentation_url); | |
50 presentation->RegisterController(render_frame_host_id, std::move(controller)); | |
51 } | |
52 | |
53 void OffscreenPresentationManager::UnregisterOffscreenPresentationController( | |
54 const std::string& presentation_id, | |
55 const RenderFrameHostId& render_frame_host_id) { | |
56 DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id | |
57 << ", [render_frame_host_id]: " << render_frame_host_id.second; | |
58 DCHECK(thread_checker_.CalledOnValidThread()); | |
59 | |
60 auto it = offscreen_presentations_.find(presentation_id); | |
61 if (it == offscreen_presentations_.end()) | |
62 return; | |
63 | |
64 // Remove presentation if no controller and receiver. | |
65 it->second->UnregisterController(render_frame_host_id); | |
66 if (it->second->pending_controllers_.size() == 0 && | |
67 it->second->receiver_callback_.is_null()) | |
68 offscreen_presentations_.erase(presentation_id); | |
69 } | |
70 | |
71 void OffscreenPresentationManager::OnOffscreenPresentationReceiverCreated( | |
72 const std::string& presentation_id, | |
73 const GURL& presentation_url, | |
74 const content::ReceiverConnectionAvailableCallback& receiver_callback) { | |
75 DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id; | |
76 DCHECK(thread_checker_.CalledOnValidThread()); | |
77 auto presentation = | |
78 GetOrCreateOffscreenPresentation(presentation_id, presentation_url); | |
79 presentation->RegisterReceiver(receiver_callback); | |
80 } | |
81 | |
82 void OffscreenPresentationManager::OnOffscreenPresentationReceiverTerminated( | |
83 const std::string& presentation_id) { | |
84 DVLOG(2) << __FUNCTION__ << " [presentation_id]: " << presentation_id; | |
85 DCHECK(thread_checker_.CalledOnValidThread()); | |
86 | |
87 offscreen_presentations_.erase(presentation_id); | |
88 } | |
89 | |
90 // OffscreenPresentation implementation. | |
91 OffscreenPresentationManager::OffscreenPresentation::OffscreenPresentation( | |
92 const std::string& presentation_id, | |
93 const GURL& presentation_url) | |
94 : presentation_id_(presentation_id), | |
95 presentation_url_(presentation_url), | |
96 receiver_callback_(nullptr) {} | |
97 | |
98 OffscreenPresentationManager::OffscreenPresentation::~OffscreenPresentation() {} | |
99 | |
100 void OffscreenPresentationManager::OffscreenPresentation::RegisterController( | |
101 const RenderFrameHostId& render_frame_host_id, | |
102 content::PresentationConnectionPtr controller) { | |
103 if (receiver_callback_) { | |
104 // Create a receiver PresentationConnectionPtr, and connect it with | |
105 // controller PresentationConnectionPtr. | |
106 receiver_callback_.Run(content::PresentationSessionInfo( | |
107 presentation_url_.spec(), presentation_id_), | |
108 std::move(controller)); | |
109 } else { | |
110 pending_controllers_.insert( | |
111 std::make_pair(render_frame_host_id, std::move(controller))); | |
112 } | |
113 } | |
114 | |
115 void OffscreenPresentationManager::OffscreenPresentation::UnregisterController( | |
116 const RenderFrameHostId& render_frame_host_id) { | |
117 pending_controllers_.erase(render_frame_host_id); | |
118 } | |
119 | |
120 void OffscreenPresentationManager::OffscreenPresentation::RegisterReceiver( | |
121 const content::ReceiverConnectionAvailableCallback& receiver_callback) { | |
122 DCHECK(!receiver_callback_); | |
123 | |
124 for (auto& controller : pending_controllers_) { | |
125 receiver_callback.Run(content::PresentationSessionInfo( | |
126 presentation_url_.spec(), presentation_id_), | |
127 std::move(controller.second)); | |
128 } | |
129 receiver_callback_ = receiver_callback; | |
130 pending_controllers_.clear(); | |
131 } | |
132 | |
133 } // namespace media_router | |
OLD | NEW |