summaryrefslogtreecommitdiff
path: root/chromium/components/speech/downstream_loader.cc
blob: 14ca74922de05ca3fe562a0bea6a8d0dcc27298c (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
// Copyright 2020 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.

#include "components/speech/downstream_loader.h"

#include "base/callback.h"
#include "components/speech/downstream_loader_client.h"

namespace speech {

DownstreamLoader::DownstreamLoader(
    std::unique_ptr<network::ResourceRequest> resource_request,
    net::NetworkTrafficAnnotationTag upstream_traffic_annotation,
    network::mojom::URLLoaderFactory* url_loader_factory,
    DownstreamLoaderClient* downstream_loader_client)
    : downstream_loader_client_(downstream_loader_client) {
  DCHECK(downstream_loader_client_);
  simple_url_loader_ = network::SimpleURLLoader::Create(
      std::move(resource_request), upstream_traffic_annotation);
  simple_url_loader_->DownloadAsStream(url_loader_factory, this);
}

DownstreamLoader::~DownstreamLoader() = default;

void DownstreamLoader::OnDataReceived(base::StringPiece string_piece,
                                      base::OnceClosure resume) {
  downstream_loader_client_->OnDownstreamDataReceived(string_piece);
  std::move(resume).Run();
}

void DownstreamLoader::OnComplete(bool success) {
  int response_code = -1;
  if (simple_url_loader_->ResponseInfo() &&
      simple_url_loader_->ResponseInfo()->headers) {
    response_code =
        simple_url_loader_->ResponseInfo()->headers->response_code();
  }

  downstream_loader_client_->OnDownstreamDataComplete(success, response_code);
}

void DownstreamLoader::OnRetry(base::OnceClosure start_retry) {
  // Retries are not enabled for these requests.
  NOTREACHED();
}

}  // namespace speech