| 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 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 extensions::kExtensionScheme, extension_id.c_str(), | 82 extensions::kExtensionScheme, extension_id.c_str(), |
| 82 custom_path.c_str()); | 83 custom_path.c_str()); |
| 83 DCHECK(GURL(full_custom_controller_path).is_valid()); | 84 DCHECK(GURL(full_custom_controller_path).is_valid()); |
| 84 dictionary->SetString("customControllerPath", | 85 dictionary->SetString("customControllerPath", |
| 85 full_custom_controller_path); | 86 full_custom_controller_path); |
| 86 } | 87 } |
| 87 | 88 |
| 88 return dictionary.Pass(); | 89 return dictionary.Pass(); |
| 89 } | 90 } |
| 90 | 91 |
| 92 scoped_ptr<base::ListValue> RouteIdsToValue( |
| 93 const std::vector<MediaRoute::Id>& route_ids) { |
| 94 scoped_ptr<base::ListValue> value(new base::ListValue); |
| 95 |
| 96 for (const MediaRoute::Id& route_id : route_ids) { |
| 97 value->Append(new base::StringValue(route_id)); |
| 98 } |
| 99 |
| 100 return value.Pass(); |
| 101 } |
| 102 |
| 91 scoped_ptr<base::ListValue> RoutesToValue( | 103 scoped_ptr<base::ListValue> RoutesToValue( |
| 92 const std::vector<MediaRoute>& routes, const std::string& extension_id) { | 104 const std::vector<MediaRoute>& routes, const std::string& extension_id) { |
| 93 scoped_ptr<base::ListValue> value(new base::ListValue); | 105 scoped_ptr<base::ListValue> value(new base::ListValue); |
| 94 | 106 |
| 95 for (const MediaRoute& route : routes) { | 107 for (const MediaRoute& route : routes) { |
| 96 scoped_ptr<base::DictionaryValue> route_val(RouteToValue(route, | 108 scoped_ptr<base::DictionaryValue> route_val(RouteToValue(route, |
| 97 extension_id)); | 109 extension_id)); |
| 98 value->Append(route_val.release()); | 110 value->Append(route_val.release()); |
| 99 } | 111 } |
| 100 | 112 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 } | 184 } |
| 173 | 185 |
| 174 void MediaRouterWebUIMessageHandler::UpdateSinks( | 186 void MediaRouterWebUIMessageHandler::UpdateSinks( |
| 175 const std::vector<MediaSinkWithCastModes>& sinks) { | 187 const std::vector<MediaSinkWithCastModes>& sinks) { |
| 176 DVLOG(2) << "UpdateSinks"; | 188 DVLOG(2) << "UpdateSinks"; |
| 177 scoped_ptr<base::ListValue> sinks_val(SinksToValue(sinks)); | 189 scoped_ptr<base::ListValue> sinks_val(SinksToValue(sinks)); |
| 178 web_ui()->CallJavascriptFunction(kSetSinkList, *sinks_val); | 190 web_ui()->CallJavascriptFunction(kSetSinkList, *sinks_val); |
| 179 } | 191 } |
| 180 | 192 |
| 181 void MediaRouterWebUIMessageHandler::UpdateRoutes( | 193 void MediaRouterWebUIMessageHandler::UpdateRoutes( |
| 182 const std::vector<MediaRoute>& routes) { | 194 const std::vector<MediaRoute>& routes, |
| 183 DVLOG(2) << "UpdateRoutes"; | 195 const std::vector<MediaRoute::Id>& joinable_route_ids) { |
| 184 scoped_ptr<base::ListValue> routes_val(RoutesToValue(routes, | 196 scoped_ptr<base::ListValue> routes_val(RoutesToValue(routes, |
| 185 media_router_ui_->GetRouteProviderExtensionId())); | 197 media_router_ui_->GetRouteProviderExtensionId())); |
| 186 web_ui()->CallJavascriptFunction(kSetRouteList, *routes_val); | 198 scoped_ptr<base::ListValue> joinable_routes_ids_val( |
| 199 RouteIdsToValue(joinable_route_ids)); |
| 200 web_ui()->CallJavascriptFunction( |
| 201 kSetRouteList, *routes_val, *joinable_routes_ids_val); |
| 187 } | 202 } |
| 188 | 203 |
| 189 void MediaRouterWebUIMessageHandler::UpdateCastModes( | 204 void MediaRouterWebUIMessageHandler::UpdateCastModes( |
| 190 const CastModeSet& cast_modes, | 205 const CastModeSet& cast_modes, |
| 191 const std::string& source_host) { | 206 const std::string& source_host) { |
| 192 DVLOG(2) << "UpdateCastModes"; | 207 DVLOG(2) << "UpdateCastModes"; |
| 193 scoped_ptr<base::ListValue> cast_modes_val( | 208 scoped_ptr<base::ListValue> cast_modes_val( |
| 194 CastModesToValue(cast_modes, source_host)); | 209 CastModesToValue(cast_modes, source_host)); |
| 195 web_ui()->CallJavascriptFunction(kSetCastModeList, *cast_modes_val); | 210 web_ui()->CallJavascriptFunction(kSetCastModeList, *cast_modes_val); |
| 196 } | 211 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 base::Unretained(this))); | 257 base::Unretained(this))); |
| 243 web_ui()->RegisterMessageCallback( | 258 web_ui()->RegisterMessageCallback( |
| 244 kActOnIssue, | 259 kActOnIssue, |
| 245 base::Bind(&MediaRouterWebUIMessageHandler::OnActOnIssue, | 260 base::Bind(&MediaRouterWebUIMessageHandler::OnActOnIssue, |
| 246 base::Unretained(this))); | 261 base::Unretained(this))); |
| 247 web_ui()->RegisterMessageCallback( | 262 web_ui()->RegisterMessageCallback( |
| 248 kCloseRoute, | 263 kCloseRoute, |
| 249 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseRoute, | 264 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseRoute, |
| 250 base::Unretained(this))); | 265 base::Unretained(this))); |
| 251 web_ui()->RegisterMessageCallback( | 266 web_ui()->RegisterMessageCallback( |
| 267 kJoinRoute, |
| 268 base::Bind(&MediaRouterWebUIMessageHandler::OnJoinRoute, |
| 269 base::Unretained(this))); |
| 270 web_ui()->RegisterMessageCallback( |
| 252 kCloseDialog, | 271 kCloseDialog, |
| 253 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseDialog, | 272 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseDialog, |
| 254 base::Unretained(this))); | 273 base::Unretained(this))); |
| 255 web_ui()->RegisterMessageCallback( | 274 web_ui()->RegisterMessageCallback( |
| 256 kReportSinkCount, | 275 kReportSinkCount, |
| 257 base::Bind(&MediaRouterWebUIMessageHandler::OnReportSinkCount, | 276 base::Bind(&MediaRouterWebUIMessageHandler::OnReportSinkCount, |
| 258 base::Unretained(this))); | 277 base::Unretained(this))); |
| 259 } | 278 } |
| 260 | 279 |
| 261 void MediaRouterWebUIMessageHandler::OnRequestInitialData( | 280 void MediaRouterWebUIMessageHandler::OnRequestInitialData( |
| 262 const base::ListValue* args) { | 281 const base::ListValue* args) { |
| 263 DVLOG(1) << "OnRequestInitialData"; | 282 DVLOG(1) << "OnRequestInitialData"; |
| 264 base::DictionaryValue initial_data; | 283 base::DictionaryValue initial_data; |
| 265 | 284 |
| 266 // "No Cast devices found?" Chromecast help center page. | 285 // "No Cast devices found?" Chromecast help center page. |
| 267 initial_data.SetString("deviceMissingUrl", | 286 initial_data.SetString("deviceMissingUrl", |
| 268 base::StringPrintf(kHelpPageUrlPrefix, 3249268)); | 287 base::StringPrintf(kHelpPageUrlPrefix, 3249268)); |
| 269 | 288 |
| 270 scoped_ptr<base::ListValue> sinks(SinksToValue(media_router_ui_->sinks())); | 289 scoped_ptr<base::ListValue> sinks(SinksToValue(media_router_ui_->sinks())); |
| 271 initial_data.Set("sinks", sinks.release()); | 290 initial_data.Set("sinks", sinks.release()); |
| 272 | 291 |
| 273 scoped_ptr<base::ListValue> routes(RoutesToValue(media_router_ui_->routes(), | 292 scoped_ptr<base::ListValue> routes(RoutesToValue(media_router_ui_->routes(), |
| 274 media_router_ui_->GetRouteProviderExtensionId())); | 293 media_router_ui_->GetRouteProviderExtensionId())); |
| 275 initial_data.Set("routes", routes.release()); | 294 initial_data.Set("routes", routes.release()); |
| 276 | 295 |
| 296 |
| 297 scoped_ptr<base::ListValue> joinable_route_ids( |
| 298 RouteIdsToValue(media_router_ui_->joinable_route_ids())); |
| 299 initial_data.Set("joinableRouteIds", joinable_route_ids.release()); |
| 300 |
| 277 const std::set<MediaCastMode> cast_modes = media_router_ui_->cast_modes(); | 301 const std::set<MediaCastMode> cast_modes = media_router_ui_->cast_modes(); |
| 278 scoped_ptr<base::ListValue> cast_modes_list( | 302 scoped_ptr<base::ListValue> cast_modes_list( |
| 279 CastModesToValue(cast_modes, media_router_ui_->GetFrameURLHost())); | 303 CastModesToValue(cast_modes, media_router_ui_->GetFrameURLHost())); |
| 280 initial_data.Set("castModes", cast_modes_list.release()); | 304 initial_data.Set("castModes", cast_modes_list.release()); |
| 281 if (!cast_modes.empty()) { | 305 if (!cast_modes.empty()) { |
| 282 initial_data.SetInteger("initialCastModeType", | 306 initial_data.SetInteger("initialCastModeType", |
| 283 GetPreferredCastMode(cast_modes)); | 307 GetPreferredCastMode(cast_modes)); |
| 284 } | 308 } |
| 285 | 309 |
| 286 web_ui()->CallJavascriptFunction(kSetInitialData, initial_data); | 310 web_ui()->CallJavascriptFunction(kSetInitialData, initial_data); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 DVLOG(1) << "Invalid action type: " << action_type_num; | 377 DVLOG(1) << "Invalid action type: " << action_type_num; |
| 354 return; | 378 return; |
| 355 } | 379 } |
| 356 IssueAction::Type action_type = | 380 IssueAction::Type action_type = |
| 357 static_cast<IssueAction::Type>(action_type_num); | 381 static_cast<IssueAction::Type>(action_type_num); |
| 358 if (ActOnIssueType(action_type, args_dict)) | 382 if (ActOnIssueType(action_type, args_dict)) |
| 359 DVLOG(1) << "ActOnIssueType failed for Issue ID " << issue_id; | 383 DVLOG(1) << "ActOnIssueType failed for Issue ID " << issue_id; |
| 360 media_router_ui_->ClearIssue(issue_id); | 384 media_router_ui_->ClearIssue(issue_id); |
| 361 } | 385 } |
| 362 | 386 |
| 363 void MediaRouterWebUIMessageHandler::OnCloseRoute( | 387 void MediaRouterWebUIMessageHandler::OnJoinRoute(const base::ListValue* args) { |
| 364 const base::ListValue* args) { | 388 DVLOG(1) << "OnJoinRoute"; |
| 389 const base::DictionaryValue* args_dict = nullptr; |
| 390 std::string route_id; |
| 391 std::string sink_id; |
| 392 if (!args->GetDictionary(0, &args_dict) || |
| 393 !args_dict->GetString("sinkId", &sink_id) || |
| 394 !args_dict->GetString("routeId", &route_id)) { |
| 395 DVLOG(1) << "Unable to extract args."; |
| 396 return; |
| 397 } |
| 398 |
| 399 if (sink_id.empty()) { |
| 400 DVLOG(1) << "Media Route UI did not respond with a " |
| 401 << "valid sink ID. Aborting."; |
| 402 return; |
| 403 } |
| 404 |
| 405 if (route_id.empty()) { |
| 406 DVLOG(1) << "Media Route UI did not respond with a " |
| 407 << "valid route ID. Aborting."; |
| 408 return; |
| 409 } |
| 410 |
| 411 MediaRouterUI* media_router_ui = |
| 412 static_cast<MediaRouterUI*>(web_ui()->GetController()); |
| 413 if (media_router_ui->HasPendingRouteRequest()) { |
| 414 DVLOG(1) << "UI already has pending route request. Ignoring."; |
| 415 Issue issue( |
| 416 l10n_util::GetStringUTF8(IDS_MEDIA_ROUTER_ISSUE_PENDING_ROUTE), |
| 417 std::string(), IssueAction(IssueAction::TYPE_DISMISS), |
| 418 std::vector<IssueAction>(), std::string(), Issue::NOTIFICATION, |
| 419 false, std::string()); |
| 420 media_router_ui_->AddIssue(issue); |
| 421 return; |
| 422 } |
| 423 |
| 424 DVLOG(2) << __FUNCTION__ << ": sink id: " << sink_id |
| 425 << ", route id: " << route_id; |
| 426 |
| 427 if (!media_router_ui_->JoinRoute(sink_id, route_id)) { |
| 428 DVLOG(1) << "Error initiating route join request."; |
| 429 } |
| 430 } |
| 431 |
| 432 void MediaRouterWebUIMessageHandler::OnCloseRoute(const base::ListValue* args) { |
| 365 DVLOG(1) << "OnCloseRoute"; | 433 DVLOG(1) << "OnCloseRoute"; |
| 366 const base::DictionaryValue* args_dict = nullptr; | 434 const base::DictionaryValue* args_dict = nullptr; |
| 367 std::string route_id; | 435 std::string route_id; |
| 368 if (!args->GetDictionary(0, &args_dict) || | 436 if (!args->GetDictionary(0, &args_dict) || |
| 369 !args_dict->GetString("routeId", &route_id)) { | 437 !args_dict->GetString("routeId", &route_id)) { |
| 370 DVLOG(1) << "Unable to extract args."; | 438 DVLOG(1) << "Unable to extract args."; |
| 371 return; | 439 return; |
| 372 } | 440 } |
| 373 media_router_ui_->CloseRoute(route_id); | 441 media_router_ui_->CloseRoute(route_id); |
| 374 } | 442 } |
| (...skipping 30 matching lines...) Expand all Loading... |
| 405 open_args->AppendString(learn_more_url); | 473 open_args->AppendString(learn_more_url); |
| 406 web_ui()->CallJavascriptFunction(kWindowOpen, *open_args); | 474 web_ui()->CallJavascriptFunction(kWindowOpen, *open_args); |
| 407 return true; | 475 return true; |
| 408 } else { | 476 } else { |
| 409 // Do nothing; no other issue action types require any other action. | 477 // Do nothing; no other issue action types require any other action. |
| 410 return true; | 478 return true; |
| 411 } | 479 } |
| 412 } | 480 } |
| 413 | 481 |
| 414 } // namespace media_router | 482 } // namespace media_router |
| OLD | NEW |