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
|
// 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/files/file_path.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/chromeos/imageburner/burn_manager.h"
#include "chromeos/network/network_state_handler.h"
#include "grit/generated_resources.h"
#include "url/gurl.h"
namespace chromeos {
namespace imageburner {
namespace {
// 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);
class BurnControllerImpl
: public BurnController,
public StateMachine::Observer,
public BurnManager::Observer {
public:
explicit BurnControllerImpl(BurnController::Delegate* delegate)
: burn_manager_(NULL),
state_machine_(NULL),
working_(false),
delegate_(delegate) {
burn_manager_ = BurnManager::GetInstance();
burn_manager_->AddObserver(this);
state_machine_ = burn_manager_->state_machine();
state_machine_->AddObserver(this);
}
virtual ~BurnControllerImpl() {
state_machine_->RemoveObserver(this);
burn_manager_->RemoveObserver(this);
}
// BurnManager::Observer override.
virtual void OnDeviceAdded(
const disks::DiskMountManager::Disk& disk) OVERRIDE {
delegate_->OnDeviceAdded(disk);
}
// BurnManager::Observer override.
virtual void OnDeviceRemoved(
const disks::DiskMountManager::Disk& disk) OVERRIDE {
delegate_->OnDeviceRemoved(disk);
}
// BurnManager::Observer override.
virtual void OnNetworkDetected() OVERRIDE {
delegate_->OnNetworkDetected();
}
// BurnManager::Observer override.
virtual void OnSuccess() OVERRIDE {
delegate_->OnSuccess();
// TODO(hidehiko): Remove |working_| flag.
working_ = false;
}
// BurnManager::Observer override.
virtual void OnProgressWithRemainingTime(
ProgressType progress_type,
int64 received_bytes,
int64 total_bytes,
const base::TimeDelta& estimated_remaining_time) OVERRIDE {
delegate_->OnProgressWithRemainingTime(
progress_type, received_bytes, total_bytes, estimated_remaining_time);
}
// BurnManager::Observer override.
virtual void OnProgress(ProgressType progress_type,
int64 received_bytes,
int64 total_bytes) OVERRIDE {
delegate_->OnProgress(progress_type, received_bytes, total_bytes);
}
// StateMachine::Observer interface.
virtual void OnBurnStateChanged(StateMachine::State state) OVERRIDE {
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.
burn_manager_->DoBurn();
} 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 {
// Now this is just a proxy to the BurnManager.
// TODO(hidehiko): Remove this method.
return burn_manager_->GetBurnableDevices();
}
// BurnController override.
virtual void CancelBurnImage() OVERRIDE {
burn_manager_->Cancel();
}
// 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 (!NetworkHandler::Get()->network_state_handler()->DefaultNetwork()) {
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 {
burn_manager_->FetchConfigFile();
}
}
private:
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;
}
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
|