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