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