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