| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chromeos/imageburner/burn_manager.h" | 5 #include "chrome/browser/chromeos/imageburner/burn_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/threading/worker_pool.h" | 10 #include "base/threading/worker_pool.h" |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 //////////////////////////////////////////////////////////////////////////////// | 207 //////////////////////////////////////////////////////////////////////////////// |
| 208 // | 208 // |
| 209 // BurnManager | 209 // BurnManager |
| 210 // | 210 // |
| 211 //////////////////////////////////////////////////////////////////////////////// | 211 //////////////////////////////////////////////////////////////////////////////// |
| 212 | 212 |
| 213 BurnManager::BurnManager( | 213 BurnManager::BurnManager( |
| 214 const base::FilePath& downloads_directory, | 214 const base::FilePath& downloads_directory, |
| 215 scoped_refptr<net::URLRequestContextGetter> context_getter) | 215 scoped_refptr<net::URLRequestContextGetter> context_getter) |
| 216 : device_handler_(disks::DiskMountManager::GetInstance()), | 216 : device_handler_(disks::DiskMountManager::GetInstance()), |
| 217 image_dir_created_(false), |
| 217 unzipping_(false), | 218 unzipping_(false), |
| 218 cancelled_(false), | 219 cancelled_(false), |
| 219 burning_(false), | 220 burning_(false), |
| 220 block_burn_signals_(false), | 221 block_burn_signals_(false), |
| 221 image_dir_(downloads_directory.Append(kTempImageFolderName)), | 222 image_dir_(downloads_directory.Append(kTempImageFolderName)), |
| 222 config_file_url_(kConfigFileUrl), | 223 config_file_url_(kConfigFileUrl), |
| 223 config_file_fetched_(false), | 224 config_file_fetched_(false), |
| 224 state_machine_(new StateMachine()), | 225 state_machine_(new StateMachine()), |
| 225 url_request_context_getter_(context_getter), | 226 url_request_context_getter_(context_getter), |
| 226 bytes_image_download_progress_last_reported_(0), | 227 bytes_image_download_progress_last_reported_(0), |
| 227 weak_ptr_factory_(this) { | 228 weak_ptr_factory_(this) { |
| 228 NetworkStateHandler::Get()->AddObserver(this); | 229 NetworkStateHandler::Get()->AddObserver(this); |
| 229 base::WeakPtr<BurnManager> weak_ptr(weak_ptr_factory_.GetWeakPtr()); | 230 base::WeakPtr<BurnManager> weak_ptr(weak_ptr_factory_.GetWeakPtr()); |
| 230 device_handler_.SetCallbacks( | 231 device_handler_.SetCallbacks( |
| 231 base::Bind(&BurnManager::NotifyDeviceAdded, weak_ptr), | 232 base::Bind(&BurnManager::NotifyDeviceAdded, weak_ptr), |
| 232 base::Bind(&BurnManager::NotifyDeviceRemoved, weak_ptr)); | 233 base::Bind(&BurnManager::NotifyDeviceRemoved, weak_ptr)); |
| 233 DBusThreadManager::Get()->GetImageBurnerClient()->SetEventHandlers( | 234 DBusThreadManager::Get()->GetImageBurnerClient()->SetEventHandlers( |
| 234 base::Bind(&BurnManager::OnBurnFinished, | 235 base::Bind(&BurnManager::OnBurnFinished, |
| 235 weak_ptr_factory_.GetWeakPtr()), | 236 weak_ptr_factory_.GetWeakPtr()), |
| 236 base::Bind(&BurnManager::OnBurnProgressUpdate, | 237 base::Bind(&BurnManager::OnBurnProgressUpdate, |
| 237 weak_ptr_factory_.GetWeakPtr())); | 238 weak_ptr_factory_.GetWeakPtr())); |
| 238 } | 239 } |
| 239 | 240 |
| 240 BurnManager::~BurnManager() { | 241 BurnManager::~BurnManager() { |
| 241 if (!image_dir_.empty()) { | 242 if (image_dir_created_) { |
| 242 file_util::Delete(image_dir_, true); | 243 file_util::Delete(image_dir_, true); |
| 243 } | 244 } |
| 244 NetworkStateHandler::Get()->RemoveObserver(this); | 245 NetworkStateHandler::Get()->RemoveObserver(this); |
| 245 DBusThreadManager::Get()->GetImageBurnerClient()->ResetEventHandlers(); | 246 DBusThreadManager::Get()->GetImageBurnerClient()->ResetEventHandlers(); |
| 246 } | 247 } |
| 247 | 248 |
| 248 // static | 249 // static |
| 249 void BurnManager::Initialize( | 250 void BurnManager::Initialize( |
| 250 const base::FilePath& downloads_directory, | 251 const base::FilePath& downloads_directory, |
| 251 scoped_refptr<net::URLRequestContextGetter> context_getter) { | 252 scoped_refptr<net::URLRequestContextGetter> context_getter) { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 312 CancelImageFetch(); | 313 CancelImageFetch(); |
| 313 } else if (state == StateMachine::BURNING) { | 314 } else if (state == StateMachine::BURNING) { |
| 314 // Burn library doesn't send cancelled signal upon CancelBurnImage | 315 // Burn library doesn't send cancelled signal upon CancelBurnImage |
| 315 // invokation. | 316 // invokation. |
| 316 CancelBurnImage(); | 317 CancelBurnImage(); |
| 317 } | 318 } |
| 318 ResetTargetPaths(); | 319 ResetTargetPaths(); |
| 319 } | 320 } |
| 320 | 321 |
| 321 void BurnManager::CreateImageDir() { | 322 void BurnManager::CreateImageDir() { |
| 322 if (image_dir_.empty()) { | 323 if (!image_dir_created_) { |
| 323 BrowserThread::PostBlockingPoolTask( | 324 BrowserThread::PostBlockingPoolTask( |
| 324 FROM_HERE, | 325 FROM_HERE, |
| 325 base::Bind(CreateDirectory, | 326 base::Bind(CreateDirectory, |
| 326 image_dir_, | 327 image_dir_, |
| 327 base::Bind(&BurnManager::OnImageDirCreated, | 328 base::Bind(&BurnManager::OnImageDirCreated, |
| 328 weak_ptr_factory_.GetWeakPtr()))); | 329 weak_ptr_factory_.GetWeakPtr()))); |
| 329 } else { | 330 } else { |
| 330 const bool success = true; | 331 const bool success = true; |
| 331 OnImageDirCreated(success); | 332 OnImageDirCreated(success); |
| 332 } | 333 } |
| 333 } | 334 } |
| 334 | 335 |
| 335 void BurnManager::OnImageDirCreated(bool success) { | 336 void BurnManager::OnImageDirCreated(bool success) { |
| 336 if (!success) { | 337 if (!success) { |
| 337 // Failed to create the directory. Finish the burning process | 338 // Failed to create the directory. Finish the burning process |
| 338 // with failure state. | 339 // with failure state. |
| 339 OnError(IDS_IMAGEBURN_DOWNLOAD_ERROR); | 340 OnError(IDS_IMAGEBURN_DOWNLOAD_ERROR); |
| 340 return; | 341 return; |
| 341 } | 342 } |
| 342 | 343 |
| 344 image_dir_created_ = true; |
| 343 zip_image_file_path_ = image_dir_.Append(kImageZipFileName); | 345 zip_image_file_path_ = image_dir_.Append(kImageZipFileName); |
| 344 FetchConfigFile(); | 346 FetchConfigFile(); |
| 345 } | 347 } |
| 346 | 348 |
| 347 const base::FilePath& BurnManager::GetImageDir() { | 349 base::FilePath BurnManager::GetImageDir() { |
| 350 if (!image_dir_created_) |
| 351 return base::FilePath(); |
| 348 return image_dir_; | 352 return image_dir_; |
| 349 } | 353 } |
| 350 | 354 |
| 351 void BurnManager::FetchConfigFile() { | 355 void BurnManager::FetchConfigFile() { |
| 352 if (config_file_fetched_) { | 356 if (config_file_fetched_) { |
| 353 // The config file is already fetched. So start to fetch the image. | 357 // The config file is already fetched. So start to fetch the image. |
| 354 FetchImage(); | 358 FetchImage(); |
| 355 return; | 359 return; |
| 356 } | 360 } |
| 357 | 361 |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 // Note: in theory, this is not a part of notification, but cancelling | 645 // Note: in theory, this is not a part of notification, but cancelling |
| 642 // the running burning task. However, there is no good place to be in the | 646 // the running burning task. However, there is no good place to be in the |
| 643 // current code. | 647 // current code. |
| 644 // TODO(hidehiko): Clean this up after refactoring. | 648 // TODO(hidehiko): Clean this up after refactoring. |
| 645 OnError(IDS_IMAGEBURN_DEVICE_NOT_FOUND_ERROR); | 649 OnError(IDS_IMAGEBURN_DEVICE_NOT_FOUND_ERROR); |
| 646 } | 650 } |
| 647 } | 651 } |
| 648 | 652 |
| 649 } // namespace imageburner | 653 } // namespace imageburner |
| 650 } // namespace chromeos | 654 } // namespace chromeos |
| OLD | NEW |