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