summaryrefslogtreecommitdiff
path: root/chromium/components/image_fetcher/ios/ios_image_data_fetcher_wrapper.h
blob: 428be06bc3913f5cd3b710189dc5e29890645928 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2017 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_IMAGE_FETCHER_IOS_IOS_IMAGE_DATA_FETCHER_WRAPPER_H_
#define COMPONENTS_IMAGE_FETCHER_IOS_IOS_IMAGE_DATA_FETCHER_WRAPPER_H_

#import <Foundation/Foundation.h>

#include "base/memory/ref_counted.h"
#include "components/data_use_measurement/core/data_use_user_data.h"
#include "components/image_fetcher/core/image_data_fetcher.h"

namespace net {
class URLRequestContextGetter;
}

class GURL;

namespace image_fetcher {

// Callback that informs of the download of an image encoded in |data| and the
// associated metadata. If an error prevented a http response,
// |metadata.http_response_code| will be RESPONSE_CODE_INVALID.
using IOSImageDataFetcherCallback = void (^)(NSData* data,
                                             const RequestMetadata& metadata);

class IOSImageDataFetcherWrapper {
 public:
  using DataUseServiceName = data_use_measurement::DataUseUserData::ServiceName;

  // The TaskRunner is used to decode the image if it is WebP-encoded.
  explicit IOSImageDataFetcherWrapper(
      net::URLRequestContextGetter* url_request_context_getter);
  virtual ~IOSImageDataFetcherWrapper();

  // Helper to start downloading and possibly decoding the image without a
  // referrer.
  virtual void FetchImageDataWebpDecoded(const GURL& image_url,
                                         IOSImageDataFetcherCallback callback);

  // Start downloading the image at the given |image_url|. The |callback| will
  // be called with the downloaded image, or nil if any error happened. If the
  // image is WebP it will be decoded.
  // The |referrer| and |referrer_policy| will be passed on to the underlying
  // URLFetcher.
  // |callback| cannot be nil.
  void FetchImageDataWebpDecoded(
      const GURL& image_url,
      IOSImageDataFetcherCallback callback,
      const std::string& referrer,
      net::URLRequest::ReferrerPolicy referrer_policy);

  // Sets a service name against which to track data usage.
  void SetDataUseServiceName(DataUseServiceName data_use_service_name);

 private:
  ImageDataFetcher::ImageDataFetcherCallback CallbackForImageDataFetcher(
      IOSImageDataFetcherCallback callback);

  ImageDataFetcher image_data_fetcher_;

  DISALLOW_COPY_AND_ASSIGN(IOSImageDataFetcherWrapper);
};

}  // namespace image_fetcher

#endif  // COMPONENTS_IMAGE_FETCHER_IOS_IOS_IMAGE_DATA_FETCHER_WRAPPER_H_