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

Side by Side Diff: chrome/browser/extensions/api/image_writer_private/operation_linux.cc

Issue 149313003: Significantly cleans up the ImageWriter Operation class and subclasses. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review feedback: Creates StartImpl function, updates platformfile lifetime to use passed ScopedPlat… 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 #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 base::ScopedPlatformFileCloser Operation::OpenFile(
22 const base::FilePath& path,
23 const base::PlatformFileFlags mode) {
24 base::PlatformFileError result;
25 base::ScopedPlatformFileCloser file_closer(new base::PlatformFile());
tbarzic 2014/02/13 21:32:39 will the created PlatformFile get deleted with |fi
Drew Haven 2014/02/13 23:30:01 Yeah, this doesn't match the semantics of scoped_p
tbarzic 2014/02/13 23:42:46 Yep, totally agree :)
26
27 *file_closer = base::CreatePlatformFile(
28 path, base::PLATFORM_FILE_OPEN | mode, NULL, &result);
29
30 if (result != base::PLATFORM_FILE_OK) {
31 return base::ScopedPlatformFileCloser();
32 }
33
34 return file_closer.Pass();
35 }
36
37 void Operation::Write(const base::Closure& continuation) {
22 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
23 if (IsCancelled()) { 39 if (IsCancelled()) {
24 return; 40 return;
25 } 41 }
26 42
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); 43 SetStage(image_writer_api::STAGE_WRITE);
36 44
37 // TODO (haven): Unmount partitions before writing. http://crbug.com/284834 45 // TODO (haven): Unmount partitions before writing. http://crbug.com/284834
38 46
39 scoped_ptr<image_writer_utils::ImageReader> reader( 47 base::ScopedPlatformFileCloser source =
40 new image_writer_utils::ImageReader()); 48 OpenFile(image_path_, base::PLATFORM_FILE_READ);
41 scoped_ptr<image_writer_utils::ImageWriter> writer( 49 if (!source) {
42 new image_writer_utils::ImageWriter());
43 base::FilePath storage_path(storage_unit_id_);
44
45 if (reader->Open(image_path_)) {
46 if (!writer->Open(storage_path)) {
47 reader->Close();
48 Error(error::kDeviceOpenError);
49 return;
50 }
51 } else {
52 Error(error::kImageOpenError); 50 Error(error::kImageOpenError);
53 return; 51 return;
54 } 52 }
55 53
56 BrowserThread::PostTask( 54 base::ScopedPlatformFileCloser target =
57 BrowserThread::FILE, 55 OpenFile(device_path_, base::PLATFORM_FILE_WRITE);
58 FROM_HERE, 56 if (!target) {
59 base::Bind(&Operation::WriteChunk, 57 Error(error::kImageOpenError);
60 this,
61 base::Passed(&reader),
62 base::Passed(&writer),
63 0));
64 }
65
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; 58 return;
73 } 59 }
74 60
75 char buffer[kBurningBlockSize]; 61 int64 total_size;
76 int64 image_size = reader->GetSize(); 62 base::GetFileSize(image_path_, &total_size);
77 int len = reader->Read(buffer, kBurningBlockSize); 63
64 BrowserThread::PostTask(BrowserThread::FILE,
65 FROM_HERE,
66 base::Bind(&Operation::WriteChunk,
67 this,
68 base::Passed(&source),
69 base::Passed(&target),
70 0,
71 total_size,
72 continuation));
73 }
74
75 void Operation::WriteChunk(base::ScopedPlatformFileCloser source,
76 base::ScopedPlatformFileCloser target,
77 const int64& bytes_written,
78 const int64& total_size,
79 const base::Closure& continuation) {
80 if (IsCancelled()) {
81 return;
82 }
83
84 scoped_ptr<char[]> buffer(new char[kBurningBlockSize]);
85 int64 len = base::ReadPlatformFile(
86 *source, bytes_written, buffer.get(), kBurningBlockSize);
78 87
79 if (len > 0) { 88 if (len > 0) {
80 if (writer->Write(buffer, len) == len) { 89 if (base::WritePlatformFile(*target, bytes_written, buffer.get(), len) ==
81 int percent_prev = kProgressComplete * bytes_written / image_size; 90 len) {
82 int percent_curr = kProgressComplete * (bytes_written + len) / image_size; 91 int percent_curr = kProgressComplete * (bytes_written + len) / total_size;
83 92
84 if (percent_curr > percent_prev) { 93 SetProgress(percent_curr);
85 SetProgress(percent_curr);
86 }
87 94
88 BrowserThread::PostTask( 95 BrowserThread::PostTask(BrowserThread::FILE,
89 BrowserThread::FILE, 96 FROM_HERE,
90 FROM_HERE, 97 base::Bind(&Operation::WriteChunk,
91 base::Bind(&Operation::WriteChunk, 98 this,
92 this, 99 base::Passed(&source),
93 base::Passed(&reader), 100 base::Passed(&target),
94 base::Passed(&writer), 101 bytes_written + len,
95 bytes_written + len)); 102 total_size,
103 continuation));
96 } else { 104 } else {
97 WriteCleanUp(reader.Pass(), writer.Pass());
98 Error(error::kDeviceWriteError); 105 Error(error::kDeviceWriteError);
99 } 106 }
100 } else if (len == 0) { 107 } else if (len == 0) {
101 if (bytes_written == image_size) { 108 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 109 } else { // len < 0
114 WriteCleanUp(reader.Pass(), writer.Pass());
115 Error(error::kImageReadError); 110 Error(error::kImageReadError);
116 } 111 }
117 } 112 }
118 113
119 bool Operation::WriteCleanUp( 114 void Operation::WriteComplete(const base::Closure& continuation) {
120 scoped_ptr<image_writer_utils::ImageReader> reader, 115 SetProgress(kProgressComplete);
121 scoped_ptr<image_writer_utils::ImageWriter> writer) { 116 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 } 117 }
135 118
136 void Operation::WriteComplete() { 119 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)); 120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
154 if (IsCancelled()) { 121 if (IsCancelled()) {
155 return; 122 return;
156 } 123 }
157 124
158 DVLOG(1) << "Starting verification stage.";
159
160 SetStage(image_writer_api::STAGE_VERIFYWRITE); 125 SetStage(image_writer_api::STAGE_VERIFYWRITE);
161 126
162 scoped_ptr<base::FilePath> image_path(new base::FilePath(image_path_)); 127 base::ScopedPlatformFileCloser image =
163 128 OpenFile(image_path_, base::PLATFORM_FILE_READ);
164 GetMD5SumOfFile( 129 if (!image) {
165 image_path.Pass(), 130 Error(error::kImageOpenError);
166 -1,
167 0, // progress_offset
168 50, // progress_scale
169 base::Bind(&Operation::VerifyWriteStage2,
170 this));
171 }
172
173 void Operation::VerifyWriteStage2(
174 scoped_ptr<std::string> image_hash) {
175 DVLOG(1) << "Building MD5 sum of device: " << storage_unit_id_;
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; 131 return;
183 } 132 }
184 133
185 GetMD5SumOfFile( 134 base::ScopedPlatformFileCloser device =
186 device_path.Pass(), 135 OpenFile(device_path_, base::PLATFORM_FILE_READ);
187 image_size, 136 if (!device) {
188 50, // progress_offset 137 Error(error::kDeviceOpenError);
189 50, // progress_scale
190 base::Bind(&Operation::VerifyWriteCompare,
191 this,
192 base::Passed(&image_hash)));
193 }
194
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; 138 return;
203 } 139 }
204 140
205 DVLOG(2) << "Completed write verification of " << image_path_.value(); 141 int64 total_size;
142 base::GetFileSize(image_path_, &total_size);
206 143
144 BrowserThread::PostTask(BrowserThread::FILE,
145 FROM_HERE,
146 base::Bind(&Operation::VerifyWriteChunk,
147 this,
148 base::Passed(&image),
149 base::Passed(&device),
150 0,
151 total_size,
152 continuation));
153 }
154
155 void Operation::VerifyWriteChunk(base::ScopedPlatformFileCloser image,
156 base::ScopedPlatformFileCloser device,
157 const int64& bytes_processed,
158 const int64& total_size,
159 const base::Closure& continuation) {
160 if (IsCancelled()) {
161 return;
162 }
163
164 scoped_ptr<char[]> source_buffer(new char[kBurningBlockSize]);
165 scoped_ptr<char[]> target_buffer(new char[kBurningBlockSize]);
166
167 int64 source_bytes_read = base::ReadPlatformFile(
168 *image, bytes_processed, source_buffer.get(), kBurningBlockSize);
169
170 if (source_bytes_read > 0) {
171 int64 target_bytes_read = base::ReadPlatformFile(
172 *device, bytes_processed, target_buffer.get(), source_bytes_read);
173 if (source_bytes_read == target_bytes_read &&
174 memcmp(source_buffer.get(), target_buffer.get(), source_bytes_read) ==
175 0) {
176 int percent_curr = kProgressComplete *
177 (bytes_processed + source_bytes_read) / total_size;
178
179 SetProgress(percent_curr);
180
181 BrowserThread::PostTask(BrowserThread::FILE,
182 FROM_HERE,
183 base::Bind(&Operation::VerifyWriteChunk,
184 this,
185 base::Passed(&image),
186 base::Passed(&device),
187 bytes_processed + source_bytes_read,
188 total_size,
189 continuation));
190 } else {
191 Error(error::kVerificationFailed);
192 }
193 } else if (source_bytes_read == 0) {
194 VerifyWriteComplete(continuation);
195 } else { // len < 0
196 Error(error::kImageReadError);
197 }
198 }
199
200 void Operation::VerifyWriteComplete(const base::Closure& continuation) {
207 SetProgress(kProgressComplete); 201 SetProgress(kProgressComplete);
208 202 continuation.Run();
209 Finish();
210 } 203 }
211 204
212 } // namespace image_writer 205 } // namespace image_writer
213 } // namespace extensions 206 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698