summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/image_writer_private/image_writer_utility_client.cc
blob: d60993f8242f32a30e684e17741f94cc67d523a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// 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/location.h"
#include "base/thread_task_runner_handle.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/image_writer_private/image_writer_utility_client.h"
#include "chrome/common/extensions/chrome_utility_extensions_messages.h"
#include "chrome/grit/generated_resources.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/l10n/l10n_util.h"

using content::BrowserThread;

ImageWriterUtilityClient::ImageWriterUtilityClient()
    : task_runner_(base::ThreadTaskRunnerHandle::Get()) {
}
ImageWriterUtilityClient::~ImageWriterUtilityClient() {}

void ImageWriterUtilityClient::Write(const ProgressCallback& progress_callback,
                                     const SuccessCallback& success_callback,
                                     const ErrorCallback& error_callback,
                                     const base::FilePath& source,
                                     const base::FilePath& target) {
  DCHECK_CURRENTLY_ON(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.";
    task_runner_->PostTask(
        FROM_HERE, base::Bind(error_callback_, "IPC communication failed"));
  }
}

void ImageWriterUtilityClient::Verify(const ProgressCallback& progress_callback,
                                      const SuccessCallback& success_callback,
                                      const ErrorCallback& error_callback,
                                      const base::FilePath& source,
                                      const base::FilePath& target) {
  DCHECK_CURRENTLY_ON(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.";
    task_runner_->PostTask(
        FROM_HERE, base::Bind(error_callback_, "IPC communication failed"));
  }
}

void ImageWriterUtilityClient::Cancel(const CancelCallback& cancel_callback) {
  DCHECK_CURRENTLY_ON(BrowserThread::IO);

  if (!utility_process_host_) {
    // If we haven't connected, there is nothing to cancel.
    task_runner_->PostTask(FROM_HERE, cancel_callback);
    return;
  }

  cancel_callback_ = cancel_callback;

  if (!Send(new ChromeUtilityMsg_ImageWriter_Cancel())) {
    DLOG(ERROR) << "Unable to send Cancel message to Utility Process.";
  }
}

void ImageWriterUtilityClient::Shutdown() {
  if (utility_process_host_ &&
      Send(new ChromeUtilityMsg_ImageWriter_Cancel())) {
    utility_process_host_->EndBatchMode();
  }

  // Clear handlers to not hold any reference to the caller.
  success_callback_ = base::Closure();
  progress_callback_ = base::Callback<void(int64_t)>();
  error_callback_ = base::Callback<void(const std::string&)>();
  cancel_callback_ = base::Closure();
}

void ImageWriterUtilityClient::StartHost() {
  if (!utility_process_host_) {
    scoped_refptr<base::SequencedTaskRunner> task_runner =
        base::MessageLoop::current()->task_runner();
    utility_process_host_ = content::UtilityProcessHost::Create(
                                this, task_runner.get())->AsWeakPtr();
    utility_process_host_->SetName(l10n_util::GetStringUTF16(
        IDS_UTILITY_PROCESS_IMAGE_WRITER_NAME));

#if defined(OS_WIN)
    utility_process_host_->ElevatePrivileges();
#else
    utility_process_host_->DisableSandbox();
#endif
    utility_process_host_->StartBatchMode();
  }
}

void ImageWriterUtilityClient::OnProcessCrashed(int exit_code) {
  task_runner_->PostTask(
      FROM_HERE, base::Bind(error_callback_, "Utility process crashed."));
}

void ImageWriterUtilityClient::OnProcessLaunchFailed() {
  task_runner_->PostTask(
      FROM_HERE, base::Bind(error_callback_, "Process launch failed."));
}

bool ImageWriterUtilityClient::OnMessageReceived(const IPC::Message& message) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(ImageWriterUtilityClient, 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 ImageWriterUtilityClient::Send(IPC::Message* msg) {
  return utility_process_host_ && utility_process_host_->Send(msg);
}

void ImageWriterUtilityClient::OnWriteImageSucceeded() {
  if (!success_callback_.is_null()) {
    task_runner_->PostTask(FROM_HERE, success_callback_);
  }
}

void ImageWriterUtilityClient::OnWriteImageCancelled() {
  if (!cancel_callback_.is_null()) {
    task_runner_->PostTask(FROM_HERE, cancel_callback_);
  }
}

void ImageWriterUtilityClient::OnWriteImageFailed(const std::string& message) {
  if (!error_callback_.is_null()) {
    task_runner_->PostTask(FROM_HERE, base::Bind(error_callback_, message));
  }
}

void ImageWriterUtilityClient::OnWriteImageProgress(int64_t progress) {
  if (!progress_callback_.is_null()) {
    task_runner_->PostTask(FROM_HERE, base::Bind(progress_callback_, progress));
  }
}