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 auto it = std::find(joinable_route_ids.begin(), | |
|
imcheng
2015/12/01 23:45:06
This can be replaced with:
bool canJoin = Contain
matt.boetger
2015/12/03 01:19:21
Done.
| |
| 101 joinable_route_ids.end(), | |
| 102 route.media_route_id()); | |
| 103 bool canJoin = (it != joinable_route_ids.end()); | |
| 96 scoped_ptr<base::DictionaryValue> route_val(RouteToValue(route, | 104 scoped_ptr<base::DictionaryValue> route_val(RouteToValue(route, |
| 97 extension_id)); | 105 canJoin, extension_id)); |
| 98 value->Append(route_val.release()); | 106 value->Append(route_val.release()); |
| 99 } | 107 } |
| 100 | 108 |
| 101 return value.Pass(); | 109 return value.Pass(); |
| 102 } | 110 } |
| 103 | 111 |
| 104 scoped_ptr<base::ListValue> CastModesToValue(const CastModeSet& cast_modes, | 112 scoped_ptr<base::ListValue> CastModesToValue(const CastModeSet& cast_modes, |
| 105 const std::string& source_host) { | 113 const std::string& source_host) { |
| 106 scoped_ptr<base::ListValue> value(new base::ListValue); | 114 scoped_ptr<base::ListValue> value(new base::ListValue); |
| 107 | 115 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 172 } | 180 } |
| 173 | 181 |
| 174 void MediaRouterWebUIMessageHandler::UpdateSinks( | 182 void MediaRouterWebUIMessageHandler::UpdateSinks( |
| 175 const std::vector<MediaSinkWithCastModes>& sinks) { | 183 const std::vector<MediaSinkWithCastModes>& sinks) { |
| 176 DVLOG(2) << "UpdateSinks"; | 184 DVLOG(2) << "UpdateSinks"; |
| 177 scoped_ptr<base::ListValue> sinks_val(SinksToValue(sinks)); | 185 scoped_ptr<base::ListValue> sinks_val(SinksToValue(sinks)); |
| 178 web_ui()->CallJavascriptFunction(kSetSinkList, *sinks_val); | 186 web_ui()->CallJavascriptFunction(kSetSinkList, *sinks_val); |
| 179 } | 187 } |
| 180 | 188 |
| 181 void MediaRouterWebUIMessageHandler::UpdateRoutes( | 189 void MediaRouterWebUIMessageHandler::UpdateRoutes( |
| 182 const std::vector<MediaRoute>& routes) { | 190 const std::vector<MediaRoute>& routes, |
| 183 DVLOG(2) << "UpdateRoutes"; | 191 const std::vector<MediaRoute::Id>& joinable_route_ids) { |
| 184 scoped_ptr<base::ListValue> routes_val(RoutesToValue(routes, | 192 scoped_ptr<base::ListValue> routes_val(RoutesToValue(routes, |
| 193 joinable_route_ids, | |
| 185 media_router_ui_->GetRouteProviderExtensionId())); | 194 media_router_ui_->GetRouteProviderExtensionId())); |
| 186 web_ui()->CallJavascriptFunction(kSetRouteList, *routes_val); | 195 web_ui()->CallJavascriptFunction( |
| 196 kSetRouteList, *routes_val); | |
| 187 } | 197 } |
| 188 | 198 |
| 189 void MediaRouterWebUIMessageHandler::UpdateCastModes( | 199 void MediaRouterWebUIMessageHandler::UpdateCastModes( |
| 190 const CastModeSet& cast_modes, | 200 const CastModeSet& cast_modes, |
| 191 const std::string& source_host) { | 201 const std::string& source_host) { |
| 192 DVLOG(2) << "UpdateCastModes"; | 202 DVLOG(2) << "UpdateCastModes"; |
| 193 scoped_ptr<base::ListValue> cast_modes_val( | 203 scoped_ptr<base::ListValue> cast_modes_val( |
| 194 CastModesToValue(cast_modes, source_host)); | 204 CastModesToValue(cast_modes, source_host)); |
| 195 web_ui()->CallJavascriptFunction(kSetCastModeList, *cast_modes_val); | 205 web_ui()->CallJavascriptFunction(kSetCastModeList, *cast_modes_val); |
| 196 } | 206 } |
| 197 | 207 |
| 198 void MediaRouterWebUIMessageHandler::OnCreateRouteResponseReceived( | 208 void MediaRouterWebUIMessageHandler::OnCreateRouteResponseReceived( |
| 199 const MediaSink::Id& sink_id, | 209 const MediaSink::Id& sink_id, |
| 200 const MediaRoute* route) { | 210 const MediaRoute* route) { |
| 201 DVLOG(2) << "OnCreateRouteResponseReceived"; | 211 DVLOG(2) << "OnCreateRouteResponseReceived"; |
| 202 if (route) { | 212 if (route) { |
| 203 scoped_ptr<base::DictionaryValue> route_value(RouteToValue(*route, | 213 scoped_ptr<base::DictionaryValue> route_value(RouteToValue(*route, false, |
| 204 media_router_ui_->GetRouteProviderExtensionId())); | 214 media_router_ui_->GetRouteProviderExtensionId())); |
| 205 web_ui()->CallJavascriptFunction(kOnCreateRouteResponseReceived, | 215 web_ui()->CallJavascriptFunction(kOnCreateRouteResponseReceived, |
| 206 base::StringValue(sink_id), *route_value); | 216 base::StringValue(sink_id), *route_value); |
| 207 UMA_HISTOGRAM_BOOLEAN("MediaRouter.Ui.Action.StartLocalSessionSuccessful", | 217 UMA_HISTOGRAM_BOOLEAN("MediaRouter.Ui.Action.StartLocalSessionSuccessful", |
| 208 true); | 218 true); |
| 209 } else { | 219 } else { |
| 210 web_ui()->CallJavascriptFunction(kOnCreateRouteResponseReceived, | 220 web_ui()->CallJavascriptFunction(kOnCreateRouteResponseReceived, |
| 211 base::StringValue(sink_id), | 221 base::StringValue(sink_id), |
| 212 *base::Value::CreateNullValue()); | 222 *base::Value::CreateNullValue()); |
| 213 UMA_HISTOGRAM_BOOLEAN("MediaRouter.Ui.Action.StartLocalSessionSuccessful", | 223 UMA_HISTOGRAM_BOOLEAN("MediaRouter.Ui.Action.StartLocalSessionSuccessful", |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 242 base::Unretained(this))); | 252 base::Unretained(this))); |
| 243 web_ui()->RegisterMessageCallback( | 253 web_ui()->RegisterMessageCallback( |
| 244 kActOnIssue, | 254 kActOnIssue, |
| 245 base::Bind(&MediaRouterWebUIMessageHandler::OnActOnIssue, | 255 base::Bind(&MediaRouterWebUIMessageHandler::OnActOnIssue, |
| 246 base::Unretained(this))); | 256 base::Unretained(this))); |
| 247 web_ui()->RegisterMessageCallback( | 257 web_ui()->RegisterMessageCallback( |
| 248 kCloseRoute, | 258 kCloseRoute, |
| 249 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseRoute, | 259 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseRoute, |
| 250 base::Unretained(this))); | 260 base::Unretained(this))); |
| 251 web_ui()->RegisterMessageCallback( | 261 web_ui()->RegisterMessageCallback( |
| 262 kJoinRoute, | |
| 263 base::Bind(&MediaRouterWebUIMessageHandler::OnJoinRoute, | |
| 264 base::Unretained(this))); | |
| 265 web_ui()->RegisterMessageCallback( | |
| 252 kCloseDialog, | 266 kCloseDialog, |
| 253 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseDialog, | 267 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseDialog, |
| 254 base::Unretained(this))); | 268 base::Unretained(this))); |
| 255 web_ui()->RegisterMessageCallback( | 269 web_ui()->RegisterMessageCallback( |
| 256 kReportSinkCount, | 270 kReportSinkCount, |
| 257 base::Bind(&MediaRouterWebUIMessageHandler::OnReportSinkCount, | 271 base::Bind(&MediaRouterWebUIMessageHandler::OnReportSinkCount, |
| 258 base::Unretained(this))); | 272 base::Unretained(this))); |
| 259 } | 273 } |
| 260 | 274 |
| 261 void MediaRouterWebUIMessageHandler::OnRequestInitialData( | 275 void MediaRouterWebUIMessageHandler::OnRequestInitialData( |
| 262 const base::ListValue* args) { | 276 const base::ListValue* args) { |
| 263 DVLOG(1) << "OnRequestInitialData"; | 277 DVLOG(1) << "OnRequestInitialData"; |
| 264 base::DictionaryValue initial_data; | 278 base::DictionaryValue initial_data; |
| 265 | 279 |
| 266 // "No Cast devices found?" Chromecast help center page. | 280 // "No Cast devices found?" Chromecast help center page. |
| 267 initial_data.SetString("deviceMissingUrl", | 281 initial_data.SetString("deviceMissingUrl", |
| 268 base::StringPrintf(kHelpPageUrlPrefix, 3249268)); | 282 base::StringPrintf(kHelpPageUrlPrefix, 3249268)); |
| 269 | 283 |
| 270 scoped_ptr<base::ListValue> sinks(SinksToValue(media_router_ui_->sinks())); | 284 scoped_ptr<base::ListValue> sinks(SinksToValue(media_router_ui_->sinks())); |
| 271 initial_data.Set("sinks", sinks.release()); | 285 initial_data.Set("sinks", sinks.release()); |
| 272 | 286 |
| 273 scoped_ptr<base::ListValue> routes(RoutesToValue(media_router_ui_->routes(), | 287 scoped_ptr<base::ListValue> routes(RoutesToValue(media_router_ui_->routes(), |
| 288 media_router_ui_->joinable_route_ids(), | |
| 274 media_router_ui_->GetRouteProviderExtensionId())); | 289 media_router_ui_->GetRouteProviderExtensionId())); |
| 275 initial_data.Set("routes", routes.release()); | 290 initial_data.Set("routes", routes.release()); |
| 276 | 291 |
| 277 const std::set<MediaCastMode> cast_modes = media_router_ui_->cast_modes(); | 292 const std::set<MediaCastMode> cast_modes = media_router_ui_->cast_modes(); |
| 278 scoped_ptr<base::ListValue> cast_modes_list( | 293 scoped_ptr<base::ListValue> cast_modes_list( |
| 279 CastModesToValue(cast_modes, media_router_ui_->GetFrameURLHost())); | 294 CastModesToValue(cast_modes, media_router_ui_->GetFrameURLHost())); |
| 280 initial_data.Set("castModes", cast_modes_list.release()); | 295 initial_data.Set("castModes", cast_modes_list.release()); |
| 281 if (!cast_modes.empty()) { | 296 if (!cast_modes.empty()) { |
| 282 initial_data.SetInteger("initialCastModeType", | 297 initial_data.SetInteger("initialCastModeType", |
| 283 GetPreferredCastMode(cast_modes)); | 298 GetPreferredCastMode(cast_modes)); |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 353 DVLOG(1) << "Invalid action type: " << action_type_num; | 368 DVLOG(1) << "Invalid action type: " << action_type_num; |
| 354 return; | 369 return; |
| 355 } | 370 } |
| 356 IssueAction::Type action_type = | 371 IssueAction::Type action_type = |
| 357 static_cast<IssueAction::Type>(action_type_num); | 372 static_cast<IssueAction::Type>(action_type_num); |
| 358 if (ActOnIssueType(action_type, args_dict)) | 373 if (ActOnIssueType(action_type, args_dict)) |
| 359 DVLOG(1) << "ActOnIssueType failed for Issue ID " << issue_id; | 374 DVLOG(1) << "ActOnIssueType failed for Issue ID " << issue_id; |
| 360 media_router_ui_->ClearIssue(issue_id); | 375 media_router_ui_->ClearIssue(issue_id); |
| 361 } | 376 } |
| 362 | 377 |
| 363 void MediaRouterWebUIMessageHandler::OnCloseRoute( | 378 void MediaRouterWebUIMessageHandler::OnJoinRoute(const base::ListValue* args) { |
| 364 const base::ListValue* args) { | 379 DVLOG(1) << "OnJoinRoute"; |
| 380 const base::DictionaryValue* args_dict = nullptr; | |
| 381 std::string route_id; | |
| 382 std::string sink_id; | |
| 383 if (!args->GetDictionary(0, &args_dict) || | |
| 384 !args_dict->GetString("sinkId", &sink_id) || | |
| 385 !args_dict->GetString("routeId", &route_id)) { | |
| 386 DVLOG(1) << "Unable to extract args."; | |
| 387 return; | |
| 388 } | |
| 389 | |
| 390 if (sink_id.empty()) { | |
| 391 DVLOG(1) << "Media Route UI did not respond with a " | |
| 392 << "valid sink ID. Aborting."; | |
| 393 return; | |
| 394 } | |
| 395 | |
| 396 if (route_id.empty()) { | |
| 397 DVLOG(1) << "Media Route UI did not respond with a " | |
| 398 << "valid route ID. Aborting."; | |
| 399 return; | |
| 400 } | |
| 401 | |
| 402 MediaRouterUI* media_router_ui = | |
| 403 static_cast<MediaRouterUI*>(web_ui()->GetController()); | |
| 404 if (media_router_ui->HasPendingRouteRequest()) { | |
| 405 DVLOG(1) << "UI already has pending route request. Ignoring."; | |
| 406 Issue issue( | |
| 407 l10n_util::GetStringUTF8(IDS_MEDIA_ROUTER_ISSUE_PENDING_ROUTE), | |
| 408 std::string(), IssueAction(IssueAction::TYPE_DISMISS), | |
| 409 std::vector<IssueAction>(), std::string(), Issue::NOTIFICATION, | |
| 410 false, std::string()); | |
| 411 media_router_ui_->AddIssue(issue); | |
| 412 return; | |
| 413 } | |
| 414 | |
| 415 DVLOG(2) << __FUNCTION__ << ": sink id: " << sink_id | |
| 416 << ", route id: " << route_id; | |
| 417 | |
| 418 if (!media_router_ui_->JoinRoute(sink_id, route_id)) { | |
| 419 DVLOG(1) << "Error initiating route join request."; | |
| 420 } | |
| 421 } | |
| 422 | |
| 423 void MediaRouterWebUIMessageHandler::OnCloseRoute(const base::ListValue* args) { | |
| 365 DVLOG(1) << "OnCloseRoute"; | 424 DVLOG(1) << "OnCloseRoute"; |
| 366 const base::DictionaryValue* args_dict = nullptr; | 425 const base::DictionaryValue* args_dict = nullptr; |
| 367 std::string route_id; | 426 std::string route_id; |
| 368 if (!args->GetDictionary(0, &args_dict) || | 427 if (!args->GetDictionary(0, &args_dict) || |
| 369 !args_dict->GetString("routeId", &route_id)) { | 428 !args_dict->GetString("routeId", &route_id)) { |
| 370 DVLOG(1) << "Unable to extract args."; | 429 DVLOG(1) << "Unable to extract args."; |
| 371 return; | 430 return; |
| 372 } | 431 } |
| 373 media_router_ui_->CloseRoute(route_id); | 432 media_router_ui_->CloseRoute(route_id); |
| 374 } | 433 } |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 405 open_args->AppendString(learn_more_url); | 464 open_args->AppendString(learn_more_url); |
| 406 web_ui()->CallJavascriptFunction(kWindowOpen, *open_args); | 465 web_ui()->CallJavascriptFunction(kWindowOpen, *open_args); |
| 407 return true; | 466 return true; |
| 408 } else { | 467 } else { |
| 409 // Do nothing; no other issue action types require any other action. | 468 // Do nothing; no other issue action types require any other action. |
| 410 return true; | 469 return true; |
| 411 } | 470 } |
| 412 } | 471 } |
| 413 | 472 |
| 414 } // namespace media_router | 473 } // namespace media_router |
| OLD | NEW |