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; | |
Lei Zhang
2014/02/15 00:26:59
nit: Put these in the initializer list?
Drew Haven
2014/02/15 01:23:58
Not a constructor here.
| |
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 message_loop_proxy_->PostTask( | |
34 FROM_HERE, base::Bind(error_callback_, "IPC communication failed")); | |
35 } | |
36 } | |
37 | |
38 void ImageWriterClient::Verify(const ProgressCallback& progress_callback, | |
39 const SuccessCallback& success_callback, | |
40 const ErrorCallback& error_callback, | |
41 const base::FilePath& source, | |
42 const base::FilePath& target) { | |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
44 | |
45 StartHost(); | |
46 | |
47 progress_callback_ = progress_callback; | |
48 success_callback_ = success_callback; | |
49 error_callback_ = error_callback; | |
50 | |
51 if (!Send(new ChromeUtilityMsg_ImageWriter_Verify(source, target))) { | |
52 DLOG(ERROR) << "Unable to send Verify message to Utility Process."; | |
53 message_loop_proxy_->PostTask( | |
54 FROM_HERE, base::Bind(error_callback_, "IPC communication failed")); | |
55 } | |
56 } | |
57 | |
58 void ImageWriterClient::Cancel(const CancelCallback& cancel_callback) { | |
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
60 | |
61 if (!utility_process_host_) { | |
62 // If we haven't connected, there is nothing to cancel. | |
63 message_loop_proxy_->PostTask(FROM_HERE, cancel_callback); | |
64 return; | |
65 } | |
66 | |
67 cancel_callback_ = cancel_callback; | |
68 | |
69 if (!Send(new ChromeUtilityMsg_ImageWriter_Cancel())) { | |
70 DLOG(ERROR) << "Unable to send Cancel message to Utility Process."; | |
71 } | |
72 } | |
73 | |
74 void ImageWriterClient::Shutdown() { | |
75 if (utility_process_host_ && | |
Lei Zhang
2014/02/15 00:26:59
not needed, already done in Send()
Drew Haven
2014/02/15 01:23:58
Done.
| |
76 Send(new ChromeUtilityMsg_ImageWriter_Cancel())) { | |
77 utility_process_host_->EndBatchMode(); | |
78 } | |
79 } | |
80 | |
81 void ImageWriterClient::StartHost() { | |
82 if (!utility_process_host_) { | |
83 scoped_refptr<base::SequencedTaskRunner> task_runner = | |
84 base::MessageLoop::current()->message_loop_proxy(); | |
Lei Zhang
2014/02/15 00:26:59
base::MessageLoopProxy::current() ?
Drew Haven
2014/02/15 01:23:58
Done.
| |
85 utility_process_host_ = content::UtilityProcessHost::Create( | |
86 this, task_runner.get())->AsWeakPtr(); | |
87 | |
88 #if defined(OS_WIN) | |
89 utility_process_host_->ElevatePrivileges(); | |
90 #else | |
91 utility_process_host_->DisableSandbox(); | |
92 #endif | |
93 utility_process_host_->StartBatchMode(); | |
94 utility_process_host_->DisableSandbox(); | |
95 } | |
96 } | |
97 | |
98 void ImageWriterClient::OnProcessCrashed(int exit_code) { | |
99 message_loop_proxy_->PostTask( | |
100 FROM_HERE, base::Bind(error_callback_, "Utility process crashed.")); | |
101 } | |
102 | |
103 void ImageWriterClient::OnProcessLaunchFailed() { | |
104 message_loop_proxy_->PostTask( | |
105 FROM_HERE, base::Bind(error_callback_, "Process launch failed.")); | |
106 } | |
107 | |
108 bool ImageWriterClient::OnMessageReceived(const IPC::Message& message) { | |
109 bool handled = true; | |
110 IPC_BEGIN_MESSAGE_MAP(ImageWriterClient, message) | |
111 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Succeeded, | |
112 OnWriteImageSucceeded) | |
113 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Cancelled, | |
114 OnWriteImageCancelled) | |
115 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Failed, | |
116 OnWriteImageFailed) | |
117 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_ImageWriter_Progress, | |
118 OnWriteImageProgress) | |
119 IPC_MESSAGE_UNHANDLED(handled = false) | |
120 IPC_END_MESSAGE_MAP() | |
121 return handled; | |
122 } | |
123 | |
124 bool ImageWriterClient::Send(IPC::Message* msg) { | |
125 return utility_process_host_ && utility_process_host_->Send(msg); | |
126 } | |
127 | |
128 void ImageWriterClient::OnWriteImageSucceeded() { | |
129 if (!success_callback_.is_null()) { | |
130 message_loop_proxy_->PostTask(FROM_HERE, success_callback_); | |
131 } | |
132 } | |
133 | |
134 void ImageWriterClient::OnWriteImageCancelled() { | |
135 if (!cancel_callback_.is_null()) { | |
136 message_loop_proxy_->PostTask(FROM_HERE, cancel_callback_); | |
137 } | |
138 } | |
139 | |
140 void ImageWriterClient::OnWriteImageFailed(const std::string& message) { | |
141 if (!error_callback_.is_null()) { | |
142 message_loop_proxy_->PostTask(FROM_HERE, | |
143 base::Bind(error_callback_, message)); | |
144 } | |
145 } | |
146 | |
147 void ImageWriterClient::OnWriteImageProgress(int64 progress) { | |
148 if (!progress_callback_.is_null()) { | |
149 message_loop_proxy_->PostTask(FROM_HERE, | |
150 base::Bind(progress_callback_, progress)); | |
151 } | |
152 } | |
OLD | NEW |