| 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" | |
| 10 #include "content/public/browser/web_ui.h" | 9 #include "content/public/browser/web_ui.h" |
| 11 #include "ui/base/l10n/l10n_util.h" | |
| 12 | 10 |
| 13 namespace media_router { | 11 namespace media_router { |
| 14 | 12 |
| 15 namespace { | 13 namespace { |
| 16 | 14 |
| 17 // Message names. | 15 // Message names. |
| 18 const char kGetInitialSettings[] = "getInitialSettings"; | 16 const char kGetInitialSettings[] = "getInitialSettings"; |
| 19 const char kCreateRoute[] = "requestRoute"; | 17 const char kCreateRoute[] = "requestRoute"; |
| 20 const char kActOnIssue[] = "actOnIssue"; | 18 const char kActOnIssue[] = "actOnIssue"; |
| 21 const char kCloseRoute[] = "closeRoute"; | 19 const char kCloseRoute[] = "closeRoute"; |
| 22 const char kCloseDialog[] = "closeDialog"; | 20 const char kCloseDialog[] = "closeDialog"; |
| 23 | 21 |
| 24 // JS function names. | 22 // TODO(imcheng): Define JS function names here. |
| 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 } | |
| 92 | 23 |
| 93 } // namespace | 24 } // namespace |
| 94 | 25 |
| 95 MediaRouterWebUIMessageHandler::MediaRouterWebUIMessageHandler() | 26 MediaRouterWebUIMessageHandler::MediaRouterWebUIMessageHandler() |
| 96 : dialog_closing_(false) { | 27 : dialog_closing_(false) { |
| 97 } | 28 } |
| 98 | 29 |
| 99 MediaRouterWebUIMessageHandler::~MediaRouterWebUIMessageHandler() { | 30 MediaRouterWebUIMessageHandler::~MediaRouterWebUIMessageHandler() { |
| 100 } | 31 } |
| 101 | 32 |
| 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 | |
| 139 void MediaRouterWebUIMessageHandler::RegisterMessages() { | 33 void MediaRouterWebUIMessageHandler::RegisterMessages() { |
| 140 web_ui()->RegisterMessageCallback( | 34 web_ui()->RegisterMessageCallback( |
| 141 kGetInitialSettings, | 35 kGetInitialSettings, |
| 142 base::Bind(&MediaRouterWebUIMessageHandler::OnGetInitialSettings, | 36 base::Bind(&MediaRouterWebUIMessageHandler::OnGetInitialSettings, |
| 143 base::Unretained(this))); | 37 base::Unretained(this))); |
| 144 web_ui()->RegisterMessageCallback( | 38 web_ui()->RegisterMessageCallback( |
| 145 kCreateRoute, | 39 kCreateRoute, |
| 146 base::Bind(&MediaRouterWebUIMessageHandler::OnCreateRoute, | 40 base::Bind(&MediaRouterWebUIMessageHandler::OnCreateRoute, |
| 147 base::Unretained(this))); | 41 base::Unretained(this))); |
| 148 web_ui()->RegisterMessageCallback( | 42 web_ui()->RegisterMessageCallback( |
| 149 kActOnIssue, | 43 kActOnIssue, |
| 150 base::Bind(&MediaRouterWebUIMessageHandler::OnActOnIssue, | 44 base::Bind(&MediaRouterWebUIMessageHandler::OnActOnIssue, |
| 151 base::Unretained(this))); | 45 base::Unretained(this))); |
| 152 web_ui()->RegisterMessageCallback( | 46 web_ui()->RegisterMessageCallback( |
| 153 kCloseRoute, | 47 kCloseRoute, |
| 154 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseRoute, | 48 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseRoute, |
| 155 base::Unretained(this))); | 49 base::Unretained(this))); |
| 156 web_ui()->RegisterMessageCallback( | 50 web_ui()->RegisterMessageCallback( |
| 157 kCloseDialog, | 51 kCloseDialog, |
| 158 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseDialog, | 52 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseDialog, |
| 159 base::Unretained(this))); | 53 base::Unretained(this))); |
| 160 } | 54 } |
| 161 | 55 |
| 162 void MediaRouterWebUIMessageHandler::OnGetInitialSettings( | 56 void MediaRouterWebUIMessageHandler::OnGetInitialSettings( |
| 163 const base::ListValue* args) { | 57 const base::ListValue* args) { |
| 164 MediaRouterUI* media_router_ui = GetMediaRouterUI(); | 58 // TODO(imcheng): Implement. |
| 165 | 59 NOTIMPLEMENTED(); |
| 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(); | |
| 186 } | 60 } |
| 187 | 61 |
| 188 void MediaRouterWebUIMessageHandler::OnCreateRoute( | 62 void MediaRouterWebUIMessageHandler::OnCreateRoute( |
| 189 const base::ListValue* args) { | 63 const base::ListValue* args) { |
| 190 std::string sink_id; | 64 // TODO(imcheng): Implement. |
| 191 int cast_mode_num; | 65 NOTIMPLEMENTED(); |
| 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."; | |
| 227 } | 66 } |
| 228 | 67 |
| 229 void MediaRouterWebUIMessageHandler::OnActOnIssue( | 68 void MediaRouterWebUIMessageHandler::OnActOnIssue( |
| 230 const base::ListValue* args) { | 69 const base::ListValue* args) { |
| 231 // TODO(imcheng): Implement (crbug.com/464216). | 70 // TODO(imcheng): Implement. |
| 232 NOTIMPLEMENTED(); | 71 NOTIMPLEMENTED(); |
| 233 } | 72 } |
| 234 | 73 |
| 235 void MediaRouterWebUIMessageHandler::OnCloseRoute( | 74 void MediaRouterWebUIMessageHandler::OnCloseRoute( |
| 236 const base::ListValue* args) { | 75 const base::ListValue* args) { |
| 237 DVLOG(2) << "OnCloseRoute"; | 76 // TODO(imcheng): Implement. |
| 238 std::string route_id; | 77 NOTIMPLEMENTED(); |
| 239 if (!args->GetString(0, &route_id)) { | |
| 240 LOG(ERROR) << "Unable to extract args."; | |
| 241 return; | |
| 242 } | |
| 243 GetMediaRouterUI()->CloseRoute(route_id); | |
| 244 } | 78 } |
| 245 | 79 |
| 246 void MediaRouterWebUIMessageHandler::OnCloseDialog( | 80 void MediaRouterWebUIMessageHandler::OnCloseDialog( |
| 247 const base::ListValue* args) { | 81 const base::ListValue* args) { |
| 248 if (dialog_closing_) | 82 CHECK(!dialog_closing_); |
| 249 return; | |
| 250 | |
| 251 dialog_closing_ = true; | 83 dialog_closing_ = true; |
| 252 GetMediaRouterUI()->Close(); | 84 GetMediaRouterUI()->Close(); |
| 253 } | 85 } |
| 254 | 86 |
| 255 MediaRouterUI* MediaRouterWebUIMessageHandler::GetMediaRouterUI() const { | 87 MediaRouterUI* MediaRouterWebUIMessageHandler::GetMediaRouterUI() const { |
| 256 MediaRouterUI* media_router_ui = | 88 MediaRouterUI* media_router_ui = |
| 257 static_cast<MediaRouterUI*>(web_ui()->GetController()); | 89 static_cast<MediaRouterUI*>(web_ui()->GetController()); |
| 258 DCHECK(media_router_ui); | 90 DCHECK(media_router_ui); |
| 259 return media_router_ui; | 91 return media_router_ui; |
| 260 } | 92 } |
| 261 | 93 |
| 262 } // namespace media_router | 94 } // namespace media_router |
| 263 | 95 |
| OLD | NEW |