1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "chrome/browser/ui/webui/chromeos/imageburner/imageburner_ui.h" 6 7 #include "base/bind.h" 8 #include "base/files/file_path.h" 9 #include "base/i18n/rtl.h" 10 #include "base/strings/string16.h" 11 #include "base/strings/utf_string_conversions.h" 12 #include "base/values.h" 13 #include "chrome/browser/chromeos/imageburner/burn_controller.h" 14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/common/url_constants.h" 16 #include "content/public/browser/web_ui.h" 17 #include "content/public/browser/web_ui_data_source.h" 18 #include "content/public/browser/web_ui_message_handler.h" 19 #include "grit/browser_resources.h" 20 #include "grit/generated_resources.h" 21 #include "ui/base/l10n/l10n_util.h" 22 #include "ui/base/l10n/time_format.h" 23 #include "ui/base/text/bytes_formatting.h" 24 #include "url/gurl.h" 25 26 namespace chromeos { 27 namespace imageburner { 28 29 namespace { 30 31 const char kPropertyDevicePath[] = "devicePath"; 32 const char kPropertyFilePath[] = "filePath"; 33 const char kPropertyLabel[] = "label"; 34 const char kPropertyDeviceType[] = "type"; 35 36 // Link displayed on imageburner ui. 37 const char kMoreInfoLink[] = 38 "http://www.chromium.org/chromium-os/chromiumos-design-docs/recovery-mode"; 39 40 content::WebUIDataSource* CreateImageburnerUIHTMLSource() { 41 content::WebUIDataSource* source = 42 content::WebUIDataSource::Create(chrome::kChromeUIImageBurnerHost); 43 44 source->AddLocalizedString("headerTitle", IDS_IMAGEBURN_HEADER_TITLE); 45 source->AddLocalizedString("headerDescription", 46 IDS_IMAGEBURN_HEADER_DESCRIPTION); 47 source->AddLocalizedString("headerLink", IDS_IMAGEBURN_HEADER_LINK); 48 source->AddLocalizedString("statusDevicesNone", 49 IDS_IMAGEBURN_NO_DEVICES_STATUS); 50 source->AddLocalizedString("warningDevicesNone", 51 IDS_IMAGEBURN_NO_DEVICES_WARNING); 52 source->AddLocalizedString("statusDevicesMultiple", 53 IDS_IMAGEBURN_MUL_DEVICES_STATUS); 54 source->AddLocalizedString("statusDeviceUSB", 55 IDS_IMAGEBURN_USB_DEVICE_STATUS); 56 source->AddLocalizedString("statusDeviceSD", 57 IDS_IMAGEBURN_SD_DEVICE_STATUS); 58 source->AddLocalizedString("warningDevices", 59 IDS_IMAGEBURN_DEVICES_WARNING); 60 source->AddLocalizedString("statusNoConnection", 61 IDS_IMAGEBURN_NO_CONNECTION_STATUS); 62 source->AddLocalizedString("warningNoConnection", 63 IDS_IMAGEBURN_NO_CONNECTION_WARNING); 64 source->AddLocalizedString("statusNoSpace", 65 IDS_IMAGEBURN_INSUFFICIENT_SPACE_STATUS); 66 source->AddLocalizedString("warningNoSpace", 67 IDS_IMAGEBURN_INSUFFICIENT_SPACE_WARNING); 68 source->AddLocalizedString("statusDownloading", 69 IDS_IMAGEBURN_DOWNLOADING_STATUS); 70 source->AddLocalizedString("statusUnzip", IDS_IMAGEBURN_UNZIP_STATUS); 71 source->AddLocalizedString("statusBurn", IDS_IMAGEBURN_BURN_STATUS); 72 source->AddLocalizedString("statusError", IDS_IMAGEBURN_ERROR_STATUS); 73 source->AddLocalizedString("statusSuccess", IDS_IMAGEBURN_SUCCESS_STATUS); 74 source->AddLocalizedString("warningSuccess", IDS_IMAGEBURN_SUCCESS_DESC); 75 source->AddLocalizedString("title", IDS_IMAGEBURN_PAGE_TITLE); 76 source->AddLocalizedString("confirmButton", IDS_IMAGEBURN_CONFIRM_BUTTON); 77 source->AddLocalizedString("cancelButton", IDS_IMAGEBURN_CANCEL_BUTTON); 78 source->AddLocalizedString("retryButton", IDS_IMAGEBURN_RETRY_BUTTON); 79 source->AddString("moreInfoLink", base::ASCIIToUTF16(kMoreInfoLink)); 80 81 source->SetJsonPath("strings.js"); 82 source->AddResourcePath("image_burner.js", IDR_IMAGEBURNER_JS); 83 source->SetDefaultResource(IDR_IMAGEBURNER_HTML); 84 return source; 85 } 86 87 class WebUIHandler 88 : public content::WebUIMessageHandler, 89 public BurnController::Delegate { 90 public: 91 explicit WebUIHandler(content::WebContents* contents) 92 : burn_controller_(BurnController::CreateBurnController(contents, this)){ 93 } 94 95 virtual ~WebUIHandler() { 96 } 97 98 // WebUIMessageHandler implementation. 99 virtual void RegisterMessages() OVERRIDE { 100 web_ui()->RegisterMessageCallback( 101 "getDevices", 102 base::Bind(&WebUIHandler::HandleGetDevices, base::Unretained(this))); 103 web_ui()->RegisterMessageCallback( 104 "burnImage", 105 base::Bind(&WebUIHandler::HandleBurnImage, base::Unretained(this))); 106 web_ui()->RegisterMessageCallback( 107 "cancelBurnImage", 108 base::Bind(&WebUIHandler::HandleCancelBurnImage, 109 base::Unretained(this))); 110 web_ui()->RegisterMessageCallback( 111 "webuiInitialized", 112 base::Bind(&WebUIHandler::HandleWebUIInitialized, 113 base::Unretained(this))); 114 } 115 116 // BurnController::Delegate override. 117 virtual void OnSuccess() OVERRIDE { 118 web_ui()->CallJavascriptFunction("browserBridge.reportSuccess"); 119 } 120 121 // BurnController::Delegate override. 122 virtual void OnFail(int error_message_id) OVERRIDE { 123 base::StringValue error_message( 124 l10n_util::GetStringUTF16(error_message_id)); 125 web_ui()->CallJavascriptFunction("browserBridge.reportFail", error_message); 126 } 127 128 // BurnController::Delegate override. 129 virtual void OnDeviceAdded(const disks::DiskMountManager::Disk& disk) 130 OVERRIDE { 131 base::DictionaryValue disk_value; 132 CreateDiskValue(disk, &disk_value); 133 web_ui()->CallJavascriptFunction("browserBridge.deviceAdded", disk_value); 134 } 135 136 // BurnController::Delegate override. 137 virtual void OnDeviceRemoved(const disks::DiskMountManager::Disk& disk) 138 OVERRIDE { 139 base::StringValue device_path_value(disk.device_path()); 140 web_ui()->CallJavascriptFunction("browserBridge.deviceRemoved", 141 device_path_value); 142 } 143 144 // BurnController::Delegate override. 145 virtual void OnDeviceTooSmall(int64 device_size) OVERRIDE { 146 base::string16 size; 147 GetDataSizeText(device_size, &size); 148 base::StringValue device_size_text(size); 149 web_ui()->CallJavascriptFunction("browserBridge.reportDeviceTooSmall", 150 device_size_text); 151 } 152 153 // BurnController::Delegate override. 154 virtual void OnProgress(ProgressType progress_type, 155 int64 amount_finished, 156 int64 amount_total) OVERRIDE { 157 const base::string16 time_remaining_text = 158 l10n_util::GetStringUTF16(IDS_IMAGEBURN_PROGRESS_TIME_UNKNOWN); 159 SendProgressSignal(progress_type, amount_finished, amount_total, 160 time_remaining_text); 161 } 162 163 // BurnController::Delegate override. 164 virtual void OnProgressWithRemainingTime( 165 ProgressType progress_type, 166 int64 amount_finished, 167 int64 amount_total, 168 const base::TimeDelta& time_remaining) OVERRIDE { 169 const base::string16 time_remaining_text = l10n_util::GetStringFUTF16( 170 IDS_IMAGEBURN_DOWNLOAD_TIME_REMAINING, 171 ui::TimeFormat::Simple(ui::TimeFormat::FORMAT_REMAINING, 172 ui::TimeFormat::LENGTH_SHORT, time_remaining)); 173 SendProgressSignal(progress_type, amount_finished, amount_total, 174 time_remaining_text); 175 } 176 177 // BurnController::Delegate override. 178 virtual void OnNetworkDetected() OVERRIDE { 179 web_ui()->CallJavascriptFunction("browserBridge.reportNetworkDetected"); 180 } 181 182 // BurnController::Delegate override. 183 virtual void OnNoNetwork() OVERRIDE { 184 web_ui()->CallJavascriptFunction("browserBridge.reportNoNetwork"); 185 } 186 187 private: 188 void CreateDiskValue(const disks::DiskMountManager::Disk& disk, 189 base::DictionaryValue* disk_value) { 190 base::string16 label = base::ASCIIToUTF16(disk.drive_label()); 191 base::i18n::AdjustStringForLocaleDirection(&label); 192 disk_value->SetString(std::string(kPropertyLabel), label); 193 disk_value->SetString(std::string(kPropertyFilePath), disk.file_path()); 194 disk_value->SetString(std::string(kPropertyDevicePath), disk.device_path()); 195 disk_value->SetString(std::string(kPropertyDeviceType), 196 disks::DiskMountManager::DeviceTypeToString(disk.device_type())); 197 } 198 199 // Callback for the "getDevices" message. 200 void HandleGetDevices(const base::ListValue* args) { 201 const std::vector<disks::DiskMountManager::Disk> disks 202 = burn_controller_->GetBurnableDevices(); 203 base::ListValue results_value; 204 for (size_t i = 0; i != disks.size(); ++i) { 205 base::DictionaryValue* disk_value = new base::DictionaryValue(); 206 CreateDiskValue(disks[i], disk_value); 207 results_value.Append(disk_value); 208 } 209 web_ui()->CallJavascriptFunction("browserBridge.getDevicesCallback", 210 results_value); 211 } 212 213 // Callback for the webuiInitialized message. 214 void HandleWebUIInitialized(const base::ListValue* args) { 215 burn_controller_->Init(); 216 } 217 218 // Callback for the "cancelBurnImage" message. 219 void HandleCancelBurnImage(const base::ListValue* args) { 220 burn_controller_->CancelBurnImage(); 221 } 222 223 // Callback for the "burnImage" message. 224 // It may be called with NULL if there is a handler that has started burning, 225 // and thus set the target paths. 226 void HandleBurnImage(const base::ListValue* args) { 227 base::FilePath target_device_path; 228 ExtractTargetedDevicePath(*args, 0, &target_device_path); 229 230 base::FilePath target_file_path; 231 ExtractTargetedDevicePath(*args, 1, &target_file_path); 232 233 burn_controller_->StartBurnImage(target_device_path, target_file_path); 234 } 235 236 // Reports update to UI 237 void SendProgressSignal(ProgressType progress_type, 238 int64 amount_finished, 239 int64 amount_total, 240 const base::string16& time_remaining_text) { 241 base::DictionaryValue progress; 242 int progress_message_id = 0; 243 switch (progress_type) { 244 case DOWNLOADING: 245 progress.SetString("progressType", "download"); 246 progress_message_id = IDS_IMAGEBURN_DOWNLOAD_PROGRESS_TEXT; 247 break; 248 case UNZIPPING: 249 progress.SetString("progressType", "unzip"); 250 break; 251 case BURNING: 252 progress.SetString("progressType", "burn"); 253 progress_message_id = IDS_IMAGEBURN_BURN_PROGRESS_TEXT; 254 break; 255 default: 256 return; 257 } 258 259 progress.SetInteger("amountFinished", amount_finished); 260 progress.SetInteger("amountTotal", amount_total); 261 if (amount_total != 0) { 262 base::string16 progress_text; 263 GetProgressText(progress_message_id, amount_finished, amount_total, 264 &progress_text); 265 progress.SetString("progressText", progress_text); 266 } else { 267 progress.SetString("progressText", ""); 268 } 269 progress.SetString("timeLeftText", time_remaining_text); 270 271 web_ui()->CallJavascriptFunction("browserBridge.updateProgress", progress); 272 } 273 274 // size_text should be previously created. 275 void GetDataSizeText(int64 size, base::string16* size_text) { 276 *size_text = ui::FormatBytes(size); 277 base::i18n::AdjustStringForLocaleDirection(size_text); 278 } 279 280 // progress_text should be previously created. 281 void GetProgressText(int message_id, 282 int64 amount_finished, 283 int64 amount_total, 284 base::string16* progress_text) { 285 base::string16 finished; 286 GetDataSizeText(amount_finished, &finished); 287 base::string16 total; 288 GetDataSizeText(amount_total, &total); 289 *progress_text = l10n_util::GetStringFUTF16(message_id, finished, total); 290 } 291 292 // device_path has to be previously created. 293 void ExtractTargetedDevicePath(const base::ListValue& list_value, 294 int index, 295 base::FilePath* device_path) { 296 const base::Value* list_member; 297 std::string image_dest; 298 if (list_value.Get(index, &list_member) && 299 list_member->GetType() == base::Value::TYPE_STRING && 300 list_member->GetAsString(&image_dest)) { 301 *device_path = base::FilePath(image_dest); 302 } else { 303 LOG(ERROR) << "Unable to get path string"; 304 device_path->clear(); 305 } 306 } 307 308 scoped_ptr<BurnController> burn_controller_; 309 310 DISALLOW_COPY_AND_ASSIGN(WebUIHandler); 311 }; 312 313 } // namespace 314 315 } // namespace imageburner 316 } // namespace chromeos 317 318 //////////////////////////////////////////////////////////////////////////////// 319 // 320 // ImageBurnUI 321 // 322 //////////////////////////////////////////////////////////////////////////////// 323 324 ImageBurnUI::ImageBurnUI(content::WebUI* web_ui) : WebUIController(web_ui) { 325 chromeos::imageburner::WebUIHandler* handler = 326 new chromeos::imageburner::WebUIHandler(web_ui->GetWebContents()); 327 web_ui->AddMessageHandler(handler); 328 329 Profile* profile = Profile::FromWebUI(web_ui); 330 content::WebUIDataSource::Add( 331 profile, chromeos::imageburner::CreateImageburnerUIHTMLSource()); 332 } 333