Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extensions/api/image_writer_private/error_messages.h" | 5 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" |
| 6 #include "chrome/browser/extensions/api/image_writer_private/operation.h" | 6 #include "chrome/browser/extensions/api/image_writer_private/operation.h" |
| 7 #include "chromeos/dbus/dbus_thread_manager.h" | 7 #include "chromeos/dbus/dbus_thread_manager.h" |
| 8 #include "chromeos/dbus/image_burner_client.h" | 8 #include "chromeos/dbus/image_burner_client.h" |
| 9 #include "chromeos/disks/disk_mount_manager.h" | |
| 9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 10 | 11 |
| 11 namespace extensions { | 12 namespace extensions { |
| 12 namespace image_writer { | 13 namespace image_writer { |
| 13 | 14 |
| 15 using chromeos::disks::DiskMountManager; | |
| 14 using chromeos::ImageBurnerClient; | 16 using chromeos::ImageBurnerClient; |
| 15 using content::BrowserThread; | 17 using content::BrowserThread; |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 21 // TODO(haven): The Image Burner cannot handle multiple burns. crbug.com/373575 | |
|
tbarzic
2014/05/15 16:41:40
nit: I'd put this in StartWriteOnUIThread
Drew Haven
2014/05/15 17:37:21
Done. But now it is longer than 80 chars and I ha
| |
| 22 | |
| 19 void ClearImageBurner() { | 23 void ClearImageBurner() { |
| 20 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 24 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 21 BrowserThread::PostTask(BrowserThread::UI, | 25 BrowserThread::PostTask(BrowserThread::UI, |
| 22 FROM_HERE, | 26 FROM_HERE, |
| 23 base::Bind(&ClearImageBurner)); | 27 base::Bind(&ClearImageBurner)); |
| 24 return; | 28 return; |
| 25 } | 29 } |
| 26 | 30 |
| 27 chromeos::DBusThreadManager::Get()-> | 31 chromeos::DBusThreadManager::Get()-> |
| 28 GetImageBurnerClient()-> | 32 GetImageBurnerClient()-> |
| 29 ResetEventHandlers(); | 33 ResetEventHandlers(); |
| 30 } | 34 } |
| 31 | 35 |
| 32 } // namespace | 36 } // namespace |
| 33 | 37 |
| 34 void Operation::Write(const base::Closure& continuation) { | 38 void Operation::Write(const base::Closure& continuation) { |
| 35 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 39 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 36 SetStage(image_writer_api::STAGE_WRITE); | 40 SetStage(image_writer_api::STAGE_WRITE); |
| 37 | 41 |
| 42 // Note this has to be run on the FILE thread to avoid concurrent access. | |
| 43 AddCleanUpFunction(base::Bind(&ClearImageBurner)); | |
| 44 | |
| 38 BrowserThread::PostTask( | 45 BrowserThread::PostTask( |
| 39 BrowserThread::UI, | 46 BrowserThread::UI, |
| 40 FROM_HERE, | 47 FROM_HERE, |
| 41 base::Bind(&Operation::StartWriteOnUIThread, this, continuation)); | 48 base::Bind(&Operation::UnmountVolumes, this, continuation)); |
| 42 | |
| 43 AddCleanUpFunction(base::Bind(&ClearImageBurner)); | |
| 44 } | 49 } |
| 45 | 50 |
| 46 void Operation::VerifyWrite(const base::Closure& continuation) { | 51 void Operation::VerifyWrite(const base::Closure& continuation) { |
| 47 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 52 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
| 48 | 53 |
| 49 // No verification is available in Chrome OS currently. | 54 // No verification is available in Chrome OS currently. |
| 50 continuation.Run(); | 55 continuation.Run(); |
| 51 } | 56 } |
| 52 | 57 |
| 53 void Operation::StartWriteOnUIThread(const base::Closure& continuation) { | 58 void Operation::UnmountVolumes(const base::Closure& continuation) { |
| 59 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 60 DiskMountManager::GetInstance()->UnmountDeviceRecursively( | |
| 61 device_path_.value(), | |
| 62 base::Bind(&Operation::UnmountVolumesCallback, this, continuation)); | |
| 63 } | |
| 64 | |
| 65 void Operation::UnmountVolumesCallback(const base::Closure& continuation, | |
| 66 bool success) { | |
| 67 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 68 | |
| 69 if (!success) { | |
| 70 LOG(ERROR) << "Volume unmounting failed."; | |
| 71 Error(error::kUnmountVolumesError); | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 const DiskMountManager::DiskMap& disks = | |
| 76 DiskMountManager::GetInstance()->disks(); | |
| 77 DiskMountManager::DiskMap::const_iterator iter = | |
| 78 disks.find(device_path_.value()); | |
| 79 | |
| 80 if (iter == disks.end()) { | |
| 81 LOG(ERROR) << "Disk not found in disk list after unmounting volumes."; | |
| 82 Error(error::kUnmountVolumesError); | |
| 83 return; | |
| 84 } | |
| 85 | |
| 86 StartWriteOnUIThread(iter->second->file_path(), continuation); | |
| 87 } | |
| 88 | |
| 89 void Operation::StartWriteOnUIThread(const std::string& target_path, | |
| 90 const base::Closure& continuation) { | |
| 54 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 91 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 55 | 92 |
| 56 ImageBurnerClient* burner = | 93 ImageBurnerClient* burner = |
| 57 chromeos::DBusThreadManager::Get()->GetImageBurnerClient(); | 94 chromeos::DBusThreadManager::Get()->GetImageBurnerClient(); |
| 58 | 95 |
| 59 burner->SetEventHandlers( | 96 burner->SetEventHandlers( |
| 60 base::Bind(&Operation::OnBurnFinished, this, continuation), | 97 base::Bind(&Operation::OnBurnFinished, this, continuation), |
| 61 base::Bind(&Operation::OnBurnProgress, this)); | 98 base::Bind(&Operation::OnBurnProgress, this)); |
| 62 | 99 |
| 63 burner->BurnImage(image_path_.value(), | 100 burner->BurnImage(image_path_.value(), |
| 64 device_path_.value(), | 101 target_path, |
| 65 base::Bind(&Operation::OnBurnError, this)); | 102 base::Bind(&Operation::OnBurnError, this)); |
| 66 } | 103 } |
| 67 | 104 |
| 68 void Operation::OnBurnFinished(const base::Closure& continuation, | 105 void Operation::OnBurnFinished(const base::Closure& continuation, |
| 69 const std::string& target_path, | 106 const std::string& target_path, |
| 70 bool success, | 107 bool success, |
| 71 const std::string& error) { | 108 const std::string& error) { |
| 72 if (success) { | 109 if (success) { |
| 73 SetProgress(kProgressComplete); | 110 SetProgress(kProgressComplete); |
| 74 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); | 111 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); |
| 75 } else { | 112 } else { |
| 76 DLOG(ERROR) << "Error encountered while burning: " << error; | 113 DLOG(ERROR) << "Error encountered while burning: " << error; |
| 77 Error(error::kChromeOSImageBurnerError); | 114 Error(error::kChromeOSImageBurnerError); |
| 78 } | 115 } |
| 79 } | 116 } |
| 80 | 117 |
| 81 void Operation::OnBurnProgress(const std::string& target_path, | 118 void Operation::OnBurnProgress(const std::string& target_path, |
| 82 int64 num_bytes_burnt, | 119 int64 num_bytes_burnt, |
| 83 int64 total_size) { | 120 int64 total_size) { |
| 84 int progress = kProgressComplete * num_bytes_burnt / total_size; | 121 int progress = kProgressComplete * num_bytes_burnt / total_size; |
| 85 SetProgress(progress); | 122 SetProgress(progress); |
| 86 } | 123 } |
| 87 | 124 |
| 88 void Operation::OnBurnError() { | 125 void Operation::OnBurnError() { |
| 89 Error(error::kChromeOSImageBurnerError); | 126 Error(error::kChromeOSImageBurnerError); |
| 90 } | 127 } |
| 91 | 128 |
| 92 } // namespace image_writer | 129 } // namespace image_writer |
| 93 } // namespace extensions | 130 } // namespace extensions |
| OLD | NEW |