File: bitmap_fetcher.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (107 lines) | stat: -rw-r--r-- 3,865 bytes parent folder | download | duplicates (5)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"

#include "base/functional/bind.h"
#include "base/task/sequenced_task_runner.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/storage_partition.h"
#include "net/base/data_url.h"
#include "net/cookies/site_for_cookies.h"
#include "net/http/http_request_headers.h"
#include "services/network/public/cpp/resource_request.h"
#include "url/url_constants.h"

BitmapFetcher::BitmapFetcher(
    const GURL& url,
    BitmapFetcherDelegate* delegate,
    const net::NetworkTrafficAnnotationTag& traffic_annotation)
    : BitmapFetcher(url, delegate, traffic_annotation, nullptr) {}

BitmapFetcher::BitmapFetcher(
    const GURL& url,
    BitmapFetcherDelegate* delegate,
    const net::NetworkTrafficAnnotationTag& traffic_annotation,
    data_decoder::DataDecoder* data_decoder)
    : ImageDecoder::ImageRequest(data_decoder),
      url_(url),
      delegate_(delegate),
      traffic_annotation_(traffic_annotation) {}

BitmapFetcher::~BitmapFetcher() = default;

void BitmapFetcher::Init(net::ReferrerPolicy referrer_policy,
                         network::mojom::CredentialsMode credentials_mode,
                         const net::HttpRequestHeaders& additional_headers,
                         const url::Origin& initiator,
                         bool is_same_site_request) {
  if (simple_loader_ != nullptr)
    return;

  auto resource_request = std::make_unique<network::ResourceRequest>();
  resource_request->url = url_;
  resource_request->referrer_policy = referrer_policy;
  resource_request->credentials_mode = credentials_mode;
  if (is_same_site_request) {
    resource_request->site_for_cookies =
        net::SiteForCookies::FromUrl(resource_request->url);
  }
  resource_request->headers.MergeFrom(additional_headers);
  resource_request->request_initiator = initiator;
  simple_loader_ = network::SimpleURLLoader::Create(std::move(resource_request),
                                                    traffic_annotation_);
}

void BitmapFetcher::Start(network::mojom::URLLoaderFactory* loader_factory) {
  network::SimpleURLLoader::BodyAsStringCallbackDeprecated callback =
      base::BindOnce(&BitmapFetcher::OnSimpleLoaderComplete,
                     weak_factory_.GetWeakPtr());

  // Early exit to handle data URLs.
  if (url_.SchemeIs(url::kDataScheme)) {
    std::string mime_type, charset, data;
    std::unique_ptr<std::string> response_body;
    if (net::DataURL::Parse(url_, &mime_type, &charset, &data))
      response_body = std::make_unique<std::string>(std::move(data));

    // Post a task to maintain our guarantee that the delegate will only be
    // called asynchronously.
    base::SequencedTaskRunner::GetCurrentDefault()->PostTask(
        FROM_HERE, BindOnce(std::move(callback), std::move(response_body)));
    return;
  }

  if (simple_loader_) {
    simple_loader_->DownloadToStringOfUnboundedSizeUntilCrashAndDie(
        loader_factory, std::move(callback));
  }
}

void BitmapFetcher::OnSimpleLoaderComplete(
    std::unique_ptr<std::string> response_body) {
  if (!response_body) {
    ReportFailure();
    return;
  }

  // Call start to begin decoding.  The ImageDecoder will call OnImageDecoded
  // with the data when it is done.
  ImageDecoder::Start(this, std::move(*response_body));
}

// Methods inherited from ImageDecoder::ImageRequest.

void BitmapFetcher::OnImageDecoded(const SkBitmap& decoded_image) {
  delegate_->OnFetchComplete(url_, &decoded_image);
}

void BitmapFetcher::OnDecodeImageFailed() {
  ReportFailure();
}

void BitmapFetcher::ReportFailure() {
  delegate_->OnFetchComplete(url_, nullptr);
}