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"
16 const int kMd5DigestBufferSize
= 512 * 1024; // 512 kB.
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
) {
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
,
47 // Error - just return empty string.
50 } else if (bytes_read
== 0) {
52 base::MD5Digest digest
;
53 base::MD5Final(&digest
, &md5_context_
);
54 std::string result
= base::MD5DigestToBase16(digest
);
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
);