Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(174)

Side by Side Diff: chrome/browser/ui/webui/media_router/media_router_ui.cc

Issue 1139203003: [Media Router] MediaRouterUI + WebUI handler implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 namespace {
24
25 // Observes when issue to show in UI has been updated.
Wez 2015/05/20 17:51:45 nit: Can we word this to be clearer; this class ca
imcheng (use chromium acct) 2015/05/20 22:01:12 Done according to the document in IssuesObserver.
26 class UIIssuesObserver : public IssuesObserver {
Wez 2015/05/20 17:51:45 nit: UiIssuesObserver
imcheng (use chromium acct) 2015/05/20 22:01:12 Acknowledged.
27 public:
28 explicit UIIssuesObserver(MediaRouterUI* ui) : ui_(ui) {
29 DCHECK(ui);
30 }
31
32 ~UIIssuesObserver() override {}
33
34 // IssuesObserver implementation.
35 void OnIssueUpdated(const Issue* issue) override {
36 ui_->SetIssue(issue);
37 }
38
39 private:
40 // Not owned by this class.
Wez 2015/05/20 17:51:46 Rather than "not owned by" I'd suggest something l
imcheng (use chromium acct) 2015/05/20 22:01:11 Done.
41 MediaRouterUI* ui_;
42
43 DISALLOW_COPY_AND_ASSIGN(UIIssuesObserver);
44 };
45
46 class UIMediaRoutesObserver : public MediaRoutesObserver {
47 public:
48 UIMediaRoutesObserver(MediaRouter* router, MediaRouterUI* ui)
49 : MediaRoutesObserver(router), ui_(ui) {
50 DCHECK(ui_);
51 }
52
53 void OnRoutesUpdated(const std::vector<MediaRoute>& routes) override {
54 ui_->OnRoutesUpdated(routes);
55 }
56
57 private:
58 // Not owned by this class.
59 MediaRouterUI* ui_;
60
61 DISALLOW_COPY_AND_ASSIGN(UIMediaRoutesObserver);
62 };
63
64 } // namespace
65
20 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui) 66 MediaRouterUI::MediaRouterUI(content::WebUI* web_ui)
21 : ConstrainedWebDialogUI(web_ui), 67 : ConstrainedWebDialogUI(web_ui),
22 handler_(new MediaRouterWebUIMessageHandler()) { 68 handler_(new MediaRouterWebUIMessageHandler()),
69 ui_initialized_(false),
Wez 2015/05/20 17:51:45 nit: is_initialized_?
imcheng (use chromium acct) 2015/05/20 22:01:12 See other comment.
70 has_pending_route_request_(false),
71 router_(nullptr),
72 weak_factory_(this) {
23 // Create a WebUIDataSource containing the chrome://media-router page's 73 // Create a WebUIDataSource containing the chrome://media-router page's
24 // content. 74 // content.
25 scoped_ptr<content::WebUIDataSource> html_source( 75 scoped_ptr<content::WebUIDataSource> html_source(
26 content::WebUIDataSource::Create(chrome::kChromeUIMediaRouterHost)); 76 content::WebUIDataSource::Create(chrome::kChromeUIMediaRouterHost));
27 AddLocalizedStrings(html_source.get()); 77 AddLocalizedStrings(html_source.get());
28 AddMediaRouterUIResources(html_source.get()); 78 AddMediaRouterUIResources(html_source.get());
29 Profile* profile = Profile::FromWebUI(web_ui);
30 // Ownership of |html_source| is transferred to the BrowserContext. 79 // Ownership of |html_source| is transferred to the BrowserContext.
31 content::WebUIDataSource::Add(profile, html_source.release()); 80 content::WebUIDataSource::Add(Profile::FromWebUI(web_ui),
81 html_source.release());
Wez 2015/05/20 17:51:45 nit: Blank line after this, for consistency.
imcheng (use chromium acct) 2015/05/20 22:01:12 Done.
32 // Ownership of |handler_| is transferred to |web_ui|. 82 // Ownership of |handler_| is transferred to |web_ui|.
33 web_ui->AddMessageHandler(handler_); 83 web_ui->AddMessageHandler(handler_);
84
85 content::WebContents* wc = web_ui->GetWebContents();
86 DCHECK(wc);
87
88 /* TODO(imcheng): Uncomment once Kevin's MediaRouterMojoImpl patch landed.
Wez 2015/05/20 17:51:45 What's the bug # for that patch? Does this class
imcheng (use chromium acct) 2015/05/20 22:01:12 If it's null then it will just crash at some point
Wez 2015/05/21 22:58:36 Acknowledged.
89 router_ = MediaRouterMojoImplFactory::GetApiForBrowserContext(
90 wc->GetBrowserContext());
91 DCHECK(router_);
92 */
93
94 issues_observer_.reset(new UIIssuesObserver(this));
95 routes_observer_.reset(new UIMediaRoutesObserver(router_, this));
Wez 2015/05/20 17:51:45 nit: Suggest a comment on this block "Register for
imcheng (use chromium acct) 2015/05/20 22:01:12 Done.
34 } 96 }
35 97
36 MediaRouterUI::~MediaRouterUI() { 98 MediaRouterUI::~MediaRouterUI() {
99 if (query_result_manager_.get())
100 query_result_manager_->RemoveObserver(this);
101 }
102
103 void MediaRouterUI::OnResultsUpdated(
104 const std::vector<MediaSinkWithCastModes>& sinks) {
105 sinks_ = sinks;
106 if (ui_initialized_)
107 handler_->UpdateSinks(sinks_);
108 }
109
110 void MediaRouterUI::OnRoutesUpdated(const std::vector<MediaRoute>& routes) {
111 routes_ = routes;
112 if (ui_initialized_)
113 handler_->UpdateRoutes(routes_);
37 } 114 }
38 115
39 void MediaRouterUI::Close() { 116 void MediaRouterUI::Close() {
40 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate(); 117 ConstrainedWebDialogDelegate* delegate = GetConstrainedDelegate();
41 if (delegate) { 118 if (delegate) {
42 delegate->GetWebDialogDelegate()->OnDialogClosed(std::string()); 119 delegate->GetWebDialogDelegate()->OnDialogClosed(std::string());
43 delegate->OnDialogCloseFromWebUI(); 120 delegate->OnDialogCloseFromWebUI();
44 } 121 }
45 } 122 }
46 123
124 void MediaRouterUI::UIInitialized() {
125 ui_initialized_ = true;
126 }
127
128 bool MediaRouterUI::CreateRoute(const MediaSinkId& sink_id) {
129 return DoCreateRoute(sink_id, GetPreferredCastMode(cast_modes_));
Wez 2015/05/20 17:51:46 Where does cast_modes_ ever get set..?
imcheng (use chromium acct) 2015/05/20 22:01:12 In the Init* functions to be upstreamed separately
Wez 2015/05/21 22:58:36 Acknowledged.
130 }
131
132 bool MediaRouterUI::CreateRouteWithCastModeOverride(
Wez 2015/05/20 17:51:45 Why do we need the word "override"? i.e. not Creat
imcheng (use chromium acct) 2015/05/20 22:01:11 See other comment.
133 const MediaSinkId& sink_id, MediaCastMode cast_mode_override) {
134 // NOTE: It's actually not an override if
135 // |cast_mode_override| == |GetPreferredCastMode(cast_modes_)|.
Wez 2015/05/20 17:51:45 Not sure what this comment is intended to achieve?
imcheng (use chromium acct) 2015/05/20 22:01:12 It's for later when we pass in an override flag to
Wez 2015/05/21 22:58:36 Is the distinction important?
imcheng (use chromium acct) 2015/05/22 00:02:00 Probably. We don't want to pass in that flag if th
136 return DoCreateRoute(sink_id, cast_mode_override);
137 }
138
139 void MediaRouterUI::CloseRoute(const MediaRouteId& route_id) {
140 router_->CloseRoute(route_id);
141 }
142
143 void MediaRouterUI::SetIssue(const Issue* issue) {
144 if (ui_initialized_)
145 handler_->UpdateIssue(issue);
146 }
147
148 void MediaRouterUI::ClearIssue(const std::string& issue_id) {
149 // TODO(imcheng): Uncomment once Kevin's MediaRouterMojoImpl patch landed.
Wez 2015/05/20 17:51:45 See above re bug #.
imcheng (use chromium acct) 2015/05/20 22:01:11 Done.
150 // router_->ClearIssue(issue_id);
151 }
152
153 std::string MediaRouterUI::GetInitialHeaderText() const {
154 if (cast_modes_.empty())
155 return std::string();
156
157 return MediaCastModeToTitle(GetPreferredCastMode(cast_modes_), source_host_);
158 }
159
160 void MediaRouterUI::OnRouteResponseReceived(scoped_ptr<MediaRoute> route,
161 const std::string& error) {
162 DVLOG(1) << "OnRouteResponseReceived";
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(
172 const MediaSinkId& sink_id, MediaCastMode cast_mode) {
173 DCHECK(query_result_manager_.get());
174
175 // Note that it is possible for this source to be different from
176 // the MediaSource in MediaRouterUI if an update occurred between the user
Wez 2015/05/20 17:51:45 It's not clear what "the MediaSource in MediaRoute
imcheng (use chromium acct) 2015/05/20 22:01:12 This describes a case that is theoretically possib
Wez 2015/05/21 22:58:36 OK, so I think you need to make clear that what yo
imcheng (use chromium acct) 2015/05/22 00:02:00 Done.
177 // click and invocation of this function.
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 router_->CreateRoute(source.id(), sink_id,
185 base::Bind(&MediaRouterUI::OnRouteResponseReceived,
Wez 2015/05/20 17:51:46 nit: Indentation - consider git cl format. :)
imcheng (use chromium acct) 2015/05/20 22:01:12 Done.
186 weak_factory_.GetWeakPtr()));
187
188 has_pending_route_request_ = true;
189 return true;
190 }
191
47 } // namespace media_router 192 } // namespace media_router
48 193
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698