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 "chrome/browser/extensions/api/image_writer_private/operation.h" | 5 #include "chrome/browser/extensions/api/image_writer_private/operation.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/file_enumerator.h" | 8 #include "base/files/file_enumerator.h" |
9 #include "base/lazy_instance.h" | |
10 #include "base/threading/worker_pool.h" | 9 #include "base/threading/worker_pool.h" |
11 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" | 10 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" |
12 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" | 11 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" |
13 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
14 | 13 |
15 namespace extensions { | 14 namespace extensions { |
16 namespace image_writer { | 15 namespace image_writer { |
17 | 16 |
18 using content::BrowserThread; | 17 using content::BrowserThread; |
19 | 18 |
20 const int kMD5BufferSize = 1024; | 19 const int kMD5BufferSize = 1024; |
21 #if defined(OS_CHROMEOS) | 20 #if defined(OS_CHROMEOS) |
22 // Chrome OS only has a 1 GB temporary partition. This is too small to hold our | 21 // Chrome OS only has a 1 GB temporary partition. This is too small to hold our |
23 // unzipped image. Fortunately we mount part of the temporary partition under | 22 // unzipped image. Fortunately we mount part of the temporary partition under |
24 // /var/tmp. | 23 // /var/tmp. |
25 const char kChromeOSTempRoot[] = "/var/tmp"; | 24 const char kChromeOSTempRoot[] = "/var/tmp"; |
26 #endif | 25 #endif |
27 | 26 |
28 #if !defined(OS_CHROMEOS) | |
29 static base::LazyInstance<scoped_refptr<ImageWriterUtilityClient> > | |
30 g_utility_client = LAZY_INSTANCE_INITIALIZER; | |
31 #endif | |
32 | |
33 Operation::Operation(base::WeakPtr<OperationManager> manager, | 27 Operation::Operation(base::WeakPtr<OperationManager> manager, |
34 const ExtensionId& extension_id, | 28 const ExtensionId& extension_id, |
35 const std::string& device_path) | 29 const std::string& device_path) |
36 : manager_(manager), | 30 : manager_(manager), |
37 extension_id_(extension_id), | 31 extension_id_(extension_id), |
38 #if defined(OS_WIN) | 32 #if defined(OS_WIN) |
39 device_path_(base::FilePath::FromUTF8Unsafe(device_path)), | 33 device_path_(base::FilePath::FromUTF8Unsafe(device_path)), |
40 #else | 34 #else |
41 device_path_(device_path), | 35 device_path_(device_path), |
42 #endif | 36 #endif |
(...skipping 17 matching lines...) Expand all Loading... |
60 | 54 |
61 int Operation::GetProgress() { | 55 int Operation::GetProgress() { |
62 return progress_; | 56 return progress_; |
63 } | 57 } |
64 | 58 |
65 image_writer_api::Stage Operation::GetStage() { | 59 image_writer_api::Stage Operation::GetStage() { |
66 return stage_; | 60 return stage_; |
67 } | 61 } |
68 | 62 |
69 #if !defined(OS_CHROMEOS) | 63 #if !defined(OS_CHROMEOS) |
70 // static | |
71 void Operation::SetUtilityClientForTesting( | 64 void Operation::SetUtilityClientForTesting( |
72 scoped_refptr<ImageWriterUtilityClient> client) { | 65 scoped_refptr<ImageWriterUtilityClient> client) { |
73 g_utility_client.Get() = client; | 66 image_writer_client_ = client; |
| 67 AddCleanUpFunction( |
| 68 base::Bind(&ImageWriterUtilityClient::Shutdown, image_writer_client_)); |
74 } | 69 } |
75 #endif | 70 #endif |
76 | 71 |
77 void Operation::Start() { | 72 void Operation::Start() { |
78 #if defined(OS_CHROMEOS) | 73 #if defined(OS_CHROMEOS) |
79 if (!temp_dir_.CreateUniqueTempDirUnderPath( | 74 if (!temp_dir_.CreateUniqueTempDirUnderPath( |
80 base::FilePath(kChromeOSTempRoot))) { | 75 base::FilePath(kChromeOSTempRoot))) { |
81 #else | 76 #else |
82 if (!temp_dir_.CreateUniqueTempDir()) { | 77 if (!temp_dir_.CreateUniqueTempDir()) { |
83 #endif | 78 #endif |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 | 237 |
243 void Operation::CompleteAndContinue(const base::Closure& continuation) { | 238 void Operation::CompleteAndContinue(const base::Closure& continuation) { |
244 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 239 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
245 SetProgress(kProgressComplete); | 240 SetProgress(kProgressComplete); |
246 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); | 241 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, continuation); |
247 } | 242 } |
248 | 243 |
249 #if !defined(OS_CHROMEOS) | 244 #if !defined(OS_CHROMEOS) |
250 void Operation::StartUtilityClient() { | 245 void Operation::StartUtilityClient() { |
251 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 246 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
252 if (g_utility_client.Get()) { | |
253 image_writer_client_ = g_utility_client.Get(); | |
254 return; | |
255 } | |
256 if (!image_writer_client_) { | 247 if (!image_writer_client_) { |
257 image_writer_client_ = new ImageWriterUtilityClient(); | 248 image_writer_client_ = new ImageWriterUtilityClient(); |
258 AddCleanUpFunction(base::Bind(&Operation::StopUtilityClient, this)); | 249 AddCleanUpFunction(base::Bind(&Operation::StopUtilityClient, this)); |
259 } | 250 } |
260 } | 251 } |
261 | 252 |
262 void Operation::StopUtilityClient() { | 253 void Operation::StopUtilityClient() { |
263 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 254 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
264 BrowserThread::PostTask( | 255 BrowserThread::PostTask( |
265 BrowserThread::IO, | 256 BrowserThread::IO, |
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); | 378 for (std::vector<base::Closure>::iterator it = cleanup_functions_.begin(); |
388 it != cleanup_functions_.end(); | 379 it != cleanup_functions_.end(); |
389 ++it) { | 380 ++it) { |
390 it->Run(); | 381 it->Run(); |
391 } | 382 } |
392 cleanup_functions_.clear(); | 383 cleanup_functions_.clear(); |
393 } | 384 } |
394 | 385 |
395 } // namespace image_writer | 386 } // namespace image_writer |
396 } // namespace extensions | 387 } // namespace extensions |
OLD | NEW |