Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(439)

Side by Side Diff: chrome/browser/extensions/api/image_writer_private/image_writer_utility_client.cc

Issue 2663603002: Convert utility process ImageWriter IPC to mojo (Closed)
Patch Set: Remove test DLOG, minor name changes. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "chrome/browser/extensions/api/image_writer_private/image_writer_utilit y_client.h"
6
5 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/callback.h"
Sam McNally 2017/02/02 21:54:17 Remove.
Noel Gordon 2017/02/03 13:54:56 Done.
9 #include "base/files/file_path.h"
6 #include "base/location.h" 10 #include "base/location.h"
7 #include "base/strings/stringprintf.h" 11 #include "base/memory/ptr_util.h"
12 #include "base/optional.h"
13 #include "base/single_thread_task_runner.h"
8 #include "base/threading/thread_task_runner_handle.h" 14 #include "base/threading/thread_task_runner_handle.h"
9 #include "build/build_config.h" 15 #include "chrome/common/extensions/removable_storage_writer.mojom.h"
10 #include "chrome/browser/browser_process.h"
11 #include "chrome/browser/extensions/api/image_writer_private/image_writer_utilit y_client.h"
12 #include "chrome/common/extensions/chrome_utility_extensions_messages.h"
13 #include "chrome/grit/generated_resources.h" 16 #include "chrome/grit/generated_resources.h"
14 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/utility_process_mojo_client.h"
19 #include "mojo/public/cpp/bindings/binding.h"
15 #include "ui/base/l10n/l10n_util.h" 20 #include "ui/base/l10n/l10n_util.h"
16 21
17 using content::BrowserThread; 22 class ImageWriterUtilityClient::RemovableStorageWriterClientImpl
23 : public extensions::mojom::RemovableStorageWriterClient {
24 public:
25 RemovableStorageWriterClientImpl(
26 ImageWriterUtilityClient* owner,
27 extensions::mojom::RemovableStorageWriterClientPtr* interface)
28 : binding_(this, interface), image_writer_utility_client_(owner) {
29 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
30
31 binding_.set_connection_error_handler(
32 base::Bind(&ImageWriterUtilityClient::UtilityProcessError,
33 image_writer_utility_client_));
34 }
35
36 ~RemovableStorageWriterClientImpl() override = default;
37
38 private:
39 void Progress(int64_t progress) override {
40 image_writer_utility_client_->OperationProgress(progress);
41 }
42
43 void Complete(const base::Optional<std::string>& error) override {
44 if (error) {
45 image_writer_utility_client_->OperationFailed(error.value());
46 } else {
47 image_writer_utility_client_->OperationSucceeded();
48 }
49 }
50
51 mojo::Binding<extensions::mojom::RemovableStorageWriterClient> binding_;
52 ImageWriterUtilityClient* const image_writer_utility_client_;
Sam McNally 2017/02/02 21:54:17 Comment that |image_writer_utility_client_| owns |
Noel Gordon 2017/02/03 13:54:56 Done.
53
54 DISALLOW_COPY_AND_ASSIGN(RemovableStorageWriterClientImpl);
55 };
18 56
19 ImageWriterUtilityClient::ImageWriterUtilityClient() 57 ImageWriterUtilityClient::ImageWriterUtilityClient()
20 : task_runner_(base::ThreadTaskRunnerHandle::Get()) { 58 : task_runner_(base::ThreadTaskRunnerHandle::Get()) {
21 } 59 }
22 ImageWriterUtilityClient::~ImageWriterUtilityClient() {} 60
61 ImageWriterUtilityClient::~ImageWriterUtilityClient() = default;
23 62
24 void ImageWriterUtilityClient::Write(const ProgressCallback& progress_callback, 63 void ImageWriterUtilityClient::Write(const ProgressCallback& progress_callback,
25 const SuccessCallback& success_callback, 64 const SuccessCallback& success_callback,
26 const ErrorCallback& error_callback, 65 const ErrorCallback& error_callback,
27 const base::FilePath& source, 66 const base::FilePath& source,
28 const base::FilePath& target) { 67 const base::FilePath& target) {
29 DCHECK_CURRENTLY_ON(BrowserThread::IO); 68 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
30 69 DCHECK(!removable_storage_writer_client_);
31 StartHost();
32 70
33 progress_callback_ = progress_callback; 71 progress_callback_ = progress_callback;
34 success_callback_ = success_callback; 72 success_callback_ = success_callback;
35 error_callback_ = error_callback; 73 error_callback_ = error_callback;
36 74
37 if (!Send(new ChromeUtilityMsg_ImageWriter_Write(source, target))) { 75 StartUtilityProcess();
38 DLOG(ERROR) << "Unable to send Write message to Utility Process."; 76
39 task_runner_->PostTask( 77 extensions::mojom::RemovableStorageWriterClientPtr client;
40 FROM_HERE, base::Bind(error_callback_, "IPC communication failed")); 78 removable_storage_writer_client_ =
41 } 79 base::MakeUnique<RemovableStorageWriterClientImpl>(this, &client);
80
81 UtilityProcessService()->Write(source, target, std::move(client));
42 } 82 }
43 83
44 void ImageWriterUtilityClient::Verify(const ProgressCallback& progress_callback, 84 void ImageWriterUtilityClient::Verify(const ProgressCallback& progress_callback,
45 const SuccessCallback& success_callback, 85 const SuccessCallback& success_callback,
46 const ErrorCallback& error_callback, 86 const ErrorCallback& error_callback,
47 const base::FilePath& source, 87 const base::FilePath& source,
48 const base::FilePath& target) { 88 const base::FilePath& target) {
49 DCHECK_CURRENTLY_ON(BrowserThread::IO); 89 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
50 90 DCHECK(!removable_storage_writer_client_);
51 StartHost();
52 91
53 progress_callback_ = progress_callback; 92 progress_callback_ = progress_callback;
54 success_callback_ = success_callback; 93 success_callback_ = success_callback;
55 error_callback_ = error_callback; 94 error_callback_ = error_callback;
56 95
57 if (!Send(new ChromeUtilityMsg_ImageWriter_Verify(source, target))) { 96 StartUtilityProcess();
58 DLOG(ERROR) << "Unable to send Verify message to Utility Process."; 97
59 task_runner_->PostTask( 98 extensions::mojom::RemovableStorageWriterClientPtr client;
60 FROM_HERE, base::Bind(error_callback_, "IPC communication failed")); 99 removable_storage_writer_client_ =
61 } 100 base::MakeUnique<RemovableStorageWriterClientImpl>(this, &client);
101
102 UtilityProcessService()->Verify(source, target, std::move(client));
62 } 103 }
63 104
64 void ImageWriterUtilityClient::Cancel(const CancelCallback& cancel_callback) { 105 void ImageWriterUtilityClient::Cancel(const CancelCallback& cancel_callback) {
65 DCHECK_CURRENTLY_ON(BrowserThread::IO); 106 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
66 107
67 if (!utility_process_host_) { 108 ResetRequest();
68 // If we haven't connected, there is nothing to cancel. 109 task_runner_->PostTask(FROM_HERE, cancel_callback);
69 task_runner_->PostTask(FROM_HERE, cancel_callback);
70 return;
71 }
72
73 cancel_callback_ = cancel_callback;
74
75 if (!Send(new ChromeUtilityMsg_ImageWriter_Cancel())) {
76 DLOG(ERROR) << "Unable to send Cancel message to Utility Process.";
77 }
78 } 110 }
79 111
80 void ImageWriterUtilityClient::Shutdown() { 112 void ImageWriterUtilityClient::Shutdown() {
81 if (utility_process_host_ && 113 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
82 Send(new ChromeUtilityMsg_ImageWriter_Cancel())) { 114
83 utility_process_host_->EndBatchMode(); 115 ResetRequest();
84 } 116 utility_process_mojo_client_.reset();
117 }
118
119 void ImageWriterUtilityClient::StartUtilityProcess() {
120 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
121
122 if (utility_process_mojo_client_)
123 return;
124
125 const base::string16 utility_process_name =
126 l10n_util::GetStringUTF16(IDS_UTILITY_PROCESS_IMAGE_WRITER_NAME);
127
128 utility_process_mojo_client_.reset(
129 new content::UtilityProcessMojoClient<
130 extensions::mojom::RemovableStorageWriter>(utility_process_name));
131 utility_process_mojo_client_->set_error_callback(
132 base::Bind(&ImageWriterUtilityClient::UtilityProcessError, this));
133
134 utility_process_mojo_client_->set_disable_sandbox();
135 #if defined(OS_WIN)
136 utility_process_mojo_client_->set_run_elevated();
137 #endif
138
139 utility_process_mojo_client_->Start();
140 }
141
142 void ImageWriterUtilityClient::UtilityProcessError() {
143 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
144
145 OperationFailed("Utility process mojo connection error");
Sam McNally 2017/02/02 21:54:17 Is this message used for anything user-facing?
Noel Gordon 2017/02/03 13:54:56 A chrome extension could could show it if it wante
146 utility_process_mojo_client_.reset();
147 }
148
149 void ImageWriterUtilityClient::OperationProgress(int64_t progress) {
150 if (progress_callback_)
151 task_runner_->PostTask(FROM_HERE, base::Bind(progress_callback_, progress));
152 }
153
154 void ImageWriterUtilityClient::OperationSucceeded() {
155 SuccessCallback success_callback = success_callback_;
156 ResetRequest();
157 if (success_callback)
158 task_runner_->PostTask(FROM_HERE, success_callback);
159 }
160
161 void ImageWriterUtilityClient::OperationFailed(const std::string& error) {
162 ErrorCallback error_callback = error_callback_;
163 ResetRequest();
164 if (error_callback)
165 task_runner_->PostTask(FROM_HERE, base::Bind(error_callback, error));
166 }
167
168 void ImageWriterUtilityClient::ResetRequest() {
169 removable_storage_writer_client_.reset();
85 170
86 // Clear handlers to not hold any reference to the caller. 171 // Clear handlers to not hold any reference to the caller.
87 success_callback_ = base::Closure(); 172 progress_callback_.Reset();
88 progress_callback_ = base::Callback<void(int64_t)>(); 173 success_callback_.Reset();
89 error_callback_ = base::Callback<void(const std::string&)>(); 174 error_callback_.Reset();
90 cancel_callback_ = base::Closure();
91 } 175 }
92
93 void ImageWriterUtilityClient::StartHost() {
94 if (!utility_process_host_) {
95 scoped_refptr<base::SequencedTaskRunner> task_runner =
96 base::ThreadTaskRunnerHandle::Get();
97 utility_process_host_ = content::UtilityProcessHost::Create(
98 this, task_runner.get())->AsWeakPtr();
99 utility_process_host_->SetName(l10n_util::GetStringUTF16(
100 IDS_UTILITY_PROCESS_IMAGE_WRITER_NAME));
101
102 #if defined(OS_WIN)
103 utility_process_host_->ElevatePrivileges();
104 #else
105 utility_process_host_->DisableSandbox();
106 #endif
107 utility_process_host_->StartBatchMode();
108 }
109 }
110
111 void ImageWriterUtilityClient::OnProcessCrashed(int exit_code) {
112 task_runner_->PostTask(
113 FROM_HERE,
114 base::Bind(error_callback_,
115 base::StringPrintf("Utility process crashed with code %08x.",
116 exit_code)));
117 }
118
119 void ImageWriterUtilityClient::OnProcessLaunchFailed(int error_code) {
120 task_runner_->PostTask(
121 FROM_HERE,
122 base::Bind(error_callback_,
123 base::StringPrintf("Process launch failed with code %08x.",
124 error_code)));
125 }
126
127 bool ImageWriterUtilityClient::OnMessageReceived(const IPC::Message& message) {
128 bool handled = true;
129 IPC_BEGIN_MESSAGE_MAP(ImageWriterUtilityClient, message)
130 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Succeeded,
131 OnWriteImageSucceeded)
132 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Cancelled,
133 OnWriteImageCancelled)
134 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Failed,
135 OnWriteImageFailed)
136 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Progress,
137 OnWriteImageProgress)
138 IPC_MESSAGE_UNHANDLED(handled = false)
139 IPC_END_MESSAGE_MAP()
140 return handled;
141 }
142
143 bool ImageWriterUtilityClient::Send(IPC::Message* msg) {
144 return utility_process_host_ && utility_process_host_->Send(msg);
145 }
146
147 void ImageWriterUtilityClient::OnWriteImageSucceeded() {
148 if (!success_callback_.is_null()) {
149 task_runner_->PostTask(FROM_HERE, success_callback_);
150 }
151 }
152
153 void ImageWriterUtilityClient::OnWriteImageCancelled() {
154 if (!cancel_callback_.is_null()) {
155 task_runner_->PostTask(FROM_HERE, cancel_callback_);
156 }
157 }
158
159 void ImageWriterUtilityClient::OnWriteImageFailed(const std::string& message) {
160 if (!error_callback_.is_null()) {
161 task_runner_->PostTask(FROM_HERE, base::Bind(error_callback_, message));
162 }
163 }
164
165 void ImageWriterUtilityClient::OnWriteImageProgress(int64_t progress) {
166 if (!progress_callback_.is_null()) {
167 task_runner_->PostTask(FROM_HERE, base::Bind(progress_callback_, progress));
168 }
169 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698