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" | 12 |
| 13 namespace { | |
| 14 | |
| 15 bool IsTestDevice(const base::FilePath& device) { | |
| 16 return device.AsUTF8Unsafe() == | |
| 17 extensions::mojom::RemovableStorageWriter::kTestDevice; | |
| 18 } | |
| 19 | |
| 20 base::FilePath MakeTestDevicePath(const base::FilePath& image) { | |
| 21 return image.ReplaceExtension(FILE_PATH_LITERAL("out")); | |
| 22 } | |
| 23 | |
| 24 } // namespace | |
| 11 | 25 |
| 12 namespace image_writer { | 26 namespace image_writer { |
| 13 | 27 |
| 14 ImageWriterHandler::ImageWriterHandler() {} | 28 ImageWriterHandler::ImageWriterHandler() = default; |
| 15 ImageWriterHandler::~ImageWriterHandler() {} | |
| 16 | 29 |
| 17 void ImageWriterHandler::SendSucceeded() { | 30 ImageWriterHandler::~ImageWriterHandler() = default; |
| 18 Send(new ChromeUtilityHostMsg_ImageWriter_Succeeded()); | |
| 19 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | |
| 20 } | |
| 21 | 31 |
| 22 void ImageWriterHandler::SendCancelled() { | 32 void ImageWriterHandler::Write( |
| 23 Send(new ChromeUtilityHostMsg_ImageWriter_Cancelled()); | 33 const base::FilePath& image, |
| 24 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | 34 const base::FilePath& device, |
| 25 } | 35 extensions::mojom::RemovableStorageWriterClientPtr client) { |
| 36 client_ = std::move(client); | |
| 37 client_.set_connection_error_handler( | |
| 38 base::Bind(&ImageWriterHandler::Cancel, base::Unretained(this))); | |
| 26 | 39 |
| 27 void ImageWriterHandler::SendFailed(const std::string& message) { | 40 base::FilePath target_device = device; |
| 28 Send(new ChromeUtilityHostMsg_ImageWriter_Failed(message)); | 41 const bool test_mode = IsTestDevice(device); |
| 29 content::UtilityThread::Get()->ReleaseProcessIfNeeded(); | 42 if (test_mode) |
| 30 } | 43 target_device = MakeTestDevicePath(image); |
| 31 | 44 |
| 32 void ImageWriterHandler::SendProgress(int64_t progress) { | 45 // http://crbug.com/352442 |
|
dcheng
2017/02/08 09:31:18
Nit: pointer to the bug is good, but a brief expla
Noel Gordon
2017/02/08 12:12:59
Done. I think the URL is the best pointer I can g
dcheng
2017/02/09 01:25:36
Hmm... I personally don't like bug URLs as the onl
| |
| 33 Send(new ChromeUtilityHostMsg_ImageWriter_Progress(progress)); | |
| 34 } | |
| 35 | |
| 36 void ImageWriterHandler::Send(IPC::Message* msg) { | |
| 37 content::UtilityThread::Get()->Send(msg); | |
| 38 } | |
| 39 | |
| 40 bool ImageWriterHandler::OnMessageReceived(const IPC::Message& message) { | |
| 41 bool handled = true; | |
| 42 IPC_BEGIN_MESSAGE_MAP(ImageWriterHandler, message) | |
| 43 IPC_MESSAGE_HANDLER(ChromeUtilityMsg_ImageWriter_Write, OnWriteStart) | |
| 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 | |
| 51 void ImageWriterHandler::OnWriteStart(const base::FilePath& image, | |
| 52 const base::FilePath& device) { | |
| 53 if (!image_writer_.get() || image != image_writer_->GetImagePath() || | 46 if (!image_writer_.get() || image != image_writer_->GetImagePath() || |
| 54 device != image_writer_->GetDevicePath()) { | 47 target_device != image_writer_->GetDevicePath()) { |
| 55 image_writer_.reset(new ImageWriter(this, image, device)); | 48 image_writer_.reset(new ImageWriter(this, image, target_device)); |
| 56 } | 49 } |
| 57 | 50 |
| 58 if (image_writer_->IsRunning()) { | 51 if (image_writer_->IsRunning()) { |
| 59 SendFailed(error::kOperationAlreadyInProgress); | 52 SendFailed(error::kOperationAlreadyInProgress); |
| 60 return; | 53 return; |
| 61 } | 54 } |
| 62 | 55 |
| 56 if (test_mode) { | |
| 57 image_writer_->Write(); | |
| 58 return; | |
| 59 } | |
| 60 | |
| 63 if (!image_writer_->IsValidDevice()) { | 61 if (!image_writer_->IsValidDevice()) { |
| 64 SendFailed(error::kInvalidDevice); | 62 SendFailed(error::kInvalidDevice); |
| 65 return; | 63 return; |
| 66 } | 64 } |
| 67 | 65 |
| 68 image_writer_->UnmountVolumes( | 66 image_writer_->UnmountVolumes( |
| 69 base::Bind(&ImageWriter::Write, image_writer_->AsWeakPtr())); | 67 base::Bind(&ImageWriter::Write, image_writer_->AsWeakPtr())); |
| 70 } | 68 } |
| 71 | 69 |
| 72 void ImageWriterHandler::OnVerifyStart(const base::FilePath& image, | 70 void ImageWriterHandler::Verify( |
| 73 const base::FilePath& device) { | 71 const base::FilePath& image, |
| 72 const base::FilePath& device, | |
| 73 extensions::mojom::RemovableStorageWriterClientPtr client) { | |
| 74 client_ = std::move(client); | |
| 75 client_.set_connection_error_handler( | |
| 76 base::Bind(&ImageWriterHandler::Cancel, base::Unretained(this))); | |
| 77 | |
| 78 base::FilePath target_device = device; | |
| 79 const bool test_mode = IsTestDevice(device); | |
| 80 if (test_mode) | |
| 81 target_device = MakeTestDevicePath(image); | |
| 82 | |
| 83 // http://crbug.com/352442 | |
|
dcheng
2017/02/08 09:31:18
Ditto.
Noel Gordon
2017/02/08 12:12:59
Done.
| |
| 74 if (!image_writer_.get() || image != image_writer_->GetImagePath() || | 84 if (!image_writer_.get() || image != image_writer_->GetImagePath() || |
| 75 device != image_writer_->GetDevicePath()) { | 85 target_device != image_writer_->GetDevicePath()) { |
| 76 image_writer_.reset(new ImageWriter(this, image, device)); | 86 image_writer_.reset(new ImageWriter(this, image, target_device)); |
| 77 } | 87 } |
| 78 | 88 |
| 79 if (image_writer_->IsRunning()) { | 89 if (image_writer_->IsRunning()) { |
| 80 SendFailed(error::kOperationAlreadyInProgress); | 90 SendFailed(error::kOperationAlreadyInProgress); |
| 81 return; | 91 return; |
| 82 } | 92 } |
| 83 | 93 |
| 94 if (test_mode) { | |
| 95 image_writer_->Verify(); | |
| 96 return; | |
| 97 } | |
| 98 | |
| 84 if (!image_writer_->IsValidDevice()) { | 99 if (!image_writer_->IsValidDevice()) { |
| 85 SendFailed(error::kInvalidDevice); | 100 SendFailed(error::kInvalidDevice); |
| 86 return; | 101 return; |
| 87 } | 102 } |
| 88 | 103 |
| 89 image_writer_->Verify(); | 104 image_writer_->Verify(); |
| 90 } | 105 } |
| 91 | 106 |
| 92 void ImageWriterHandler::OnCancel() { | 107 void ImageWriterHandler::SendProgress(int64_t progress) { |
| 93 if (image_writer_.get()) { | 108 client_->Progress(progress); |
| 109 } | |
| 110 | |
| 111 void ImageWriterHandler::SendSucceeded() { | |
| 112 client_->Complete(base::nullopt); | |
| 113 client_.reset(); | |
| 114 } | |
| 115 | |
| 116 void ImageWriterHandler::SendFailed(const std::string& error) { | |
| 117 client_->Complete(error); | |
| 118 client_.reset(); | |
| 119 } | |
| 120 | |
| 121 void ImageWriterHandler::Cancel() { | |
| 122 if (image_writer_) | |
| 94 image_writer_->Cancel(); | 123 image_writer_->Cancel(); |
| 95 } else { | 124 client_.reset(); |
| 96 SendCancelled(); | |
| 97 } | |
| 98 } | 125 } |
| 99 | 126 |
| 100 } // namespace image_writer | 127 } // namespace image_writer |
| OLD | NEW |