| 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 "base/bind.h" | 5 #include "base/bind.h" |
| 6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
| 7 #include "base/thread_task_runner_handle.h" |
| 7 #include "chrome/browser/browser_process.h" | 8 #include "chrome/browser/browser_process.h" |
| 8 #include "chrome/browser/extensions/api/image_writer_private/image_writer_utilit
y_client.h" | 9 #include "chrome/browser/extensions/api/image_writer_private/image_writer_utilit
y_client.h" |
| 9 #include "chrome/common/extensions/chrome_utility_extensions_messages.h" | 10 #include "chrome/common/extensions/chrome_utility_extensions_messages.h" |
| 10 #include "chrome/grit/generated_resources.h" | 11 #include "chrome/grit/generated_resources.h" |
| 11 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 12 #include "ui/base/l10n/l10n_util.h" | 13 #include "ui/base/l10n/l10n_util.h" |
| 13 | 14 |
| 14 using content::BrowserThread; | 15 using content::BrowserThread; |
| 15 | 16 |
| 16 ImageWriterUtilityClient::ImageWriterUtilityClient() | 17 ImageWriterUtilityClient::ImageWriterUtilityClient() |
| 17 : message_loop_proxy_(base::MessageLoopProxy::current()) {} | 18 : thread_task_runner_(base::ThreadTaskRunnerHandle::Get()) {} |
| 18 ImageWriterUtilityClient::~ImageWriterUtilityClient() {} | 19 ImageWriterUtilityClient::~ImageWriterUtilityClient() {} |
| 19 | 20 |
| 20 void ImageWriterUtilityClient::Write(const ProgressCallback& progress_callback, | 21 void ImageWriterUtilityClient::Write(const ProgressCallback& progress_callback, |
| 21 const SuccessCallback& success_callback, | 22 const SuccessCallback& success_callback, |
| 22 const ErrorCallback& error_callback, | 23 const ErrorCallback& error_callback, |
| 23 const base::FilePath& source, | 24 const base::FilePath& source, |
| 24 const base::FilePath& target) { | 25 const base::FilePath& target) { |
| 25 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 26 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 26 | 27 |
| 27 StartHost(); | 28 StartHost(); |
| 28 | 29 |
| 29 progress_callback_ = progress_callback; | 30 progress_callback_ = progress_callback; |
| 30 success_callback_ = success_callback; | 31 success_callback_ = success_callback; |
| 31 error_callback_ = error_callback; | 32 error_callback_ = error_callback; |
| 32 | 33 |
| 33 if (!Send(new ChromeUtilityMsg_ImageWriter_Write(source, target))) { | 34 if (!Send(new ChromeUtilityMsg_ImageWriter_Write(source, target))) { |
| 34 DLOG(ERROR) << "Unable to send Write message to Utility Process."; | 35 DLOG(ERROR) << "Unable to send Write message to Utility Process."; |
| 35 message_loop_proxy_->PostTask( | 36 thread_task_runner_->PostTask( |
| 36 FROM_HERE, base::Bind(error_callback_, "IPC communication failed")); | 37 FROM_HERE, base::Bind(error_callback_, "IPC communication failed")); |
| 37 } | 38 } |
| 38 } | 39 } |
| 39 | 40 |
| 40 void ImageWriterUtilityClient::Verify(const ProgressCallback& progress_callback, | 41 void ImageWriterUtilityClient::Verify(const ProgressCallback& progress_callback, |
| 41 const SuccessCallback& success_callback, | 42 const SuccessCallback& success_callback, |
| 42 const ErrorCallback& error_callback, | 43 const ErrorCallback& error_callback, |
| 43 const base::FilePath& source, | 44 const base::FilePath& source, |
| 44 const base::FilePath& target) { | 45 const base::FilePath& target) { |
| 45 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 46 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 46 | 47 |
| 47 StartHost(); | 48 StartHost(); |
| 48 | 49 |
| 49 progress_callback_ = progress_callback; | 50 progress_callback_ = progress_callback; |
| 50 success_callback_ = success_callback; | 51 success_callback_ = success_callback; |
| 51 error_callback_ = error_callback; | 52 error_callback_ = error_callback; |
| 52 | 53 |
| 53 if (!Send(new ChromeUtilityMsg_ImageWriter_Verify(source, target))) { | 54 if (!Send(new ChromeUtilityMsg_ImageWriter_Verify(source, target))) { |
| 54 DLOG(ERROR) << "Unable to send Verify message to Utility Process."; | 55 DLOG(ERROR) << "Unable to send Verify message to Utility Process."; |
| 55 message_loop_proxy_->PostTask( | 56 thread_task_runner_->PostTask( |
| 56 FROM_HERE, base::Bind(error_callback_, "IPC communication failed")); | 57 FROM_HERE, base::Bind(error_callback_, "IPC communication failed")); |
| 57 } | 58 } |
| 58 } | 59 } |
| 59 | 60 |
| 60 void ImageWriterUtilityClient::Cancel(const CancelCallback& cancel_callback) { | 61 void ImageWriterUtilityClient::Cancel(const CancelCallback& cancel_callback) { |
| 61 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 62 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 62 | 63 |
| 63 if (!utility_process_host_) { | 64 if (!utility_process_host_) { |
| 64 // If we haven't connected, there is nothing to cancel. | 65 // If we haven't connected, there is nothing to cancel. |
| 65 message_loop_proxy_->PostTask(FROM_HERE, cancel_callback); | 66 thread_task_runner_->PostTask(FROM_HERE, cancel_callback); |
| 66 return; | 67 return; |
| 67 } | 68 } |
| 68 | 69 |
| 69 cancel_callback_ = cancel_callback; | 70 cancel_callback_ = cancel_callback; |
| 70 | 71 |
| 71 if (!Send(new ChromeUtilityMsg_ImageWriter_Cancel())) { | 72 if (!Send(new ChromeUtilityMsg_ImageWriter_Cancel())) { |
| 72 DLOG(ERROR) << "Unable to send Cancel message to Utility Process."; | 73 DLOG(ERROR) << "Unable to send Cancel message to Utility Process."; |
| 73 } | 74 } |
| 74 } | 75 } |
| 75 | 76 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 98 #if defined(OS_WIN) | 99 #if defined(OS_WIN) |
| 99 utility_process_host_->ElevatePrivileges(); | 100 utility_process_host_->ElevatePrivileges(); |
| 100 #else | 101 #else |
| 101 utility_process_host_->DisableSandbox(); | 102 utility_process_host_->DisableSandbox(); |
| 102 #endif | 103 #endif |
| 103 utility_process_host_->StartBatchMode(); | 104 utility_process_host_->StartBatchMode(); |
| 104 } | 105 } |
| 105 } | 106 } |
| 106 | 107 |
| 107 void ImageWriterUtilityClient::OnProcessCrashed(int exit_code) { | 108 void ImageWriterUtilityClient::OnProcessCrashed(int exit_code) { |
| 108 message_loop_proxy_->PostTask( | 109 thread_task_runner_->PostTask( |
| 109 FROM_HERE, base::Bind(error_callback_, "Utility process crashed.")); | 110 FROM_HERE, base::Bind(error_callback_, "Utility process crashed.")); |
| 110 } | 111 } |
| 111 | 112 |
| 112 void ImageWriterUtilityClient::OnProcessLaunchFailed() { | 113 void ImageWriterUtilityClient::OnProcessLaunchFailed() { |
| 113 message_loop_proxy_->PostTask( | 114 thread_task_runner_->PostTask( |
| 114 FROM_HERE, base::Bind(error_callback_, "Process launch failed.")); | 115 FROM_HERE, base::Bind(error_callback_, "Process launch failed.")); |
| 115 } | 116 } |
| 116 | 117 |
| 117 bool ImageWriterUtilityClient::OnMessageReceived(const IPC::Message& message) { | 118 bool ImageWriterUtilityClient::OnMessageReceived(const IPC::Message& message) { |
| 118 bool handled = true; | 119 bool handled = true; |
| 119 IPC_BEGIN_MESSAGE_MAP(ImageWriterUtilityClient, message) | 120 IPC_BEGIN_MESSAGE_MAP(ImageWriterUtilityClient, message) |
| 120 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Succeeded, | 121 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Succeeded, |
| 121 OnWriteImageSucceeded) | 122 OnWriteImageSucceeded) |
| 122 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Cancelled, | 123 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Cancelled, |
| 123 OnWriteImageCancelled) | 124 OnWriteImageCancelled) |
| 124 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Failed, | 125 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Failed, |
| 125 OnWriteImageFailed) | 126 OnWriteImageFailed) |
| 126 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Progress, | 127 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Progress, |
| 127 OnWriteImageProgress) | 128 OnWriteImageProgress) |
| 128 IPC_MESSAGE_UNHANDLED(handled = false) | 129 IPC_MESSAGE_UNHANDLED(handled = false) |
| 129 IPC_END_MESSAGE_MAP() | 130 IPC_END_MESSAGE_MAP() |
| 130 return handled; | 131 return handled; |
| 131 } | 132 } |
| 132 | 133 |
| 133 bool ImageWriterUtilityClient::Send(IPC::Message* msg) { | 134 bool ImageWriterUtilityClient::Send(IPC::Message* msg) { |
| 134 return utility_process_host_ && utility_process_host_->Send(msg); | 135 return utility_process_host_ && utility_process_host_->Send(msg); |
| 135 } | 136 } |
| 136 | 137 |
| 137 void ImageWriterUtilityClient::OnWriteImageSucceeded() { | 138 void ImageWriterUtilityClient::OnWriteImageSucceeded() { |
| 138 if (!success_callback_.is_null()) { | 139 if (!success_callback_.is_null()) { |
| 139 message_loop_proxy_->PostTask(FROM_HERE, success_callback_); | 140 thread_task_runner_->PostTask(FROM_HERE, success_callback_); |
| 140 } | 141 } |
| 141 } | 142 } |
| 142 | 143 |
| 143 void ImageWriterUtilityClient::OnWriteImageCancelled() { | 144 void ImageWriterUtilityClient::OnWriteImageCancelled() { |
| 144 if (!cancel_callback_.is_null()) { | 145 if (!cancel_callback_.is_null()) { |
| 145 message_loop_proxy_->PostTask(FROM_HERE, cancel_callback_); | 146 thread_task_runner_->PostTask(FROM_HERE, cancel_callback_); |
| 146 } | 147 } |
| 147 } | 148 } |
| 148 | 149 |
| 149 void ImageWriterUtilityClient::OnWriteImageFailed(const std::string& message) { | 150 void ImageWriterUtilityClient::OnWriteImageFailed(const std::string& message) { |
| 150 if (!error_callback_.is_null()) { | 151 if (!error_callback_.is_null()) { |
| 151 message_loop_proxy_->PostTask(FROM_HERE, | 152 thread_task_runner_->PostTask(FROM_HERE, |
| 152 base::Bind(error_callback_, message)); | 153 base::Bind(error_callback_, message)); |
| 153 } | 154 } |
| 154 } | 155 } |
| 155 | 156 |
| 156 void ImageWriterUtilityClient::OnWriteImageProgress(int64 progress) { | 157 void ImageWriterUtilityClient::OnWriteImageProgress(int64 progress) { |
| 157 if (!progress_callback_.is_null()) { | 158 if (!progress_callback_.is_null()) { |
| 158 message_loop_proxy_->PostTask(FROM_HERE, | 159 thread_task_runner_->PostTask(FROM_HERE, |
| 159 base::Bind(progress_callback_, progress)); | 160 base::Bind(progress_callback_, progress)); |
| 160 } | 161 } |
| 161 } | 162 } |
| OLD | NEW |