Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: chrome/browser/extensions/api/image_writer_private/operation.h

Issue 170713007: Resubmit of 149313003: Significantly cleans up the ImageWriter Operation class and subclasses. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixes comments. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/md5.h" 10 #include "base/md5.h"
10 #include "base/memory/ref_counted_memory.h" 11 #include "base/memory/ref_counted_memory.h"
11 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
12 #include "base/task/cancelable_task_tracker.h" 13 #include "base/task/cancelable_task_tracker.h"
13 #include "base/timer/timer.h" 14 #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" 15 #include "chrome/common/extensions/api/image_writer_private.h"
16 #include "third_party/zlib/google/zip_reader.h" 16 #include "third_party/zlib/google/zip_reader.h"
17 17
18 namespace image_writer_api = extensions::api::image_writer_private; 18 namespace image_writer_api = extensions::api::image_writer_private;
19 19
20 namespace base { 20 namespace base {
21 class FilePath; 21 class FilePath;
22 } // namespace base 22 } // namespace base
23 23
24 namespace extensions { 24 namespace extensions {
25 namespace image_writer { 25 namespace image_writer {
26 26
27 const int kProgressComplete = 100; 27 const int kProgressComplete = 100;
28 28
29 class OperationManager; 29 class OperationManager;
30 30
31 // Encapsulates an operation being run on behalf of the 31 // Encapsulates an operation being run on behalf of the
32 // OperationManager. Construction of the operation does not start 32 // OperationManager. Construction of the operation does not start
33 // anything. The operation's Start method should be called to start it, and 33 // anything. The operation's Start method should be called to start it, and
34 // then the Cancel method will stop it. The operation will call back to the 34 // then the Cancel method will stop it. The operation will call back to the
35 // OperationManager periodically or on any significant event. 35 // OperationManager periodically or on any significant event.
36 // 36 //
37 // Each stage of the operation is generally divided into three phases: Start, 37 // Each stage of the operation is generally divided into three phases: Start,
38 // Run, Complete. Start and Complete run on the UI thread and are responsible 38 // Run, Complete. Start and Complete run on the UI thread and are responsible
39 // for advancing to the next stage and other UI interaction. The Run phase does 39 // 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. 40 // the work on the FILE thread and calls SendProgress or Error as appropriate.
41 //
42 // TODO(haven): This class is current refcounted because it is owned by the
43 // OperationManager on the UI thread but needs to do work on the FILE thread.
44 // There is probably a better way to organize this so that it can be represented
45 // by a WeakPtr, but those are not thread-safe. Additionally, if destruction is
46 // done on the UI thread then that causes problems if any of the fields were
47 // allocated/accessed on the FILE thread. http://crbug.com/344713
41 class Operation : public base::RefCountedThreadSafe<Operation> { 48 class Operation : public base::RefCountedThreadSafe<Operation> {
42 public: 49 public:
43 typedef base::Callback<void(bool, const std::string&)> StartWriteCallback; 50 typedef base::Callback<void(bool, const std::string&)> StartWriteCallback;
44 typedef base::Callback<void(bool, const std::string&)> CancelWriteCallback; 51 typedef base::Callback<void(bool, const std::string&)> CancelWriteCallback;
45 typedef std::string ExtensionId; 52 typedef std::string ExtensionId;
46 53
47 Operation(base::WeakPtr<OperationManager> manager, 54 Operation(base::WeakPtr<OperationManager> manager,
48 const ExtensionId& extension_id, 55 const ExtensionId& extension_id,
49 const std::string& storage_unit_id); 56 const std::string& device_path);
50 57
51 // Starts the operation. 58 // Starts the operation.
52 virtual void Start() = 0; 59 void Start();
53 60
54 // Cancel the operation. This must be called to clean up internal state and 61 // 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 62 // cause the the operation to actually stop. It will not be destroyed until
56 // all callbacks have completed. 63 // all callbacks have completed.
57 void Cancel(); 64 void Cancel();
58 65
59 // Aborts the operation, cancelling it and generating an error. 66 // Aborts the operation, cancelling it and generating an error.
60 void Abort(); 67 void Abort();
61 68
62 // Informational getters. 69 // Informational getters.
63 int GetProgress(); 70 int GetProgress();
64 image_writer_api::Stage GetStage(); 71 image_writer_api::Stage GetStage();
65 72
66 protected: 73 protected:
67 virtual ~Operation(); 74 virtual ~Operation();
68 75
76 // This function should be overriden by subclasses to set up the work of the
77 // operation. It will be called from Start().
78 virtual void StartImpl() = 0;
79
80 // Unzips the current file if it ends in ".zip". The current_file will be set
81 // to the unzipped file.
82 void Unzip(const base::Closure& continuation);
83
84 // Writes the current file to device_path.
85 void Write(const base::Closure& continuation);
86
87 // Verifies that the current file and device_path contents match.
88 void VerifyWrite(const base::Closure& continuation);
89
90 // Completes the operation.
91 void Finish();
92
69 // Generates an error. 93 // Generates an error.
70 // |error_message| is used to create an OnWriteError event which is 94 // |error_message| is used to create an OnWriteError event which is
71 // sent to the extension 95 // sent to the extension
72 virtual void Error(const std::string& error_message); 96 virtual void Error(const std::string& error_message);
73 97
74 // Set |progress_| and send an event. Progress should be in the interval 98 // Set |progress_| and send an event. Progress should be in the interval
75 // [0,100] 99 // [0,100]
76 void SetProgress(int progress); 100 void SetProgress(int progress);
77 // Change to a new |stage_| and set |progress_| to zero. Triggers a progress 101 // Change to a new |stage_| and set |progress_| to zero. Triggers a progress
78 // event. 102 // event.
79 void SetStage(image_writer_api::Stage stage); 103 void SetStage(image_writer_api::Stage stage);
80 104
81 // 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.
82 bool IsCancelled(); 106 bool IsCancelled();
83 107
84 // 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
85 // is aborted, encounters and error, or finishes successfully. These 109 // is aborted, encounters and error, or finishes successfully. These
86 // functions will be run on the FILE thread. 110 // functions will be run on the FILE thread.
87 void AddCleanUpFunction(base::Closure); 111 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 112
94 // 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,
95 // otherwise the entire file will be read. 114 // otherwise the entire file will be read.
96 // |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.
97 // 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
98 // 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
99 // of the MD5 sum before updating |progress_| but after scaling. 118 // of the MD5 sum before updating |progress_| but after scaling.
100 void GetMD5SumOfFile( 119 void GetMD5SumOfFile(
101 scoped_ptr<base::FilePath> file, 120 const base::FilePath& file,
102 int64 file_size, 121 int64 file_size,
103 int progress_offset, 122 int progress_offset,
104 int progress_scale, 123 int progress_scale,
105 const base::Callback<void(scoped_ptr<std::string>)>& callback); 124 const base::Callback<void(const std::string&)>& callback);
106 125
107 base::WeakPtr<OperationManager> manager_; 126 base::WeakPtr<OperationManager> manager_;
108 const ExtensionId extension_id_; 127 const ExtensionId extension_id_;
109 128
110 base::FilePath image_path_; 129 base::FilePath image_path_;
111 const std::string storage_unit_id_; 130 base::FilePath device_path_;
112 131
113 // Whether or not to run the final verification step. 132 // Temporary directory to store files as we go.
114 bool verify_write_; 133 base::ScopedTempDir temp_dir_;
115 134
116 private: 135 private:
117 friend class base::RefCountedThreadSafe<Operation>; 136 friend class base::RefCountedThreadSafe<Operation>;
118 137
119 // TODO(haven): Clean up these switches. http://crbug.com/292956 138 // TODO(haven): Clean up these switches. http://crbug.com/292956
120 #if defined(OS_LINUX) && !defined(CHROMEOS) 139 #if defined(OS_LINUX) && !defined(CHROMEOS)
121 void WriteRun(); 140 void WriteChunk(const int64& bytes_written,
122 void WriteChunk(scoped_ptr<image_writer_utils::ImageReader> reader, 141 const int64& total_size,
123 scoped_ptr<image_writer_utils::ImageWriter> writer, 142 const base::Closure& continuation);
124 int64 bytes_written); 143 void WriteComplete(const base::Closure& continuation);
125 bool WriteCleanUp(scoped_ptr<image_writer_utils::ImageReader> reader,
126 scoped_ptr<image_writer_utils::ImageWriter> writer);
127 void WriteComplete();
128 144
129 void VerifyWriteStage2(scoped_ptr<std::string> image_hash); 145 void VerifyWriteChunk(const int64& bytes_written,
130 void VerifyWriteCompare(scoped_ptr<std::string> image_hash, 146 const int64& total_size,
131 scoped_ptr<std::string> device_hash); 147 const base::Closure& continuation);
148 void VerifyWriteComplete(const base::Closure& continuation);
149
150 base::PlatformFile image_file_;
151 base::PlatformFile device_file_;
132 #endif 152 #endif
133 153
134 #if defined(OS_CHROMEOS) 154 #if defined(OS_CHROMEOS)
135 void StartWriteOnUIThread(); 155 void StartWriteOnUIThread(const base::Closure& continuation);
136 156
137 void OnBurnFinished(const std::string& target_path, 157 void OnBurnFinished(const base::Closure& continuation,
158 const std::string& target_path,
138 bool success, 159 bool success,
139 const std::string& error); 160 const std::string& error);
140 void OnBurnProgress(const std::string& target_path, 161 void OnBurnProgress(const std::string& target_path,
141 int64 num_bytes_burnt, 162 int64 num_bytes_burnt,
142 int64 total_size); 163 int64 total_size);
143 void OnBurnError(); 164 void OnBurnError();
144 #endif 165 #endif
145 166
146 // Incrementally calculates the MD5 sum of a file. 167 // Incrementally calculates the MD5 sum of a file.
147 void MD5Chunk(scoped_ptr<image_writer_utils::ImageReader> reader, 168 void MD5Chunk(const base::PlatformFile& file,
148 int64 bytes_processed, 169 int64 bytes_processed,
149 int64 bytes_total, 170 int64 bytes_total,
150 int progress_offset, 171 int progress_offset,
151 int progress_scale, 172 int progress_scale,
152 const base::Callback<void(scoped_ptr<std::string>)>& callback); 173 const base::Callback<void(const std::string&)>& callback);
153 174
154 // Callbacks for zip::ZipReader. 175 // Callbacks for zip::ZipReader.
155 void OnUnzipSuccess(); 176 void OnUnzipSuccess(const base::Closure& continuation);
156 void OnUnzipFailure(); 177 void OnUnzipFailure();
157 void OnUnzipProgress(int64 total_bytes, int64 progress_bytes); 178 void OnUnzipProgress(int64 total_bytes, int64 progress_bytes);
158 179
159 // Runs all cleanup functions. 180 // Runs all cleanup functions.
160 void CleanUp(); 181 void CleanUp();
161 182
162 // |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
163 // |SetProgress| to update. Progress should be in the interval [0,100] 184 // |SetProgress| to update. Progress should be in the interval [0,100]
164 image_writer_api::Stage stage_; 185 image_writer_api::Stage stage_;
165 int progress_; 186 int progress_;
166 187
167 // 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
168 // 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.
169 base::MD5Context md5_context_; 190 base::MD5Context md5_context_;
170 191
171 // Zip reader for unzip operations. 192 // Zip reader for unzip operations.
172 zip::ZipReader zip_reader_; 193 zip::ZipReader zip_reader_;
173 194
174 // 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
175 // FILE thread. 196 // FILE thread.
176 std::vector<base::Closure> cleanup_functions_; 197 std::vector<base::Closure> cleanup_functions_;
177 }; 198 };
178 199
179 } // namespace image_writer 200 } // namespace image_writer
180 } // namespace extensions 201 } // namespace extensions
181 202
182 #endif // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_H_ 203 #endif // CHROME_BROWSER_EXTENSIONS_API_IMAGE_WRITER_PRIVATE_OPERATION_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698