| 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" |
| (...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 : buffer_(new net::IOBuffer(kMd5DigestBufferSize)) { | 170 : buffer_(new net::IOBuffer(kMd5DigestBufferSize)) { |
| 171 } | 171 } |
| 172 | 172 |
| 173 FileStreamMd5Digester::~FileStreamMd5Digester() { | 173 FileStreamMd5Digester::~FileStreamMd5Digester() { |
| 174 } | 174 } |
| 175 | 175 |
| 176 void FileStreamMd5Digester::GetMd5Digest( | 176 void FileStreamMd5Digester::GetMd5Digest( |
| 177 scoped_ptr<storage::FileStreamReader> stream_reader, | 177 scoped_ptr<storage::FileStreamReader> stream_reader, |
| 178 const ResultCallback& callback) { | 178 const ResultCallback& callback) { |
| 179 reader_ = stream_reader.Pass(); | 179 reader_ = stream_reader.Pass(); |
| 180 callback_ = callback; | |
| 181 base::MD5Init(&md5_context_); | 180 base::MD5Init(&md5_context_); |
| 182 | 181 |
| 183 // Start the read/hash. | 182 // Start the read/hash. |
| 184 ReadNextChunk(); | 183 ReadNextChunk(callback); |
| 185 } | 184 } |
| 186 | 185 |
| 187 void FileStreamMd5Digester::ReadNextChunk() { | 186 void FileStreamMd5Digester::ReadNextChunk(const ResultCallback& callback) { |
| 188 const int result = reader_->Read( | 187 const int result = |
| 189 buffer_.get(), kMd5DigestBufferSize, | 188 reader_->Read(buffer_.get(), kMd5DigestBufferSize, |
| 190 base::Bind(&FileStreamMd5Digester::OnChunkRead, base::Unretained(this))); | 189 base::Bind(&FileStreamMd5Digester::OnChunkRead, |
| 190 base::Unretained(this), callback)); |
| 191 if (result != net::ERR_IO_PENDING) | 191 if (result != net::ERR_IO_PENDING) |
| 192 OnChunkRead(result); | 192 OnChunkRead(callback, result); |
| 193 } | 193 } |
| 194 | 194 |
| 195 void FileStreamMd5Digester::OnChunkRead(int bytesRead) { | 195 void FileStreamMd5Digester::OnChunkRead(const ResultCallback& callback, |
| 196 if (bytesRead < 0) { | 196 int bytes_read) { |
| 197 if (bytes_read < 0) { |
| 197 // Error - just return empty string. | 198 // Error - just return empty string. |
| 198 callback_.Run(""); | 199 callback.Run(""); |
| 199 return; | 200 return; |
| 200 } else if (bytesRead == 0) { | 201 } else if (bytes_read == 0) { |
| 201 // EOF. | 202 // EOF. |
| 202 base::MD5Digest digest; | 203 base::MD5Digest digest; |
| 203 base::MD5Final(&digest, &md5_context_); | 204 base::MD5Final(&digest, &md5_context_); |
| 204 std::string result = MD5DigestToBase16(digest); | 205 std::string result = MD5DigestToBase16(digest); |
| 205 callback_.Run(result); | 206 callback.Run(result); |
| 206 return; | 207 return; |
| 207 } | 208 } |
| 208 | 209 |
| 209 // Read data and digest it. | 210 // Read data and digest it. |
| 210 base::MD5Update(&md5_context_, base::StringPiece(buffer_->data(), bytesRead)); | 211 base::MD5Update(&md5_context_, |
| 212 base::StringPiece(buffer_->data(), bytes_read)); |
| 211 | 213 |
| 212 // Kick off the next read. | 214 // Kick off the next read. |
| 213 ReadNextChunk(); | 215 ReadNextChunk(callback); |
| 214 } | 216 } |
| 215 | 217 |
| 216 std::string GetHostedDocumentExtension(const std::string& mime_type) { | 218 std::string GetHostedDocumentExtension(const std::string& mime_type) { |
| 217 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { | 219 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { |
| 218 if (mime_type == kHostedDocumentKinds[i].mime_type) | 220 if (mime_type == kHostedDocumentKinds[i].mime_type) |
| 219 return kHostedDocumentKinds[i].extension; | 221 return kHostedDocumentKinds[i].extension; |
| 220 } | 222 } |
| 221 return kUnknownHostedDocumentExtension; | 223 return kUnknownHostedDocumentExtension; |
| 222 } | 224 } |
| 223 | 225 |
| 224 bool IsKnownHostedDocumentMimeType(const std::string& mime_type) { | 226 bool IsKnownHostedDocumentMimeType(const std::string& mime_type) { |
| 225 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { | 227 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { |
| 226 if (mime_type == kHostedDocumentKinds[i].mime_type) | 228 if (mime_type == kHostedDocumentKinds[i].mime_type) |
| 227 return true; | 229 return true; |
| 228 } | 230 } |
| 229 return false; | 231 return false; |
| 230 } | 232 } |
| 231 | 233 |
| 232 bool HasHostedDocumentExtension(const base::FilePath& path) { | 234 bool HasHostedDocumentExtension(const base::FilePath& path) { |
| 233 const std::string extension = base::FilePath(path.Extension()).AsUTF8Unsafe(); | 235 const std::string extension = base::FilePath(path.Extension()).AsUTF8Unsafe(); |
| 234 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { | 236 for (size_t i = 0; i < arraysize(kHostedDocumentKinds); ++i) { |
| 235 if (extension == kHostedDocumentKinds[i].extension) | 237 if (extension == kHostedDocumentKinds[i].extension) |
| 236 return true; | 238 return true; |
| 237 } | 239 } |
| 238 return extension == kUnknownHostedDocumentExtension; | 240 return extension == kUnknownHostedDocumentExtension; |
| 239 } | 241 } |
| 240 | 242 |
| 241 } // namespace util | 243 } // namespace util |
| 242 } // namespace drive | 244 } // namespace drive |
| OLD | NEW |