| 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 "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 // 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 scoped_ptr<base::ListValue> SinksToValue( |
| 32 const std::vector<MediaSinkWithCastModes>& sinks) { |
| 33 scoped_ptr<base::ListValue> value(new base::ListValue); |
| 34 |
| 35 for (const MediaSinkWithCastModes& sink_with_cast_modes : sinks) { |
| 36 scoped_ptr<base::DictionaryValue> sink_val(new base::DictionaryValue); |
| 37 |
| 38 const MediaSink& sink = sink_with_cast_modes.sink; |
| 39 sink_val->SetString("id", sink.id()); |
| 40 sink_val->SetString("name", sink.name()); |
| 41 |
| 42 scoped_ptr<base::ListValue> cast_modes_val(new base::ListValue); |
| 43 for (MediaCastMode cast_mode : sink_with_cast_modes.cast_modes) |
| 44 cast_modes_val->AppendInteger(cast_mode); |
| 45 sink_val->Set("castModes", cast_modes_val.Pass()); |
| 46 |
| 47 value->Append(sink_val.release()); |
| 48 } |
| 49 |
| 50 return value.Pass(); |
| 51 } |
| 52 |
| 53 scoped_ptr<base::DictionaryValue> RouteToValue(const MediaRoute& route) { |
| 54 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue); |
| 55 |
| 56 dictionary->SetString("id", route.media_route_id()); |
| 57 dictionary->SetString("sinkId", route.media_sink().id()); |
| 58 dictionary->SetString("title", route.description()); |
| 59 dictionary->SetBoolean("isLocal", route.is_local()); |
| 60 |
| 61 return dictionary.Pass(); |
| 62 } |
| 63 |
| 64 scoped_ptr<base::ListValue> RoutesToValue( |
| 65 const std::vector<MediaRoute>& routes) { |
| 66 scoped_ptr<base::ListValue> value(new base::ListValue); |
| 67 |
| 68 for (const MediaRoute& route : routes) { |
| 69 scoped_ptr<base::DictionaryValue> route_val(RouteToValue(route)); |
| 70 value->Append(route_val.release()); |
| 71 } |
| 72 |
| 73 return value.Pass(); |
| 74 } |
| 75 |
| 76 scoped_ptr<base::ListValue> CastModesToValue(const CastModeSet& cast_modes, |
| 77 const std::string& source_host) { |
| 78 scoped_ptr<base::ListValue> value(new base::ListValue); |
| 79 |
| 80 for (const MediaCastMode& cast_mode : cast_modes) { |
| 81 scoped_ptr<base::DictionaryValue> cast_mode_val(new base::DictionaryValue); |
| 82 cast_mode_val->SetInteger("type", cast_mode); |
| 83 cast_mode_val->SetString("title", |
| 84 MediaCastModeToTitle(cast_mode, source_host)); |
| 85 cast_mode_val->SetString( |
| 86 "description", MediaCastModeToDescription(cast_mode, source_host)); |
| 87 value->Append(cast_mode_val.release()); |
| 88 } |
| 89 |
| 90 return value.Pass(); |
| 91 } |
| 23 | 92 |
| 24 } // namespace | 93 } // namespace |
| 25 | 94 |
| 26 MediaRouterWebUIMessageHandler::MediaRouterWebUIMessageHandler() | 95 MediaRouterWebUIMessageHandler::MediaRouterWebUIMessageHandler() |
| 27 : dialog_closing_(false) { | 96 : dialog_closing_(false) { |
| 28 } | 97 } |
| 29 | 98 |
| 30 MediaRouterWebUIMessageHandler::~MediaRouterWebUIMessageHandler() { | 99 MediaRouterWebUIMessageHandler::~MediaRouterWebUIMessageHandler() { |
| 31 } | 100 } |
| 32 | 101 |
| 102 void MediaRouterWebUIMessageHandler::UpdateSinks( |
| 103 const std::vector<MediaSinkWithCastModes>& sinks) { |
| 104 DVLOG(2) << "UpdateSinks"; |
| 105 scoped_ptr<base::ListValue> sinks_val(SinksToValue(sinks)); |
| 106 web_ui()->CallJavascriptFunction(kSetSinkList, *sinks_val); |
| 107 } |
| 108 |
| 109 void MediaRouterWebUIMessageHandler::UpdateRoutes( |
| 110 const std::vector<MediaRoute>& routes) { |
| 111 DVLOG(2) << "UpdateRoutes"; |
| 112 scoped_ptr<base::ListValue> routes_val(RoutesToValue(routes)); |
| 113 web_ui()->CallJavascriptFunction(kSetRouteList, *routes_val); |
| 114 } |
| 115 |
| 116 void MediaRouterWebUIMessageHandler::UpdateCastModes( |
| 117 const CastModeSet& cast_modes, |
| 118 const std::string& source_host) { |
| 119 DVLOG(2) << "UpdateCastModes"; |
| 120 scoped_ptr<base::ListValue> cast_modes_val( |
| 121 CastModesToValue(cast_modes, source_host)); |
| 122 web_ui()->CallJavascriptFunction(kSetCastModeList, *cast_modes_val); |
| 123 } |
| 124 |
| 125 void MediaRouterWebUIMessageHandler::AddRoute(const MediaRoute& route) { |
| 126 DVLOG(2) << "AddRoute"; |
| 127 |
| 128 scoped_ptr<base::DictionaryValue> route_value(RouteToValue(route)); |
| 129 web_ui()->CallJavascriptFunction(kAddRoute, *route_value); |
| 130 } |
| 131 |
| 132 void MediaRouterWebUIMessageHandler::UpdateIssue(const Issue* issue) { |
| 133 DVLOG(2) << "UpdateIssue"; |
| 134 // TODO(imcheng): Implement conversion from Issue to dictionary object |
| 135 // (crbug.com/464216). |
| 136 NOTIMPLEMENTED(); |
| 137 } |
| 138 |
| 33 void MediaRouterWebUIMessageHandler::RegisterMessages() { | 139 void MediaRouterWebUIMessageHandler::RegisterMessages() { |
| 34 web_ui()->RegisterMessageCallback( | 140 web_ui()->RegisterMessageCallback( |
| 35 kGetInitialSettings, | 141 kGetInitialSettings, |
| 36 base::Bind(&MediaRouterWebUIMessageHandler::OnGetInitialSettings, | 142 base::Bind(&MediaRouterWebUIMessageHandler::OnGetInitialSettings, |
| 37 base::Unretained(this))); | 143 base::Unretained(this))); |
| 38 web_ui()->RegisterMessageCallback( | 144 web_ui()->RegisterMessageCallback( |
| 39 kCreateRoute, | 145 kCreateRoute, |
| 40 base::Bind(&MediaRouterWebUIMessageHandler::OnCreateRoute, | 146 base::Bind(&MediaRouterWebUIMessageHandler::OnCreateRoute, |
| 41 base::Unretained(this))); | 147 base::Unretained(this))); |
| 42 web_ui()->RegisterMessageCallback( | 148 web_ui()->RegisterMessageCallback( |
| 43 kActOnIssue, | 149 kActOnIssue, |
| 44 base::Bind(&MediaRouterWebUIMessageHandler::OnActOnIssue, | 150 base::Bind(&MediaRouterWebUIMessageHandler::OnActOnIssue, |
| 45 base::Unretained(this))); | 151 base::Unretained(this))); |
| 46 web_ui()->RegisterMessageCallback( | 152 web_ui()->RegisterMessageCallback( |
| 47 kCloseRoute, | 153 kCloseRoute, |
| 48 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseRoute, | 154 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseRoute, |
| 49 base::Unretained(this))); | 155 base::Unretained(this))); |
| 50 web_ui()->RegisterMessageCallback( | 156 web_ui()->RegisterMessageCallback( |
| 51 kCloseDialog, | 157 kCloseDialog, |
| 52 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseDialog, | 158 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseDialog, |
| 53 base::Unretained(this))); | 159 base::Unretained(this))); |
| 54 } | 160 } |
| 55 | 161 |
| 56 void MediaRouterWebUIMessageHandler::OnGetInitialSettings( | 162 void MediaRouterWebUIMessageHandler::OnGetInitialSettings( |
| 57 const base::ListValue* args) { | 163 const base::ListValue* args) { |
| 58 // TODO(imcheng): Implement. | 164 MediaRouterUI* media_router_ui = GetMediaRouterUI(); |
| 59 NOTIMPLEMENTED(); | 165 |
| 166 base::DictionaryValue initial_settings; |
| 167 |
| 168 initial_settings.SetString("headerText", |
| 169 media_router_ui->GetInitialHeaderText()); |
| 170 |
| 171 scoped_ptr<base::ListValue> sinks(SinksToValue(media_router_ui->sinks())); |
| 172 initial_settings.Set("sinks", sinks.release()); |
| 173 |
| 174 scoped_ptr<base::ListValue> routes(RoutesToValue(media_router_ui->routes())); |
| 175 initial_settings.Set("routes", routes.release()); |
| 176 |
| 177 scoped_ptr<base::ListValue> cast_modes(CastModesToValue( |
| 178 media_router_ui->cast_modes(), |
| 179 // TODO(imcheng): Use media_router_ui->source_host() once DEFAULT mode |
| 180 // is upstreamed. |
| 181 std::string())); |
| 182 initial_settings.Set("castModes", cast_modes.release()); |
| 183 |
| 184 web_ui()->CallJavascriptFunction(kSetInitialSettings, initial_settings); |
| 185 media_router_ui->UIInitialized(); |
| 60 } | 186 } |
| 61 | 187 |
| 62 void MediaRouterWebUIMessageHandler::OnCreateRoute( | 188 void MediaRouterWebUIMessageHandler::OnCreateRoute( |
| 63 const base::ListValue* args) { | 189 const base::ListValue* args) { |
| 64 // TODO(imcheng): Implement. | 190 std::string sink_id; |
| 65 NOTIMPLEMENTED(); | 191 int cast_mode_num; |
| 192 if (!args->GetString(0, &sink_id) || !args->GetInteger(1, &cast_mode_num)) { |
| 193 LOG(ERROR) << "Unable to extract args."; |
| 194 return; |
| 195 } |
| 196 |
| 197 if (sink_id.empty()) { |
| 198 LOG(ERROR) << "Media Route Provider Manager did not respond with a " |
| 199 << "valid sink ID. Aborting."; |
| 200 return; |
| 201 } |
| 202 |
| 203 MediaRouterUI* media_router_ui = |
| 204 static_cast<MediaRouterUI*>(web_ui()->GetController()); |
| 205 if (media_router_ui->has_pending_route_request()) { |
| 206 LOG(ERROR) << "UI already has pending route request. Ignoring."; |
| 207 return; |
| 208 } |
| 209 |
| 210 DVLOG(2) << "sink id: " << sink_id << ", cast mode: " << cast_mode_num; |
| 211 |
| 212 // TODO(haibinlu): Pass additional parameters into the CreateRoute request, |
| 213 // e.g. low-fps-mirror, user-override. (crbug.com/490364) |
| 214 bool success = false; |
| 215 if (IsValidCastModeNum(cast_mode_num)) { |
| 216 // User explicitly selected cast mode. |
| 217 DVLOG(2) << "Cast mode override: " << cast_mode_num; |
| 218 success = media_router_ui->CreateRouteWithCastModeOverride( |
| 219 sink_id, static_cast<MediaCastMode>(cast_mode_num)); |
| 220 } else { |
| 221 success = media_router_ui->CreateRoute(sink_id); |
| 222 } |
| 223 |
| 224 // TODO(imcheng): Display error in UI. (crbug.com/490372) |
| 225 if (!success) |
| 226 LOG(ERROR) << "Error initiating route request."; |
| 66 } | 227 } |
| 67 | 228 |
| 68 void MediaRouterWebUIMessageHandler::OnActOnIssue( | 229 void MediaRouterWebUIMessageHandler::OnActOnIssue( |
| 69 const base::ListValue* args) { | 230 const base::ListValue* args) { |
| 70 // TODO(imcheng): Implement. | 231 // TODO(imcheng): Implement (crbug.com/464216). |
| 71 NOTIMPLEMENTED(); | 232 NOTIMPLEMENTED(); |
| 72 } | 233 } |
| 73 | 234 |
| 74 void MediaRouterWebUIMessageHandler::OnCloseRoute( | 235 void MediaRouterWebUIMessageHandler::OnCloseRoute( |
| 75 const base::ListValue* args) { | 236 const base::ListValue* args) { |
| 76 // TODO(imcheng): Implement. | 237 DVLOG(2) << "OnCloseRoute"; |
| 77 NOTIMPLEMENTED(); | 238 std::string route_id; |
| 239 if (!args->GetString(0, &route_id)) { |
| 240 LOG(ERROR) << "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_) |
| 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 |
| OLD | NEW |