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 "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. | |
|
Wez
2015/05/21 22:58:37
Bug #?
imcheng (use chromium acct)
2015/05/22 00:02:01
Done.
| |
| 135 NOTIMPLEMENTED(); | |
| 136 } | |
| 137 | |
| 33 void MediaRouterWebUIMessageHandler::RegisterMessages() { | 138 void MediaRouterWebUIMessageHandler::RegisterMessages() { |
| 34 web_ui()->RegisterMessageCallback( | 139 web_ui()->RegisterMessageCallback( |
| 35 kGetInitialSettings, | 140 kGetInitialSettings, |
| 36 base::Bind(&MediaRouterWebUIMessageHandler::OnGetInitialSettings, | 141 base::Bind(&MediaRouterWebUIMessageHandler::OnGetInitialSettings, |
| 37 base::Unretained(this))); | 142 base::Unretained(this))); |
| 38 web_ui()->RegisterMessageCallback( | 143 web_ui()->RegisterMessageCallback( |
| 39 kCreateRoute, | 144 kCreateRoute, |
| 40 base::Bind(&MediaRouterWebUIMessageHandler::OnCreateRoute, | 145 base::Bind(&MediaRouterWebUIMessageHandler::OnCreateRoute, |
| 41 base::Unretained(this))); | 146 base::Unretained(this))); |
| 42 web_ui()->RegisterMessageCallback( | 147 web_ui()->RegisterMessageCallback( |
| 43 kActOnIssue, | 148 kActOnIssue, |
| 44 base::Bind(&MediaRouterWebUIMessageHandler::OnActOnIssue, | 149 base::Bind(&MediaRouterWebUIMessageHandler::OnActOnIssue, |
| 45 base::Unretained(this))); | 150 base::Unretained(this))); |
| 46 web_ui()->RegisterMessageCallback( | 151 web_ui()->RegisterMessageCallback( |
| 47 kCloseRoute, | 152 kCloseRoute, |
| 48 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseRoute, | 153 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseRoute, |
| 49 base::Unretained(this))); | 154 base::Unretained(this))); |
| 50 web_ui()->RegisterMessageCallback( | 155 web_ui()->RegisterMessageCallback( |
| 51 kCloseDialog, | 156 kCloseDialog, |
| 52 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseDialog, | 157 base::Bind(&MediaRouterWebUIMessageHandler::OnCloseDialog, |
| 53 base::Unretained(this))); | 158 base::Unretained(this))); |
| 54 } | 159 } |
| 55 | 160 |
| 56 void MediaRouterWebUIMessageHandler::OnGetInitialSettings( | 161 void MediaRouterWebUIMessageHandler::OnGetInitialSettings( |
| 57 const base::ListValue* args) { | 162 const base::ListValue* args) { |
| 58 // TODO(imcheng): Implement. | 163 MediaRouterUI* media_router_ui = GetMediaRouterUI(); |
| 59 NOTIMPLEMENTED(); | 164 |
| 165 base::DictionaryValue initial_settings; | |
| 166 | |
| 167 initial_settings.SetString("headerText", | |
| 168 media_router_ui->GetInitialHeaderText()); | |
| 169 | |
| 170 scoped_ptr<base::ListValue> sinks(SinksToValue(media_router_ui->sinks())); | |
| 171 initial_settings.Set("sinks", sinks.release()); | |
| 172 | |
| 173 scoped_ptr<base::ListValue> routes(RoutesToValue(media_router_ui->routes())); | |
| 174 initial_settings.Set("routes", routes.release()); | |
| 175 | |
| 176 scoped_ptr<base::ListValue> cast_modes(CastModesToValue( | |
| 177 media_router_ui->cast_modes(), | |
| 178 // TODO(imcheng): Use media_router_ui->source_host() once DEFAULT mode | |
| 179 // is upstreamed. | |
| 180 std::string())); | |
| 181 initial_settings.Set("castModes", cast_modes.release()); | |
| 182 | |
| 183 web_ui()->CallJavascriptFunction(kSetInitialSettings, initial_settings); | |
| 184 media_router_ui->UIInitialized(); | |
| 60 } | 185 } |
| 61 | 186 |
| 62 void MediaRouterWebUIMessageHandler::OnCreateRoute( | 187 void MediaRouterWebUIMessageHandler::OnCreateRoute( |
| 63 const base::ListValue* args) { | 188 const base::ListValue* args) { |
| 64 // TODO(imcheng): Implement. | 189 std::string sink_id; |
| 65 NOTIMPLEMENTED(); | 190 int cast_mode_num; |
| 191 if (!args->GetString(0, &sink_id) || !args->GetInteger(1, &cast_mode_num)) { | |
| 192 LOG(ERROR) << "Unable to extract args."; | |
| 193 return; | |
| 194 } | |
| 195 | |
| 196 if (sink_id.empty()) { | |
| 197 LOG(ERROR) << "Media Route Provider Manager did not respond with a " | |
| 198 << "valid sink ID. Aborting."; | |
| 199 return; | |
| 200 } | |
| 201 | |
| 202 MediaRouterUI* media_router_ui = | |
| 203 static_cast<MediaRouterUI*>(web_ui()->GetController()); | |
| 204 if (media_router_ui->has_pending_route_request()) { | |
| 205 LOG(ERROR) << "UI already has pending route request. Ignoring."; | |
| 206 return; | |
| 207 } | |
| 208 | |
| 209 DVLOG(2) << "sink id: " << sink_id << ", cast mode: " << cast_mode_num; | |
| 210 | |
| 211 // TODO(haibinlu): Pass additional parameters into the CreateRoute request, | |
| 212 // e.g. low-fps-mirror, user-override. (crbug.com/490364) | |
| 213 bool success = false; | |
| 214 if (IsValidCastModeNum(cast_mode_num)) { | |
| 215 // User explicitly selected cast mode. | |
| 216 DVLOG(2) << "Cast mode override: " << cast_mode_num; | |
| 217 success = media_router_ui->CreateRouteWithCastModeOverride( | |
| 218 sink_id, static_cast<MediaCastMode>(cast_mode_num)); | |
| 219 } else { | |
| 220 success = media_router_ui->CreateRoute(sink_id); | |
| 221 } | |
| 222 | |
| 223 // TODO(imcheng): Display error in UI. (crbug.com/490372) | |
| 224 if (!success) | |
| 225 LOG(ERROR) << "Error initiating route request."; | |
| 66 } | 226 } |
| 67 | 227 |
| 68 void MediaRouterWebUIMessageHandler::OnActOnIssue( | 228 void MediaRouterWebUIMessageHandler::OnActOnIssue( |
| 69 const base::ListValue* args) { | 229 const base::ListValue* args) { |
| 70 // TODO(imcheng): Implement. | 230 // TODO(imcheng): Implement. |
|
Wez
2015/05/21 22:58:37
Bug#?
imcheng (use chromium acct)
2015/05/22 00:02:01
Done.
| |
| 71 NOTIMPLEMENTED(); | 231 NOTIMPLEMENTED(); |
| 72 } | 232 } |
| 73 | 233 |
| 74 void MediaRouterWebUIMessageHandler::OnCloseRoute( | 234 void MediaRouterWebUIMessageHandler::OnCloseRoute( |
| 75 const base::ListValue* args) { | 235 const base::ListValue* args) { |
| 76 // TODO(imcheng): Implement. | 236 DVLOG(2) << "OnCloseRoute"; |
| 77 NOTIMPLEMENTED(); | 237 std::string route_id; |
| 238 if (!args->GetString(0, &route_id)) { | |
| 239 LOG(ERROR) << "Unable to extract args."; | |
| 240 return; | |
| 241 } | |
| 242 GetMediaRouterUI()->CloseRoute(route_id); | |
| 78 } | 243 } |
| 79 | 244 |
| 80 void MediaRouterWebUIMessageHandler::OnCloseDialog( | 245 void MediaRouterWebUIMessageHandler::OnCloseDialog( |
| 81 const base::ListValue* args) { | 246 const base::ListValue* args) { |
| 82 CHECK(!dialog_closing_); | 247 if (dialog_closing_) |
| 248 return; | |
| 249 | |
| 83 dialog_closing_ = true; | 250 dialog_closing_ = true; |
| 84 GetMediaRouterUI()->Close(); | 251 GetMediaRouterUI()->Close(); |
| 85 } | 252 } |
| 86 | 253 |
| 87 MediaRouterUI* MediaRouterWebUIMessageHandler::GetMediaRouterUI() const { | 254 MediaRouterUI* MediaRouterWebUIMessageHandler::GetMediaRouterUI() const { |
| 88 MediaRouterUI* media_router_ui = | 255 MediaRouterUI* media_router_ui = |
| 89 static_cast<MediaRouterUI*>(web_ui()->GetController()); | 256 static_cast<MediaRouterUI*>(web_ui()->GetController()); |
| 90 DCHECK(media_router_ui); | 257 DCHECK(media_router_ui); |
| 91 return media_router_ui; | 258 return media_router_ui; |
| 92 } | 259 } |
| 93 | 260 |
| 94 } // namespace media_router | 261 } // namespace media_router |
| 95 | 262 |
| OLD | NEW |