Chromium Code Reviews| OLD | NEW |
|---|---|
| 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; |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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(kBufferSize_)), weak_ptr_factory_(this) { | |
| 171 } | |
| 172 | |
| 173 FileStreamMd5Digester::~FileStreamMd5Digester() { | |
| 174 } | |
| 175 | |
| 176 void FileStreamMd5Digester::GetMd5Digest( | |
| 177 scoped_ptr<storage::FileStreamReader> streamReader, | |
| 178 const base::Callback<void(const std::string&)>& callback) { | |
| 179 reader_ = streamReader.Pass(); | |
| 180 callback_ = callback; | |
| 181 base::MD5Init(&md5Context_); | |
| 182 | |
| 183 // Start the read/hash. | |
| 184 readNextChunk(); | |
| 185 } | |
| 186 | |
| 187 void FileStreamMd5Digester::readNextChunk() { | |
| 188 int result = reader_->Read(buffer_.get(), kBufferSize_, | |
|
mtomasz
2015/01/31 04:53:12
IIRC reader may have to be called on IO thread onl
mtomasz
2015/01/31 04:53:12
nit: const int
Ben Kwa
2015/02/02 22:22:11
Hm, you're right; I misread the FileStreamReader::
Ben Kwa
2015/02/02 22:22:11
Done.
| |
| 189 base::Bind(&FileStreamMd5Digester::OnChunkRead, | |
| 190 weak_ptr_factory_.GetWeakPtr())); | |
| 191 if (result != net::ERR_IO_PENDING) { | |
| 192 OnChunkRead(result); | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 void FileStreamMd5Digester::OnChunkRead(int result) { | |
| 197 if (result < 0) { | |
| 198 // Error - just return empty string | |
|
mtomasz
2015/01/31 04:53:12
nit: period at the end of comments.
Ben Kwa
2015/02/02 22:22:11
Done.
| |
| 199 callback_.Run(""); | |
| 200 return; | |
| 201 } else if (result == 0) { | |
| 202 // EOF | |
| 203 base::MD5Digest digest; | |
| 204 base::MD5Final(&digest, &md5Context_); | |
| 205 std::string result = MD5DigestToBase16(digest); | |
| 206 callback_.Run(result); | |
| 207 return; | |
| 208 } | |
| 209 | |
| 210 // Read data and digest it. | |
| 211 base::MD5Update(&md5Context_, base::StringPiece(buffer_->data(), result)); | |
| 212 | |
| 213 // Kick off the next read. | |
| 214 readNextChunk(); | |
| 215 } | |
| 216 | |
| 166 std::string GetHostedDocumentExtension(const std::string& mime_type) { | 217 std::string GetHostedDocumentExtension(const std::string& mime_type) { |
| 167 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { | 218 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { |
| 168 if (mime_type == kHostedDocumentKinds[i].mime_type) | 219 if (mime_type == kHostedDocumentKinds[i].mime_type) |
| 169 return kHostedDocumentKinds[i].extension; | 220 return kHostedDocumentKinds[i].extension; |
| 170 } | 221 } |
| 171 return kUnknownHostedDocumentExtension; | 222 return kUnknownHostedDocumentExtension; |
| 172 } | 223 } |
| 173 | 224 |
| 174 bool IsKnownHostedDocumentMimeType(const std::string& mime_type) { | 225 bool IsKnownHostedDocumentMimeType(const std::string& mime_type) { |
| 175 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { | 226 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { |
| 176 if (mime_type == kHostedDocumentKinds[i].mime_type) | 227 if (mime_type == kHostedDocumentKinds[i].mime_type) |
| 177 return true; | 228 return true; |
| 178 } | 229 } |
| 179 return false; | 230 return false; |
| 180 } | 231 } |
| 181 | 232 |
| 182 bool HasHostedDocumentExtension(const base::FilePath& path) { | 233 bool HasHostedDocumentExtension(const base::FilePath& path) { |
| 183 const std::string extension = base::FilePath(path.Extension()).AsUTF8Unsafe(); | 234 const std::string extension = base::FilePath(path.Extension()).AsUTF8Unsafe(); |
| 184 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { | 235 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { |
| 185 if (extension == kHostedDocumentKinds[i].extension) | 236 if (extension == kHostedDocumentKinds[i].extension) |
| 186 return true; | 237 return true; |
| 187 } | 238 } |
| 188 return extension == kUnknownHostedDocumentExtension; | 239 return extension == kUnknownHostedDocumentExtension; |
| 189 } | 240 } |
| 190 | 241 |
| 191 } // namespace util | 242 } // namespace util |
| 192 } // namespace drive | 243 } // namespace drive |
| OLD | NEW |