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 #ifndef CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/md5.h" | 10 #include "base/md5.h" |
11 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
12 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
13 #include "base/task/cancelable_task_tracker.h" | 13 #include "base/task/cancelable_task_tracker.h" |
14 #include "base/timer/timer.h" | 14 #include "base/timer/timer.h" |
| 15 #include "chrome/browser/extensions/api/image_writer_private/image_writer_utilit
y_client.h" |
15 #include "chrome/common/extensions/api/image_writer_private.h" | 16 #include "chrome/common/extensions/api/image_writer_private.h" |
16 #include "third_party/zlib/google/zip_reader.h" | 17 #include "third_party/zlib/google/zip_reader.h" |
17 | 18 |
18 namespace image_writer_api = extensions::api::image_writer_private; | 19 namespace image_writer_api = extensions::api::image_writer_private; |
19 | 20 |
20 namespace base { | 21 namespace base { |
21 class FilePath; | 22 class FilePath; |
22 } // namespace base | 23 } // namespace base |
23 | 24 |
24 namespace extensions { | 25 namespace extensions { |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 // all callbacks have completed. | 64 // all callbacks have completed. |
64 void Cancel(); | 65 void Cancel(); |
65 | 66 |
66 // Aborts the operation, cancelling it and generating an error. | 67 // Aborts the operation, cancelling it and generating an error. |
67 void Abort(); | 68 void Abort(); |
68 | 69 |
69 // Informational getters. | 70 // Informational getters. |
70 int GetProgress(); | 71 int GetProgress(); |
71 image_writer_api::Stage GetStage(); | 72 image_writer_api::Stage GetStage(); |
72 | 73 |
| 74 #if !defined(OS_CHROMEOS) |
| 75 // Set an ImageWriterClient to use. Should be called only when testing. |
| 76 void SetUtilityClientForTesting( |
| 77 scoped_refptr<ImageWriterUtilityClient> client); |
| 78 #endif |
| 79 |
73 protected: | 80 protected: |
74 virtual ~Operation(); | 81 virtual ~Operation(); |
75 | 82 |
76 // This function should be overriden by subclasses to set up the work of the | 83 // This function should be overriden by subclasses to set up the work of the |
77 // operation. It will be called from Start(). | 84 // operation. It will be called from Start(). |
78 virtual void StartImpl() = 0; | 85 virtual void StartImpl() = 0; |
79 | 86 |
80 // Unzips the current file if it ends in ".zip". The current_file will be set | 87 // Unzips the current file if it ends in ".zip". The current_file will be set |
81 // to the unzipped file. | 88 // to the unzipped file. |
82 void Unzip(const base::Closure& continuation); | 89 void Unzip(const base::Closure& continuation); |
(...skipping 20 matching lines...) Expand all Loading... |
103 void SetStage(image_writer_api::Stage stage); | 110 void SetStage(image_writer_api::Stage stage); |
104 | 111 |
105 // Can be queried to safely determine if the operation has been cancelled. | 112 // Can be queried to safely determine if the operation has been cancelled. |
106 bool IsCancelled(); | 113 bool IsCancelled(); |
107 | 114 |
108 // Adds a callback that will be called during clean-up, whether the operation | 115 // Adds a callback that will be called during clean-up, whether the operation |
109 // is aborted, encounters and error, or finishes successfully. These | 116 // is aborted, encounters and error, or finishes successfully. These |
110 // functions will be run on the FILE thread. | 117 // functions will be run on the FILE thread. |
111 void AddCleanUpFunction(const base::Closure& callback); | 118 void AddCleanUpFunction(const base::Closure& callback); |
112 | 119 |
| 120 // Completes the current operation (progress set to 100) and runs the |
| 121 // continuation. |
| 122 void CompleteAndContinue(const base::Closure& continuation); |
| 123 |
113 // If |file_size| is non-zero, only |file_size| bytes will be read from file, | 124 // If |file_size| is non-zero, only |file_size| bytes will be read from file, |
114 // otherwise the entire file will be read. | 125 // otherwise the entire file will be read. |
115 // |progress_scale| is a percentage to which the progress will be scale, e.g. | 126 // |progress_scale| is a percentage to which the progress will be scale, e.g. |
116 // a scale of 50 means it will increment from 0 to 50 over the course of the | 127 // a scale of 50 means it will increment from 0 to 50 over the course of the |
117 // sum. |progress_offset| is an percentage that will be added to the progress | 128 // sum. |progress_offset| is an percentage that will be added to the progress |
118 // of the MD5 sum before updating |progress_| but after scaling. | 129 // of the MD5 sum before updating |progress_| but after scaling. |
119 void GetMD5SumOfFile( | 130 void GetMD5SumOfFile( |
120 const base::FilePath& file, | 131 const base::FilePath& file, |
121 int64 file_size, | 132 int64 file_size, |
122 int progress_offset, | 133 int progress_offset, |
123 int progress_scale, | 134 int progress_scale, |
124 const base::Callback<void(const std::string&)>& callback); | 135 const base::Callback<void(const std::string&)>& callback); |
125 | 136 |
126 base::WeakPtr<OperationManager> manager_; | 137 base::WeakPtr<OperationManager> manager_; |
127 const ExtensionId extension_id_; | 138 const ExtensionId extension_id_; |
128 | 139 |
129 base::FilePath image_path_; | 140 base::FilePath image_path_; |
130 base::FilePath device_path_; | 141 base::FilePath device_path_; |
131 | 142 |
132 // Temporary directory to store files as we go. | 143 // Temporary directory to store files as we go. |
133 base::ScopedTempDir temp_dir_; | 144 base::ScopedTempDir temp_dir_; |
134 | 145 |
135 private: | 146 private: |
136 friend class base::RefCountedThreadSafe<Operation>; | 147 friend class base::RefCountedThreadSafe<Operation>; |
137 | 148 |
138 // TODO(haven): Clean up these switches. http://crbug.com/292956 | 149 #if !defined(OS_CHROMEOS) |
139 #if defined(OS_LINUX) && !defined(CHROMEOS) | 150 // Ensures the client is started. This may be called many times but will only |
140 void WriteChunk(const int64& bytes_written, | 151 // instantiate one client which should exist for the lifetime of the |
141 const int64& total_size, | 152 // Operation. |
142 const base::Closure& continuation); | 153 void StartUtilityClient(); |
143 void WriteComplete(const base::Closure& continuation); | |
144 | 154 |
145 void VerifyWriteChunk(const int64& bytes_written, | 155 // Stops the client. This must be called to ensure the utility process can |
146 const int64& total_size, | 156 // shutdown. |
147 const base::Closure& continuation); | 157 void StopUtilityClient(); |
148 void VerifyWriteComplete(const base::Closure& continuation); | |
149 | 158 |
150 base::PlatformFile image_file_; | 159 // Reports progress from the client, transforming from bytes to percentage. |
151 base::PlatformFile device_file_; | 160 virtual void WriteImageProgress(int64 total_bytes, int64 curr_bytes); |
| 161 |
| 162 scoped_refptr<ImageWriterUtilityClient> image_writer_client_; |
152 #endif | 163 #endif |
153 | 164 |
154 #if defined(OS_CHROMEOS) | 165 #if defined(OS_CHROMEOS) |
155 void StartWriteOnUIThread(const base::Closure& continuation); | 166 void StartWriteOnUIThread(const base::Closure& continuation); |
156 | 167 |
157 void OnBurnFinished(const base::Closure& continuation, | 168 void OnBurnFinished(const base::Closure& continuation, |
158 const std::string& target_path, | 169 const std::string& target_path, |
159 bool success, | 170 bool success, |
160 const std::string& error); | 171 const std::string& error); |
161 void OnBurnProgress(const std::string& target_path, | 172 void OnBurnProgress(const std::string& target_path, |
162 int64 num_bytes_burnt, | 173 int64 num_bytes_burnt, |
163 int64 total_size); | 174 int64 total_size); |
164 void OnBurnError(); | 175 void OnBurnError(); |
165 #endif | 176 #endif |
166 | 177 |
167 // Incrementally calculates the MD5 sum of a file. | 178 // Incrementally calculates the MD5 sum of a file. |
168 void MD5Chunk(const base::PlatformFile& file, | 179 void MD5Chunk(const base::PlatformFile& file, |
169 int64 bytes_processed, | 180 int64 bytes_processed, |
170 int64 bytes_total, | 181 int64 bytes_total, |
171 int progress_offset, | 182 int progress_offset, |
172 int progress_scale, | 183 int progress_scale, |
173 const base::Callback<void(const std::string&)>& callback); | 184 const base::Callback<void(const std::string&)>& callback); |
174 | 185 |
175 // Callbacks for zip::ZipReader. | 186 // Callbacks for zip::ZipReader. |
176 void OnUnzipSuccess(const base::Closure& continuation); | |
177 void OnUnzipFailure(); | 187 void OnUnzipFailure(); |
178 void OnUnzipProgress(int64 total_bytes, int64 progress_bytes); | 188 void OnUnzipProgress(int64 total_bytes, int64 progress_bytes); |
179 | 189 |
180 // Runs all cleanup functions. | 190 // Runs all cleanup functions. |
181 void CleanUp(); | 191 void CleanUp(); |
182 | 192 |
183 // |stage_| and |progress_| are owned by the FILE thread, use |SetStage| and | 193 // |stage_| and |progress_| are owned by the FILE thread, use |SetStage| and |
184 // |SetProgress| to update. Progress should be in the interval [0,100] | 194 // |SetProgress| to update. Progress should be in the interval [0,100] |
185 image_writer_api::Stage stage_; | 195 image_writer_api::Stage stage_; |
186 int progress_; | 196 int progress_; |
187 | 197 |
188 // MD5 contexts don't play well with smart pointers. Just going to allocate | 198 // MD5 contexts don't play well with smart pointers. Just going to allocate |
189 // memory here. This requires that we only do one MD5 sum at a time. | 199 // memory here. This requires that we only do one MD5 sum at a time. |
190 base::MD5Context md5_context_; | 200 base::MD5Context md5_context_; |
191 | 201 |
192 // Zip reader for unzip operations. | 202 // Zip reader for unzip operations. |
193 zip::ZipReader zip_reader_; | 203 zip::ZipReader zip_reader_; |
194 | 204 |
195 // CleanUp operations that must be run. All these functions are run on the | 205 // CleanUp operations that must be run. All these functions are run on the |
196 // FILE thread. | 206 // FILE thread. |
197 std::vector<base::Closure> cleanup_functions_; | 207 std::vector<base::Closure> cleanup_functions_; |
198 }; | 208 }; |
199 | 209 |
200 } // namespace image_writer | 210 } // namespace image_writer |
201 } // namespace extensions | 211 } // namespace extensions |
202 | 212 |
203 #endif // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_H_ | 213 #endif // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_H_ |
OLD | NEW |