Chromium Code Reviews| Index: chrome/browser/image_writer_client.cc |
| diff --git a/chrome/browser/image_writer_client.cc b/chrome/browser/image_writer_client.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..aacf57005c1d64b85d1dabbf05f6d58c6db7ce11 |
| --- /dev/null |
| +++ b/chrome/browser/image_writer_client.cc |
| @@ -0,0 +1,147 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/bind.h" |
| +#include "base/message_loop/message_loop.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/image_writer_client.h" |
| +#include "chrome/common/chrome_utility_messages.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +using content::BrowserThread; |
| + |
| +ImageWriterClient::ImageWriterClient() |
| + : message_loop_proxy_(base::MessageLoopProxy::current()) {} |
| +ImageWriterClient::~ImageWriterClient() {} |
| + |
| +void ImageWriterClient::Write(const ProgressCallback& progress_callback, |
| + const SuccessCallback& success_callback, |
| + const ErrorCallback& error_callback, |
| + const base::FilePath& source, |
| + const base::FilePath& target) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + StartHost(); |
| + |
| + progress_callback_ = progress_callback; |
| + success_callback_ = success_callback; |
| + error_callback_ = error_callback; |
| + |
| + if (!Send(new ChromeUtilityMsg_ImageWriter_Write(source, target))) { |
| + DLOG(ERROR) << "Unable to send Write message to Utility Process."; |
| + error_callback_.Run("Send message failed."); |
| + } |
| +} |
| + |
| +void ImageWriterClient::Verify(const ProgressCallback& progress_callback, |
| + const SuccessCallback& success_callback, |
| + const ErrorCallback& error_callback, |
| + const base::FilePath& source, |
| + const base::FilePath& target) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + StartHost(); |
| + |
| + progress_callback_ = progress_callback; |
| + success_callback_ = success_callback; |
| + error_callback_ = error_callback; |
| + |
| + if (!Send(new ChromeUtilityMsg_ImageWriter_Verify(source, target))) { |
| + DLOG(ERROR) << "Unable to send Verify message to Utility Process."; |
| + |
| + message_loop_proxy_->PostTask( |
|
Matt Perry
2014/02/14 00:16:23
Why is this done with PostTask but not so for Writ
Drew Haven
2014/02/14 02:32:56
Whoops, right. We're running things here on the I
|
| + FROM_HERE, base::Bind(error_callback_, "Send message failed")); |
| + // error_callback_.Run("Send message failed."); |
|
Matt Perry
2014/02/14 00:16:23
remove commented code
Drew Haven
2014/02/14 02:32:56
Done.
|
| + } |
| +} |
| + |
| +void ImageWriterClient::Cancel(const CancelCallback& cancel_callback) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + cancel_callback_ = cancel_callback; |
| + |
| + if (!Send(new ChromeUtilityMsg_ImageWriter_Cancel())) { |
| + DLOG(ERROR) << "Unable to send Cancel message to Utility Process."; |
| + } |
| +} |
| + |
| +void ImageWriterClient::Shutdown() { |
| + if (utility_process_host_ && |
| + Send(new ChromeUtilityMsg_ImageWriter_Cancel())) { |
| + utility_process_host_->EndBatchMode(); |
| + } |
| +} |
| + |
| +void ImageWriterClient::StartHost() { |
| + if (!utility_process_host_) { |
| + scoped_refptr<base::SequencedTaskRunner> task_runner = |
| + base::MessageLoop::current()->message_loop_proxy(); |
| + utility_process_host_ = content::UtilityProcessHost::Create( |
| + this, task_runner.get())->AsWeakPtr(); |
| + |
| +#if defined(OS_WIN) |
| + utility_process_host_->ElevatePrivileges(); |
| +#else |
| + utility_process_host_->DisableSandbox(); |
| +#endif |
| + utility_process_host_->StartBatchMode(); |
| + utility_process_host_->DisableSandbox(); |
|
Matt Perry
2014/02/14 00:16:23
Did you mean to call this twice for !OS_WIN?
Drew Haven
2014/02/14 02:32:56
Done.
|
| + } |
| +} |
| + |
| +void ImageWriterClient::OnProcessCrashed(int exit_code) { |
| + message_loop_proxy_->PostTask( |
| + FROM_HERE, base::Bind(error_callback_, "Utility process crashed.")); |
| +} |
| + |
| +void ImageWriterClient::OnProcessLaunchFailed() { |
| + message_loop_proxy_->PostTask( |
| + FROM_HERE, base::Bind(error_callback_, "Process launch failed.")); |
| +} |
| + |
| +bool ImageWriterClient::OnMessageReceived(const IPC::Message& message) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(ImageWriterClient, message) |
| + IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Succeeded, |
| + OnWriteImageSucceeded) |
| + IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Cancelled, |
| + OnWriteImageCancelled) |
| + IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Failed, |
| + OnWriteImageFailed) |
| + IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Progress, |
| + OnWriteImageProgress) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +bool ImageWriterClient::Send(IPC::Message* msg) { |
| + return utility_process_host_ && utility_process_host_->Send(msg); |
| +} |
| + |
| +void ImageWriterClient::OnWriteImageSucceeded() { |
| + if (!success_callback_.is_null()) { |
| + message_loop_proxy_->PostTask(FROM_HERE, success_callback_); |
| + } |
| +} |
| + |
| +void ImageWriterClient::OnWriteImageCancelled() { |
| + if (!cancel_callback_.is_null()) { |
| + message_loop_proxy_->PostTask(FROM_HERE, cancel_callback_); |
| + } |
| +} |
| + |
| +void ImageWriterClient::OnWriteImageFailed(const std::string& message) { |
| + if (!error_callback_.is_null()) { |
| + message_loop_proxy_->PostTask(FROM_HERE, |
| + base::Bind(error_callback_, message)); |
| + } |
| +} |
| + |
| +void ImageWriterClient::OnWriteImageProgress(int64 progress) { |
| + if (!progress_callback_.is_null()) { |
| + message_loop_proxy_->PostTask(FROM_HERE, |
| + base::Bind(progress_callback_, progress)); |
| + } |
| +} |