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

Side by Side Diff: chrome/browser/ui/webui/media_router/media_router_webui_message_handler.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_webui_message_handle r.h" 5 #include "chrome/browser/ui/webui/media_router/media_router_webui_message_handle r.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" 8 #include "chrome/browser/ui/webui/media_router/media_router_ui.h"
9 #include "chrome/grit/generated_resources.h"
9 #include "content/public/browser/web_ui.h" 10 #include "content/public/browser/web_ui.h"
11 #include "ui/base/l10n/l10n_util.h"
10 12
11 namespace media_router { 13 namespace media_router {
12 14
13 namespace { 15 namespace {
14 16
15 // Message names. 17 // Message names.
16 const char kGetInitialSettings[] = "getInitialSettings"; 18 const char kGetInitialSettings[] = "getInitialSettings";
17 const char kCreateRoute[] = "requestRoute"; 19 const char kCreateRoute[] = "requestRoute";
18 const char kActOnIssue[] = "actOnIssue"; 20 const char kActOnIssue[] = "actOnIssue";
19 const char kCloseRoute[] = "closeRoute"; 21 const char kCloseRoute[] = "closeRoute";
20 const char kCloseDialog[] = "closeDialog"; 22 const char kCloseDialog[] = "closeDialog";
21 23
22 // TODO(imcheng): Define JS function names here. 24 // Callback names.
Wez 2015/05/20 17:51:47 Are these actually callback names, or event/notifi
imcheng (use chromium acct) 2015/05/20 22:01:14 These are JS function names.
25 const char kSetInitialSettings[] = "media_router.setInitialSettings";
26 const char kAddRoute[] = "media_router.ui.addRoute";
27 const char kSetSinkList[] = "media_router.ui.setSinkList";
28 const char kSetRouteList[] = "media_router.ui.setRouteList";
29 const char kSetCastModeList[] = "media_router.ui.setCastModeList";
30
31 void SetSinksListValue(
Wez 2015/05/20 17:51:47 nit: As below, can you just have this accept the s
imcheng (use chromium acct) 2015/05/20 22:01:14 Done.
32 const std::vector<MediaSinkWithCastModes>& sinks,
33 base::ListValue* value) {
34 value->Clear();
35
36 for (const MediaSinkWithCastModes& sink_with_cast_modes : sinks) {
Wez 2015/05/20 17:51:47 nit: Suggest adding some blank lines in here to br
imcheng (use chromium acct) 2015/05/20 22:01:14 Done.
37 const MediaSink& sink = sink_with_cast_modes.sink;
38 scoped_ptr<base::DictionaryValue> sink_val(new base::DictionaryValue);
39 sink_val->SetString("id", sink.id());
40 sink_val->SetString("name", sink.name());
41 scoped_ptr<base::ListValue> cast_modes_val(new base::ListValue);
42 for (MediaCastMode cast_mode : sink_with_cast_modes.cast_modes)
43 cast_modes_val->AppendInteger(cast_mode);
44 sink_val->Set("castModes", cast_modes_val.Pass());
45 value->Append(sink_val.release());
46 }
47 }
48
49 void SetMediaRouteDictionary(
Wez 2015/05/20 17:51:47 This name's not very descriptive - suggest e.g. Me
imcheng (use chromium acct) 2015/05/20 22:01:14 Renamed to RouteToValue and similarly for others.
50 const MediaRoute& route, base::DictionaryValue* dictionary) {
51 dictionary->SetString("id", route.media_route_id());
52 dictionary->SetString("sinkId", route.media_sink().id());
53 dictionary->SetString("title", route.description());
54 dictionary->SetBoolean("isLocal", route.is_local());
55 }
56
57 void SetRoutesListValue(
58 const std::vector<MediaRoute>& routes,
59 base::ListValue* value) {
Wez 2015/05/20 17:51:47 See comments on preceding methods re naming and sc
imcheng (use chromium acct) 2015/05/20 22:01:14 Done.
60 value->Clear();
61
62 for (const MediaRoute& route : routes) {
63 scoped_ptr<base::DictionaryValue> route_val(new base::DictionaryValue);
64 SetMediaRouteDictionary(route, route_val.get());
65 value->Append(route_val.release());
66 }
67 }
68
69 void SetCastModesListValue(
70 const CastModeSet& cast_modes,
71 const std::string& source_host,
72 base::ListValue* value) {
Wez 2015/05/20 17:51:47 Same here.
imcheng (use chromium acct) 2015/05/20 22:01:14 Done.
73 value->Clear();
74
75 for (const MediaCastMode& cast_mode : cast_modes) {
76 scoped_ptr<base::DictionaryValue> cast_mode_val(new base::DictionaryValue);
77 cast_mode_val->SetInteger("type", cast_mode);
78 cast_mode_val->SetString(
79 "title", MediaCastModeToTitle(cast_mode, source_host));
80 cast_mode_val->SetString(
81 "description", MediaCastModeToDescription(cast_mode, source_host));
82 value->Append(cast_mode_val.release());
83 }
84 }
23 85
24 } // namespace 86 } // namespace
25 87
26 MediaRouterWebUIMessageHandler::MediaRouterWebUIMessageHandler() 88 MediaRouterWebUIMessageHandler::MediaRouterWebUIMessageHandler()
27 : dialog_closing_(false) { 89 : dialog_closing_(false) {
28 } 90 }
29 91
30 MediaRouterWebUIMessageHandler::~MediaRouterWebUIMessageHandler() { 92 MediaRouterWebUIMessageHandler::~MediaRouterWebUIMessageHandler() {
31 } 93 }
32 94
(...skipping 13 matching lines...) Expand all
46 web_ui()->RegisterMessageCallback( 108 web_ui()->RegisterMessageCallback(
47 kCloseRoute, 109 kCloseRoute,
48 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseRoute, 110 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseRoute,
49 base::Unretained(this))); 111 base::Unretained(this)));
50 web_ui()->RegisterMessageCallback( 112 web_ui()->RegisterMessageCallback(
51 kCloseDialog, 113 kCloseDialog,
52 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseDialog, 114 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseDialog,
53 base::Unretained(this))); 115 base::Unretained(this)));
54 } 116 }
55 117
118 void MediaRouterWebUIMessageHandler::UpdateSinks(
119 const std::vector<MediaSinkWithCastModes>& sinks) {
120 base::ListValue sinks_val;
121 SetSinksListValue(sinks, &sinks_val);
122 web_ui()->CallJavascriptFunction(kSetSinkList, sinks_val);
123 }
124
125 void MediaRouterWebUIMessageHandler::UpdateRoutes(
126 const std::vector<MediaRoute>& routes) {
127 base::ListValue routes_val;
128 SetRoutesListValue(routes, &routes_val);
129 web_ui()->CallJavascriptFunction(kSetRouteList, routes_val);
130 }
131
132 void MediaRouterWebUIMessageHandler::UpdateCastModes(
133 const CastModeSet& cast_modes, const std::string& source_host) {
134 base::ListValue cast_modes_val;
135 SetCastModesListValue(cast_modes, source_host, &cast_modes_val);
136
Wez 2015/05/20 17:51:47 nit: Why the blank line here - you don't have one
imcheng (use chromium acct) 2015/05/20 22:01:14 Done.
137 web_ui()->CallJavascriptFunction(kSetCastModeList, cast_modes_val);
138 }
139
140 void MediaRouterWebUIMessageHandler::AddRoute(const MediaRoute& route) {
141 DVLOG(1) << "AddRoute";
Wez 2015/05/20 17:51:47 nit: Why does AddRoute deserve a DVLOG but not the
imcheng (use chromium acct) 2015/05/20 22:01:13 Added DVLOG(2) to public methods.
Wez 2015/05/21 22:58:37 Acknowledged.
142
143 base::DictionaryValue route_value;
144 SetMediaRouteDictionary(route, &route_value);
145
146 web_ui()->CallJavascriptFunction(kAddRoute, route_value);
147 }
148
149 void MediaRouterWebUIMessageHandler::UpdateIssue(const Issue* issue) {
150 // TODO(imcheng): Implement conversion from Issue to dictionary object.
151 NOTIMPLEMENTED();
152 }
153
56 void MediaRouterWebUIMessageHandler::OnGetInitialSettings( 154 void MediaRouterWebUIMessageHandler::OnGetInitialSettings(
57 const base::ListValue* args) { 155 const base::ListValue* args) {
58 // TODO(imcheng): Implement. 156 MediaRouterUI* media_router_ui = GetMediaRouterUI();
59 NOTIMPLEMENTED(); 157
158 base::DictionaryValue initial_settings;
159
160 initial_settings.SetString("headerText",
161 media_router_ui->GetInitialHeaderText());
162
163 scoped_ptr<base::ListValue> sinks(new base::ListValue);
164 SetSinksListValue(media_router_ui->sinks(), sinks.get());
165 initial_settings.Set("sinks", sinks.release());
166
167 scoped_ptr<base::ListValue> routes(new base::ListValue);
168 SetRoutesListValue(media_router_ui->routes(), routes.get());
169 initial_settings.Set("routes", routes.release());
170
171 scoped_ptr<base::ListValue> cast_modes(new base::ListValue);
172 SetCastModesListValue(
173 media_router_ui->cast_modes(),
174 media_router_ui->source_host(),
175 cast_modes.get());
176 initial_settings.Set("castModes", cast_modes.release());
177
178 web_ui()->CallJavascriptFunction(kSetInitialSettings, initial_settings);
179 media_router_ui->UIInitialized();
60 } 180 }
61 181
62 void MediaRouterWebUIMessageHandler::OnCreateRoute( 182 void MediaRouterWebUIMessageHandler::OnCreateRoute(
63 const base::ListValue* args) { 183 const base::ListValue* args) {
64 // TODO(imcheng): Implement. 184 // Note that it is possible for this source to be different from
Wez 2015/05/20 17:51:47 What is "this source"?
imcheng (use chromium acct) 2015/05/20 22:01:14 Removed comment as it is duplicated with MRUI.
65 NOTIMPLEMENTED(); 185 // the MediaSource in MediaRouterUI if an update occurred between the user
186 // click and invocation of this function. But this is a rare edge case since
187 // sources don't change in practice.
188 std::string sink_id;
189 int cast_mode_num;
190 if (!args->GetString(0, &sink_id) || !args->GetInteger(1, &cast_mode_num)) {
191 LOG(ERROR) << "Unable to extract args.";
192 return;
193 }
194
195 if (sink_id.empty()) {
196 LOG(ERROR) << "Media Route Provider Manager did not respond with a "
197 << "valid sink ID. Aborting.";
Wez 2015/05/20 17:51:47 nit: Consider folding this into the "Unable to ext
imcheng (use chromium acct) 2015/05/20 22:01:13 Yes. It is checked below.
198 return;
199 }
200
201 MediaRouterUI* media_router_ui =
202 static_cast<MediaRouterUI*>(web_ui()->GetController());
203 if (media_router_ui->has_pending_route_request()) {
204 LOG(ERROR) << "UI already has pending route request. Ignoring.";
205 return;
206 }
207
208 DVLOG(1) << "sink id: " << sink_id << ", cast mode: " << cast_mode_num;
209
210 // TODO(haibinlu): add parameters to media source according to the cast mode
211 // before sending to MRPM, e.g. low-fps-mirror, user-override.
Wez 2015/05/20 17:51:47 Can you re-word this comment to be clearer - I thi
imcheng (use chromium acct) 2015/05/20 22:01:14 Done. Added tracking bug.
212 bool success = false;
213 if (IsValidCastModeNum(cast_mode_num)) {
214 // Users explicitly selected cast mode.
Wez 2015/05/20 17:51:47 nit: Users->User
imcheng (use chromium acct) 2015/05/20 22:01:14 Done.
215 DVLOG(1) << "Cast mode override: " << cast_mode_num;
216 success = media_router_ui->CreateRouteWithCastModeOverride(
217 sink_id, static_cast<MediaCastMode>(cast_mode_num));
218 } else {
219 success = media_router_ui->CreateRoute(sink_id);
220 }
221
222 if (!success) {
223 // TODO(imcheng): Display error in UI.
Wez 2015/05/20 17:51:47 Do we have a bug filed for this follow-up work?
imcheng (use chromium acct) 2015/05/20 22:01:14 Done.
224 LOG(ERROR) << "Error initiating route request.";
225 }
66 } 226 }
67 227
68 void MediaRouterWebUIMessageHandler::OnActOnIssue( 228 void MediaRouterWebUIMessageHandler::OnActOnIssue(
69 const base::ListValue* args) { 229 const base::ListValue* args) {
70 // TODO(imcheng): Implement. 230
Wez 2015/05/20 17:51:47 Still doesn't look to be implemented..?
imcheng (use chromium acct) 2015/05/20 22:01:14 Reverted.
71 NOTIMPLEMENTED();
72 } 231 }
73 232
74 void MediaRouterWebUIMessageHandler::OnCloseRoute( 233 void MediaRouterWebUIMessageHandler::OnCloseRoute(
75 const base::ListValue* args) { 234 const base::ListValue* args) {
76 // TODO(imcheng): Implement. 235 // TODO(imcheng): Implement.
77 NOTIMPLEMENTED(); 236 NOTIMPLEMENTED();
Wez 2015/05/20 17:51:47 Looks like this should be removed?
imcheng (use chromium acct) 2015/05/20 22:01:14 Done.
237 DVLOG(1) << "OnCloseRoute";
238 std::string route_id;
239 if (!args->GetString(0, &route_id)) {
240 DVLOG(1) << "Unable to extract args.";
241 return;
242 }
243 GetMediaRouterUI()->CloseRoute(route_id);
78 } 244 }
79 245
80 void MediaRouterWebUIMessageHandler::OnCloseDialog( 246 void MediaRouterWebUIMessageHandler::OnCloseDialog(
81 const base::ListValue* args) { 247 const base::ListValue* args) {
82 CHECK(!dialog_closing_); 248 if (dialog_closing_)
Wez 2015/05/20 17:51:47 How can we get OnCloseDialog if it's already closi
imcheng (use chromium acct) 2015/05/20 22:01:14 If we can manage to call it again before it is act
Wez 2015/05/21 22:58:37 Acknowledged.
249 return;
250
83 dialog_closing_ = true; 251 dialog_closing_ = true;
84 GetMediaRouterUI()->Close(); 252 GetMediaRouterUI()->Close();
85 } 253 }
86 254
87 MediaRouterUI* MediaRouterWebUIMessageHandler::GetMediaRouterUI() const { 255 MediaRouterUI* MediaRouterWebUIMessageHandler::GetMediaRouterUI() const {
88 MediaRouterUI* media_router_ui = 256 MediaRouterUI* media_router_ui =
89 static_cast<MediaRouterUI*>(web_ui()->GetController()); 257 static_cast<MediaRouterUI*>(web_ui()->GetController());
90 DCHECK(media_router_ui); 258 DCHECK(media_router_ui);
91 return media_router_ui; 259 return media_router_ui;
92 } 260 }
93 261
94 } // namespace media_router 262 } // namespace media_router
95 263
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698