| 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 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" | 5 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "chrome/browser/media/router/issues_observer.h" |
| 10 #include "chrome/browser/media/router/media_router.h" |
| 11 #include "chrome/browser/media/router/media_router_mojo_impl.h" |
| 12 #include "chrome/browser/media/router/media_router_mojo_impl_factory.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/ui/webui/media_router/media_router_localized_strings_pr
ovider.h" | 14 #include "chrome/browser/ui/webui/media_router/media_router_localized_strings_pr
ovider.h" |
| 11 #include "chrome/browser/ui/webui/media_router/media_router_resources_provider.h
" | 15 #include "chrome/browser/ui/webui/media_router/media_router_resources_provider.h
" |
| 12 #include "chrome/browser/ui/webui/media_router/media_router_webui_message_handle
r.h" | 16 #include "chrome/browser/ui/webui/media_router/media_router_webui_message_handle
r.h" |
| 13 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
| 18 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/browser/web_ui.h" | 19 #include "content/public/browser/web_ui.h" |
| 15 #include "content/public/browser/web_ui_data_source.h" | 20 #include "content/public/browser/web_ui_data_source.h" |
| 16 #include "ui/web_dialogs/web_dialog_delegate.h" | 21 #include "ui/web_dialogs/web_dialog_delegate.h" |
| 17 | 22 |
| 18 namespace media_router { | 23 namespace media_router { |
| 19 | 24 |
| 25 // This class calls to refresh the UI when the highest priority issue is |
| 26 // updated. |
| 27 class MediaRouterUI::UIIssuesObserver : public IssuesObserver { |
| 28 public: |
| 29 explicit UIIssuesObserver(MediaRouterUI* ui) : ui_(ui) { DCHECK(ui); } |
| 30 |
| 31 ~UIIssuesObserver() override {} |
| 32 |
| 33 // IssuesObserver implementation. |
| 34 void OnIssueUpdated(const Issue* issue) override { ui_->SetIssue(issue); } |
| 35 |
| 36 private: |
| 37 // Reference back to the owning MediaRouterUI instance. |
| 38 MediaRouterUI* ui_; |
| 39 |
| 40 DISALLOW_COPY_AND_ASSIGN(UIIssuesObserver); |
| 41 }; |
| 42 |
| 43 class MediaRouterUI::UIMediaRoutesObserver : public MediaRoutesObserver { |
| 44 public: |
| 45 UIMediaRoutesObserver(MediaRouter* router, MediaRouterUI* ui) |
| 46 : MediaRoutesObserver(router), ui_(ui) { |
| 47 DCHECK(ui_); |
| 48 } |
| 49 |
| 50 void OnRoutesUpdated(const std::vector<MediaRoute>& routes) override { |
| 51 ui_->OnRoutesUpdated(routes); |
| 52 } |
| 53 |
| 54 private: |
| 55 // Reference back to the owning MediaRouterUI instance. |
| 56 MediaRouterUI* ui_; |
| 57 |
| 58 DISALLOW_COPY_AND_ASSIGN(UIMediaRoutesObserver); |
| 59 }; |
| 60 |
| 20 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui) | 61 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui) |
| 21 : ConstrainedWebDialogUI(web_ui), | 62 : ConstrainedWebDialogUI(web_ui), |
| 22 handler_(new MediaRouterWebUIMessageHandler()) { | 63 handler_(new MediaRouterWebUIMessageHandler()), |
| 64 ui_initialized_(false), |
| 65 has_pending_route_request_(false), |
| 66 router_(nullptr), |
| 67 weak_factory_(this) { |
| 23 // Create a WebUIDataSource containing the chrome://media-router page's | 68 // Create a WebUIDataSource containing the chrome://media-router page's |
| 24 // content. | 69 // content. |
| 25 scoped_ptr<content::WebUIDataSource> html_source( | 70 scoped_ptr<content::WebUIDataSource> html_source( |
| 26 content::WebUIDataSource::Create(chrome::kChromeUIMediaRouterHost)); | 71 content::WebUIDataSource::Create(chrome::kChromeUIMediaRouterHost)); |
| 27 AddLocalizedStrings(html_source.get()); | 72 AddLocalizedStrings(html_source.get()); |
| 28 AddMediaRouterUIResources(html_source.get()); | 73 AddMediaRouterUIResources(html_source.get()); |
| 29 Profile* profile = Profile::FromWebUI(web_ui); | |
| 30 // Ownership of |html_source| is transferred to the BrowserContext. | 74 // Ownership of |html_source| is transferred to the BrowserContext. |
| 31 content::WebUIDataSource::Add(profile, html_source.release()); | 75 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui), |
| 76 html_source.release()); |
| 77 |
| 32 // Ownership of |handler_| is transferred to |web_ui|. | 78 // Ownership of |handler_| is transferred to |web_ui|. |
| 33 web_ui->AddMessageHandler(handler_); | 79 web_ui->AddMessageHandler(handler_); |
| 80 |
| 81 content::WebContents* wc = web_ui->GetWebContents(); |
| 82 DCHECK(wc); |
| 83 |
| 84 router_ = MediaRouterMojoImplFactory::GetApiForBrowserContext( |
| 85 wc->GetBrowserContext()); |
| 86 DCHECK(router_); |
| 87 |
| 88 // Register for Issue and MediaRoute updates. |
| 89 issues_observer_.reset(new UIIssuesObserver(this)); |
| 90 routes_observer_.reset(new UIMediaRoutesObserver(router_, this)); |
| 34 } | 91 } |
| 35 | 92 |
| 36 MediaRouterUI::~MediaRouterUI() { | 93 MediaRouterUI::~MediaRouterUI() { |
| 94 if (query_result_manager_.get()) |
| 95 query_result_manager_->RemoveObserver(this); |
| 37 } | 96 } |
| 38 | 97 |
| 39 void MediaRouterUI::Close() { | 98 void MediaRouterUI::Close() { |
| 40 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate(); | 99 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate(); |
| 41 if (delegate) { | 100 if (delegate) { |
| 42 delegate->GetWebDialogDelegate()->OnDialogClosed(std::string()); | 101 delegate->GetWebDialogDelegate()->OnDialogClosed(std::string()); |
| 43 delegate->OnDialogCloseFromWebUI(); | 102 delegate->OnDialogCloseFromWebUI(); |
| 44 } | 103 } |
| 45 } | 104 } |
| 46 | 105 |
| 106 void MediaRouterUI::UIInitialized() { |
| 107 ui_initialized_ = true; |
| 108 } |
| 109 |
| 110 bool MediaRouterUI::CreateRoute(const MediaSinkId& sink_id) { |
| 111 return DoCreateRoute(sink_id, GetPreferredCastMode(cast_modes_)); |
| 112 } |
| 113 |
| 114 bool MediaRouterUI::CreateRouteWithCastModeOverride( |
| 115 const MediaSinkId& sink_id, |
| 116 MediaCastMode cast_mode_override) { |
| 117 // NOTE: It's actually not an override if |
| 118 // |cast_mode_override| == |GetPreferredCastMode(cast_modes_)|. |
| 119 return DoCreateRoute(sink_id, cast_mode_override); |
| 120 } |
| 121 |
| 122 void MediaRouterUI::CloseRoute(const MediaRouteId& route_id) { |
| 123 router_->CloseRoute(route_id); |
| 124 } |
| 125 |
| 126 void MediaRouterUI::ClearIssue(const std::string& issue_id) { |
| 127 router_->ClearIssue(issue_id); |
| 128 } |
| 129 |
| 130 std::string MediaRouterUI::GetInitialHeaderText() const { |
| 131 if (cast_modes_.empty()) |
| 132 return std::string(); |
| 133 |
| 134 // TODO(imcheng): Pass in source_host_ once DEFAULT mode is upstreamed. |
| 135 return MediaCastModeToTitle(GetPreferredCastMode(cast_modes_), std::string()); |
| 136 } |
| 137 |
| 138 void MediaRouterUI::OnResultsUpdated( |
| 139 const std::vector<MediaSinkWithCastModes>& sinks) { |
| 140 sinks_ = sinks; |
| 141 if (ui_initialized_) |
| 142 handler_->UpdateSinks(sinks_); |
| 143 } |
| 144 |
| 145 void MediaRouterUI::SetIssue(const Issue* issue) { |
| 146 if (ui_initialized_) |
| 147 handler_->UpdateIssue(issue); |
| 148 } |
| 149 |
| 150 void MediaRouterUI::OnRoutesUpdated(const std::vector<MediaRoute>& routes) { |
| 151 routes_ = routes; |
| 152 if (ui_initialized_) |
| 153 handler_->UpdateRoutes(routes_); |
| 154 } |
| 155 |
| 156 void MediaRouterUI::OnRouteResponseReceived(scoped_ptr<MediaRoute> route, |
| 157 const std::string& error) { |
| 158 DVLOG(1) << "OnRouteResponseReceived"; |
| 159 // TODO(imcheng): Display error in UI. (crbug.com/490372) |
| 160 if (!route) |
| 161 LOG(ERROR) << "MediaRouteResponse returned error: " << error; |
| 162 else |
| 163 handler_->AddRoute(*route); |
| 164 |
| 165 has_pending_route_request_ = false; |
| 166 } |
| 167 |
| 168 bool MediaRouterUI::DoCreateRoute(const MediaSinkId& sink_id, |
| 169 MediaCastMode cast_mode) { |
| 170 DCHECK(query_result_manager_.get()); |
| 171 |
| 172 // Note that there is a rarely-encountered bug, where the MediaCastMode to |
| 173 // MediaSource mapping could have been updated, between when the user |
| 174 // clicked on the UI to start a create route request, |
| 175 // and when this function is called. |
| 176 // However, since the user does not have visibility into the MediaSource, and |
| 177 // that it occurs very rarely in practice, we leave it as-is for now. |
| 178 MediaSource source = query_result_manager_->GetSourceForCastMode(cast_mode); |
| 179 if (source.Empty()) { |
| 180 LOG(ERROR) << "No corresponding MediaSource for cast mode " << cast_mode; |
| 181 return false; |
| 182 } |
| 183 |
| 184 has_pending_route_request_ = true; |
| 185 router_->CreateRoute(source.id(), sink_id, |
| 186 base::Bind(&MediaRouterUI::OnRouteResponseReceived, |
| 187 weak_factory_.GetWeakPtr())); |
| 188 return true; |
| 189 } |
| 190 |
| 47 } // namespace media_router | 191 } // namespace media_router |
| 48 | 192 |
| OLD | NEW |