summaryrefslogtreecommitdiff
path: root/chromium/headless/public/util/url_fetcher.h
blob: 9dfc08a61546070efa800c75e21fc0c8cbe540f5 (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
69
70
71
72
73
// Copyright 2016 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 HEADLESS_PUBLIC_UTIL_URL_FETCHER_H_
#define HEADLESS_PUBLIC_UTIL_URL_FETCHER_H_

#include <stddef.h>
#include <string>
#include <vector>

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "headless/public/headless_export.h"
#include "net/base/net_errors.h"
#include "url/gurl.h"

namespace net {
class HttpRequestHeaders;
class HttpResponseHeaders;
}  // namespace net

namespace headless {

// An interface for fetching URLs. Note these are only intended to be used once.
class HEADLESS_EXPORT URLFetcher {
 public:
  URLFetcher() {}
  virtual ~URLFetcher() {}

  // Interface for reporting the fetch result.
  class HEADLESS_EXPORT ResultListener {
   public:
    ResultListener() {}

    // Informs the listener that the fetch failed.
    virtual void OnFetchStartError(net::Error error) = 0;

    // Informs the listener that the fetch succeeded and returns the HTTP
    // headers and the body.  NOTE |body| must be owned by the caller and remain
    // valid until the fetcher is destroyed,
    virtual void OnFetchComplete(
        const GURL& final_url,
        scoped_refptr<net::HttpResponseHeaders> response_headers,
        const char* body,
        size_t body_size) = 0;

    // Helper function which extracts the headers from |response_data| and calls
    // OnFetchComplete.
    void OnFetchCompleteExtractHeaders(const GURL& final_url,
                                       const char* response_data,
                                       size_t response_data_size);

   protected:
    virtual ~ResultListener() {}

   private:
    DISALLOW_COPY_AND_ASSIGN(ResultListener);
  };

  virtual void StartFetch(const GURL& url,
                          const std::string& method,
                          const std::string& post_data,
                          const net::HttpRequestHeaders& request_headers,
                          ResultListener* result_listener) = 0;

 private:
  DISALLOW_COPY_AND_ASSIGN(URLFetcher);
};

}  // namespace headless

#endif  // HEADLESS_PUBLIC_UTIL_URL_FETCHER_H_