Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Side by Side Diff: chrome/browser/media/router/offscreen_presentation_manager.h

Issue 2379703002: [Presentation API] (alternative) 1-UA: send message between controller and receiver page (Closed)
Patch Set: resolve code review comments from Mark Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef CHROME_BROWSER_MEDIA_ROUTER_OFFSCREEN_PRESENTATION_MANAGER_H_
6 #define CHROME_BROWSER_MEDIA_ROUTER_OFFSCREEN_PRESENTATION_MANAGER_H_
7
8 #include <map>
9 #include <memory>
10 #include <string>
11
12 #include "base/macros.h"
13 #include "base/threading/thread_checker.h"
14 #include "chrome/browser/media/router/render_frame_host_id.h"
15 #include "components/keyed_service/core/keyed_service.h"
16 #include "content/public/browser/presentation_service_delegate.h"
17
18 class GURL;
19
20 namespace media_router {
mark a. foltz 2016/10/08 00:33:41 From a layering point of view I wonder if this bel
zhaobin 2016/10/12 02:27:32 Leave it in media/router folder for now. We will m
21
22 // Instances of this class manages all offscreen presentations started in the
23 // associated Profile and facilitates communication between the controllers and
24 // the receiver of an offscreen presentation.
25 //
26 // Example usage:
27 //
28 // (PresentationConnectionPtr - blink::mojom::PresentationConnectionPtr, mojo
29 // handler for blink::PresentationConnection object in render process)
30 //
31 // Receiver is created to host the offscreen presentation and registers itself
32 // so that controller frames can connect to it:
33 //
34 // OffscreenPresentationManager* manager =
35 // OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext(
36 // Profile::FromBrowserContext(web_contents_->GetBrowserContext())
mark a. foltz 2016/10/08 00:33:41 Where does |web_contents_| come from here? Is it
zhaobin 2016/10/12 02:27:33 For controller, it is from controller's page; For
37 // ->GetOriginalProfile());
mark a. foltz 2016/10/08 00:33:41 Nit: the factory should take care of returning the
zhaobin 2016/10/12 02:27:33 Done.
38 // manager->RegisterOffscreenPresentationReceiver(presentation_id,
39 // base::Bind(&PresentationServiceImpl::OnReceiverConnectionAvailable));
mark a. foltz 2016/10/08 00:33:41 Should this be bound to the instance of the PSImpl
zhaobin 2016/10/12 02:27:32 Yes. The object is bind to the function: delegat
40 // ...
mark a. foltz 2016/10/08 00:33:41 I would add a comment here like, // Invoked on r
zhaobin 2016/10/12 02:27:33 Done.
41 // void PresentationServiceImpl::OnReceiverConnectionAvailable(
42 // const content::PresentationSessionInfo& session,
43 // PresentationConnectionPtr&& controller) {
44 // [Create a PresentationConnection on receiver page.
45 // Connect receiver PresentationConnectionPtr with controller
46 // PresenatationConnectionPtr]
mark a. foltz 2016/10/08 00:33:41 Typo
zhaobin 2016/10/12 02:27:32 Done.
47 // client_->OnReceiverConnectionAvailable();
48 // }
49 //
50 // Controller frame establishes connection with the receiver side, resulting
51 // in a connection with the two endpoints being the controller
52 // PresentationConnectionPtr and receiver PresentationConnectionPtr.
53 // Note calling this will trigger receiver frame's
54 // PresentationServiceImpl::OnReceiverConnectionAvailable.
55 //
56 // manager->RegisterOffscreenPresentationController(
57 // presentation_id, controller_frame_id, controller_ptr);
58 //
59 // Send message from controller/receiver to receiver/controller:
60 //
61 // In controller's (or receiver's) PresentationConnctionPtr::SendString() {
62 // ...
63 // target_connection_->OnSessionMessageReceived();
64 // }
65 //
66 // A controller or receiver leaves the offscreen presentation (e.g.,
67 // due to navigation) by unregistering themselves from
68 // OffscreenPresentationConnection object.
69 //
70 // When the receiver is no longer associated with an offscreen presentation, it
71 // shall remove itself from associated controllers and then unregister itself
72 // with OffscreenPresentationManager. Unregistration will prevent additional
73 // controllers from establishing a connection with the receiver:
74 //
75 // In receiver's PSImpl::Reset() {
76 // for (controller : offscreen_presentation_observers_)
77 // controller->RemoveOffscreenPresentationClient(receiver);
78 // }
79 // manager->UnregisterOffscreenPresentationReceiver(presentation_id);
80 //
81 // This class is not thread safe. All functions must be invoked on the UI
82 // thread. All callbacks passed into this class will also be invoked on UI
83 // thread.
84 class OffscreenPresentationManager : public KeyedService {
85 public:
86 ~OffscreenPresentationManager() override;
87
88 // Registers controller PresentationConnectionPtr to presentation
89 // with |presentation_id|, |render_frame_id|.
90 // Creates a new presentation if no presentation with |presentation_id|
91 // exists.
92 // |controller| Not owned by this class.
93 void RegisterOffscreenPresentationController(
94 const std::string& presentation_id,
95 const GURL& presentation_url,
96 const RenderFrameHostId& render_frame_id,
97 content::PresentationConnectionPtr controller);
98
99 // Unregisters controller PSImpl to presentation with |presentation_id|,
100 // |render_frame_id|.
101 void UnregisterOffscreenPresentationController(
102 const std::string& presentation_id,
103 const RenderFrameHostId& render_frame_id);
104
105 // Registers receiverConnectionAvailablecallback to presentation
106 // with |presentation_id|.
107 void OnOffscreenPresentationReceiverCreated(
108 const std::string& presentation_id,
109 const GURL& presentation_url,
110 const content::ReceiverConnectionAvailableCallback& receiver_callback);
111
112 void OnOffscreenPresentationReceiverTerminated(
113 const std::string& presentation_id);
114
115 private:
116 // Represents an offscreen presentation registered with
117 // OffscreenPresentationManager.
118 // Contains callback to the receiver to inform it of new connections
119 // established from a controller.
120 // Contains set of controllers registered to OffscreenPresentationManager
121 // before corresponding receiver.
122 class OffscreenPresentation {
123 public:
124 explicit OffscreenPresentation(const std::string& presentation_id,
mark a. foltz 2016/10/08 00:33:41 explicit is not needed for two-arg constructors
zhaobin 2016/10/12 02:27:33 Done.
125 const GURL& presentation_url);
126 ~OffscreenPresentation();
127
128 void RegisterController(const RenderFrameHostId& render_frame_id,
129 content::PresentationConnectionPtr controller);
130
131 void UnregisterController(const RenderFrameHostId& render_frame_id);
132
133 void RegisterReceiver(
134 const content::ReceiverConnectionAvailableCallback& receiver_callback);
135
136 private:
137 friend class OffscreenPresentationManagerTest;
138 friend class OffscreenPresentationManager;
139
140 // presentation_id for current presentation.
141 const std::string presentation_id_;
142
143 // URL for current presentation.
144 const GURL presentation_url_;
145
146 // Bind to receiver frame's
147 // PresentationServiceImpl::OnReceiverConnectionAvailable.
148 content::ReceiverConnectionAvailableCallback receiver_callback_;
149
150 // proxy to controller PSImpl
151 // It only contains controllers registered before receiver_callback_
152 // is set. This map will be cleared in RegisterReceiver().
153 std::unordered_map<RenderFrameHostId,
154 content::PresentationConnectionPtr,
155 RenderFrameHostIdHasher>
156 pending_controllers_;
157
158 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentation);
159 };
160
161 private:
162 friend class OffscreenPresentationManagerFactory;
163 friend class OffscreenPresentationManagerTest;
164
165 // Used by OffscreenPresentationManagerFactory::GetOrCreateForBrowserContext.
166 OffscreenPresentationManager();
167
168 // Creates an offscreen presentation with
169 // |presentation_id| and |presentation_url|.
170 OffscreenPresentation* GetOrCreateOffscreenPresentation(
171 const std::string& presentation_id,
172 const GURL& presentation_url);
173
174 // Maps from presentation ID to OffscreenPresentation.
175 std::map<std::string, OffscreenPresentation*> offscreen_presentations_;
176
177 base::ThreadChecker thread_checker_;
178
179 DISALLOW_COPY_AND_ASSIGN(OffscreenPresentationManager);
180 };
181
182 } // namespace media_router
183
184 #endif // CHROME_BROWSER_MEDIA_ROUTER_OFFSCREEN_PRESENTATION_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698