Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / extensions / api / image_writer_private / image_writer_utility_client.cc
blobc27385d8152a3ae9edaf6f05dcd4e730f3f74c21
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.
5 #include "base/bind.h"
6 #include "base/location.h"
7 #include "base/thread_task_runner_handle.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/extensions/api/image_writer_private/image_writer_utility_client.h"
10 #include "chrome/common/extensions/chrome_utility_extensions_messages.h"
11 #include "chrome/grit/generated_resources.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "ui/base/l10n/l10n_util.h"
15 using content::BrowserThread;
17 ImageWriterUtilityClient::ImageWriterUtilityClient()
18 : task_runner_(base::ThreadTaskRunnerHandle::Get()) {
20 ImageWriterUtilityClient::~ImageWriterUtilityClient() {}
22 void ImageWriterUtilityClient::Write(const ProgressCallback& progress_callback,
23 const SuccessCallback& success_callback,
24 const ErrorCallback& error_callback,
25 const base::FilePath& source,
26 const base::FilePath& target) {
27 DCHECK_CURRENTLY_ON(BrowserThread::IO);
29 StartHost();
31 progress_callback_ = progress_callback;
32 success_callback_ = success_callback;
33 error_callback_ = error_callback;
35 if (!Send(new ChromeUtilityMsg_ImageWriter_Write(source, target))) {
36 DLOG(ERROR) << "Unable to send Write message to Utility Process.";
37 task_runner_->PostTask(
38 FROM_HERE, base::Bind(error_callback_, "IPC communication failed"));
42 void ImageWriterUtilityClient::Verify(const ProgressCallback& progress_callback,
43 const SuccessCallback& success_callback,
44 const ErrorCallback& error_callback,
45 const base::FilePath& source,
46 const base::FilePath& target) {
47 DCHECK_CURRENTLY_ON(BrowserThread::IO);
49 StartHost();
51 progress_callback_ = progress_callback;
52 success_callback_ = success_callback;
53 error_callback_ = error_callback;
55 if (!Send(new ChromeUtilityMsg_ImageWriter_Verify(source, target))) {
56 DLOG(ERROR) << "Unable to send Verify message to Utility Process.";
57 task_runner_->PostTask(
58 FROM_HERE, base::Bind(error_callback_, "IPC communication failed"));
62 void ImageWriterUtilityClient::Cancel(const CancelCallback& cancel_callback) {
63 DCHECK_CURRENTLY_ON(BrowserThread::IO);
65 if (!utility_process_host_) {
66 // If we haven't connected, there is nothing to cancel.
67 task_runner_->PostTask(FROM_HERE, cancel_callback);
68 return;
71 cancel_callback_ = cancel_callback;
73 if (!Send(new ChromeUtilityMsg_ImageWriter_Cancel())) {
74 DLOG(ERROR) << "Unable to send Cancel message to Utility Process.";
78 void ImageWriterUtilityClient::Shutdown() {
79 if (utility_process_host_ &&
80 Send(new ChromeUtilityMsg_ImageWriter_Cancel())) {
81 utility_process_host_->EndBatchMode();
84 // Clear handlers to not hold any reference to the caller.
85 success_callback_ = base::Closure();
86 progress_callback_ = base::Callback<void(int64)>();
87 error_callback_ = base::Callback<void(const std::string&)>();
88 cancel_callback_ = base::Closure();
91 void ImageWriterUtilityClient::StartHost() {
92 if (!utility_process_host_) {
93 scoped_refptr<base::SequencedTaskRunner> task_runner =
94 base::MessageLoop::current()->task_runner();
95 utility_process_host_ = content::UtilityProcessHost::Create(
96 this, task_runner.get())->AsWeakPtr();
97 utility_process_host_->SetName(l10n_util::GetStringUTF16(
98 IDS_UTILITY_PROCESS_IMAGE_WRITER_NAME));
100 #if defined(OS_WIN)
101 utility_process_host_->ElevatePrivileges();
102 #else
103 utility_process_host_->DisableSandbox();
104 #endif
105 utility_process_host_->StartBatchMode();
109 void ImageWriterUtilityClient::OnProcessCrashed(int exit_code) {
110 task_runner_->PostTask(
111 FROM_HERE, base::Bind(error_callback_, "Utility process crashed."));
114 void ImageWriterUtilityClient::OnProcessLaunchFailed() {
115 task_runner_->PostTask(
116 FROM_HERE, base::Bind(error_callback_, "Process launch failed."));
119 bool ImageWriterUtilityClient::OnMessageReceived(const IPC::Message& message) {
120 bool handled = true;
121 IPC_BEGIN_MESSAGE_MAP(ImageWriterUtilityClient, message)
122 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Succeeded,
123 OnWriteImageSucceeded)
124 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Cancelled,
125 OnWriteImageCancelled)
126 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Failed,
127 OnWriteImageFailed)
128 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Progress,
129 OnWriteImageProgress)
130 IPC_MESSAGE_UNHANDLED(handled = false)
131 IPC_END_MESSAGE_MAP()
132 return handled;
135 bool ImageWriterUtilityClient::Send(IPC::Message* msg) {
136 return utility_process_host_ && utility_process_host_->Send(msg);
139 void ImageWriterUtilityClient::OnWriteImageSucceeded() {
140 if (!success_callback_.is_null()) {
141 task_runner_->PostTask(FROM_HERE, success_callback_);
145 void ImageWriterUtilityClient::OnWriteImageCancelled() {
146 if (!cancel_callback_.is_null()) {
147 task_runner_->PostTask(FROM_HERE, cancel_callback_);
151 void ImageWriterUtilityClient::OnWriteImageFailed(const std::string& message) {
152 if (!error_callback_.is_null()) {
153 task_runner_->PostTask(FROM_HERE, base::Bind(error_callback_, message));
157 void ImageWriterUtilityClient::OnWriteImageProgress(int64 progress) {
158 if (!progress_callback_.is_null()) {
159 task_runner_->PostTask(FROM_HERE, base::Bind(progress_callback_, progress));