Convert browser_tests to Swarming.
[chromium-blink-merge.git] / chrome / browser / media / router / presentation_service_delegate_impl.h
blob82af0d96fc18d5eaf573c71c4ba69079743a8246
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.
5 #ifndef CHROME_BROWSER_MEDIA_ROUTER_PRESENTATION_SERVICE_DELEGATE_IMPL_H_
6 #define CHROME_BROWSER_MEDIA_ROUTER_PRESENTATION_SERVICE_DELEGATE_IMPL_H_
8 #include <map>
9 #include <string>
10 #include <utility>
12 #include "base/gtest_prod_util.h"
13 #include "base/macros.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/observer_list.h"
17 #include "chrome/browser/media/router/media_router.h"
18 #include "chrome/browser/media/router/media_source.h"
19 #include "content/public/browser/presentation_service_delegate.h"
20 #include "content/public/browser/web_contents_observer.h"
21 #include "content/public/browser/web_contents_user_data.h"
23 namespace content {
24 class RenderFrameHost;
25 class PresentationScreenAvailabilityListener;
26 class WebContents;
27 struct PresentationSessionInfo;
28 struct PresentationSessionMessage;
29 } // namespace content
31 namespace media_router {
33 class MediaRoute;
34 class MediaSinksObserver;
35 class PresentationFrameManager;
37 // Implementation of PresentationServiceDelegate that interfaces an
38 // instance of WebContents with the Chrome Media Router. It uses the Media
39 // Router to handle presentation API calls forwarded from
40 // PresentationServiceImpl. In addition, it also
41 // provides default presentation URL that is required for creating
42 // browser-initiated sessions.
43 // It is scoped to the lifetime of a WebContents, and is managed by the
44 // associated WebContents.
45 class PresentationServiceDelegateImpl
46 : public content::WebContentsUserData<PresentationServiceDelegateImpl>,
47 public content::PresentationServiceDelegate {
48 public:
49 // Retrieves the instance of PresentationServiceDelegateImpl that was attached
50 // to the specified WebContents. If no instance was attached, creates one,
51 // and attaches it to the specified WebContents.
52 static PresentationServiceDelegateImpl* GetOrCreateForWebContents(
53 content::WebContents* web_contents);
55 ~PresentationServiceDelegateImpl() override;
57 // content::PresentationServiceDelegate implementation.
58 void AddObserver(
59 int render_process_id,
60 int render_frame_id,
61 content::PresentationServiceDelegate::Observer* observer) override;
62 void RemoveObserver(int render_process_id, int render_frame_id) override;
63 bool AddScreenAvailabilityListener(
64 int render_process_id,
65 int render_frame_id,
66 content::PresentationScreenAvailabilityListener* listener) override;
67 void RemoveScreenAvailabilityListener(
68 int render_process_id,
69 int render_frame_id,
70 content::PresentationScreenAvailabilityListener* listener) override;
71 void Reset(int render_process_id, int render_frame_id) override;
72 void SetDefaultPresentationUrl(
73 int render_process_id,
74 int render_frame_id,
75 const std::string& default_presentation_url,
76 const std::string& default_presentation_id) override;
77 void StartSession(int render_process_id,
78 int render_frame_id,
79 const std::string& presentation_url,
80 const std::string& presentation_id,
81 const PresentationSessionSuccessCallback& success_cb,
82 const PresentationSessionErrorCallback& error_cb) override;
83 void JoinSession(int render_process_id,
84 int render_frame_id,
85 const std::string& presentation_url,
86 const std::string& presentation_id,
87 const PresentationSessionSuccessCallback& success_cb,
88 const PresentationSessionErrorCallback& error_cb) override;
89 void ListenForSessionMessages(
90 int render_process_id,
91 int render_frame_id,
92 const PresentationSessionMessageCallback& message_cb) override;
93 void SendMessage(
94 int render_process_id,
95 int render_frame_id,
96 scoped_ptr<content::PresentationSessionMessage> message_request,
97 const SendMessageCallback& send_message_cb) override;
99 // Callback invoked when a |route| has been created or joined outside of a
100 // Presentation API request. The route could be due to
101 // browser action (e.g., browser initiated media router dialog) or
102 // a media route provider (e.g., autojoin).
103 void OnRouteCreated(const MediaRoute& route);
105 // Returns the default MediaSource for this tab if there is one.
106 // Returns an empty MediaSource otherwise.
107 MediaSource default_source() const { return default_source_; }
109 content::WebContents* web_contents() const { return web_contents_; }
110 const GURL& default_frame_url() const { return default_frame_url_; }
112 // Observer interface for listening to default MediaSource changes for the
113 // WebContents.
114 class DefaultMediaSourceObserver {
115 public:
116 virtual ~DefaultMediaSourceObserver() {}
118 // Called when default media source for the corresponding WebContents has
119 // changed.
120 // |source|: New default MediaSource, or empty if default was removed.
121 // |frame_url|: URL of the frame that contains the default media
122 // source, or empty if there is no default media source.
123 virtual void OnDefaultMediaSourceChanged(const MediaSource& source,
124 const GURL& frame_url) = 0;
127 // Adds / removes an observer for listening to default MediaSource changes.
128 void AddDefaultMediaSourceObserver(DefaultMediaSourceObserver* observer);
129 void RemoveDefaultMediaSourceObserver(DefaultMediaSourceObserver* observer);
131 void SetMediaRouterForTest(MediaRouter* router);
132 bool HasScreenAvailabilityListenerForTest(
133 int render_process_id,
134 int render_frame_id,
135 const MediaSource::Id& source_id) const;
137 base::WeakPtr<PresentationServiceDelegateImpl> GetWeakPtr();
139 private:
140 explicit PresentationServiceDelegateImpl(content::WebContents* web_contents);
141 friend class content::WebContentsUserData<PresentationServiceDelegateImpl>;
143 FRIEND_TEST_ALL_PREFIXES(PresentationServiceDelegateImplTest,
144 DelegateObservers);
146 // Returns |listener|'s presentation URL as a MediaSource. If |listener| does
147 // not have a persentation URL, returns the tab mirroring MediaSource.
148 MediaSource GetMediaSourceFromListener(
149 content::PresentationScreenAvailabilityListener* listener);
151 void OnJoinRouteResponse(int render_process_id,
152 int render_frame_id,
153 const content::PresentationSessionInfo& session,
154 const PresentationSessionSuccessCallback& success_cb,
155 const PresentationSessionErrorCallback& error_cb,
156 scoped_ptr<MediaRoute> route,
157 const std::string& error_text);
159 void OnStartSessionSucceeded(
160 int render_process_id,
161 int render_frame_id,
162 const PresentationSessionSuccessCallback& success_cb,
163 const content::PresentationSessionInfo& new_session,
164 const MediaRoute::Id& route_id);
166 // Returns |true| if the frame is the main frame of |web_contents_|.
167 bool IsMainFrame(int render_process_id, int render_frame_id) const;
169 // Updates tab-level default MediaSource and/or default frame URL. If either
170 // changed, notify the observers.
171 void UpdateDefaultMediaSourceAndNotifyObservers(
172 const MediaSource& new_default_source,
173 const GURL& new_default_frame_url);
175 // Default MediaSource for the tab associated with this instance.
176 MediaSource default_source_;
177 // URL of the frame that contains the default MediaSource.
178 GURL default_frame_url_;
180 // References to the observers listening for changes to default media source.
181 base::ObserverList<
182 DefaultMediaSourceObserver> default_media_source_observers_;
184 // References to the WebContents that owns this instance, and associated
185 // browser profile's MediaRouter instance.
186 content::WebContents* web_contents_;
187 MediaRouter* router_;
189 scoped_ptr<PresentationFrameManager> frame_manager_;
191 base::WeakPtrFactory<PresentationServiceDelegateImpl> weak_factory_;
193 DISALLOW_COPY_AND_ASSIGN(PresentationServiceDelegateImpl);
196 } // namespace media_router
198 #endif // CHROME_BROWSER_MEDIA_ROUTER_PRESENTATION_SERVICE_DELEGATE_IMPL_H_