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

Side by Side Diff: chrome/browser/drive/drive_api_util.cc

Issue 875513006: Files.app: Implement md5 hashing over file streams. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address feedback. Created 5 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
« no previous file with comments | « chrome/browser/drive/drive_api_util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/drive/drive_api_util.h" 5 #include "chrome/browser/drive/drive_api_util.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/files/file.h" 9 #include "base/files/file.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/md5.h" 11 #include "base/md5.h"
12 #include "base/strings/string16.h" 12 #include "base/strings/string16.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 #include "base/strings/utf_string_conversions.h" 15 #include "base/strings/utf_string_conversions.h"
16 #include "base/values.h" 16 #include "base/values.h"
17 #include "google_apis/drive/drive_api_parser.h" 17 #include "google_apis/drive/drive_api_parser.h"
18 #include "net/base/escape.h" 18 #include "net/base/escape.h"
19 #include "net/base/io_buffer.h"
20 #include "net/base/net_errors.h"
21 #include "storage/browser/blob/file_stream_reader.h"
19 #include "third_party/re2/re2/re2.h" 22 #include "third_party/re2/re2/re2.h"
20 #include "url/gurl.h" 23 #include "url/gurl.h"
21 24
22 namespace drive { 25 namespace drive {
23 namespace util { 26 namespace util {
24 namespace { 27 namespace {
25 28
26 struct HostedDocumentKind { 29 struct HostedDocumentKind {
27 const char* mime_type; 30 const char* mime_type;
28 const char* extension; 31 const char* extension;
29 }; 32 };
30 33
31 const HostedDocumentKind kHostedDocumentKinds[] = { 34 const HostedDocumentKind kHostedDocumentKinds[] = {
32 {kGoogleDocumentMimeType, ".gdoc"}, 35 {kGoogleDocumentMimeType, ".gdoc"},
33 {kGoogleSpreadsheetMimeType, ".gsheet"}, 36 {kGoogleSpreadsheetMimeType, ".gsheet"},
34 {kGooglePresentationMimeType, ".gslides"}, 37 {kGooglePresentationMimeType, ".gslides"},
35 {kGoogleDrawingMimeType, ".gdraw"}, 38 {kGoogleDrawingMimeType, ".gdraw"},
36 {kGoogleTableMimeType, ".gtable"}, 39 {kGoogleTableMimeType, ".gtable"},
37 {kGoogleFormMimeType, ".gform"}, 40 {kGoogleFormMimeType, ".gform"},
38 {kGoogleMapMimeType, ".gmaps"}, 41 {kGoogleMapMimeType, ".gmaps"},
39 }; 42 };
40 43
41 const char kUnknownHostedDocumentExtension[] = ".glink"; 44 const char kUnknownHostedDocumentExtension[] = ".glink";
42 45
46 const int kMd5DigestBufferSize = 512 * 1024; // 512 kB.
47
43 } // namespace 48 } // namespace
44 49
45 std::string EscapeQueryStringValue(const std::string& str) { 50 std::string EscapeQueryStringValue(const std::string& str) {
46 std::string result; 51 std::string result;
47 result.reserve(str.size()); 52 result.reserve(str.size());
48 for (size_t i = 0; i < str.size(); ++i) { 53 for (size_t i = 0; i < str.size(); ++i) {
49 if (str[i] == '\\' || str[i] == '\'') { 54 if (str[i] == '\\' || str[i] == '\'') {
50 result.push_back('\\'); 55 result.push_back('\\');
51 } 56 }
52 result.push_back(str[i]); 57 result.push_back(str[i]);
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
124 // If resource ID is in the old WAPI format starting with a prefix like 129 // If resource ID is in the old WAPI format starting with a prefix like
125 // "document:", strip it and return the remaining part. 130 // "document:", strip it and return the remaining part.
126 std::string stripped_resource_id; 131 std::string stripped_resource_id;
127 if (RE2::FullMatch(resource_id, "^[a-z-]+(?::|%3A)([\\w-]+)$", 132 if (RE2::FullMatch(resource_id, "^[a-z-]+(?::|%3A)([\\w-]+)$",
128 &stripped_resource_id)) 133 &stripped_resource_id))
129 return stripped_resource_id; 134 return stripped_resource_id;
130 return resource_id; 135 return resource_id;
131 } 136 }
132 137
133 std::string GetMd5Digest(const base::FilePath& file_path) { 138 std::string GetMd5Digest(const base::FilePath& file_path) {
134 const int kBufferSize = 512 * 1024; // 512kB.
135
136 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ); 139 base::File file(file_path, base::File::FLAG_OPEN | base::File::FLAG_READ);
137 if (!file.IsValid()) 140 if (!file.IsValid())
138 return std::string(); 141 return std::string();
139 142
140 base::MD5Context context; 143 base::MD5Context context;
141 base::MD5Init(&context); 144 base::MD5Init(&context);
142 145
143 int64 offset = 0; 146 int64 offset = 0;
144 scoped_ptr<char[]> buffer(new char[kBufferSize]); 147 scoped_ptr<char[]> buffer(new char[kMd5DigestBufferSize]);
145 while (true) { 148 while (true) {
146 int result = file.Read(offset, buffer.get(), kBufferSize); 149 int result = file.Read(offset, buffer.get(), kMd5DigestBufferSize);
147 if (result < 0) { 150 if (result < 0) {
148 // Found an error. 151 // Found an error.
149 return std::string(); 152 return std::string();
150 } 153 }
151 154
152 if (result == 0) { 155 if (result == 0) {
153 // End of file. 156 // End of file.
154 break; 157 break;
155 } 158 }
156 159
157 offset += result; 160 offset += result;
158 base::MD5Update(&context, base::StringPiece(buffer.get(), result)); 161 base::MD5Update(&context, base::StringPiece(buffer.get(), result));
159 } 162 }
160 163
161 base::MD5Digest digest; 164 base::MD5Digest digest;
162 base::MD5Final(&digest, &context); 165 base::MD5Final(&digest, &context);
163 return MD5DigestToBase16(digest); 166 return MD5DigestToBase16(digest);
164 } 167 }
165 168
169 FileStreamMd5Digester::FileStreamMd5Digester()
170 : buffer_(new net::IOBuffer(kMd5DigestBufferSize)) {
171 }
172
173 FileStreamMd5Digester::~FileStreamMd5Digester() {
174 }
175
176 void FileStreamMd5Digester::GetMd5Digest(
177 scoped_ptr<storage::FileStreamReader> stream_reader,
178 const ResultCallback& callback) {
179 reader_ = stream_reader.Pass();
180 callback_ = callback;
181 base::MD5Init(&md5_context_);
182
183 // Start the read/hash.
184 ReadNextChunk();
185 }
186
187 void FileStreamMd5Digester::ReadNextChunk() {
188 const int result = reader_->Read(
189 buffer_.get(), kMd5DigestBufferSize,
190 base::Bind(&FileStreamMd5Digester::OnChunkRead, base::Unretained(this)));
191 if (result != net::ERR_IO_PENDING)
192 OnChunkRead(result);
193 }
194
195 void FileStreamMd5Digester::OnChunkRead(int result) {
196 if (result < 0) {
197 // Error - just return empty string.
198 callback_.Run("");
199 return;
200 } else if (result == 0) {
201 // EOF.
202 base::MD5Digest digest;
203 base::MD5Final(&digest, &md5_context_);
204 std::string result = MD5DigestToBase16(digest);
205 callback_.Run(result);
206 return;
207 }
208
209 // Read data and digest it.
210 base::MD5Update(&md5_context_, base::StringPiece(buffer_->data(), result));
211
212 // Kick off the next read.
213 ReadNextChunk();
214 }
215
166 std::string GetHostedDocumentExtension(const std::string& mime_type) { 216 std::string GetHostedDocumentExtension(const std::string& mime_type) {
167 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { 217 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) {
168 if (mime_type == kHostedDocumentKinds[i].mime_type) 218 if (mime_type == kHostedDocumentKinds[i].mime_type)
169 return kHostedDocumentKinds[i].extension; 219 return kHostedDocumentKinds[i].extension;
170 } 220 }
171 return kUnknownHostedDocumentExtension; 221 return kUnknownHostedDocumentExtension;
172 } 222 }
173 223
174 bool IsKnownHostedDocumentMimeType(const std::string& mime_type) { 224 bool IsKnownHostedDocumentMimeType(const std::string& mime_type) {
175 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { 225 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) {
176 if (mime_type == kHostedDocumentKinds[i].mime_type) 226 if (mime_type == kHostedDocumentKinds[i].mime_type)
177 return true; 227 return true;
178 } 228 }
179 return false; 229 return false;
180 } 230 }
181 231
182 bool HasHostedDocumentExtension(const base::FilePath& path) { 232 bool HasHostedDocumentExtension(const base::FilePath& path) {
183 const std::string extension = base::FilePath(path.Extension()).AsUTF8Unsafe(); 233 const std::string extension = base::FilePath(path.Extension()).AsUTF8Unsafe();
184 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { 234 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) {
185 if (extension == kHostedDocumentKinds[i].extension) 235 if (extension == kHostedDocumentKinds[i].extension)
186 return true; 236 return true;
187 } 237 }
188 return extension == kUnknownHostedDocumentExtension; 238 return extension == kUnknownHostedDocumentExtension;
189 } 239 }
190 240
191 } // namespace util 241 } // namespace util
192 } // namespace drive 242 } // namespace drive
OLDNEW
« no previous file with comments | « chrome/browser/drive/drive_api_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698