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 #include "base/file_util.h" | 5 #include "base/file_util.h" |
6 #include "base/files/file_enumerator.h" | 6 #include "base/files/file_enumerator.h" |
7 #include "base/threading/worker_pool.h" | 7 #include "base/threading/worker_pool.h" |
8 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" | 8 #include "chrome/browser/extensions/api/image_writer_private/error_messages.h" |
9 #include "chrome/browser/extensions/api/image_writer_private/operation.h" | 9 #include "chrome/browser/extensions/api/image_writer_private/operation.h" |
10 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" | 10 #include "chrome/browser/extensions/api/image_writer_private/operation_manager.h
" |
11 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
12 #include "third_party/zlib/google/zip.h" | |
13 | 12 |
14 namespace extensions { | 13 namespace extensions { |
15 namespace image_writer { | 14 namespace image_writer { |
16 | 15 |
17 using content::BrowserThread; | 16 using content::BrowserThread; |
18 | 17 |
19 const int kBurningBlockSize = 8 * 1024; // 8 KiB | |
20 | |
21 void Operation::WriteStart() { | 18 void Operation::WriteStart() { |
22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 19 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
23 if (IsCancelled()) { | 20 if (IsCancelled()) { |
24 return; | 21 return; |
25 } | 22 } |
26 | 23 |
27 if (image_path_.empty()) { | |
28 Error(error::kImageNotFound); | |
29 return; | |
30 } | |
31 | |
32 DVLOG(1) << "Starting write of " << image_path_.value() | |
33 << " to " << storage_unit_id_; | |
34 | |
35 SetStage(image_writer_api::STAGE_WRITE); | 24 SetStage(image_writer_api::STAGE_WRITE); |
36 | 25 |
37 // TODO (haven): Unmount partitions before writing. http://crbug.com/284834 | |
38 | |
39 scoped_ptr<image_writer_utils::ImageReader> reader( | |
40 new image_writer_utils::ImageReader()); | |
41 scoped_ptr<image_writer_utils::ImageWriter> writer( | |
42 new image_writer_utils::ImageWriter()); | |
43 base::FilePath storage_path(storage_unit_id_); | 26 base::FilePath storage_path(storage_unit_id_); |
44 | 27 |
45 if (reader->Open(image_path_)) { | 28 StartUtilityClient(); |
46 if (!writer->Open(storage_path)) { | 29 |
47 reader->Close(); | 30 int64 file_size; |
48 Error(error::kDeviceOpenError); | 31 if (!base::GetFileSize(image_path_, &file_size)) { |
49 return; | 32 Error(error::kImageReadError); |
50 } | |
51 } else { | |
52 Error(error::kImageOpenError); | |
53 return; | 33 return; |
54 } | 34 } |
55 | 35 |
56 BrowserThread::PostTask( | 36 BrowserThread::PostTask( |
57 BrowserThread::FILE, | 37 BrowserThread::IO, |
58 FROM_HERE, | 38 FROM_HERE, |
59 base::Bind(&Operation::WriteChunk, | 39 base::Bind(&ImageWriterUtilityClient::Write, |
60 this, | 40 image_writer_client_, |
61 base::Passed(&reader), | 41 base::Bind(&Operation::WriteImageProgress, this, file_size), |
62 base::Passed(&writer), | 42 base::Bind(&Operation::VerifyWriteStart, this), |
63 0)); | 43 base::Bind(&Operation::Error, this), |
64 } | 44 image_path_, |
65 | 45 storage_path)); |
66 void Operation::WriteChunk( | |
67 scoped_ptr<image_writer_utils::ImageReader> reader, | |
68 scoped_ptr<image_writer_utils::ImageWriter> writer, | |
69 int64 bytes_written) { | |
70 if (IsCancelled()) { | |
71 WriteCleanUp(reader.Pass(), writer.Pass()); | |
72 return; | |
73 } | |
74 | |
75 char buffer[kBurningBlockSize]; | |
76 int64 image_size = reader->GetSize(); | |
77 int len = reader->Read(buffer, kBurningBlockSize); | |
78 | |
79 if (len > 0) { | |
80 if (writer->Write(buffer, len) == len) { | |
81 int percent_prev = kProgressComplete * bytes_written / image_size; | |
82 int percent_curr = kProgressComplete * (bytes_written + len) / image_size; | |
83 | |
84 if (percent_curr > percent_prev) { | |
85 SetProgress(percent_curr); | |
86 } | |
87 | |
88 BrowserThread::PostTask( | |
89 BrowserThread::FILE, | |
90 FROM_HERE, | |
91 base::Bind(&Operation::WriteChunk, | |
92 this, | |
93 base::Passed(&reader), | |
94 base::Passed(&writer), | |
95 bytes_written + len)); | |
96 } else { | |
97 WriteCleanUp(reader.Pass(), writer.Pass()); | |
98 Error(error::kDeviceWriteError); | |
99 } | |
100 } else if (len == 0) { | |
101 if (bytes_written == image_size) { | |
102 if (WriteCleanUp(reader.Pass(), writer.Pass())) { | |
103 BrowserThread::PostTask( | |
104 BrowserThread::FILE, | |
105 FROM_HERE, | |
106 base::Bind(&Operation::WriteComplete, | |
107 this)); | |
108 } | |
109 } else { | |
110 WriteCleanUp(reader.Pass(), writer.Pass()); | |
111 Error(error::kImageReadError); | |
112 } | |
113 } else { // len < 0 | |
114 WriteCleanUp(reader.Pass(), writer.Pass()); | |
115 Error(error::kImageReadError); | |
116 } | |
117 } | |
118 | |
119 bool Operation::WriteCleanUp( | |
120 scoped_ptr<image_writer_utils::ImageReader> reader, | |
121 scoped_ptr<image_writer_utils::ImageWriter> writer) { | |
122 | |
123 bool success = true; | |
124 if (!reader->Close()) { | |
125 Error(error::kImageCloseError); | |
126 success = false; | |
127 } | |
128 | |
129 if (!writer->Close()) { | |
130 Error(error::kDeviceCloseError); | |
131 success = false; | |
132 } | |
133 return success; | |
134 } | |
135 | |
136 void Operation::WriteComplete() { | |
137 | |
138 DVLOG(2) << "Completed write of " << image_path_.value(); | |
139 SetProgress(kProgressComplete); | |
140 | |
141 if (verify_write_) { | |
142 BrowserThread::PostTask(BrowserThread::FILE, | |
143 FROM_HERE, | |
144 base::Bind(&Operation::VerifyWriteStart, this)); | |
145 } else { | |
146 BrowserThread::PostTask(BrowserThread::FILE, | |
147 FROM_HERE, | |
148 base::Bind(&Operation::Finish, this)); | |
149 } | |
150 } | 46 } |
151 | 47 |
152 void Operation::VerifyWriteStart() { | 48 void Operation::VerifyWriteStart() { |
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
| 50 |
154 if (IsCancelled()) { | 51 if (IsCancelled()) { |
155 return; | 52 return; |
156 } | 53 } |
157 | 54 |
158 DVLOG(1) << "Starting verification stage."; | |
159 | |
160 SetStage(image_writer_api::STAGE_VERIFYWRITE); | 55 SetStage(image_writer_api::STAGE_VERIFYWRITE); |
161 | 56 |
162 scoped_ptr<base::FilePath> image_path(new base::FilePath(image_path_)); | 57 base::FilePath storage_path(storage_unit_id_); |
163 | 58 |
164 GetMD5SumOfFile( | 59 StartUtilityClient(); |
165 image_path.Pass(), | |
166 -1, | |
167 0, // progress_offset | |
168 50, // progress_scale | |
169 base::Bind(&Operation::VerifyWriteStage2, | |
170 this)); | |
171 } | |
172 | 60 |
173 void Operation::VerifyWriteStage2( | 61 int64 file_size; |
174 scoped_ptr<std::string> image_hash) { | 62 if (!base::GetFileSize(image_path_, &file_size)) { |
175 DVLOG(1) << "Building MD5 sum of device: " << storage_unit_id_; | 63 Error(error::kImageReadError); |
176 | |
177 int64 image_size; | |
178 scoped_ptr<base::FilePath> device_path(new base::FilePath(storage_unit_id_)); | |
179 | |
180 if (!base::GetFileSize(image_path_, &image_size)){ | |
181 Error(error::kImageSizeError); | |
182 return; | 64 return; |
183 } | 65 } |
184 | 66 |
185 GetMD5SumOfFile( | 67 BrowserThread::PostTask( |
186 device_path.Pass(), | 68 BrowserThread::IO, |
187 image_size, | 69 FROM_HERE, |
188 50, // progress_offset | 70 base::Bind(&ImageWriterUtilityClient::Verify, |
189 50, // progress_scale | 71 image_writer_client_, |
190 base::Bind(&Operation::VerifyWriteCompare, | 72 base::Bind(&Operation::WriteImageProgress, this, file_size), |
191 this, | 73 base::Bind(&Operation::Finish, this), |
192 base::Passed(&image_hash))); | 74 base::Bind(&Operation::Error, this), |
193 } | 75 image_path_, |
194 | 76 storage_path)); |
195 void Operation::VerifyWriteCompare( | |
196 scoped_ptr<std::string> image_hash, | |
197 scoped_ptr<std::string> device_hash) { | |
198 DVLOG(1) << "Comparing hashes: " << *image_hash << " vs " << *device_hash; | |
199 | |
200 if (*image_hash != *device_hash) { | |
201 Error(error::kVerificationFailed); | |
202 return; | |
203 } | |
204 | |
205 DVLOG(2) << "Completed write verification of " << image_path_.value(); | |
206 | |
207 SetProgress(kProgressComplete); | |
208 | |
209 Finish(); | |
210 } | 77 } |
211 | 78 |
212 } // namespace image_writer | 79 } // namespace image_writer |
213 } // namespace extensions | 80 } // namespace extensions |
OLD | NEW |