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