[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 4e100e8 | 2014-06-18 23:47:29 | [diff] [blame] | 5 | #include "chrome/utility/image_writer/image_writer_handler.h" |
| 6 | |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 7 | #include "base/bind.h" |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 9 | #include "base/optional.h" |
Ken Rockot | 2ad4db3 | 2018-02-10 16:24:10 | [diff] [blame^] | 10 | #include "chrome/services/removable_storage_writer/public/mojom/removable_storage_writer.mojom.h" |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 11 | #include "chrome/utility/image_writer/error_messages.h" |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 12 | |
| 13 | namespace { |
| 14 | |
| 15 | bool IsTestDevice(const base::FilePath& device) { |
| 16 | return device.AsUTF8Unsafe() == |
Jay Civelli | 66e0d89 | 2017-12-05 01:05:10 | [diff] [blame] | 17 | chrome::mojom::RemovableStorageWriter::kTestDevice; |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | base::FilePath MakeTestDevicePath(const base::FilePath& image) { |
| 21 | return image.ReplaceExtension(FILE_PATH_LITERAL("out")); |
| 22 | } |
| 23 | |
| 24 | } // namespace |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 25 | |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 26 | namespace image_writer { |
| 27 | |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 28 | ImageWriterHandler::ImageWriterHandler() = default; |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 29 | |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 30 | ImageWriterHandler::~ImageWriterHandler() = default; |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 31 | |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 32 | void ImageWriterHandler::Write( |
| 33 | const base::FilePath& image, |
| 34 | const base::FilePath& device, |
Jay Civelli | 66e0d89 | 2017-12-05 01:05:10 | [diff] [blame] | 35 | chrome::mojom::RemovableStorageWriterClientPtr client) { |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 36 | client_ = std::move(client); |
| 37 | client_.set_connection_error_handler( |
tzik | f7d48c73ac | 2017-06-26 09:27:04 | [diff] [blame] | 38 | base::BindOnce(&ImageWriterHandler::Cancel, base::Unretained(this))); |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 39 | |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 40 | base::FilePath target_device = device; |
| 41 | const bool test_mode = IsTestDevice(device); |
| 42 | if (test_mode) |
| 43 | target_device = MakeTestDevicePath(image); |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 44 | |
noel | 4342d3ea | 2017-03-20 08:08:32 | [diff] [blame] | 45 | if (ShouldResetImageWriter(image, target_device)) |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 46 | image_writer_.reset(new ImageWriter(this, image, target_device)); |
[email protected] | 01fe23ce | 2014-05-07 13:18:55 | [diff] [blame] | 47 | |
| 48 | if (image_writer_->IsRunning()) { |
| 49 | SendFailed(error::kOperationAlreadyInProgress); |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 50 | return; |
| 51 | } |
| 52 | |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 53 | if (test_mode) { |
| 54 | image_writer_->Write(); |
| 55 | return; |
| 56 | } |
| 57 | |
[email protected] | 01fe23ce | 2014-05-07 13:18:55 | [diff] [blame] | 58 | if (!image_writer_->IsValidDevice()) { |
| 59 | SendFailed(error::kInvalidDevice); |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 60 | return; |
| 61 | } |
[email protected] | 01fe23ce | 2014-05-07 13:18:55 | [diff] [blame] | 62 | |
[email protected] | 4e100e8 | 2014-06-18 23:47:29 | [diff] [blame] | 63 | image_writer_->UnmountVolumes( |
| 64 | base::Bind(&ImageWriter::Write, image_writer_->AsWeakPtr())); |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 65 | } |
| 66 | |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 67 | void ImageWriterHandler::Verify( |
| 68 | const base::FilePath& image, |
| 69 | const base::FilePath& device, |
Jay Civelli | 66e0d89 | 2017-12-05 01:05:10 | [diff] [blame] | 70 | chrome::mojom::RemovableStorageWriterClientPtr client) { |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 71 | client_ = std::move(client); |
| 72 | client_.set_connection_error_handler( |
tzik | f7d48c73ac | 2017-06-26 09:27:04 | [diff] [blame] | 73 | base::BindOnce(&ImageWriterHandler::Cancel, base::Unretained(this))); |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 74 | |
| 75 | base::FilePath target_device = device; |
| 76 | const bool test_mode = IsTestDevice(device); |
| 77 | if (test_mode) |
| 78 | target_device = MakeTestDevicePath(image); |
| 79 | |
noel | 4342d3ea | 2017-03-20 08:08:32 | [diff] [blame] | 80 | if (ShouldResetImageWriter(image, target_device)) |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 81 | image_writer_.reset(new ImageWriter(this, image, target_device)); |
[email protected] | 01fe23ce | 2014-05-07 13:18:55 | [diff] [blame] | 82 | |
| 83 | if (image_writer_->IsRunning()) { |
| 84 | SendFailed(error::kOperationAlreadyInProgress); |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 85 | return; |
| 86 | } |
| 87 | |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 88 | if (test_mode) { |
| 89 | image_writer_->Verify(); |
| 90 | return; |
| 91 | } |
| 92 | |
[email protected] | 01fe23ce | 2014-05-07 13:18:55 | [diff] [blame] | 93 | if (!image_writer_->IsValidDevice()) { |
| 94 | SendFailed(error::kInvalidDevice); |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 95 | return; |
| 96 | } |
[email protected] | 01fe23ce | 2014-05-07 13:18:55 | [diff] [blame] | 97 | |
| 98 | image_writer_->Verify(); |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 99 | } |
| 100 | |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 101 | void ImageWriterHandler::SendProgress(int64_t progress) { |
| 102 | client_->Progress(progress); |
| 103 | } |
| 104 | |
| 105 | void ImageWriterHandler::SendSucceeded() { |
| 106 | client_->Complete(base::nullopt); |
| 107 | client_.reset(); |
| 108 | } |
| 109 | |
| 110 | void ImageWriterHandler::SendFailed(const std::string& error) { |
Jay Civelli | 66e0d89 | 2017-12-05 01:05:10 | [diff] [blame] | 111 | if (client_) { |
| 112 | // client_ may be null as the ImageWriter implementation may have reported |
| 113 | // an error already. |
| 114 | client_->Complete(error); |
| 115 | client_.reset(); |
| 116 | } |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | void ImageWriterHandler::Cancel() { |
| 120 | if (image_writer_) |
[email protected] | 01fe23ce | 2014-05-07 13:18:55 | [diff] [blame] | 121 | image_writer_->Cancel(); |
noel | 6b03ae8 | 2017-02-09 15:28:17 | [diff] [blame] | 122 | client_.reset(); |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 123 | } |
| 124 | |
noel | 4342d3ea | 2017-03-20 08:08:32 | [diff] [blame] | 125 | bool ImageWriterHandler::ShouldResetImageWriter(const base::FilePath& image, |
| 126 | const base::FilePath& device) { |
| 127 | if (!image_writer_) |
| 128 | return true; |
| 129 | if (image != image_writer_->GetImagePath()) |
| 130 | return true; |
| 131 | if (device != image_writer_->GetDevicePath()) |
| 132 | return true; |
| 133 | |
| 134 | // When writing and verifying the same file on the same device, keep |
| 135 | // the file handles open; do not reset them since that can cause the |
| 136 | // operation to fail in unexpected ways: crbug.com/352442#c7 |
| 137 | return false; |
| 138 | } |
| 139 | |
[email protected] | 3d78cbe | 2014-02-27 13:19:30 | [diff] [blame] | 140 | } // namespace image_writer |