OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/file_enumerator.h" | 6 #include "base/files/file_enumerator.h" |
7 #include "base/threading/worker_pool.h" | 7 #include "base/threading/worker_pool.h" |
8 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" | 8 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" |
9 #include "chrome/browser/extensions/api/image_writer_private/operation.h" | 9 #include "chrome/browser/extensions/api/image_writer_private/operation.h" |
10 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h " | 10 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h " |
11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
12 | 12 |
13 namespace extensions { | 13 namespace extensions { |
14 namespace image_writer { | 14 namespace image_writer { |
15 | 15 |
16 using content::BrowserThread; | 16 using content::BrowserThread; |
17 | 17 |
18 namespace { | |
19 | |
20 const int kMD5BufferSize = 1024; | 18 const int kMD5BufferSize = 1024; |
21 | 19 |
22 void RemoveTempDirectory(const base::FilePath path) { | |
23 base::DeleteFile(path, true); | |
24 } | |
25 | |
26 } // namespace | |
27 | |
28 Operation::Operation(base::WeakPtr<OperationManager> manager, | 20 Operation::Operation(base::WeakPtr<OperationManager> manager, |
29 const ExtensionId& extension_id, | 21 const ExtensionId& extension_id, |
30 const std::string& storage_unit_id) | 22 const std::string& device_path) |
31 : manager_(manager), | 23 : manager_(manager), |
32 extension_id_(extension_id), | 24 extension_id_(extension_id), |
33 storage_unit_id_(storage_unit_id), | 25 #if defined(OS_WIN) |
34 verify_write_(true), | 26 device_path_(base::FilePath::FromUTF8Unsafe(device_path)), |
27 #else | |
28 device_path_(device_path), | |
29 #endif | |
35 stage_(image_writer_api::STAGE_UNKNOWN), | 30 stage_(image_writer_api::STAGE_UNKNOWN), |
36 progress_(0) { | 31 progress_(0) { |
32 if (!temp_dir_.CreateUniqueTempDir()) { | |
33 Error(error::kTempDirError); | |
34 Cancel(); | |
35 } | |
37 } | 36 } |
38 | 37 |
39 Operation::~Operation() { | 38 Operation::~Operation() { |
40 } | 39 } |
41 | 40 |
42 void Operation::Cancel() { | 41 void Operation::Cancel() { |
43 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
44 | 43 |
45 DVLOG(1) << "Cancelling image writing operation for ext: " << extension_id_; | 44 DVLOG(1) << "Cancelling image writing operation for ext: " << extension_id_; |
46 | 45 |
47 stage_ = image_writer_api::STAGE_NONE; | 46 stage_ = image_writer_api::STAGE_NONE; |
48 | 47 |
49 CleanUp(); | 48 CleanUp(); |
50 } | 49 } |
51 | 50 |
52 void Operation::Abort() { | 51 void Operation::Abort() { |
53 Error(error::kAborted); | 52 Error(error::kAborted); |
54 } | 53 } |
55 | 54 |
56 int Operation::GetProgress() { | 55 int Operation::GetProgress() { |
57 return progress_; | 56 return progress_; |
58 } | 57 } |
59 | 58 |
60 image_writer_api::Stage Operation::GetStage() { | 59 image_writer_api::Stage Operation::GetStage() { |
61 return stage_; | 60 return stage_; |
62 } | 61 } |
63 | 62 |
63 void Operation::Unzip(const base::Closure& continuation) { | |
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
65 if (IsCancelled()) { | |
66 return; | |
67 } | |
68 | |
69 if (image_path_.Extension() != FILE_PATH_LITERAL(".zip")) { | |
70 continuation.Run(); | |
71 return; | |
72 } | |
73 | |
74 SetStage(image_writer_api::STAGE_UNZIP); | |
75 | |
76 if (!(zip_reader_.Open(image_path_) && | |
77 zip_reader_.AdvanceToNextEntry() && | |
78 zip_reader_.OpenCurrentEntryInZip())) { | |
79 DLOG(ERROR) << "Failed to open zip file."; | |
80 Error(error::kUnzipGenericError); | |
81 return; | |
82 } | |
83 | |
84 if (zip_reader_.HasMore()) { | |
85 DLOG(ERROR) << "Image zip has more than one file."; | |
86 Error(error::kUnzipInvalidArchive); | |
87 return; | |
88 } | |
89 | |
90 // Create a new target to unzip to. The original file is opened by the | |
91 // zip_reader_. | |
92 if (!base::CreateTemporaryFileInDir(temp_dir_.path(), &image_path_)) { | |
93 DLOG(ERROR) << "Failed create temporary unzip target in " | |
94 << temp_dir_.path().value(); | |
95 Error(error::kTempDirError); | |
96 return; | |
97 } | |
98 | |
99 zip_reader_.ExtractCurrentEntryToFilePathAsync( | |
100 image_path_, | |
101 base::Bind(&Operation::OnUnzipSuccess, this, continuation), | |
102 base::Bind(&Operation::OnUnzipFailure, this), | |
103 base::Bind(&Operation::OnUnzipProgress, | |
104 this, | |
105 zip_reader_.current_entry_info()->original_size())); | |
106 } | |
107 | |
108 void Operation::Finish() { | |
109 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | |
110 BrowserThread::PostTask(BrowserThread::FILE, | |
111 FROM_HERE, | |
112 base::Bind(&Operation::Finish, this)); | |
113 return; | |
114 } | |
115 DVLOG(1) << "Write operation complete."; | |
116 | |
117 CleanUp(); | |
118 | |
119 BrowserThread::PostTask( | |
120 BrowserThread::UI, | |
121 FROM_HERE, | |
122 base::Bind(&OperationManager::OnComplete, | |
123 manager_, | |
124 extension_id_)); | |
125 } | |
126 | |
64 void Operation::Error(const std::string& error_message) { | 127 void Operation::Error(const std::string& error_message) { |
65 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | 128 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { |
66 BrowserThread::PostTask(BrowserThread::FILE, | 129 BrowserThread::PostTask(BrowserThread::FILE, |
67 FROM_HERE, | 130 FROM_HERE, |
68 base::Bind(&Operation::Error, this, error_message)); | 131 base::Bind(&Operation::Error, this, error_message)); |
69 return; | 132 return; |
70 } | 133 } |
71 | 134 |
72 BrowserThread::PostTask( | 135 BrowserThread::PostTask( |
73 BrowserThread::UI, | 136 BrowserThread::UI, |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
130 BrowserThread::PostTask( | 193 BrowserThread::PostTask( |
131 BrowserThread::UI, | 194 BrowserThread::UI, |
132 FROM_HERE, | 195 FROM_HERE, |
133 base::Bind(&OperationManager::OnProgress, | 196 base::Bind(&OperationManager::OnProgress, |
134 manager_, | 197 manager_, |
135 extension_id_, | 198 extension_id_, |
136 stage_, | 199 stage_, |
137 progress_)); | 200 progress_)); |
138 } | 201 } |
139 | 202 |
140 void Operation::Finish() { | |
141 if (!BrowserThread::CurrentlyOn(BrowserThread::FILE)) { | |
142 BrowserThread::PostTask(BrowserThread::FILE, | |
143 FROM_HERE, | |
144 base::Bind(&Operation::Finish, this)); | |
145 return; | |
146 } | |
147 DVLOG(1) << "Write operation complete."; | |
148 | |
149 CleanUp(); | |
150 | |
151 BrowserThread::PostTask( | |
152 BrowserThread::UI, | |
153 FROM_HERE, | |
154 base::Bind(&OperationManager::OnComplete, | |
155 manager_, | |
156 extension_id_)); | |
157 } | |
158 | |
159 bool Operation::IsCancelled() { | 203 bool Operation::IsCancelled() { |
160 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
161 | 205 |
162 return stage_ == image_writer_api::STAGE_NONE; | 206 return stage_ == image_writer_api::STAGE_NONE; |
163 } | 207 } |
164 | 208 |
165 void Operation::AddCleanUpFunction(base::Closure callback) { | 209 void Operation::AddCleanUpFunction(const base::Closure& callback) { |
166 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 210 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
167 | 211 |
168 cleanup_functions_.push_back(callback); | 212 cleanup_functions_.push_back(callback); |
169 } | 213 } |
170 | 214 |
171 void Operation::CleanUp() { | |
172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
173 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); | |
174 it != cleanup_functions_.end(); | |
175 ++it) { | |
176 it->Run(); | |
177 } | |
178 cleanup_functions_.clear(); | |
179 } | |
180 | |
181 void Operation::UnzipStart(scoped_ptr<base::FilePath> zip_path) { | |
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
183 if (IsCancelled()) { | |
184 return; | |
185 } | |
186 | |
187 DVLOG(1) << "Starting unzip stage for " << zip_path->value(); | |
188 | |
189 SetStage(image_writer_api::STAGE_UNZIP); | |
190 | |
191 base::FilePath tmp_dir; | |
192 if (!base::CreateTemporaryDirInDir(zip_path->DirName(), | |
193 FILE_PATH_LITERAL("image_writer"), | |
194 &tmp_dir)) { | |
195 Error(error::kTempDirError); | |
196 return; | |
197 } | |
198 | |
199 AddCleanUpFunction(base::Bind(&RemoveTempDirectory, tmp_dir)); | |
200 | |
201 if (!base::CreateTemporaryFileInDir(tmp_dir, &image_path_)) { | |
202 DLOG(ERROR) << "Failed create temporary unzip target in " | |
203 << tmp_dir.value(); | |
204 Error(error::kTempDirError); | |
205 return; | |
206 } | |
207 | |
208 if (!(zip_reader_.Open(*zip_path) && | |
209 zip_reader_.AdvanceToNextEntry() && | |
210 zip_reader_.OpenCurrentEntryInZip())) { | |
211 DLOG(ERROR) << "Failed to open zip file."; | |
212 Error(error::kUnzipGenericError); | |
213 return; | |
214 } | |
215 | |
216 if (zip_reader_.HasMore()) { | |
217 DLOG(ERROR) << "Image zip has more than one file."; | |
218 Error(error::kUnzipInvalidArchive); | |
219 return; | |
220 } | |
221 | |
222 zip_reader_.ExtractCurrentEntryToFilePathAsync( | |
223 image_path_, | |
224 base::Bind(&Operation::OnUnzipSuccess, this), | |
225 base::Bind(&Operation::OnUnzipFailure, this), | |
226 base::Bind(&Operation::OnUnzipProgress, | |
227 this, | |
228 zip_reader_.current_entry_info()->original_size())); | |
229 } | |
230 | |
231 void Operation::GetMD5SumOfFile( | 215 void Operation::GetMD5SumOfFile( |
232 scoped_ptr<base::FilePath> file_path, | 216 const base::FilePath file_path, |
233 int64 file_size, | 217 int64 file_size, |
234 int progress_offset, | 218 int progress_offset, |
235 int progress_scale, | 219 int progress_scale, |
236 const base::Callback<void(scoped_ptr<std::string>)>& callback) { | 220 const base::Callback<void(const std::string&)>& callback) { |
237 if (IsCancelled()) { | 221 if (IsCancelled()) { |
238 return; | 222 return; |
239 } | 223 } |
240 | 224 |
241 base::MD5Init(&md5_context_); | 225 base::MD5Init(&md5_context_); |
242 scoped_ptr<image_writer_utils::ImageReader> reader( | |
243 new image_writer_utils::ImageReader()); | |
244 | 226 |
245 if (!reader->Open(*file_path)) { | 227 base::PlatformFile file = base::CreatePlatformFile( |
228 file_path, | |
229 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, | |
230 NULL, | |
231 NULL); | |
232 if (file == base::kInvalidPlatformFileValue) { | |
246 Error(error::kImageOpenError); | 233 Error(error::kImageOpenError); |
247 return; | 234 return; |
248 } | 235 } |
236 | |
249 if (file_size <= 0) { | 237 if (file_size <= 0) { |
250 file_size = reader->GetSize(); | 238 if (!base::GetFileSize(file_path, &file_size)) { |
239 Error(error::kImageOpenError); | |
240 return; | |
241 } | |
251 } | 242 } |
252 | 243 |
253 BrowserThread::PostTask(BrowserThread::FILE, | 244 BrowserThread::PostTask(BrowserThread::FILE, |
254 FROM_HERE, | 245 FROM_HERE, |
255 base::Bind(&Operation::MD5Chunk, | 246 base::Bind(&Operation::MD5Chunk, |
256 this, | 247 this, |
257 base::Passed(&reader), | 248 file, |
258 0, | 249 0, |
259 file_size, | 250 file_size, |
260 progress_offset, | 251 progress_offset, |
261 progress_scale, | 252 progress_scale, |
262 callback)); | 253 callback)); |
263 } | 254 } |
264 | 255 |
265 void Operation::MD5Chunk( | 256 void Operation::MD5Chunk( |
266 scoped_ptr<image_writer_utils::ImageReader> reader, | 257 base::PlatformFile file, |
267 int64 bytes_processed, | 258 int64 bytes_processed, |
268 int64 bytes_total, | 259 int64 bytes_total, |
269 int progress_offset, | 260 int progress_offset, |
270 int progress_scale, | 261 int progress_scale, |
271 const base::Callback<void(scoped_ptr<std::string>)>& callback) { | 262 const base::Callback<void(const std::string&)>& callback) { |
272 if (IsCancelled()) { | 263 if (IsCancelled()) { |
273 reader->Close(); | 264 base::ClosePlatformFile(file); |
tbarzic
2014/02/07 22:06:42
I think ScopedPlatformFileCloser would be useful h
Drew Haven
2014/02/11 00:50:15
I just discovered that. It does mean that I need
| |
274 return; | 265 return; |
275 } | 266 } |
276 | 267 |
277 char buffer[kMD5BufferSize]; | |
278 int len; | 268 int len; |
269 scoped_ptr<char[]> buffer(new char[kMD5BufferSize]); | |
279 | 270 |
280 if (bytes_total - bytes_processed <= kMD5BufferSize) { | 271 if (bytes_total - bytes_processed <= kMD5BufferSize) { |
281 len = reader->Read(buffer, bytes_total - bytes_processed); | 272 len = base::ReadPlatformFile(file, |
273 bytes_processed, | |
274 buffer.get(), | |
275 bytes_total - bytes_processed); | |
282 } else { | 276 } else { |
283 len = reader->Read(buffer, kMD5BufferSize); | 277 len = base::ReadPlatformFile(file, |
278 bytes_processed, | |
279 buffer.get(), | |
280 kMD5BufferSize); | |
284 } | 281 } |
285 | 282 |
286 if (len > 0) { | 283 if (len > 0) { |
287 base::MD5Update(&md5_context_, base::StringPiece(buffer, len)); | 284 base::MD5Update(&md5_context_, base::StringPiece(buffer.get(), len)); |
288 int percent_prev = (bytes_processed * progress_scale + progress_offset) / | 285 int percent_prev = (bytes_processed * progress_scale + progress_offset) / |
289 (bytes_total); | 286 bytes_total; |
290 int percent_curr = ((bytes_processed + len) * progress_scale + | 287 int percent_curr = ((bytes_processed + len) * progress_scale + |
291 progress_offset) / | 288 progress_offset) / |
292 (bytes_total); | 289 bytes_total; |
293 if (percent_curr > percent_prev) { | 290 if (percent_curr > percent_prev) { |
294 SetProgress(progress_); | 291 SetProgress(percent_curr); |
295 } | 292 } |
296 | 293 |
297 BrowserThread::PostTask(BrowserThread::FILE, | 294 BrowserThread::PostTask(BrowserThread::FILE, |
298 FROM_HERE, | 295 FROM_HERE, |
299 base::Bind(&Operation::MD5Chunk, | 296 base::Bind(&Operation::MD5Chunk, |
300 this, | 297 this, |
301 base::Passed(&reader), | 298 file, |
302 bytes_processed + len, | 299 bytes_processed + len, |
303 bytes_total, | 300 bytes_total, |
304 progress_offset, | 301 progress_offset, |
305 progress_scale, | 302 progress_scale, |
306 callback)); | 303 callback)); |
304 // Skip closing the file. | |
305 return; | |
307 } else if (len == 0) { | 306 } else if (len == 0) { |
308 if (bytes_processed + len < bytes_total) { | 307 if (bytes_processed + len < bytes_total) { |
309 reader->Close(); | |
310 Error(error::kHashReadError); | 308 Error(error::kHashReadError); |
311 } else { | 309 } else { |
312 base::MD5Digest digest; | 310 base::MD5Digest digest; |
313 base::MD5Final(&digest, &md5_context_); | 311 base::MD5Final(&digest, &md5_context_); |
314 scoped_ptr<std::string> hash( | 312 std::string hash = base::MD5DigestToBase16(digest); |
315 new std::string(base::MD5DigestToBase16(digest))); | 313 callback.Run(hash); |
316 callback.Run(hash.Pass()); | |
317 } | 314 } |
318 } else { // len < 0 | 315 } else { // len < 0 |
319 reader->Close(); | |
320 Error(error::kHashReadError); | 316 Error(error::kHashReadError); |
321 } | 317 } |
318 base::ClosePlatformFile(file); | |
322 } | 319 } |
323 | 320 |
324 void Operation::OnUnzipSuccess() { | 321 void Operation::OnUnzipSuccess(const base::Closure& continuation) { |
325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 322 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
326 SetProgress(kProgressComplete); | 323 SetProgress(kProgressComplete); |
327 WriteStart(); | 324 continuation.Run(); |
328 } | 325 } |
329 | 326 |
330 void Operation::OnUnzipFailure() { | 327 void Operation::OnUnzipFailure() { |
331 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 328 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
332 Error(error::kUnzipGenericError); | 329 Error(error::kUnzipGenericError); |
333 } | 330 } |
334 | 331 |
335 void Operation::OnUnzipProgress(int64 total_bytes, int64 progress_bytes) { | 332 void Operation::OnUnzipProgress(int64 total_bytes, int64 progress_bytes) { |
336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 333 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
337 | 334 |
338 int progress_percent = 100 * progress_bytes / total_bytes; | 335 int progress_percent = 100 * progress_bytes / total_bytes; |
339 SetProgress(progress_percent); | 336 SetProgress(progress_percent); |
340 } | 337 } |
341 | 338 |
339 void Operation::CleanUp() { | |
340 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | |
341 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); | |
342 it != cleanup_functions_.end(); | |
343 ++it) { | |
344 it->Run(); | |
345 } | |
346 cleanup_functions_.clear(); | |
347 } | |
348 | |
342 } // namespace image_writer | 349 } // namespace image_writer |
343 } // namespace extensions | 350 } // namespace extensions |
OLD | NEW |