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_platform_file_closer.h" | |
10 #include "base/files/scoped_temp_dir.h" | |
9 #include "base/md5.h" | 11 #include "base/md5.h" |
10 #include "base/memory/ref_counted_memory.h" | 12 #include "base/memory/ref_counted_memory.h" |
11 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
12 #include "base/task/cancelable_task_tracker.h" | 14 #include "base/task/cancelable_task_tracker.h" |
13 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
14 #include "chrome/browser/extensions/api/image_writer_private/image_writer_utils. 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 14 matching lines...) Expand all Loading... | |
39 // for advancing to the next stage and other UI interaction. The Run phase does | 40 // for advancing to the next stage and other UI interaction. The Run phase does |
40 // the work on the FILE thread and calls SendProgress or Error as appropriate. | 41 // the work on the FILE thread and calls SendProgress or Error as appropriate. |
41 class Operation : public base::RefCountedThreadSafe<Operation> { | 42 class Operation : public base::RefCountedThreadSafe<Operation> { |
42 public: | 43 public: |
43 typedef base::Callback<void(bool, const std::string&)> StartWriteCallback; | 44 typedef base::Callback<void(bool, const std::string&)> StartWriteCallback; |
44 typedef base::Callback<void(bool, const std::string&)> CancelWriteCallback; | 45 typedef base::Callback<void(bool, const std::string&)> CancelWriteCallback; |
45 typedef std::string ExtensionId; | 46 typedef std::string ExtensionId; |
46 | 47 |
47 Operation(base::WeakPtr<OperationManager> manager, | 48 Operation(base::WeakPtr<OperationManager> manager, |
48 const ExtensionId& extension_id, | 49 const ExtensionId& extension_id, |
49 const std::string& storage_unit_id); | 50 const std::string& device_path); |
50 | 51 |
51 // Starts the operation. | 52 // Starts the operation. |
52 virtual void Start() = 0; | 53 virtual void Start(); |
53 | 54 |
54 // Cancel the operation. This must be called to clean up internal state and | 55 // Cancel the operation. This must be called to clean up internal state and |
55 // cause the the operation to actually stop. It will not be destroyed until | 56 // cause the the operation to actually stop. It will not be destroyed until |
56 // all callbacks have completed. | 57 // all callbacks have completed. |
57 void Cancel(); | 58 void Cancel(); |
58 | 59 |
59 // Aborts the operation, cancelling it and generating an error. | 60 // Aborts the operation, cancelling it and generating an error. |
60 void Abort(); | 61 void Abort(); |
61 | 62 |
62 // Informational getters. | 63 // Informational getters. |
63 int GetProgress(); | 64 int GetProgress(); |
64 image_writer_api::Stage GetStage(); | 65 image_writer_api::Stage GetStage(); |
65 | 66 |
66 protected: | 67 protected: |
67 virtual ~Operation(); | 68 virtual ~Operation(); |
68 | 69 |
70 // Unzips the current file if it ends in ".zip". The current_file will be set | |
71 // to the unzipped file. | |
72 void Unzip(const base::Closure& continuation); | |
73 | |
74 // Writes the current file to device_path. | |
75 void Write(const base::Closure& continuation); | |
76 | |
77 // Verifies that the current file and device_path contents match. | |
78 void VerifyWrite(const base::Closure& continuation); | |
79 | |
80 // Completes the operation. | |
81 void Finish(); | |
82 | |
69 // Generates an error. | 83 // Generates an error. |
70 // |error_message| is used to create an OnWriteError event which is | 84 // |error_message| is used to create an OnWriteError event which is |
71 // sent to the extension | 85 // sent to the extension |
72 virtual void Error(const std::string& error_message); | 86 virtual void Error(const std::string& error_message); |
73 | 87 |
74 // Set |progress_| and send an event. Progress should be in the interval | 88 // Set |progress_| and send an event. Progress should be in the interval |
75 // [0,100] | 89 // [0,100] |
76 void SetProgress(int progress); | 90 void SetProgress(int progress); |
77 // Change to a new |stage_| and set |progress_| to zero. Triggers a progress | 91 // Change to a new |stage_| and set |progress_| to zero. Triggers a progress |
78 // event. | 92 // event. |
79 void SetStage(image_writer_api::Stage stage); | 93 void SetStage(image_writer_api::Stage stage); |
80 | 94 |
81 // Can be queried to safely determine if the operation has been cancelled. | 95 // Can be queried to safely determine if the operation has been cancelled. |
82 bool IsCancelled(); | 96 bool IsCancelled(); |
83 | 97 |
84 // Adds a callback that will be called during clean-up, whether the operation | 98 // Adds a callback that will be called during clean-up, whether the operation |
85 // is aborted, encounters and error, or finishes successfully. These | 99 // is aborted, encounters and error, or finishes successfully. These |
86 // functions will be run on the FILE thread. | 100 // functions will be run on the FILE thread. |
87 void AddCleanUpFunction(base::Closure); | 101 void AddCleanUpFunction(const base::Closure& callback); |
88 | |
89 void UnzipStart(scoped_ptr<base::FilePath> zip_file); | |
90 void WriteStart(); | |
91 void VerifyWriteStart(); | |
92 void Finish(); | |
93 | 102 |
94 // If |file_size| is non-zero, only |file_size| bytes will be read from file, | 103 // If |file_size| is non-zero, only |file_size| bytes will be read from file, |
95 // otherwise the entire file will be read. | 104 // otherwise the entire file will be read. |
96 // |progress_scale| is a percentage to which the progress will be scale, e.g. | 105 // |progress_scale| is a percentage to which the progress will be scale, e.g. |
97 // a scale of 50 means it will increment from 0 to 50 over the course of the | 106 // a scale of 50 means it will increment from 0 to 50 over the course of the |
98 // sum. |progress_offset| is an percentage that will be added to the progress | 107 // sum. |progress_offset| is an percentage that will be added to the progress |
99 // of the MD5 sum before updating |progress_| but after scaling. | 108 // of the MD5 sum before updating |progress_| but after scaling. |
100 void GetMD5SumOfFile( | 109 void GetMD5SumOfFile( |
101 scoped_ptr<base::FilePath> file, | 110 const base::FilePath file, |
102 int64 file_size, | 111 int64 file_size, |
103 int progress_offset, | 112 int progress_offset, |
104 int progress_scale, | 113 int progress_scale, |
105 const base::Callback<void(scoped_ptr<std::string>)>& callback); | 114 const base::Callback<void(const std::string&)>& callback); |
106 | 115 |
107 base::WeakPtr<OperationManager> manager_; | 116 base::WeakPtr<OperationManager> manager_; |
108 const ExtensionId extension_id_; | 117 const ExtensionId extension_id_; |
109 | 118 |
110 base::FilePath image_path_; | 119 base::FilePath image_path_; |
111 const std::string storage_unit_id_; | 120 base::FilePath device_path_; |
112 | 121 |
113 // Whether or not to run the final verification step. | 122 // Temporary directory to store files as we go. |
114 bool verify_write_; | 123 base::ScopedTempDir temp_dir_; |
115 | 124 |
116 private: | 125 private: |
117 friend class base::RefCountedThreadSafe<Operation>; | 126 friend class base::RefCountedThreadSafe<Operation>; |
118 | 127 |
119 // TODO(haven): Clean up these switches. http://crbug.com/292956 | 128 // TODO(haven): Clean up these switches. http://crbug.com/292956 |
120 #if defined(OS_LINUX) && !defined(CHROMEOS) | 129 #if defined(OS_LINUX) && !defined(CHROMEOS) |
130 bool OpenPlatformFiles(); | |
131 | |
121 void WriteRun(); | 132 void WriteRun(); |
122 void WriteChunk(scoped_ptr<image_writer_utils::ImageReader> reader, | 133 void WriteChunk(const int64& bytes_written, |
123 scoped_ptr<image_writer_utils::ImageWriter> writer, | 134 const int64& total_size, |
124 int64 bytes_written); | 135 const base::Closure& continuation); |
125 bool WriteCleanUp(scoped_ptr<image_writer_utils::ImageReader> reader, | 136 void WriteComplete(const base::Closure& continuation); |
126 scoped_ptr<image_writer_utils::ImageWriter> writer); | |
127 void WriteComplete(); | |
128 | 137 |
129 void VerifyWriteStage2(scoped_ptr<std::string> image_hash); | 138 void VerifyWriteChunk(const int64& bytes_written, |
130 void VerifyWriteCompare(scoped_ptr<std::string> image_hash, | 139 const int64& total_size, |
131 scoped_ptr<std::string> device_hash); | 140 const base::Closure& continuation); |
141 void VerifyWriteComplete(const base::Closure& continuation); | |
142 | |
143 base::PlatformFile source_; | |
144 base::ScopedPlatformFileCloser source_closer_; | |
tbarzic
2014/02/13 06:58:52
will operation that uses this always be deleted on
Drew Haven
2014/02/13 20:48:10
I tried something new, passing around the ScopedPl
| |
145 base::PlatformFile target_; | |
146 base::ScopedPlatformFileCloser target_closer_; | |
132 #endif | 147 #endif |
133 | 148 |
134 #if defined(OS_CHROMEOS) | 149 #if defined(OS_CHROMEOS) |
135 void StartWriteOnUIThread(); | 150 void StartWriteOnUIThread(const base::Closure& continuation); |
136 | 151 |
137 void OnBurnFinished(const std::string& target_path, | 152 void OnBurnFinished(const base::Closure& continuation, |
153 const std::string& target_path, | |
138 bool success, | 154 bool success, |
139 const std::string& error); | 155 const std::string& error); |
140 void OnBurnProgress(const std::string& target_path, | 156 void OnBurnProgress(const std::string& target_path, |
141 int64 num_bytes_burnt, | 157 int64 num_bytes_burnt, |
142 int64 total_size); | 158 int64 total_size); |
143 void OnBurnError(); | 159 void OnBurnError(); |
144 #endif | 160 #endif |
145 | 161 |
146 // Incrementally calculates the MD5 sum of a file. | 162 // Incrementally calculates the MD5 sum of a file. |
147 void MD5Chunk(scoped_ptr<image_writer_utils::ImageReader> reader, | 163 void MD5Chunk(base::PlatformFile file, |
148 int64 bytes_processed, | 164 int64 bytes_processed, |
149 int64 bytes_total, | 165 int64 bytes_total, |
150 int progress_offset, | 166 int progress_offset, |
151 int progress_scale, | 167 int progress_scale, |
152 const base::Callback<void(scoped_ptr<std::string>)>& callback); | 168 const base::Callback<void(const std::string&)>& callback); |
153 | 169 |
154 // Callbacks for zip::ZipReader. | 170 // Callbacks for zip::ZipReader. |
155 void OnUnzipSuccess(); | 171 void OnUnzipSuccess(const base::Closure& continuation); |
156 void OnUnzipFailure(); | 172 void OnUnzipFailure(); |
157 void OnUnzipProgress(int64 total_bytes, int64 progress_bytes); | 173 void OnUnzipProgress(int64 total_bytes, int64 progress_bytes); |
158 | 174 |
159 // Runs all cleanup functions. | 175 // Runs all cleanup functions. |
160 void CleanUp(); | 176 void CleanUp(); |
161 | 177 |
162 // |stage_| and |progress_| are owned by the FILE thread, use |SetStage| and | 178 // |stage_| and |progress_| are owned by the FILE thread, use |SetStage| and |
163 // |SetProgress| to update. Progress should be in the interval [0,100] | 179 // |SetProgress| to update. Progress should be in the interval [0,100] |
164 image_writer_api::Stage stage_; | 180 image_writer_api::Stage stage_; |
165 int progress_; | 181 int progress_; |
166 | 182 |
167 // MD5 contexts don't play well with smart pointers. Just going to allocate | 183 // MD5 contexts don't play well with smart pointers. Just going to allocate |
168 // memory here. This requires that we only do one MD5 sum at a time. | 184 // memory here. This requires that we only do one MD5 sum at a time. |
169 base::MD5Context md5_context_; | 185 base::MD5Context md5_context_; |
170 | 186 |
171 // Zip reader for unzip operations. | 187 // Zip reader for unzip operations. |
172 zip::ZipReader zip_reader_; | 188 zip::ZipReader zip_reader_; |
173 | 189 |
174 // CleanUp operations that must be run. All these functions are run on the | 190 // CleanUp operations that must be run. All these functions are run on the |
175 // FILE thread. | 191 // FILE thread. |
176 std::vector<base::Closure> cleanup_functions_; | 192 std::vector<base::Closure> cleanup_functions_; |
177 }; | 193 }; |
178 | 194 |
179 } // namespace image_writer | 195 } // namespace image_writer |
180 } // namespace extensions | 196 } // namespace extensions |
181 | 197 |
182 #endif // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_H_ | 198 #endif // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_H_ |
OLD | NEW |