File: file_stream_md5_digester.h

package info (click to toggle)
chromium 135.0.7049.95-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,959,392 kB
  • sloc: cpp: 34,198,526; ansic: 7,100,035; javascript: 3,985,800; python: 1,395,489; asm: 896,754; xml: 722,891; pascal: 180,504; sh: 94,909; perl: 88,388; objc: 79,739; sql: 53,020; cs: 41,358; fortran: 24,137; makefile: 22,501; php: 13,699; tcl: 10,142; yacc: 8,822; ruby: 7,350; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; awk: 197; sed: 36
file content (61 lines) | stat: -rw-r--r-- 2,088 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_EXTENSIONS_FILE_MANAGER_FILE_STREAM_MD5_DIGESTER_H_
#define CHROME_BROWSER_ASH_EXTENSIONS_FILE_MANAGER_FILE_STREAM_MD5_DIGESTER_H_

#include <memory>

#include "base/functional/callback.h"
#include "base/hash/md5.h"
#include "base/memory/ref_counted.h"
#include "net/base/io_buffer.h"

namespace storage {
class FileStreamReader;
}

namespace drive {
namespace util {

// Computes the (base-16 encoded) MD5 digest of data extracted from a file
// stream.
class FileStreamMd5Digester
    : public base::RefCountedThreadSafe<FileStreamMd5Digester> {
 public:
  using ResultCallback = base::OnceCallback<void(std::string)>;

  FileStreamMd5Digester();

  FileStreamMd5Digester(const FileStreamMd5Digester&) = delete;
  FileStreamMd5Digester& operator=(const FileStreamMd5Digester&) = delete;

  // Computes an MD5 digest of data read from the given |streamReader|.  The
  // work occurs asynchronously, and the resulting hash is returned via the
  // |callback|.  If an error occurs, |callback| is called with an empty string.
  // Only one stream can be processed at a time by each digester.  Do not call
  // GetMd5Digest before the results of a previous call have been returned.
  void GetMd5Digest(std::unique_ptr<storage::FileStreamReader> stream_reader,
                    ResultCallback callback);

 private:
  friend class base::RefCountedThreadSafe<FileStreamMd5Digester>;
  ~FileStreamMd5Digester();

  // Kicks off a read of the next chunk from the stream.
  void ReadNextChunk();
  // Handles the incoming chunk of data from a stream read.
  void OnChunkRead(int bytes_read);

  // Maximum chunk size for read operations.
  std::unique_ptr<storage::FileStreamReader> reader_;
  scoped_refptr<net::IOBuffer> buffer_;
  base::MD5Context md5_context_;
  ResultCallback callback_;
};

}  // namespace util
}  // namespace drive

#endif  // CHROME_BROWSER_ASH_EXTENSIONS_FILE_MANAGER_FILE_STREAM_MD5_DIGESTER_H_