Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_MEDIA_ROUTER_DIALOG_CONTROLLER_H_ | 5 #ifndef CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_MEDIA_ROUTER_DIALOG_CONTROLLER_H_ |
| 6 #define CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_MEDIA_ROUTER_DIALOG_CONTROLLER_H_ | 6 #define CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_MEDIA_ROUTER_DIALOG_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/memory/weak_ptr.h" | |
| 9 #include "chrome/browser/media/router/create_session_request.h" | 10 #include "chrome/browser/media/router/create_session_request.h" |
| 10 #include "content/public/browser/web_contents_observer.h" | 11 #include "content/public/browser/web_contents_observer.h" |
| 11 #include "content/public/browser/web_contents_user_data.h" | 12 #include "content/public/browser/web_contents_user_data.h" |
| 12 | 13 |
| 13 namespace media_router { | 14 namespace media_router { |
| 14 | 15 |
| 16 class PresentationServiceDelegateImpl; | |
| 17 | |
| 15 // An instance of this class is tied to a WebContents known as the initiator, | 18 // An instance of this class is tied to a WebContents known as the initiator, |
| 16 // and is lazily created when a Media Router dialog needs to be shown. | 19 // and is lazily created when a Media Router dialog needs to be shown. |
| 17 // The MediaRouterDialogController allows creating, querying, and removing a | 20 // The MediaRouterDialogController allows creating, querying, and removing a |
| 18 // Media Router dialog modal to the initiator WebContents. | 21 // Media Router dialog modal to the initiator WebContents. |
| 19 // This class is not thread safe and must be called on the UI thread. | 22 // This class is not thread safe and must be called on the UI thread. |
| 20 class MediaRouterDialogController | 23 class MediaRouterDialogController |
| 21 : public content::WebContentsUserData<MediaRouterDialogController> { | 24 : public content::WebContentsUserData<MediaRouterDialogController> { |
| 22 public: | 25 public: |
| 23 ~MediaRouterDialogController() override; | 26 ~MediaRouterDialogController() override; |
| 24 | 27 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 // Data for dialogs created under a Presentation API context. | 85 // Data for dialogs created under a Presentation API context. |
| 83 // Passed from the caller of ShowMediaRouterDialogForPresentation(), and | 86 // Passed from the caller of ShowMediaRouterDialogForPresentation(), and |
| 84 // passed to the MediaRouterUI when it is initialized. | 87 // passed to the MediaRouterUI when it is initialized. |
| 85 scoped_ptr<CreateSessionRequest> presentation_request_; | 88 scoped_ptr<CreateSessionRequest> presentation_request_; |
| 86 | 89 |
| 87 base::ThreadChecker thread_checker_; | 90 base::ThreadChecker thread_checker_; |
| 88 | 91 |
| 89 DISALLOW_COPY_AND_ASSIGN(MediaRouterDialogController); | 92 DISALLOW_COPY_AND_ASSIGN(MediaRouterDialogController); |
| 90 }; | 93 }; |
| 91 | 94 |
| 95 // Class used by the Media Router dialog to signal route response and dialog | |
| 96 // destruction. In addition, it contains logic for handling Presentation API | |
| 97 // startSession requests and ondefaultsessionstart event handler. As such, this | |
| 98 // class can be used for both Presentation-API-initiated and browser-initiated | |
| 99 // dialogs. | |
| 100 // Instances of this class correspond 1:1 with Media Router dialogs and are | |
| 101 // self-managed. | |
| 102 // An instance will get notified in the following cases: | |
| 103 // (1) When the response of a Media Router route request initiated from the | |
| 104 // dialog arrives. | |
| 105 // (2) When the dialog is being destroyed. | |
| 106 // There is also an API for the dialog to attach additional listeners for (1), | |
| 107 // e.g., to update the UI on route response. | |
| 108 // Instances are self-managed and generally have the same lifetime as the | |
| 109 // dialog (i.e., they delete themselves in (2)), but will outlive the dialog | |
| 110 // if there is a pending route request at the time of dialog destruction. | |
| 111 // In that case, the instance will be destroyed on the next invocation of (1), | |
| 112 // which is assumed to always happen eventually. | |
| 113 // It also assumes that the dialog will not make concurrent route requests | |
| 114 // (That is, it will wait to make the next route request until a response has | |
| 115 // come back). | |
| 116 class MediaRouterDialogCallbacks { | |
| 117 public: | |
| 118 using DialogRouteResponseCallback = | |
| 119 base::Callback<void(const MediaRoute*, const std::string&)>; | |
| 120 | |
| 121 // Constructs an instance without a presentation request. Used for | |
| 122 // browser-initiated dialogs. | |
| 123 // |delegate|: PresentationServiceDelegateImpl instance corresponding to the | |
| 124 // initiator tab. Used for notifying ondefaultsessionstart event. | |
| 125 explicit MediaRouterDialogCallbacks( | |
| 126 const base::WeakPtr<PresentationServiceDelegateImpl>& delegate); | |
|
haibinlu
2015/07/09 21:45:59
per offline discussion, it is clearer to pass in w
imcheng
2015/07/09 22:22:29
Done. To do that, I needed to make sure initiator
| |
| 127 // Used for Presentation-API-initiated dialogs. | |
| 128 // |presentation_request|: The Presentation API startSession request. | |
| 129 // |delegate|: PresentationServiceDelegateImpl instance corresponding to the | |
| 130 // initiator tab. Used for notifying ondefaultsessionstart event. | |
| 131 MediaRouterDialogCallbacks( | |
| 132 scoped_ptr<CreateSessionRequest> presentation_request, | |
| 133 const base::WeakPtr<PresentationServiceDelegateImpl>& delegate); | |
| 134 | |
| 135 ~MediaRouterDialogCallbacks(); | |
| 136 | |
| 137 // Attaches |callback| to be invoked when |OnRouteResponseReceived| is | |
| 138 // invoked. | |
| 139 void SetRouteResponseCallback(const DialogRouteResponseCallback& callback); | |
| 140 | |
| 141 // Invoked when the corresponding Media Router dialog is being destroyed. | |
| 142 // If there is a Presentation API request and there is no pending presentation | |
| 143 // route response, the request will be rejected. | |
| 144 // |has_pending_presentation_response|: True if there is a pending | |
| 145 // presentation route response from the MediaRouter. If true, this instance | |
| 146 // will continue existing until the route response comes back. Otherwise, it | |
| 147 // will be destroyed at the end of this method. | |
| 148 void OnDialogDestroyed(bool has_pending_presentation_response); | |
| 149 | |
| 150 // Invoked when a route response for a Media Router route request initiated | |
| 151 // from the | |
| 152 // corresponding Media Router dialog comes back. | |
| 153 // If the response was for a presentationthere is a Presentation API request, | |
| 154 // two things can happen: | |
| 155 // (1) If there is a Presentation API request, then the request will be | |
| 156 // resolved/rejected accordingly. | |
| 157 // (2) Otherwise, if route response was a success, then | |
| 158 // PresentationServiceDelegateImpl will be notified that a browser-initiated | |
| 159 // presentation has started. | |
| 160 // If |route_response_cb_| is set, it will be invoked with the response. | |
| 161 // Finally, if the response arrived after the dialog has been destroyed, then | |
| 162 // this instance will be destroyed at the end of this method. | |
|
haibinlu
2015/07/09 21:45:59
If UX wants enabling multiple launch simultaneousl
imcheng
2015/07/09 22:22:29
UX mentioned that they will only allow one at a ti
| |
| 163 void OnRouteResponseReceived(bool is_presentation, | |
| 164 scoped_ptr<MediaRoute> route, | |
| 165 const std::string& error); | |
| 166 | |
| 167 base::WeakPtr<MediaRouterDialogCallbacks> GetWeakPtr(); | |
| 168 | |
| 169 const CreateSessionRequest* presentation_request() const { | |
| 170 return presentation_request_.get(); | |
| 171 } | |
| 172 | |
| 173 void set_route_response_cb(const DialogRouteResponseCallback& callback) { | |
| 174 route_response_cb_ = callback; | |
| 175 } | |
| 176 | |
| 177 private: | |
| 178 void HandleRouteResponseForPresentation(const MediaRoute* route, | |
| 179 const std::string& error); | |
| 180 | |
| 181 DialogRouteResponseCallback route_response_cb_; | |
| 182 scoped_ptr<CreateSessionRequest> presentation_request_; | |
| 183 bool pending_presentation_response_after_dialog_destroyed_; | |
| 184 | |
| 185 base::WeakPtr<PresentationServiceDelegateImpl> presentation_service_delegate_; | |
| 186 | |
| 187 base::WeakPtrFactory<MediaRouterDialogCallbacks> weak_factory_; | |
| 188 | |
| 189 DISALLOW_COPY_AND_ASSIGN(MediaRouterDialogCallbacks); | |
| 190 }; | |
| 191 | |
| 92 } // namespace media_router | 192 } // namespace media_router |
| 93 | 193 |
| 94 #endif // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_MEDIA_ROUTER_DIALOG_CONTROLLER_H _ | 194 #endif // CHROME_BROWSER_UI_WEBUI_MEDIA_ROUTER_MEDIA_ROUTER_DIALOG_CONTROLLER_H _ |
| OLD | NEW |