Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 5 #include "base/bind.h" | |
| 6 #include "base/message_loop/message_loop.h" | |
| 7 #include "chrome/browser/browser_process.h" | |
| 8 #include "chrome/browser/image_writer_client.h" | |
| 9 #include "chrome/common/chrome_utility_messages.h" | |
| 10 #include "content/public/browser/browser_thread.h" | |
| 11 | |
| 12 using content::BrowserThread; | |
| 13 | |
| 14 ImageWriterClient::ImageWriterClient() | |
| 15 : message_loop_proxy_(base::MessageLoopProxy::current()) {} | |
| 16 ImageWriterClient::~ImageWriterClient() {} | |
| 17 | |
| 18 void ImageWriterClient::Write(const ProgressCallback& progress_callback, | |
| 19 const SuccessCallback& success_callback, | |
| 20 const ErrorCallback& error_callback, | |
| 21 const base::FilePath& source, | |
| 22 const base::FilePath& target) { | |
| 23 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 24 | |
| 25 StartHost(); | |
| 26 | |
| 27 progress_callback_ = progress_callback; | |
| 28 success_callback_ = success_callback; | |
| 29 error_callback_ = error_callback; | |
| 30 | |
| 31 if (!Send(new ChromeUtilityMsg_ImageWriter_Write(source, target))) { | |
| 32 DLOG(ERROR) << "Unable to send Write message to Utility Process."; | |
| 33 error_callback_.Run("Send message failed."); | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 void ImageWriterClient::Verify(const ProgressCallback& progress_callback, | |
| 38 const SuccessCallback& success_callback, | |
| 39 const ErrorCallback& error_callback, | |
| 40 const base::FilePath& source, | |
| 41 const base::FilePath& target) { | |
| 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 43 | |
| 44 StartHost(); | |
| 45 | |
| 46 progress_callback_ = progress_callback; | |
| 47 success_callback_ = success_callback; | |
| 48 error_callback_ = error_callback; | |
| 49 | |
| 50 if (!Send(new ChromeUtilityMsg_ImageWriter_Verify(source, target))) { | |
| 51 DLOG(ERROR) << "Unable to send Verify message to Utility Process."; | |
| 52 | |
| 53 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
| |
| 54 FROM_HERE, base::Bind(error_callback_, "Send message failed")); | |
| 55 // 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.
| |
| 56 } | |
| 57 } | |
| 58 | |
| 59 void ImageWriterClient::Cancel(const CancelCallback& cancel_callback) { | |
| 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 61 | |
| 62 cancel_callback_ = cancel_callback; | |
| 63 | |
| 64 if (!Send(new ChromeUtilityMsg_ImageWriter_Cancel())) { | |
| 65 DLOG(ERROR) << "Unable to send Cancel message to Utility Process."; | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 void ImageWriterClient::Shutdown() { | |
| 70 if (utility_process_host_ && | |
| 71 Send(new ChromeUtilityMsg_ImageWriter_Cancel())) { | |
| 72 utility_process_host_->EndBatchMode(); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 void ImageWriterClient::StartHost() { | |
| 77 if (!utility_process_host_) { | |
| 78 scoped_refptr<base::SequencedTaskRunner> task_runner = | |
| 79 base::MessageLoop::current()->message_loop_proxy(); | |
| 80 utility_process_host_ = content::UtilityProcessHost::Create( | |
| 81 this, task_runner.get())->AsWeakPtr(); | |
| 82 | |
| 83 #if defined(OS_WIN) | |
| 84 utility_process_host_->ElevatePrivileges(); | |
| 85 #else | |
| 86 utility_process_host_->DisableSandbox(); | |
| 87 #endif | |
| 88 utility_process_host_->StartBatchMode(); | |
| 89 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.
| |
| 90 } | |
| 91 } | |
| 92 | |
| 93 void ImageWriterClient::OnProcessCrashed(int exit_code) { | |
| 94 message_loop_proxy_->PostTask( | |
| 95 FROM_HERE, base::Bind(error_callback_, "Utility process crashed.")); | |
| 96 } | |
| 97 | |
| 98 void ImageWriterClient::OnProcessLaunchFailed() { | |
| 99 message_loop_proxy_->PostTask( | |
| 100 FROM_HERE, base::Bind(error_callback_, "Process launch failed.")); | |
| 101 } | |
| 102 | |
| 103 bool ImageWriterClient::OnMessageReceived(const IPC::Message& message) { | |
| 104 bool handled = true; | |
| 105 IPC_BEGIN_MESSAGE_MAP(ImageWriterClient, message) | |
| 106 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Succeeded, | |
| 107 OnWriteImageSucceeded) | |
| 108 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Cancelled, | |
| 109 OnWriteImageCancelled) | |
| 110 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Failed, | |
| 111 OnWriteImageFailed) | |
| 112 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Progress, | |
| 113 OnWriteImageProgress) | |
| 114 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 115 IPC_END_MESSAGE_MAP() | |
| 116 return handled; | |
| 117 } | |
| 118 | |
| 119 bool ImageWriterClient::Send(IPC::Message* msg) { | |
| 120 return utility_process_host_ && utility_process_host_->Send(msg); | |
| 121 } | |
| 122 | |
| 123 void ImageWriterClient::OnWriteImageSucceeded() { | |
| 124 if (!success_callback_.is_null()) { | |
| 125 message_loop_proxy_->PostTask(FROM_HERE, success_callback_); | |
| 126 } | |
| 127 } | |
| 128 | |
| 129 void ImageWriterClient::OnWriteImageCancelled() { | |
| 130 if (!cancel_callback_.is_null()) { | |
| 131 message_loop_proxy_->PostTask(FROM_HERE, cancel_callback_); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 void ImageWriterClient::OnWriteImageFailed(const std::string& message) { | |
| 136 if (!error_callback_.is_null()) { | |
| 137 message_loop_proxy_->PostTask(FROM_HERE, | |
| 138 base::Bind(error_callback_, message)); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 void ImageWriterClient::OnWriteImageProgress(int64 progress) { | |
| 143 if (!progress_callback_.is_null()) { | |
| 144 message_loop_proxy_->PostTask(FROM_HERE, | |
| 145 base::Bind(progress_callback_, progress)); | |
| 146 } | |
| 147 } | |
| OLD | NEW |