Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / chromeos / extensions / file_manager / file_stream_md5_digester.cc
blob62b84e9257cc2335ef9ed97989897fb278ec4a6f
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/extensions/file_manager/file_stream_md5_digester.h"
7 #include "base/callback.h"
8 #include "net/base/net_errors.h"
9 #include "storage/browser/fileapi/file_stream_reader.h"
11 namespace drive {
12 namespace util {
14 namespace {
16 const int kMd5DigestBufferSize = 512 * 1024; // 512 kB.
18 } // namespace
20 FileStreamMd5Digester::FileStreamMd5Digester()
21 : buffer_(new net::IOBuffer(kMd5DigestBufferSize)) {}
23 FileStreamMd5Digester::~FileStreamMd5Digester() {}
25 void FileStreamMd5Digester::GetMd5Digest(
26 scoped_ptr<storage::FileStreamReader> stream_reader,
27 const ResultCallback& callback) {
28 reader_ = stream_reader.Pass();
29 base::MD5Init(&md5_context_);
31 // Start the read/hash.
32 ReadNextChunk(callback);
35 void FileStreamMd5Digester::ReadNextChunk(const ResultCallback& callback) {
36 const int result =
37 reader_->Read(buffer_.get(), kMd5DigestBufferSize,
38 base::Bind(&FileStreamMd5Digester::OnChunkRead,
39 base::Unretained(this), callback));
40 if (result != net::ERR_IO_PENDING)
41 OnChunkRead(callback, result);
44 void FileStreamMd5Digester::OnChunkRead(const ResultCallback& callback,
45 int bytes_read) {
46 if (bytes_read < 0) {
47 // Error - just return empty string.
48 callback.Run("");
49 return;
50 } else if (bytes_read == 0) {
51 // EOF.
52 base::MD5Digest digest;
53 base::MD5Final(&digest, &md5_context_);
54 std::string result = base::MD5DigestToBase16(digest);
55 callback.Run(result);
56 return;
59 // Read data and digest it.
60 base::MD5Update(&md5_context_,
61 base::StringPiece(buffer_->data(), bytes_read));
63 // Kick off the next read.
64 ReadNextChunk(callback);
67 } // namespace util
68 } // namespace drive