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