| 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/browser/extensions/api/image_writer_private/image_writer_utilit
y_client.h" |
| 6 |
| 5 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" |
| 6 #include "base/location.h" | 9 #include "base/location.h" |
| 7 #include "base/strings/stringprintf.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/optional.h" |
| 12 #include "base/single_thread_task_runner.h" |
| 8 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
| 9 #include "build/build_config.h" | 14 #include "chrome/common/extensions/removable_storage_writer.mojom.h" |
| 10 #include "chrome/browser/browser_process.h" | |
| 11 #include "chrome/browser/extensions/api/image_writer_private/image_writer_utilit
y_client.h" | |
| 12 #include "chrome/common/extensions/chrome_utility_extensions_messages.h" | |
| 13 #include "chrome/grit/generated_resources.h" | 15 #include "chrome/grit/generated_resources.h" |
| 14 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/utility_process_mojo_client.h" |
| 18 #include "mojo/public/cpp/bindings/binding.h" |
| 15 #include "ui/base/l10n/l10n_util.h" | 19 #include "ui/base/l10n/l10n_util.h" |
| 16 | 20 |
| 17 using content::BrowserThread; | 21 class ImageWriterUtilityClient::RemovableStorageWriterClientImpl |
| 22 : public extensions::mojom::RemovableStorageWriterClient { |
| 23 public: |
| 24 RemovableStorageWriterClientImpl( |
| 25 ImageWriterUtilityClient* owner, |
| 26 extensions::mojom::RemovableStorageWriterClientPtr* interface) |
| 27 : binding_(this, interface), image_writer_utility_client_(owner) { |
| 28 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 29 |
| 30 binding_.set_connection_error_handler( |
| 31 base::Bind(&ImageWriterUtilityClient::UtilityProcessError, |
| 32 image_writer_utility_client_)); |
| 33 } |
| 34 |
| 35 ~RemovableStorageWriterClientImpl() override = default; |
| 36 |
| 37 private: |
| 38 void Progress(int64_t progress) override { |
| 39 image_writer_utility_client_->OperationProgress(progress); |
| 40 } |
| 41 |
| 42 void Complete(const base::Optional<std::string>& error) override { |
| 43 if (error) { |
| 44 image_writer_utility_client_->OperationFailed(error.value()); |
| 45 } else { |
| 46 image_writer_utility_client_->OperationSucceeded(); |
| 47 } |
| 48 } |
| 49 |
| 50 mojo::Binding<extensions::mojom::RemovableStorageWriterClient> binding_; |
| 51 // |image_writer_utility_client_| owns |this|. |
| 52 ImageWriterUtilityClient* const image_writer_utility_client_; |
| 53 |
| 54 DISALLOW_COPY_AND_ASSIGN(RemovableStorageWriterClientImpl); |
| 55 }; |
| 18 | 56 |
| 19 ImageWriterUtilityClient::ImageWriterUtilityClient() | 57 ImageWriterUtilityClient::ImageWriterUtilityClient() |
| 20 : task_runner_(base::ThreadTaskRunnerHandle::Get()) { | 58 : task_runner_(base::ThreadTaskRunnerHandle::Get()) { |
| 21 } | 59 } |
| 22 ImageWriterUtilityClient::~ImageWriterUtilityClient() {} | 60 |
| 61 ImageWriterUtilityClient::~ImageWriterUtilityClient() = default; |
| 23 | 62 |
| 24 void ImageWriterUtilityClient::Write(const ProgressCallback& progress_callback, | 63 void ImageWriterUtilityClient::Write(const ProgressCallback& progress_callback, |
| 25 const SuccessCallback& success_callback, | 64 const SuccessCallback& success_callback, |
| 26 const ErrorCallback& error_callback, | 65 const ErrorCallback& error_callback, |
| 27 const base::FilePath& source, | 66 const base::FilePath& source, |
| 28 const base::FilePath& target) { | 67 const base::FilePath& target) { |
| 29 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 68 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 30 | 69 DCHECK(!removable_storage_writer_client_); |
| 31 StartHost(); | |
| 32 | 70 |
| 33 progress_callback_ = progress_callback; | 71 progress_callback_ = progress_callback; |
| 34 success_callback_ = success_callback; | 72 success_callback_ = success_callback; |
| 35 error_callback_ = error_callback; | 73 error_callback_ = error_callback; |
| 36 | 74 |
| 37 if (!Send(new ChromeUtilityMsg_ImageWriter_Write(source, target))) { | 75 StartUtilityProcess(); |
| 38 DLOG(ERROR) << "Unable to send Write message to Utility Process."; | 76 |
| 39 task_runner_->PostTask( | 77 extensions::mojom::RemovableStorageWriterClientPtr client; |
| 40 FROM_HERE, base::Bind(error_callback_, "IPC communication failed")); | 78 removable_storage_writer_client_ = |
| 41 } | 79 base::MakeUnique<RemovableStorageWriterClientImpl>(this, &client); |
| 80 |
| 81 utility_process_mojo_client_->service()->Write(source, target, |
| 82 std::move(client)); |
| 42 } | 83 } |
| 43 | 84 |
| 44 void ImageWriterUtilityClient::Verify(const ProgressCallback& progress_callback, | 85 void ImageWriterUtilityClient::Verify(const ProgressCallback& progress_callback, |
| 45 const SuccessCallback& success_callback, | 86 const SuccessCallback& success_callback, |
| 46 const ErrorCallback& error_callback, | 87 const ErrorCallback& error_callback, |
| 47 const base::FilePath& source, | 88 const base::FilePath& source, |
| 48 const base::FilePath& target) { | 89 const base::FilePath& target) { |
| 49 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 90 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 50 | 91 DCHECK(!removable_storage_writer_client_); |
| 51 StartHost(); | |
| 52 | 92 |
| 53 progress_callback_ = progress_callback; | 93 progress_callback_ = progress_callback; |
| 54 success_callback_ = success_callback; | 94 success_callback_ = success_callback; |
| 55 error_callback_ = error_callback; | 95 error_callback_ = error_callback; |
| 56 | 96 |
| 57 if (!Send(new ChromeUtilityMsg_ImageWriter_Verify(source, target))) { | 97 StartUtilityProcess(); |
| 58 DLOG(ERROR) << "Unable to send Verify message to Utility Process."; | 98 |
| 59 task_runner_->PostTask( | 99 extensions::mojom::RemovableStorageWriterClientPtr client; |
| 60 FROM_HERE, base::Bind(error_callback_, "IPC communication failed")); | 100 removable_storage_writer_client_ = |
| 61 } | 101 base::MakeUnique<RemovableStorageWriterClientImpl>(this, &client); |
| 102 |
| 103 utility_process_mojo_client_->service()->Verify(source, target, |
| 104 std::move(client)); |
| 62 } | 105 } |
| 63 | 106 |
| 64 void ImageWriterUtilityClient::Cancel(const CancelCallback& cancel_callback) { | 107 void ImageWriterUtilityClient::Cancel(const CancelCallback& cancel_callback) { |
| 65 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 108 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 66 | 109 |
| 67 if (!utility_process_host_) { | 110 ResetRequest(); |
| 68 // If we haven't connected, there is nothing to cancel. | 111 task_runner_->PostTask(FROM_HERE, cancel_callback); |
| 69 task_runner_->PostTask(FROM_HERE, cancel_callback); | |
| 70 return; | |
| 71 } | |
| 72 | |
| 73 cancel_callback_ = cancel_callback; | |
| 74 | |
| 75 if (!Send(new ChromeUtilityMsg_ImageWriter_Cancel())) { | |
| 76 DLOG(ERROR) << "Unable to send Cancel message to Utility Process."; | |
| 77 } | |
| 78 } | 112 } |
| 79 | 113 |
| 80 void ImageWriterUtilityClient::Shutdown() { | 114 void ImageWriterUtilityClient::Shutdown() { |
| 81 if (utility_process_host_ && | 115 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 82 Send(new ChromeUtilityMsg_ImageWriter_Cancel())) { | 116 |
| 83 utility_process_host_->EndBatchMode(); | 117 ResetRequest(); |
| 84 } | 118 utility_process_mojo_client_.reset(); |
| 119 } |
| 120 |
| 121 void ImageWriterUtilityClient::StartUtilityProcess() { |
| 122 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 123 |
| 124 if (utility_process_mojo_client_) |
| 125 return; |
| 126 |
| 127 const base::string16 utility_process_name = |
| 128 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_IMAGE_WRITER_NAME); |
| 129 |
| 130 utility_process_mojo_client_.reset( |
| 131 new content::UtilityProcessMojoClient< |
| 132 extensions::mojom::RemovableStorageWriter>(utility_process_name)); |
| 133 utility_process_mojo_client_->set_error_callback( |
| 134 base::Bind(&ImageWriterUtilityClient::UtilityProcessError, this)); |
| 135 |
| 136 utility_process_mojo_client_->set_disable_sandbox(); |
| 137 #if defined(OS_WIN) |
| 138 utility_process_mojo_client_->set_run_elevated(); |
| 139 #endif |
| 140 |
| 141 utility_process_mojo_client_->Start(); |
| 142 } |
| 143 |
| 144 void ImageWriterUtilityClient::UtilityProcessError() { |
| 145 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 146 |
| 147 OperationFailed("Utility process crashed or failed."); |
| 148 utility_process_mojo_client_.reset(); |
| 149 } |
| 150 |
| 151 void ImageWriterUtilityClient::OperationProgress(int64_t progress) { |
| 152 if (progress_callback_) |
| 153 task_runner_->PostTask(FROM_HERE, base::Bind(progress_callback_, progress)); |
| 154 } |
| 155 |
| 156 void ImageWriterUtilityClient::OperationSucceeded() { |
| 157 SuccessCallback success_callback = success_callback_; |
| 158 ResetRequest(); |
| 159 if (success_callback) |
| 160 task_runner_->PostTask(FROM_HERE, success_callback); |
| 161 } |
| 162 |
| 163 void ImageWriterUtilityClient::OperationFailed(const std::string& error) { |
| 164 ErrorCallback error_callback = error_callback_; |
| 165 ResetRequest(); |
| 166 if (error_callback) |
| 167 task_runner_->PostTask(FROM_HERE, base::Bind(error_callback, error)); |
| 168 } |
| 169 |
| 170 void ImageWriterUtilityClient::ResetRequest() { |
| 171 removable_storage_writer_client_.reset(); |
| 85 | 172 |
| 86 // Clear handlers to not hold any reference to the caller. | 173 // Clear handlers to not hold any reference to the caller. |
| 87 success_callback_ = base::Closure(); | 174 progress_callback_.Reset(); |
| 88 progress_callback_ = base::Callback<void(int64_t)>(); | 175 success_callback_.Reset(); |
| 89 error_callback_ = base::Callback<void(const std::string&)>(); | 176 error_callback_.Reset(); |
| 90 cancel_callback_ = base::Closure(); | |
| 91 } | 177 } |
| 92 | |
| 93 void ImageWriterUtilityClient::StartHost() { | |
| 94 if (!utility_process_host_) { | |
| 95 scoped_refptr<base::SequencedTaskRunner> task_runner = | |
| 96 base::ThreadTaskRunnerHandle::Get(); | |
| 97 utility_process_host_ = content::UtilityProcessHost::Create( | |
| 98 this, task_runner.get())->AsWeakPtr(); | |
| 99 utility_process_host_->SetName(l10n_util::GetStringUTF16( | |
| 100 IDS_UTILITY_PROCESS_IMAGE_WRITER_NAME)); | |
| 101 | |
| 102 #if defined(OS_WIN) | |
| 103 utility_process_host_->ElevatePrivileges(); | |
| 104 #else | |
| 105 utility_process_host_->DisableSandbox(); | |
| 106 #endif | |
| 107 utility_process_host_->StartBatchMode(); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 void ImageWriterUtilityClient::OnProcessCrashed(int exit_code) { | |
| 112 task_runner_->PostTask( | |
| 113 FROM_HERE, | |
| 114 base::Bind(error_callback_, | |
| 115 base::StringPrintf("Utility process crashed with code %08x.", | |
| 116 exit_code))); | |
| 117 } | |
| 118 | |
| 119 void ImageWriterUtilityClient::OnProcessLaunchFailed(int error_code) { | |
| 120 task_runner_->PostTask( | |
| 121 FROM_HERE, | |
| 122 base::Bind(error_callback_, | |
| 123 base::StringPrintf("Process launch failed with code %08x.", | |
| 124 error_code))); | |
| 125 } | |
| 126 | |
| 127 bool ImageWriterUtilityClient::OnMessageReceived(const IPC::Message& message) { | |
| 128 bool handled = true; | |
| 129 IPC_BEGIN_MESSAGE_MAP(ImageWriterUtilityClient, message) | |
| 130 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Succeeded, | |
| 131 OnWriteImageSucceeded) | |
| 132 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Cancelled, | |
| 133 OnWriteImageCancelled) | |
| 134 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Failed, | |
| 135 OnWriteImageFailed) | |
| 136 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Progress, | |
| 137 OnWriteImageProgress) | |
| 138 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 139 IPC_END_MESSAGE_MAP() | |
| 140 return handled; | |
| 141 } | |
| 142 | |
| 143 bool ImageWriterUtilityClient::Send(IPC::Message* msg) { | |
| 144 return utility_process_host_ && utility_process_host_->Send(msg); | |
| 145 } | |
| 146 | |
| 147 void ImageWriterUtilityClient::OnWriteImageSucceeded() { | |
| 148 if (!success_callback_.is_null()) { | |
| 149 task_runner_->PostTask(FROM_HERE, success_callback_); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 void ImageWriterUtilityClient::OnWriteImageCancelled() { | |
| 154 if (!cancel_callback_.is_null()) { | |
| 155 task_runner_->PostTask(FROM_HERE, cancel_callback_); | |
| 156 } | |
| 157 } | |
| 158 | |
| 159 void ImageWriterUtilityClient::OnWriteImageFailed(const std::string& message) { | |
| 160 if (!error_callback_.is_null()) { | |
| 161 task_runner_->PostTask(FROM_HERE, base::Bind(error_callback_, message)); | |
| 162 } | |
| 163 } | |
| 164 | |
| 165 void ImageWriterUtilityClient::OnWriteImageProgress(int64_t progress) { | |
| 166 if (!progress_callback_.is_null()) { | |
| 167 task_runner_->PostTask(FROM_HERE, base::Bind(progress_callback_, progress)); | |
| 168 } | |
| 169 } | |
| OLD | NEW |