Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
|
mark a. foltz
2015/05/11 07:36:21
Update copyright
haibinlu
2015/05/13 01:40:20
Done.
| |
| 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_PRESENTATION_SERVICE_DELEGATE_IMPL_H_ | |
| 6 #define CHROME_BROWSER_MEDIA_ROUTER_PRESENTATION_SERVICE_DELEGATE_IMPL_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 #include <utility> | |
| 11 | |
| 12 #include "base/gtest_prod_util.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/linked_ptr.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/memory/weak_ptr.h" | |
| 17 #include "base/observer_list.h" | |
| 18 #include "chrome/browser/media/router/media_router.h" | |
| 19 #include "chrome/browser/media/router/media_source.h" | |
| 20 #include "content/public/browser/presentation_service_delegate.h" | |
| 21 #include "content/public/browser/web_contents_observer.h" | |
| 22 #include "content/public/browser/web_contents_user_data.h" | |
| 23 | |
| 24 namespace content { | |
| 25 class RenderFrameHost; | |
| 26 class PresentationScreenAvailabilityListener; | |
| 27 class WebContents; | |
| 28 struct PresentationSessionInfo; | |
| 29 } | |
| 30 | |
| 31 namespace media_router { | |
| 32 | |
| 33 class MediaRoute; | |
| 34 class MediaSinksObserver; | |
| 35 | |
| 36 // Implementation of PresentationServiceDelegate that interfaces with the | |
|
mark a. foltz
2015/05/11 07:36:21
Suggest:
...that interfaces an instance of WebCon
haibinlu
2015/05/13 01:40:19
Done.
| |
| 37 // Chrome Media Router. An instance of this class is associated with a | |
| 38 // WebContents. In addition to fulfilling Presentation requests, it also | |
| 39 // provide tab-level information that is required for creating | |
| 40 // browser-initiated sessions. | |
| 41 class PresentationServiceDelegateImpl | |
| 42 : public content::WebContentsUserData<PresentationServiceDelegateImpl>, | |
|
mark a. foltz
2015/05/11 07:36:22
This doesn't follow the pattern in WebContentsUser
haibinlu
2015/05/13 01:40:20
Done.
| |
| 43 public content::PresentationServiceDelegate { | |
| 44 public: | |
| 45 explicit PresentationServiceDelegateImpl(content::WebContents* web_contents); | |
| 46 ~PresentationServiceDelegateImpl() override; | |
| 47 | |
| 48 // content::PresentationServiceDelegate implementation. | |
| 49 void AddObserver( | |
| 50 int render_process_id, | |
| 51 int render_frame_id, | |
| 52 content::PresentationServiceDelegate::Observer* observer) override; | |
| 53 void RemoveObserver(int render_process_id, int render_frame_id) override; | |
| 54 bool AddScreenAvailabilityListener( | |
| 55 int render_process_id, | |
| 56 int routing_id, | |
| 57 content::PresentationScreenAvailabilityListener* listener) override; | |
| 58 void RemoveScreenAvailabilityListener( | |
| 59 int render_process_id, | |
| 60 int routing_id, | |
| 61 content::PresentationScreenAvailabilityListener* listener) override; | |
| 62 void Reset(int render_process_id, int render_frame_id) override; | |
| 63 void SetDefaultPresentationUrl( | |
| 64 int render_process_id, | |
| 65 int render_frame_id, | |
| 66 const std::string& default_presentation_url, | |
| 67 const std::string& default_presentation_id) override; | |
| 68 void StartSession(int render_process_id, | |
| 69 int render_frame_id, | |
| 70 const std::string& presentation_url, | |
| 71 const std::string& presentation_id, | |
| 72 const PresentationSessionSuccessCallback& success_cb, | |
| 73 const PresentationSessionErrorCallback& error_cb) override; | |
| 74 void JoinSession(int render_process_id, | |
| 75 int render_frame_id, | |
| 76 const std::string& presentation_url, | |
| 77 const std::string& presentation_id, | |
| 78 const PresentationSessionSuccessCallback& success_cb, | |
| 79 const PresentationSessionErrorCallback& error_cb) override; | |
| 80 void ListenForSessionMessages( | |
| 81 int render_process_id, | |
| 82 int render_frame_id, | |
| 83 const PresentationSessionMessageCallback& message_cb) override; | |
| 84 | |
| 85 // Notifies this object that |route| has been created or joined outside of a | |
|
mark a. foltz
2015/05/11 07:36:21
Why does the delegate for a specific webcontents n
haibinlu
2015/05/13 01:40:19
This is for browser initiated sessions.
| |
| 86 // Presentation API request. The route could be due to | |
| 87 // browser action (e.g., browser initiated media router dialog) or | |
| 88 // a media route provider (e.g., autojoin). | |
| 89 void OnRouteCreated(const MediaRoute& route); | |
| 90 | |
| 91 // Returns the default MediaSource for this tab if there is one. | |
| 92 // Returns an empty MediaSource otherwise. | |
| 93 MediaSource GetDefaultMediaSource() const; | |
| 94 | |
| 95 // Returns the host name (without the "www.") of the frame that contains | |
|
mark a. foltz
2015/05/11 07:36:21
What is this for? Why do we assume the frame has
haibinlu
2015/05/13 01:40:20
it is used as domain name as in current extension.
| |
| 96 // the default media source. | |
| 97 std::string default_source_host() const { return default_source_host_; } | |
| 98 | |
| 99 const content::WebContents* web_contents() const { return web_contents_; } | |
|
mark a. foltz
2015/05/11 07:36:21
Do we need to expose the WebContents in the public
haibinlu
2015/05/13 01:40:19
removed
| |
| 100 | |
| 101 // Observer interface for listening to default MediaSource changes for the | |
| 102 // tab. | |
|
mark a. foltz
2015/05/11 07:36:22
s/tab/WebContents/
haibinlu
2015/05/13 01:40:19
Done.
| |
| 103 class DefaultMediaSourceObserver { | |
| 104 public: | |
| 105 virtual ~DefaultMediaSourceObserver() {} | |
| 106 | |
| 107 // Called when default media source for the corresponding tab has changed. | |
|
mark a. foltz
2015/05/11 07:36:22
s/tab/WebContents/
haibinlu
2015/05/13 01:40:19
Done.
| |
| 108 // |source|: New default MediaSource, or empty if default was removed. | |
| 109 // |source_host|: Host name of the frame that contains the default media | |
| 110 // source, or empty if there is no default media source. | |
| 111 virtual void OnDefaultMediaSourceChanged( | |
|
mark a. foltz
2015/05/11 07:36:22
Do we need to pass a ptr to |this| or WebContents*
mark a. foltz
2015/05/11 07:36:22
It would be a cleaner API to have a separate obser
haibinlu
2015/05/13 01:40:19
the call is from the webcontents
| |
| 112 const MediaSource& source, | |
| 113 const std::string& source_host) = 0; | |
| 114 }; | |
| 115 | |
| 116 // Adds / removes an observer for listening to default MediaSource changes. | |
| 117 void AddDefaultMediaSourceObserver(DefaultMediaSourceObserver* observer); | |
|
mark a. foltz
2015/05/11 07:36:21
How are these observers different from the AddObse
haibinlu
2015/05/13 01:40:20
the observer above is one per WebContents, for Web
| |
| 118 void RemoveDefaultMediaSourceObserver(DefaultMediaSourceObserver* observer); | |
| 119 | |
| 120 base::WeakPtr<PresentationServiceDelegateImpl> GetWeakPtr(); | |
| 121 | |
| 122 private: | |
| 123 friend class PresentationServiceDelegateImplTest; | |
| 124 friend class PresentationServiceDelegateImplBrowserTest; | |
| 125 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplTest, | |
| 126 AddScreenAvailabilityListener); | |
| 127 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplTest, | |
| 128 MaxNumObservers); | |
| 129 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplTest, | |
| 130 AddListenerSameUrlTwice); | |
| 131 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplTest, Reset); | |
| 132 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplBrowserTest, | |
| 133 RemoveObserversOnNavigation); | |
| 134 | |
| 135 typedef base::hash_map<MediaSourceId, linked_ptr<MediaSinksObserver>> | |
| 136 MediaSinksObserverBySource; | |
| 137 | |
| 138 // TODO(imcheng): Move this to PresentationServiceDelegate and use it in the | |
|
mark a. foltz
2015/05/11 07:36:22
Can we implement this TODO now?
haibinlu
2015/05/13 01:40:20
Done.
| |
| 139 // API. | |
| 140 typedef std::pair<int, int> RenderFrameHostId; | |
|
mark a. foltz
2015/05/11 07:36:22
Class members need to be declared after methods an
haibinlu
2015/05/13 01:40:21
Acknowledged.
| |
| 141 | |
| 142 // Maximum number of sources that can be registered at a time. | |
| 143 static const size_t kMaxNumSources = 256; | |
| 144 | |
| 145 // Helper function to extract ID for a RFH. | |
| 146 static RenderFrameHostId GetRenderFrameHostId(content::RenderFrameHost* rfh); | |
|
mark a. foltz
2015/05/11 07:36:22
I usually prefer to declare helper functions for t
haibinlu
2015/05/13 01:40:21
Done.
| |
| 147 | |
| 148 // Helper function to unregister and remove all MediaSinksObservers that | |
| 149 // are associated with |rfh_id|. | |
| 150 void UnregisterAllObserversFromFrame(RenderFrameHostId rfh_id); | |
| 151 | |
| 152 // Returns |listener|'s presentation URL as a MediaSource. If |listener| does | |
| 153 // not have a persentation URL, default to tab mirroring. | |
| 154 MediaSource GetMediaSourceFromListener( | |
| 155 content::PresentationScreenAvailabilityListener* listener); | |
| 156 | |
| 157 // Gets the host name associated with the frame given by |rfh_id|. | |
| 158 std::string GetSourceHostForFrame(RenderFrameHostId rfh_id) const; | |
|
mark a. foltz
2015/05/11 07:36:22
Does this need to be an instance method or can it
haibinlu
2015/05/13 01:40:20
No. move to anonymous namespace.
| |
| 159 | |
| 160 // Returns the presentation ID to use for frame |rfh_id|. | |
| 161 // If |presentation_id| is non-empty, return it. | |
| 162 // Otherwise, check if a default presentation ID is set for that frame, and | |
| 163 // return it. | |
| 164 // Otherwise, returns a randomly generated string. | |
| 165 std::string GetOrGeneratePresentationId( | |
|
mark a. foltz
2015/05/11 07:36:21
Ditto
haibinlu
2015/05/13 01:40:20
need to access default_presentation_infos_
| |
| 166 RenderFrameHostId rfh_id, | |
| 167 const std::string& presentation_id) const; | |
| 168 | |
| 169 // Returns |true| is frame |rfh_id| is the main frame of |web_contents_|. | |
| 170 bool IsMainFrame(RenderFrameHostId rfh_id) const; | |
|
mark a. foltz
2015/05/11 07:36:22
Ditto
haibinlu
2015/05/13 01:40:20
Need to access web_contents_
| |
| 171 | |
| 172 // Updates tab-level default MediaSource and source host name. If either | |
| 173 // changed, notify the observers. | |
| 174 void UpdateDefaultMediaSourceAndNotifyObservers( | |
| 175 const MediaSource& new_default_source, | |
| 176 const std::string& new_default_source_host); | |
| 177 | |
| 178 void SetMediaRouterForTest(MediaRouter* router); | |
| 179 | |
| 180 // Maps from RenderFrameHost ID to MediaSinksObservers for that frame. | |
| 181 base::hash_map<RenderFrameHostId, MediaSinksObserverBySource> observers_; | |
| 182 size_t num_observers_; | |
| 183 | |
| 184 // Maps from RFH ID to default presentation. | |
| 185 base::hash_map<RenderFrameHostId, | |
| 186 linked_ptr<content::PresentationSessionInfo>> | |
| 187 default_presentation_infos_; | |
| 188 | |
| 189 MediaSource default_source_; | |
| 190 std::string default_source_host_; | |
| 191 | |
| 192 content::WebContents* web_contents_; | |
|
mark a. foltz
2015/05/11 07:36:21
This is unowned correct?
haibinlu
2015/05/13 01:40:20
correct.
| |
| 193 | |
| 194 // Observers listening for changes to this class. Not owned by this class. | |
| 195 base::hash_map<RenderFrameHostId, Observer*> delegate_observers_; | |
|
mark a. foltz
2015/05/11 07:36:21
I'm confused here, why isn't this an ObserverList?
haibinlu
2015/05/13 01:40:19
Need to index it by rfh_id. linear list works too.
| |
| 196 | |
| 197 // Observers listening for changes to default media source. Not owned by | |
| 198 // this class. | |
| 199 ObserverList<DefaultMediaSourceObserver> default_media_source_observers_; | |
| 200 | |
| 201 // Does not own this object. | |
| 202 MediaRouter* router_; | |
| 203 | |
| 204 // NOTE: All WeakPtrs must be invalidated before member variables. | |
| 205 base::WeakPtrFactory<PresentationServiceDelegateImpl> weak_factory_; | |
| 206 | |
| 207 DISALLOW_COPY_AND_ASSIGN(PresentationServiceDelegateImpl); | |
| 208 }; | |
| 209 | |
| 210 } // namespace media_router | |
| 211 | |
| 212 #endif // CHROME_BROWSER_MEDIA_ROUTER_PRESENTATION_SERVICE_DELEGATE_IMPL_H_ | |
| OLD | NEW |