summaryrefslogtreecommitdiff
path: root/chromium/components/download/public/common/input_stream.h
blob: 938daba2eb469324f87d1e06f1d3fa52159a46c2 (plain)
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
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_DOWNLOAD_PUBLIC_COMMON_INPUT_STREAM_H_
#define COMPONENTS_DOWNLOAD_PUBLIC_COMMON_INPUT_STREAM_H_

#include "components/download/public/common/download_export.h"
#include "components/download/public/common/download_interrupt_reasons.h"
#include "mojo/public/cpp/system/simple_watcher.h"
#include "net/base/io_buffer.h"

namespace download {

// Input stream for the download system to read after network response is
// received.
class COMPONENTS_DOWNLOAD_EXPORT InputStream {
 public:
  // Results for reading the InputStream.
  enum StreamState {
    EMPTY = 0,
    HAS_DATA,
    WAIT_FOR_COMPLETION,  // No more data to read, waiting for completion status
    COMPLETE,
  };

  virtual ~InputStream();

  // Initializes the inputStream object.
  virtual void Initialize();

  // Returns true if the input stream contains no data, or false otherwise.
  virtual bool IsEmpty() = 0;

  // Register/clear callbacks when data become available.
  virtual void RegisterDataReadyCallback(
      const mojo::SimpleWatcher::ReadyCallback& callback);
  virtual void ClearDataReadyCallback();

  // Registers stream completion callback if needed.
  virtual void RegisterCompletionCallback(base::OnceClosure callback);

  // Reads data from the stream into |data|, |length| is the number of bytes
  // returned.
  virtual StreamState Read(scoped_refptr<net::IOBuffer>* data,
                           size_t* length) = 0;

  // Returns the completion status.
  virtual DownloadInterruptReason GetCompletionStatus() = 0;
};

}  // namespace download

#endif  // COMPONENTS_DOWNLOAD_PUBLIC_COMMON_INPUT_STREAM_H_