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
332
333
334
335
336
337
338
339
340
341
342
343
344
|
// 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/chromeos/imageburner/burn_controller.h"
#include "base/bind.h"
#include "base/file_path.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/chromeos/cros/cros_library.h"
#include "chrome/browser/chromeos/cros/network_library.h"
#include "chrome/browser/chromeos/imageburner/burn_manager.h"
#include "grit/generated_resources.h"
#include "googleurl/src/gurl.h"
namespace chromeos {
namespace imageburner {
namespace {
const char kImageZipFileName[] = "chromeos_image.bin.zip";
// 3.9GB. It is less than 4GB because true device size ussually varies a little.
const uint64 kMinDeviceSize = static_cast<uint64>(3.9 * 1000 * 1000 * 1000);
// Returns true when |disk| is a device on which we can burn recovery image.
bool IsBurnableDevice(const disks::DiskMountManager::Disk& disk) {
return disk.is_parent() && !disk.on_boot_device() && disk.has_media() &&
(disk.device_type() == DEVICE_TYPE_USB ||
disk.device_type() == DEVICE_TYPE_SD);
}
class BurnControllerImpl
: public BurnController,
public disks::DiskMountManager::Observer,
public NetworkLibrary::NetworkManagerObserver,
public StateMachine::Observer,
public BurnManager::Observer {
public:
explicit BurnControllerImpl(BurnController::Delegate* delegate)
: burn_manager_(NULL),
state_machine_(NULL),
working_(false),
delegate_(delegate) {
disks::DiskMountManager::GetInstance()->AddObserver(this);
CrosLibrary::Get()->GetNetworkLibrary()->AddNetworkManagerObserver(this);
burn_manager_ = BurnManager::GetInstance();
burn_manager_->AddObserver(this);
state_machine_ = burn_manager_->state_machine();
state_machine_->AddObserver(this);
}
virtual ~BurnControllerImpl() {
if (state_machine_)
state_machine_->RemoveObserver(this);
burn_manager_->RemoveObserver(this);
CrosLibrary::Get()->GetNetworkLibrary()->RemoveNetworkManagerObserver(this);
disks::DiskMountManager::GetInstance()->RemoveObserver(this);
}
// disks::DiskMountManager::Observer interface.
virtual void OnDiskEvent(disks::DiskMountManager::DiskEvent event,
const disks::DiskMountManager::Disk* disk) OVERRIDE {
if (!IsBurnableDevice(*disk))
return;
if (event == disks::DiskMountManager::DISK_ADDED) {
delegate_->OnDeviceAdded(*disk);
} else if (event == disks::DiskMountManager::DISK_REMOVED) {
delegate_->OnDeviceRemoved(*disk);
if (burn_manager_->target_device_path().value() == disk->device_path())
ProcessError(IDS_IMAGEBURN_DEVICE_NOT_FOUND_ERROR);
}
}
virtual void OnDeviceEvent(disks::DiskMountManager::DeviceEvent event,
const std::string& device_path) OVERRIDE {
}
virtual void OnMountEvent(
disks::DiskMountManager::MountEvent event,
MountError error_code,
const disks::DiskMountManager::MountPointInfo& mount_info) OVERRIDE {
}
virtual void OnFormatEvent(
disks::DiskMountManager::FormatEvent event,
FormatError error_code,
const std::string& device_path) OVERRIDE {
}
// NetworkLibrary::NetworkManagerObserver interface.
virtual void OnNetworkManagerChanged(NetworkLibrary* obj) OVERRIDE {
if (state_machine_->state() == StateMachine::INITIAL && CheckNetwork())
delegate_->OnNetworkDetected();
if (state_machine_->state() == StateMachine::DOWNLOADING &&
!CheckNetwork())
ProcessError(IDS_IMAGEBURN_NETWORK_ERROR);
}
// BurnManager::Observer override.
virtual void OnImageDirCreated(bool success) OVERRIDE {
if (!success) {
// Failed to create the directory. Finish the burning process
// with failure state.
DownloadCompleted(false);
return;
}
zip_image_file_path_ =
burn_manager_->GetImageDir().Append(kImageZipFileName);
burn_manager_->FetchConfigFile();
}
// Part of BurnManager::Delegate interface.
virtual void OnConfigFileFetched(bool success,
const std::string& image_file_name,
const GURL& image_download_url) OVERRIDE {
if (!success) {
DownloadCompleted(false);
return;
}
image_file_name_ = image_file_name;
if (state_machine_->download_finished()) {
BurnImage();
return;
}
if (!state_machine_->download_started()) {
burn_manager_->FetchImage(image_download_url, zip_image_file_path_);
state_machine_->OnDownloadStarted();
}
}
// BurnManager::Observer override.
virtual void OnImageFileFetchDownloadProgressUpdated(
int64 received_bytes,
int64 total_bytes,
const base::TimeDelta& estimated_remaining_time) OVERRIDE {
if (state_machine_->state() == StateMachine::DOWNLOADING) {
delegate_->OnProgressWithRemainingTime(DOWNLOADING,
received_bytes,
total_bytes,
estimated_remaining_time);
}
}
// BurnManager::Observer override.
virtual void OnImageFileFetched(bool success) OVERRIDE {
DownloadCompleted(success);
}
// BurnManager::Observer override.
virtual void OnBurnProgressUpdated(BurnEvent event,
const ImageBurnStatus& status) OVERRIDE {
switch (event) {
case(BURN_SUCCESS):
FinalizeBurn();
break;
case(BURN_FAIL):
ProcessError(IDS_IMAGEBURN_BURN_ERROR);
break;
case(BURN_UPDATE):
delegate_->OnProgress(BURNING, status.amount_burnt, status.total_size);
break;
case(UNZIP_STARTED):
delegate_->OnProgress(UNZIPPING, 0, 0);
break;
case(UNZIP_FAIL):
ProcessError(IDS_IMAGEBURN_EXTRACTING_ERROR);
break;
case(UNZIP_COMPLETE):
// We ignore this.
break;
default:
NOTREACHED();
break;
}
}
// StateMachine::Observer interface.
virtual void OnBurnStateChanged(StateMachine::State state) OVERRIDE {
if (state == StateMachine::CANCELLED) {
ProcessError(IDS_IMAGEBURN_USER_ERROR);
} else if (state != StateMachine::INITIAL && !working_) {
// User has started burn process, so let's start observing.
StartBurnImage(base::FilePath(), base::FilePath());
}
}
virtual void OnError(int error_message_id) OVERRIDE {
delegate_->OnFail(error_message_id);
working_ = false;
}
// BurnController override.
virtual void Init() OVERRIDE {
if (state_machine_->state() == StateMachine::BURNING) {
// There is nothing else left to do but observe burn progress.
BurnImage();
} else if (state_machine_->state() != StateMachine::INITIAL) {
// User has started burn process, so let's start observing.
StartBurnImage(base::FilePath(), base::FilePath());
}
}
// BurnController override.
virtual std::vector<disks::DiskMountManager::Disk> GetBurnableDevices()
OVERRIDE {
const disks::DiskMountManager::DiskMap& disks =
disks::DiskMountManager::GetInstance()->disks();
std::vector<disks::DiskMountManager::Disk> result;
for (disks::DiskMountManager::DiskMap::const_iterator iter = disks.begin();
iter != disks.end();
++iter) {
const disks::DiskMountManager::Disk& disk = *iter->second;
if (IsBurnableDevice(disk))
result.push_back(disk);
}
return result;
}
// BurnController override.
virtual void CancelBurnImage() OVERRIDE {
state_machine_->OnCancelation();
}
// BurnController override.
// May be called with empty values if there is a handler that has started
// burning, and thus set the target paths.
virtual void StartBurnImage(const base::FilePath& target_device_path,
const base::FilePath& target_file_path) OVERRIDE {
if (!target_device_path.empty() && !target_file_path.empty() &&
state_machine_->new_burn_posible()) {
if (!CheckNetwork()) {
delegate_->OnNoNetwork();
return;
}
burn_manager_->set_target_device_path(target_device_path);
burn_manager_->set_target_file_path(target_file_path);
uint64 device_size = GetDeviceSize(
burn_manager_->target_device_path().value());
if (device_size < kMinDeviceSize) {
delegate_->OnDeviceTooSmall(device_size);
return;
}
}
if (working_)
return;
working_ = true;
// Send progress signal now so ui doesn't hang in intial state until we get
// config file
delegate_->OnProgress(DOWNLOADING, 0, 0);
if (burn_manager_->GetImageDir().empty()) {
burn_manager_->CreateImageDir();
} else {
OnImageDirCreated(true);
}
}
private:
void DownloadCompleted(bool success) {
if (success) {
state_machine_->OnDownloadFinished();
BurnImage();
} else {
ProcessError(IDS_IMAGEBURN_DOWNLOAD_ERROR);
}
}
void BurnImage() {
if (state_machine_->state() == StateMachine::BURNING)
return;
state_machine_->OnBurnStarted();
burn_manager_->DoBurn(zip_image_file_path_, image_file_name_);
}
void FinalizeBurn() {
state_machine_->OnSuccess();
burn_manager_->ResetTargetPaths();
delegate_->OnSuccess();
working_ = false;
}
// Error is ussually detected by all existing Burn handlers, but only first
// one that calls ProcessError should actually process it.
void ProcessError(int message_id) {
// If we are in intial state, error has already been dispached.
if (state_machine_->state() == StateMachine::INITIAL) {
return;
}
// Remember burner state, since it will be reset after OnError call.
StateMachine::State state = state_machine_->state();
// Dispach error. All hadlers' OnError event will be called before returning
// from this. This includes us, too.
state_machine_->OnError(message_id);
// Do cleanup.
if (state == StateMachine::DOWNLOADING) {
burn_manager_->CancelImageFetch();
} else if (state == StateMachine::BURNING) {
// Burn library doesn't send cancelled signal upon CancelBurnImage
// invokation.
burn_manager_->CancelBurnImage();
}
burn_manager_->ResetTargetPaths();
}
int64 GetDeviceSize(const std::string& device_path) {
disks::DiskMountManager* disk_mount_manager =
disks::DiskMountManager::GetInstance();
const disks::DiskMountManager::Disk* disk =
disk_mount_manager->FindDiskBySourcePath(device_path);
return disk ? disk->total_size_in_bytes() : 0;
}
bool CheckNetwork() {
return CrosLibrary::Get()->GetNetworkLibrary()->Connected();
}
base::FilePath zip_image_file_path_;
std::string image_file_name_;
BurnManager* burn_manager_;
StateMachine* state_machine_;
bool working_;
BurnController::Delegate* delegate_;
DISALLOW_COPY_AND_ASSIGN(BurnControllerImpl);
};
} // namespace
// static
BurnController* BurnController::CreateBurnController(
content::WebContents* web_contents,
Delegate* delegate) {
return new BurnControllerImpl(delegate);
}
} // namespace imageburner
} // namespace chromeos
|