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_webui_message_handle r.h" | 5 #include "chrome/browser/ui/webui/media_router/media_router_webui_message_handle r.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "chrome/browser/media/router/issue.h" | 12 #include "chrome/browser/media/router/issue.h" |
| 13 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" | 13 #include "chrome/browser/ui/webui/media_router/media_router_ui.h" |
| 14 #include "chrome/grit/generated_resources.h" | 14 #include "chrome/grit/generated_resources.h" |
| 15 #include "extensions/common/constants.h" | 15 #include "extensions/common/constants.h" |
| 16 #include "ui/base/l10n/l10n_util.h" | 16 #include "ui/base/l10n/l10n_util.h" |
| 17 | 17 |
| 18 namespace media_router { | 18 namespace media_router { |
| 19 | 19 |
| 20 namespace { | 20 namespace { |
| 21 | 21 |
| 22 const char kHelpPageUrlPrefix[] = | 22 const char kHelpPageUrlPrefix[] = |
| 23 "https://support.google.com/chromecast/answer/%d"; | 23 "https://support.google.com/chromecast/answer/%d"; |
| 24 | 24 |
| 25 // Message names. | 25 // Message names. |
| 26 const char kRequestInitialData[] = "requestInitialData"; | 26 const char kRequestInitialData[] = "requestInitialData"; |
| 27 const char kCreateRoute[] = "requestRoute"; | 27 const char kCreateRoute[] = "requestRoute"; |
| 28 const char kActOnIssue[] = "actOnIssue"; | 28 const char kActOnIssue[] = "actOnIssue"; |
| 29 const char kCloseRoute[] = "closeRoute"; | 29 const char kCloseRoute[] = "closeRoute"; |
| 30 const char kJoinRoute[] = "joinRoute"; | |
| 30 const char kCloseDialog[] = "closeDialog"; | 31 const char kCloseDialog[] = "closeDialog"; |
| 31 const char kReportSinkCount[] = "reportSinkCount"; | 32 const char kReportSinkCount[] = "reportSinkCount"; |
| 32 | 33 |
| 33 // JS function names. | 34 // JS function names. |
| 34 const char kSetInitialData[] = "media_router.ui.setInitialData"; | 35 const char kSetInitialData[] = "media_router.ui.setInitialData"; |
| 35 const char kNotifyRouteCreationTimeout[] = | 36 const char kNotifyRouteCreationTimeout[] = |
| 36 "media_router.ui.onNotifyRouteCreationTimeout"; | 37 "media_router.ui.onNotifyRouteCreationTimeout"; |
| 37 const char kOnCreateRouteResponseReceived[] = | 38 const char kOnCreateRouteResponseReceived[] = |
| 38 "media_router.ui.onCreateRouteResponseReceived"; | 39 "media_router.ui.onCreateRouteResponseReceived"; |
| 39 const char kSetIssue[] = "media_router.ui.setIssue"; | 40 const char kSetIssue[] = "media_router.ui.setIssue"; |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 59 cast_modes_val->AppendInteger(cast_mode); | 60 cast_modes_val->AppendInteger(cast_mode); |
| 60 sink_val->Set("castModes", cast_modes_val.Pass()); | 61 sink_val->Set("castModes", cast_modes_val.Pass()); |
| 61 | 62 |
| 62 value->Append(sink_val.release()); | 63 value->Append(sink_val.release()); |
| 63 } | 64 } |
| 64 | 65 |
| 65 return value.Pass(); | 66 return value.Pass(); |
| 66 } | 67 } |
| 67 | 68 |
| 68 scoped_ptr<base::DictionaryValue> RouteToValue( | 69 scoped_ptr<base::DictionaryValue> RouteToValue( |
| 69 const MediaRoute& route, const std::string& extension_id) { | 70 const MediaRoute& route, bool canJoin, const std::string& extension_id) { |
| 70 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue); | 71 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue); |
| 71 | 72 |
| 72 dictionary->SetString("id", route.media_route_id()); | 73 dictionary->SetString("id", route.media_route_id()); |
| 73 dictionary->SetString("sinkId", route.media_sink_id()); | 74 dictionary->SetString("sinkId", route.media_sink_id()); |
| 74 dictionary->SetString("description", route.description()); | 75 dictionary->SetString("description", route.description()); |
| 75 dictionary->SetBoolean("isLocal", route.is_local()); | 76 dictionary->SetBoolean("isLocal", route.is_local()); |
| 77 dictionary->SetBoolean("canJoin", canJoin); | |
| 76 | 78 |
| 77 const std::string& custom_path = route.custom_controller_path(); | 79 const std::string& custom_path = route.custom_controller_path(); |
| 78 | 80 |
| 79 if (!custom_path.empty()) { | 81 if (!custom_path.empty()) { |
| 80 std::string full_custom_controller_path = base::StringPrintf("%s://%s/%s", | 82 std::string full_custom_controller_path = base::StringPrintf("%s://%s/%s", |
| 81 extensions::kExtensionScheme, extension_id.c_str(), | 83 extensions::kExtensionScheme, extension_id.c_str(), |
| 82 custom_path.c_str()); | 84 custom_path.c_str()); |
| 83 DCHECK(GURL(full_custom_controller_path).is_valid()); | 85 DCHECK(GURL(full_custom_controller_path).is_valid()); |
| 84 dictionary->SetString("customControllerPath", | 86 dictionary->SetString("customControllerPath", |
| 85 full_custom_controller_path); | 87 full_custom_controller_path); |
| 86 } | 88 } |
| 87 | 89 |
| 88 return dictionary.Pass(); | 90 return dictionary.Pass(); |
| 89 } | 91 } |
| 90 | 92 |
| 91 scoped_ptr<base::ListValue> RoutesToValue( | 93 scoped_ptr<base::ListValue> RoutesToValue( |
| 92 const std::vector<MediaRoute>& routes, const std::string& extension_id) { | 94 const std::vector<MediaRoute>& routes, |
| 95 const std::vector<MediaRoute::Id>& joinable_route_ids, | |
| 96 const std::string& extension_id) { | |
| 93 scoped_ptr<base::ListValue> value(new base::ListValue); | 97 scoped_ptr<base::ListValue> value(new base::ListValue); |
| 94 | 98 |
| 95 for (const MediaRoute& route : routes) { | 99 for (const MediaRoute& route : routes) { |
| 100 bool canJoin = ContainsValue(joinable_route_ids, route.media_route_id()); | |
| 96 scoped_ptr<base::DictionaryValue> route_val(RouteToValue(route, | 101 scoped_ptr<base::DictionaryValue> route_val(RouteToValue(route, |
| 97 extension_id)); | 102 canJoin, extension_id)); |
| 98 value->Append(route_val.release()); | 103 value->Append(route_val.release()); |
| 99 } | 104 } |
| 100 | 105 |
| 101 return value.Pass(); | 106 return value.Pass(); |
| 102 } | 107 } |
| 103 | 108 |
| 104 scoped_ptr<base::ListValue> CastModesToValue(const CastModeSet& cast_modes, | 109 scoped_ptr<base::ListValue> CastModesToValue(const CastModeSet& cast_modes, |
| 105 const std::string& source_host) { | 110 const std::string& source_host) { |
| 106 scoped_ptr<base::ListValue> value(new base::ListValue); | 111 scoped_ptr<base::ListValue> value(new base::ListValue); |
| 107 | 112 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 } | 177 } |
| 173 | 178 |
| 174 void MediaRouterWebUIMessageHandler::UpdateSinks( | 179 void MediaRouterWebUIMessageHandler::UpdateSinks( |
| 175 const std::vector<MediaSinkWithCastModes>& sinks) { | 180 const std::vector<MediaSinkWithCastModes>& sinks) { |
| 176 DVLOG(2) << "UpdateSinks"; | 181 DVLOG(2) << "UpdateSinks"; |
| 177 scoped_ptr<base::ListValue> sinks_val(SinksToValue(sinks)); | 182 scoped_ptr<base::ListValue> sinks_val(SinksToValue(sinks)); |
| 178 web_ui()->CallJavascriptFunction(kSetSinkList, *sinks_val); | 183 web_ui()->CallJavascriptFunction(kSetSinkList, *sinks_val); |
| 179 } | 184 } |
| 180 | 185 |
| 181 void MediaRouterWebUIMessageHandler::UpdateRoutes( | 186 void MediaRouterWebUIMessageHandler::UpdateRoutes( |
| 182 const std::vector<MediaRoute>& routes) { | 187 const std::vector<MediaRoute>& routes, |
| 183 DVLOG(2) << "UpdateRoutes"; | 188 const std::vector<MediaRoute::Id>& joinable_route_ids) { |
| 184 scoped_ptr<base::ListValue> routes_val(RoutesToValue(routes, | 189 scoped_ptr<base::ListValue> routes_val(RoutesToValue(routes, |
| 190 joinable_route_ids, | |
| 185 media_router_ui_->GetRouteProviderExtensionId())); | 191 media_router_ui_->GetRouteProviderExtensionId())); |
| 186 web_ui()->CallJavascriptFunction(kSetRouteList, *routes_val); | 192 web_ui()->CallJavascriptFunction( |
| 193 kSetRouteList, *routes_val); | |
|
apacible
2015/12/09 19:56:25
nit: Move back to previous line.
matt.boetger
2015/12/16 00:21:08
Done.
| |
| 187 } | 194 } |
| 188 | 195 |
| 189 void MediaRouterWebUIMessageHandler::UpdateCastModes( | 196 void MediaRouterWebUIMessageHandler::UpdateCastModes( |
| 190 const CastModeSet& cast_modes, | 197 const CastModeSet& cast_modes, |
| 191 const std::string& source_host) { | 198 const std::string& source_host) { |
| 192 DVLOG(2) << "UpdateCastModes"; | 199 DVLOG(2) << "UpdateCastModes"; |
| 193 scoped_ptr<base::ListValue> cast_modes_val( | 200 scoped_ptr<base::ListValue> cast_modes_val( |
| 194 CastModesToValue(cast_modes, source_host)); | 201 CastModesToValue(cast_modes, source_host)); |
| 195 web_ui()->CallJavascriptFunction(kSetCastModeList, *cast_modes_val); | 202 web_ui()->CallJavascriptFunction(kSetCastModeList, *cast_modes_val); |
| 196 } | 203 } |
| 197 | 204 |
| 198 void MediaRouterWebUIMessageHandler::OnCreateRouteResponseReceived( | 205 void MediaRouterWebUIMessageHandler::OnCreateRouteResponseReceived( |
| 199 const MediaSink::Id& sink_id, | 206 const MediaSink::Id& sink_id, |
| 200 const MediaRoute* route) { | 207 const MediaRoute* route) { |
| 201 DVLOG(2) << "OnCreateRouteResponseReceived"; | 208 DVLOG(2) << "OnCreateRouteResponseReceived"; |
| 202 if (route) { | 209 if (route) { |
| 203 scoped_ptr<base::DictionaryValue> route_value(RouteToValue(*route, | 210 scoped_ptr<base::DictionaryValue> route_value(RouteToValue(*route, false, |
| 204 media_router_ui_->GetRouteProviderExtensionId())); | 211 media_router_ui_->GetRouteProviderExtensionId())); |
| 205 web_ui()->CallJavascriptFunction(kOnCreateRouteResponseReceived, | 212 web_ui()->CallJavascriptFunction(kOnCreateRouteResponseReceived, |
| 206 base::StringValue(sink_id), *route_value); | 213 base::StringValue(sink_id), *route_value); |
| 207 UMA_HISTOGRAM_BOOLEAN("MediaRouter.Ui.Action.StartLocalSessionSuccessful", | 214 UMA_HISTOGRAM_BOOLEAN("MediaRouter.Ui.Action.StartLocalSessionSuccessful", |
| 208 true); | 215 true); |
| 209 } else { | 216 } else { |
| 210 web_ui()->CallJavascriptFunction(kOnCreateRouteResponseReceived, | 217 web_ui()->CallJavascriptFunction(kOnCreateRouteResponseReceived, |
| 211 base::StringValue(sink_id), | 218 base::StringValue(sink_id), |
| 212 *base::Value::CreateNullValue()); | 219 *base::Value::CreateNullValue()); |
| 213 UMA_HISTOGRAM_BOOLEAN("MediaRouter.Ui.Action.StartLocalSessionSuccessful", | 220 UMA_HISTOGRAM_BOOLEAN("MediaRouter.Ui.Action.StartLocalSessionSuccessful", |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 242 base::Unretained(this))); | 249 base::Unretained(this))); |
| 243 web_ui()->RegisterMessageCallback( | 250 web_ui()->RegisterMessageCallback( |
| 244 kActOnIssue, | 251 kActOnIssue, |
| 245 base::Bind(&MediaRouterWebUIMessageHandler::OnActOnIssue, | 252 base::Bind(&MediaRouterWebUIMessageHandler::OnActOnIssue, |
| 246 base::Unretained(this))); | 253 base::Unretained(this))); |
| 247 web_ui()->RegisterMessageCallback( | 254 web_ui()->RegisterMessageCallback( |
| 248 kCloseRoute, | 255 kCloseRoute, |
| 249 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseRoute, | 256 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseRoute, |
| 250 base::Unretained(this))); | 257 base::Unretained(this))); |
| 251 web_ui()->RegisterMessageCallback( | 258 web_ui()->RegisterMessageCallback( |
| 259 kJoinRoute, | |
| 260 base::Bind(&MediaRouterWebUIMessageHandler::OnJoinRoute, | |
| 261 base::Unretained(this))); | |
| 262 web_ui()->RegisterMessageCallback( | |
| 252 kCloseDialog, | 263 kCloseDialog, |
| 253 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseDialog, | 264 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseDialog, |
| 254 base::Unretained(this))); | 265 base::Unretained(this))); |
| 255 web_ui()->RegisterMessageCallback( | 266 web_ui()->RegisterMessageCallback( |
| 256 kReportSinkCount, | 267 kReportSinkCount, |
| 257 base::Bind(&MediaRouterWebUIMessageHandler::OnReportSinkCount, | 268 base::Bind(&MediaRouterWebUIMessageHandler::OnReportSinkCount, |
| 258 base::Unretained(this))); | 269 base::Unretained(this))); |
| 259 } | 270 } |
| 260 | 271 |
| 261 void MediaRouterWebUIMessageHandler::OnRequestInitialData( | 272 void MediaRouterWebUIMessageHandler::OnRequestInitialData( |
| 262 const base::ListValue* args) { | 273 const base::ListValue* args) { |
| 263 DVLOG(1) << "OnRequestInitialData"; | 274 DVLOG(1) << "OnRequestInitialData"; |
| 264 base::DictionaryValue initial_data; | 275 base::DictionaryValue initial_data; |
| 265 | 276 |
| 266 // "No Cast devices found?" Chromecast help center page. | 277 // "No Cast devices found?" Chromecast help center page. |
| 267 initial_data.SetString("deviceMissingUrl", | 278 initial_data.SetString("deviceMissingUrl", |
| 268 base::StringPrintf(kHelpPageUrlPrefix, 3249268)); | 279 base::StringPrintf(kHelpPageUrlPrefix, 3249268)); |
| 269 | 280 |
| 270 scoped_ptr<base::ListValue> sinks(SinksToValue(media_router_ui_->sinks())); | 281 scoped_ptr<base::ListValue> sinks(SinksToValue(media_router_ui_->sinks())); |
| 271 initial_data.Set("sinks", sinks.release()); | 282 initial_data.Set("sinks", sinks.release()); |
| 272 | 283 |
| 273 scoped_ptr<base::ListValue> routes(RoutesToValue(media_router_ui_->routes(), | 284 scoped_ptr<base::ListValue> routes(RoutesToValue(media_router_ui_->routes(), |
| 285 media_router_ui_->joinable_route_ids(), | |
| 274 media_router_ui_->GetRouteProviderExtensionId())); | 286 media_router_ui_->GetRouteProviderExtensionId())); |
| 275 initial_data.Set("routes", routes.release()); | 287 initial_data.Set("routes", routes.release()); |
| 276 | 288 |
| 277 const std::set<MediaCastMode> cast_modes = media_router_ui_->cast_modes(); | 289 const std::set<MediaCastMode> cast_modes = media_router_ui_->cast_modes(); |
| 278 scoped_ptr<base::ListValue> cast_modes_list( | 290 scoped_ptr<base::ListValue> cast_modes_list( |
| 279 CastModesToValue(cast_modes, | 291 CastModesToValue(cast_modes, |
| 280 media_router_ui_->GetPresentationRequestSourceName())); | 292 media_router_ui_->GetPresentationRequestSourceName())); |
| 281 initial_data.Set("castModes", cast_modes_list.release()); | 293 initial_data.Set("castModes", cast_modes_list.release()); |
| 282 if (!cast_modes.empty()) { | 294 if (!cast_modes.empty()) { |
| 283 initial_data.SetInteger("initialCastModeType", | 295 initial_data.SetInteger("initialCastModeType", |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 354 DVLOG(1) << "Invalid action type: " << action_type_num; | 366 DVLOG(1) << "Invalid action type: " << action_type_num; |
| 355 return; | 367 return; |
| 356 } | 368 } |
| 357 IssueAction::Type action_type = | 369 IssueAction::Type action_type = |
| 358 static_cast<IssueAction::Type>(action_type_num); | 370 static_cast<IssueAction::Type>(action_type_num); |
| 359 if (ActOnIssueType(action_type, args_dict)) | 371 if (ActOnIssueType(action_type, args_dict)) |
| 360 DVLOG(1) << "ActOnIssueType failed for Issue ID " << issue_id; | 372 DVLOG(1) << "ActOnIssueType failed for Issue ID " << issue_id; |
| 361 media_router_ui_->ClearIssue(issue_id); | 373 media_router_ui_->ClearIssue(issue_id); |
| 362 } | 374 } |
| 363 | 375 |
| 364 void MediaRouterWebUIMessageHandler::OnCloseRoute( | 376 void MediaRouterWebUIMessageHandler::OnJoinRoute(const base::ListValue* args) { |
| 365 const base::ListValue* args) { | 377 DVLOG(1) << "OnJoinRoute"; |
| 378 const base::DictionaryValue* args_dict = nullptr; | |
| 379 std::string route_id; | |
| 380 std::string sink_id; | |
| 381 if (!args->GetDictionary(0, &args_dict) || | |
| 382 !args_dict->GetString("sinkId", &sink_id) || | |
| 383 !args_dict->GetString("routeId", &route_id)) { | |
| 384 DVLOG(1) << "Unable to extract args."; | |
| 385 return; | |
| 386 } | |
| 387 | |
| 388 if (sink_id.empty()) { | |
| 389 DVLOG(1) << "Media Route UI did not respond with a " | |
| 390 << "valid sink ID. Aborting."; | |
| 391 return; | |
| 392 } | |
| 393 | |
| 394 if (route_id.empty()) { | |
| 395 DVLOG(1) << "Media Route UI did not respond with a " | |
| 396 << "valid route ID. Aborting."; | |
| 397 return; | |
| 398 } | |
| 399 | |
| 400 MediaRouterUI* media_router_ui = | |
| 401 static_cast<MediaRouterUI*>(web_ui()->GetController()); | |
| 402 if (media_router_ui->HasPendingRouteRequest()) { | |
| 403 DVLOG(1) << "UI already has pending route request. Ignoring."; | |
| 404 Issue issue( | |
| 405 l10n_util::GetStringUTF8(IDS_MEDIA_ROUTER_ISSUE_PENDING_ROUTE), | |
| 406 std::string(), IssueAction(IssueAction::TYPE_DISMISS), | |
| 407 std::vector<IssueAction>(), std::string(), Issue::NOTIFICATION, | |
| 408 false, std::string()); | |
| 409 media_router_ui_->AddIssue(issue); | |
| 410 return; | |
| 411 } | |
| 412 | |
| 413 DVLOG(2) << __FUNCTION__ << ": sink id: " << sink_id | |
| 414 << ", route id: " << route_id; | |
| 415 | |
| 416 if (!media_router_ui_->JoinRoute(sink_id, route_id)) { | |
|
apacible
2015/12/09 19:56:25
Do we want to surface an error if we fail to join?
matt.boetger
2015/12/16 00:21:08
There is a TODO for imcheng in the CreateRoute met
apacible
2015/12/18 22:34:19
Acknowledged.
| |
| 417 DVLOG(1) << "Error initiating route join request."; | |
| 418 } | |
| 419 } | |
| 420 | |
| 421 void MediaRouterWebUIMessageHandler::OnCloseRoute(const base::ListValue* args) { | |
| 366 DVLOG(1) << "OnCloseRoute"; | 422 DVLOG(1) << "OnCloseRoute"; |
| 367 const base::DictionaryValue* args_dict = nullptr; | 423 const base::DictionaryValue* args_dict = nullptr; |
| 368 std::string route_id; | 424 std::string route_id; |
| 369 if (!args->GetDictionary(0, &args_dict) || | 425 if (!args->GetDictionary(0, &args_dict) || |
| 370 !args_dict->GetString("routeId", &route_id)) { | 426 !args_dict->GetString("routeId", &route_id)) { |
| 371 DVLOG(1) << "Unable to extract args."; | 427 DVLOG(1) << "Unable to extract args."; |
| 372 return; | 428 return; |
| 373 } | 429 } |
| 374 media_router_ui_->CloseRoute(route_id); | 430 media_router_ui_->CloseRoute(route_id); |
| 375 } | 431 } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 406 open_args->AppendString(learn_more_url); | 462 open_args->AppendString(learn_more_url); |
| 407 web_ui()->CallJavascriptFunction(kWindowOpen, *open_args); | 463 web_ui()->CallJavascriptFunction(kWindowOpen, *open_args); |
| 408 return true; | 464 return true; |
| 409 } else { | 465 } else { |
| 410 // Do nothing; no other issue action types require any other action. | 466 // Do nothing; no other issue action types require any other action. |
| 411 return true; | 467 return true; |
| 412 } | 468 } |
| 413 } | 469 } |
| 414 | 470 |
| 415 } // namespace media_router | 471 } // namespace media_router |
| OLD | NEW |