Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/utility/image_writer/image_writer_handler.h" | 5 #include "chrome/utility/image_writer/image_writer_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | |
| 7 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 8 #include "chrome/common/extensions/chrome_utility_extensions_messages.h" | 9 #include "base/optional.h" |
| 10 #include "chrome/common/extensions/removable_storage_writer.mojom.h" | |
| 9 #include "chrome/utility/image_writer/error_messages.h" | 11 #include "chrome/utility/image_writer/error_messages.h" |
| 10 #include "content/public/utility/utility_thread.h" | |
| 11 | 12 |
| 12 namespace image_writer { | 13 namespace image_writer { |
| 13 | 14 |
|
Sam McNally
2017/02/02 21:54:17
namespace {
Noel Gordon
2017/02/03 13:54:57
Done.
| |
| 14 ImageWriterHandler::ImageWriterHandler() {} | 15 inline bool IsTestDevice(const base::FilePath& device) { |
|
Sam McNally
2017/02/02 21:54:17
inline is not required.
Noel Gordon
2017/02/03 13:54:56
Done.
| |
| 15 ImageWriterHandler::~ImageWriterHandler() {} | 16 return device.AsUTF8Unsafe() == |
| 16 | 17 extensions::mojom::RemovableStorageWriter::kTestDevice; |
| 17 void ImageWriterHandler::SendSucceeded() { | |
| 18 Send(new ChromeUtilityHostMsg_ImageWriter_Succeeded()); | |
| 19 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | |
| 20 } | 18 } |
| 21 | 19 |
| 22 void ImageWriterHandler::SendCancelled() { | 20 base::FilePath MakeTestDevicePath(const base::FilePath& image) { |
| 23 Send(new ChromeUtilityHostMsg_ImageWriter_Cancelled()); | 21 return image.ReplaceExtension(FILE_PATH_LITERAL("out")); |
| 24 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | |
| 25 } | 22 } |
| 26 | 23 |
|
Sam McNally
2017/02/02 21:54:17
} // namespace
Noel Gordon
2017/02/03 13:54:56
Done.
| |
| 27 void ImageWriterHandler::SendFailed(const std::string& message) { | 24 ImageWriterHandler::ImageWriterHandler() = default; |
| 28 Send(new ChromeUtilityHostMsg_ImageWriter_Failed(message)); | |
| 29 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | |
| 30 } | |
| 31 | 25 |
| 32 void ImageWriterHandler::SendProgress(int64_t progress) { | 26 ImageWriterHandler::~ImageWriterHandler() = default; |
| 33 Send(new ChromeUtilityHostMsg_ImageWriter_Progress(progress)); | |
| 34 } | |
| 35 | 27 |
| 36 void ImageWriterHandler::Send(IPC::Message* msg) { | 28 void ImageWriterHandler::Write( |
| 37 content::UtilityThread::Get()->Send(msg); | 29 const base::FilePath& image, |
| 38 } | 30 const base::FilePath& device, |
| 31 extensions::mojom::RemovableStorageWriterClientPtr client) { | |
| 32 client_ = std::move(client); | |
| 33 client_.set_connection_error_handler( | |
| 34 base::Bind(&ImageWriterHandler::Cancel, base::Unretained(this))); | |
| 39 | 35 |
| 40 bool ImageWriterHandler::OnMessageReceived(const IPC::Message& message) { | 36 base::FilePath target_device = device; |
| 41 bool handled = true; | 37 const bool test_mode = IsTestDevice(device); |
| 42 IPC_BEGIN_MESSAGE_MAP(ImageWriterHandler, message) | 38 if (test_mode) |
| 43 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ImageWriter_Write, OnWriteStart) | 39 target_device = MakeTestDevicePath(image); |
| 44 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ImageWriter_Verify, OnVerifyStart) | |
| 45 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ImageWriter_Cancel, OnCancel) | |
| 46 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 47 IPC_END_MESSAGE_MAP() | |
| 48 return handled; | |
| 49 } | |
| 50 | 40 |
| 51 void ImageWriterHandler::OnWriteStart(const base::FilePath& image, | 41 // http://crbug.com/352442 |
| 52 const base::FilePath& device) { | |
| 53 if (!image_writer_.get() || image != image_writer_->GetImagePath() || | 42 if (!image_writer_.get() || image != image_writer_->GetImagePath() || |
| 54 device != image_writer_->GetDevicePath()) { | 43 target_device != image_writer_->GetDevicePath()) { |
| 55 image_writer_.reset(new ImageWriter(this, image, device)); | 44 image_writer_.reset(new ImageWriter(this, image, target_device)); |
| 56 } | 45 } |
| 57 | 46 |
| 58 if (image_writer_->IsRunning()) { | 47 if (image_writer_->IsRunning()) { |
| 59 SendFailed(error::kOperationAlreadyInProgress); | 48 SendFailed(error::kOperationAlreadyInProgress); |
| 60 return; | 49 return; |
| 61 } | 50 } |
| 62 | 51 |
| 52 if (test_mode) { | |
| 53 image_writer_->Write(); | |
| 54 return; | |
| 55 } | |
| 56 | |
| 63 if (!image_writer_->IsValidDevice()) { | 57 if (!image_writer_->IsValidDevice()) { |
| 64 SendFailed(error::kInvalidDevice); | 58 SendFailed(error::kInvalidDevice); |
| 65 return; | 59 return; |
| 66 } | 60 } |
| 67 | 61 |
| 68 image_writer_->UnmountVolumes( | 62 image_writer_->UnmountVolumes( |
| 69 base::Bind(&ImageWriter::Write, image_writer_->AsWeakPtr())); | 63 base::Bind(&ImageWriter::Write, image_writer_->AsWeakPtr())); |
| 70 } | 64 } |
| 71 | 65 |
| 72 void ImageWriterHandler::OnVerifyStart(const base::FilePath& image, | 66 void ImageWriterHandler::Verify( |
| 73 const base::FilePath& device) { | 67 const base::FilePath& image, |
| 68 const base::FilePath& device, | |
| 69 extensions::mojom::RemovableStorageWriterClientPtr client) { | |
| 70 client_ = std::move(client); | |
| 71 client_.set_connection_error_handler( | |
| 72 base::Bind(&ImageWriterHandler::Cancel, base::Unretained(this))); | |
| 73 | |
| 74 base::FilePath target_device = device; | |
| 75 const bool test_mode = IsTestDevice(device); | |
| 76 if (test_mode) | |
| 77 target_device = MakeTestDevicePath(image); | |
| 78 | |
| 79 // http://crbug.com/352442 | |
| 74 if (!image_writer_.get() || image != image_writer_->GetImagePath() || | 80 if (!image_writer_.get() || image != image_writer_->GetImagePath() || |
| 75 device != image_writer_->GetDevicePath()) { | 81 target_device != image_writer_->GetDevicePath()) { |
| 76 image_writer_.reset(new ImageWriter(this, image, device)); | 82 image_writer_.reset(new ImageWriter(this, image, target_device)); |
| 77 } | 83 } |
| 78 | 84 |
| 79 if (image_writer_->IsRunning()) { | 85 if (image_writer_->IsRunning()) { |
| 80 SendFailed(error::kOperationAlreadyInProgress); | 86 SendFailed(error::kOperationAlreadyInProgress); |
| 81 return; | 87 return; |
| 82 } | 88 } |
| 83 | 89 |
| 90 if (test_mode) { | |
| 91 image_writer_->Verify(); | |
| 92 return; | |
| 93 } | |
| 94 | |
| 84 if (!image_writer_->IsValidDevice()) { | 95 if (!image_writer_->IsValidDevice()) { |
| 85 SendFailed(error::kInvalidDevice); | 96 SendFailed(error::kInvalidDevice); |
| 86 return; | 97 return; |
| 87 } | 98 } |
| 88 | 99 |
| 89 image_writer_->Verify(); | 100 image_writer_->Verify(); |
| 90 } | 101 } |
| 91 | 102 |
| 92 void ImageWriterHandler::OnCancel() { | 103 void ImageWriterHandler::SendProgress(int64_t progress) { |
| 93 if (image_writer_.get()) { | 104 client_->Progress(progress); |
| 105 } | |
| 106 | |
| 107 void ImageWriterHandler::SendSucceeded() { | |
| 108 client_->Complete(base::nullopt); | |
| 109 client_.reset(); | |
| 110 } | |
| 111 | |
| 112 void ImageWriterHandler::SendFailed(const std::string& error) { | |
| 113 client_->Complete(error); | |
| 114 client_.reset(); | |
| 115 } | |
| 116 | |
| 117 void ImageWriterHandler::Cancel() { | |
| 118 if (image_writer_.get()) | |
| 94 image_writer_->Cancel(); | 119 image_writer_->Cancel(); |
| 95 } else { | 120 client_.reset(); |
| 96 SendCancelled(); | |
| 97 } | |
| 98 } | 121 } |
| 99 | 122 |
| 100 } // namespace image_writer | 123 } // namespace image_writer |
| OLD | NEW |