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.
5 #include "chrome/browser/chromeos/imageburner/burn_controller.h"
8 #include "base/files/file_path.h"
9 #include "base/memory/weak_ptr.h"
10 #include "chrome/browser/chromeos/imageburner/burn_manager.h"
11 #include "chromeos/network/network_state_handler.h"
15 namespace imageburner {
19 // 3.9GB. It is less than 4GB because true device size ussually varies a little.
20 const uint64 kMinDeviceSize = static_cast<uint64>(3.9 * 1000 * 1000 * 1000);
22 class BurnControllerImpl
23 : public BurnController,
24 public StateMachine::Observer,
25 public BurnManager::Observer {
27 explicit BurnControllerImpl(BurnController::Delegate* delegate)
28 : burn_manager_(NULL),
32 burn_manager_ = BurnManager::GetInstance();
33 burn_manager_->AddObserver(this);
34 state_machine_ = burn_manager_->state_machine();
35 state_machine_->AddObserver(this);
38 virtual ~BurnControllerImpl() {
39 state_machine_->RemoveObserver(this);
40 burn_manager_->RemoveObserver(this);
43 // BurnManager::Observer override.
44 virtual void OnDeviceAdded(
45 const disks::DiskMountManager::Disk& disk) OVERRIDE {
46 delegate_->OnDeviceAdded(disk);
49 // BurnManager::Observer override.
50 virtual void OnDeviceRemoved(
51 const disks::DiskMountManager::Disk& disk) OVERRIDE {
52 delegate_->OnDeviceRemoved(disk);
55 // BurnManager::Observer override.
56 virtual void OnNetworkDetected() OVERRIDE {
57 delegate_->OnNetworkDetected();
60 // BurnManager::Observer override.
61 virtual void OnSuccess() OVERRIDE {
62 delegate_->OnSuccess();
63 // TODO(hidehiko): Remove |working_| flag.
67 // BurnManager::Observer override.
68 virtual void OnProgressWithRemainingTime(
69 ProgressType progress_type,
72 const base::TimeDelta& estimated_remaining_time) OVERRIDE {
73 delegate_->OnProgressWithRemainingTime(
74 progress_type, received_bytes, total_bytes, estimated_remaining_time);
77 // BurnManager::Observer override.
78 virtual void OnProgress(ProgressType progress_type,
80 int64 total_bytes) OVERRIDE {
81 delegate_->OnProgress(progress_type, received_bytes, total_bytes);
84 // StateMachine::Observer interface.
85 virtual void OnBurnStateChanged(StateMachine::State state) OVERRIDE {
86 if (state != StateMachine::INITIAL && !working_) {
87 // User has started burn process, so let's start observing.
88 StartBurnImage(base::FilePath(), base::FilePath());
92 virtual void OnError(int error_message_id) OVERRIDE {
93 delegate_->OnFail(error_message_id);
97 // BurnController override.
98 virtual void Init() OVERRIDE {
99 if (state_machine_->state() == StateMachine::BURNING) {
100 // There is nothing else left to do but observe burn progress.
101 burn_manager_->DoBurn();
102 } else if (state_machine_->state() != StateMachine::INITIAL) {
103 // User has started burn process, so let's start observing.
104 StartBurnImage(base::FilePath(), base::FilePath());
108 // BurnController override.
109 virtual std::vector<disks::DiskMountManager::Disk> GetBurnableDevices()
111 // Now this is just a proxy to the BurnManager.
112 // TODO(hidehiko): Remove this method.
113 return burn_manager_->GetBurnableDevices();
116 // BurnController override.
117 virtual void CancelBurnImage() OVERRIDE {
118 burn_manager_->Cancel();
121 // BurnController override.
122 // May be called with empty values if there is a handler that has started
123 // burning, and thus set the target paths.
124 virtual void StartBurnImage(const base::FilePath& target_device_path,
125 const base::FilePath& target_file_path) OVERRIDE {
126 if (!target_device_path.empty() && !target_file_path.empty() &&
127 state_machine_->new_burn_posible()) {
128 if (!NetworkHandler::Get()->network_state_handler()->DefaultNetwork()) {
129 delegate_->OnNoNetwork();
132 burn_manager_->set_target_device_path(target_device_path);
133 burn_manager_->set_target_file_path(target_file_path);
134 uint64 device_size = GetDeviceSize(
135 burn_manager_->target_device_path().value());
136 if (device_size < kMinDeviceSize) {
137 delegate_->OnDeviceTooSmall(device_size);
144 // Send progress signal now so ui doesn't hang in intial state until we get
146 delegate_->OnProgress(DOWNLOADING, 0, 0);
147 if (burn_manager_->GetImageDir().empty()) {
148 burn_manager_->CreateImageDir();
150 burn_manager_->FetchConfigFile();
155 int64 GetDeviceSize(const std::string& device_path) {
156 disks::DiskMountManager* disk_mount_manager =
157 disks::DiskMountManager::GetInstance();
158 const disks::DiskMountManager::Disk* disk =
159 disk_mount_manager->FindDiskBySourcePath(device_path);
160 return disk ? disk->total_size_in_bytes() : 0;
163 BurnManager* burn_manager_;
164 StateMachine* state_machine_;
166 BurnController::Delegate* delegate_;
168 DISALLOW_COPY_AND_ASSIGN(BurnControllerImpl);
174 BurnController* BurnController::CreateBurnController(
175 content::WebContents* web_contents,
176 Delegate* delegate) {
177 return new BurnControllerImpl(delegate);
180 } // namespace imageburner
181 } // namespace chromeos