| 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/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/optional.h" | 9 #include "base/optional.h" |
| 10 #include "chrome/common/extensions/removable_storage_writer.mojom.h" | 10 #include "chrome/common/extensions/removable_storage_writer.mojom.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 extensions::mojom::RemovableStorageWriterClientPtr client) { | 35 extensions::mojom::RemovableStorageWriterClientPtr client) { |
| 36 client_ = std::move(client); | 36 client_ = std::move(client); |
| 37 client_.set_connection_error_handler( | 37 client_.set_connection_error_handler( |
| 38 base::Bind(&ImageWriterHandler::Cancel, base::Unretained(this))); | 38 base::Bind(&ImageWriterHandler::Cancel, base::Unretained(this))); |
| 39 | 39 |
| 40 base::FilePath target_device = device; | 40 base::FilePath target_device = device; |
| 41 const bool test_mode = IsTestDevice(device); | 41 const bool test_mode = IsTestDevice(device); |
| 42 if (test_mode) | 42 if (test_mode) |
| 43 target_device = MakeTestDevicePath(image); | 43 target_device = MakeTestDevicePath(image); |
| 44 | 44 |
| 45 // https://crbug.com/352442 | 45 if (ShouldResetImageWriter(image, target_device)) |
| 46 if (!image_writer_.get() || image != image_writer_->GetImagePath() || | |
| 47 target_device != image_writer_->GetDevicePath()) { | |
| 48 image_writer_.reset(new ImageWriter(this, image, target_device)); | 46 image_writer_.reset(new ImageWriter(this, image, target_device)); |
| 49 } | |
| 50 | 47 |
| 51 if (image_writer_->IsRunning()) { | 48 if (image_writer_->IsRunning()) { |
| 52 SendFailed(error::kOperationAlreadyInProgress); | 49 SendFailed(error::kOperationAlreadyInProgress); |
| 53 return; | 50 return; |
| 54 } | 51 } |
| 55 | 52 |
| 56 if (test_mode) { | 53 if (test_mode) { |
| 57 image_writer_->Write(); | 54 image_writer_->Write(); |
| 58 return; | 55 return; |
| 59 } | 56 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 73 extensions::mojom::RemovableStorageWriterClientPtr client) { | 70 extensions::mojom::RemovableStorageWriterClientPtr client) { |
| 74 client_ = std::move(client); | 71 client_ = std::move(client); |
| 75 client_.set_connection_error_handler( | 72 client_.set_connection_error_handler( |
| 76 base::Bind(&ImageWriterHandler::Cancel, base::Unretained(this))); | 73 base::Bind(&ImageWriterHandler::Cancel, base::Unretained(this))); |
| 77 | 74 |
| 78 base::FilePath target_device = device; | 75 base::FilePath target_device = device; |
| 79 const bool test_mode = IsTestDevice(device); | 76 const bool test_mode = IsTestDevice(device); |
| 80 if (test_mode) | 77 if (test_mode) |
| 81 target_device = MakeTestDevicePath(image); | 78 target_device = MakeTestDevicePath(image); |
| 82 | 79 |
| 83 // https://crbug.com/352442 | 80 if (ShouldResetImageWriter(image, target_device)) |
| 84 if (!image_writer_.get() || image != image_writer_->GetImagePath() || | |
| 85 target_device != image_writer_->GetDevicePath()) { | |
| 86 image_writer_.reset(new ImageWriter(this, image, target_device)); | 81 image_writer_.reset(new ImageWriter(this, image, target_device)); |
| 87 } | |
| 88 | 82 |
| 89 if (image_writer_->IsRunning()) { | 83 if (image_writer_->IsRunning()) { |
| 90 SendFailed(error::kOperationAlreadyInProgress); | 84 SendFailed(error::kOperationAlreadyInProgress); |
| 91 return; | 85 return; |
| 92 } | 86 } |
| 93 | 87 |
| 94 if (test_mode) { | 88 if (test_mode) { |
| 95 image_writer_->Verify(); | 89 image_writer_->Verify(); |
| 96 return; | 90 return; |
| 97 } | 91 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 117 client_->Complete(error); | 111 client_->Complete(error); |
| 118 client_.reset(); | 112 client_.reset(); |
| 119 } | 113 } |
| 120 | 114 |
| 121 void ImageWriterHandler::Cancel() { | 115 void ImageWriterHandler::Cancel() { |
| 122 if (image_writer_) | 116 if (image_writer_) |
| 123 image_writer_->Cancel(); | 117 image_writer_->Cancel(); |
| 124 client_.reset(); | 118 client_.reset(); |
| 125 } | 119 } |
| 126 | 120 |
| 121 bool ImageWriterHandler::ShouldResetImageWriter(const base::FilePath& image, |
| 122 const base::FilePath& device) { |
| 123 if (!image_writer_) |
| 124 return true; |
| 125 if (image != image_writer_->GetImagePath()) |
| 126 return true; |
| 127 if (device != image_writer_->GetDevicePath()) |
| 128 return true; |
| 129 |
| 130 // When writing and verifying the same file on the same device, keep |
| 131 // the file handles open; do not reset them since that can cause the |
| 132 // operation to fail in unexpected ways: crbug.com/352442#c7 |
| 133 return false; |
| 134 } |
| 135 |
| 127 } // namespace image_writer | 136 } // namespace image_writer |
| OLD | NEW |