blob: bc271b233d80a5d1c7ef1e8c201957e3d5d37e45 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// This file contains URLFetcher, a wrapper around URLRequest that handles
6// low-level details like thread safety, ref counting, and incremental buffer
7// reading. This is useful for callers who simply want to get the data from a
8// URL and don't care about all the nitty-gritty details.
9//
10// NOTE(willchan): Only one "IO" thread is supported for URLFetcher. This is a
11// temporary situation. We will work on allowing support for multiple "io"
12// threads per process.
13
14#ifndef NET_URL_REQUEST_URL_FETCHER_IMPL_H_
15#define NET_URL_REQUEST_URL_FETCHER_IMPL_H_
16
Torne (Richard Coles)cedac222014-06-03 10:58:34 +010017#include <string>
18
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019#include "base/basictypes.h"
20#include "base/compiler_specific.h"
21#include "net/base/net_export.h"
22#include "net/url_request/url_fetcher.h"
23
24namespace net {
25class URLFetcherCore;
26class URLFetcherDelegate;
27class URLFetcherFactory;
28
29class NET_EXPORT_PRIVATE URLFetcherImpl : public URLFetcher {
30 public:
31 // |url| is the URL to send the request to.
32 // |request_type| is the type of request to make.
33 // |d| the object that will receive the callback on fetch completion.
34 URLFetcherImpl(const GURL& url,
35 RequestType request_type,
36 URLFetcherDelegate* d);
37 virtual ~URLFetcherImpl();
38
39 // URLFetcher implementation:
40 virtual void SetUploadData(const std::string& upload_content_type,
41 const std::string& upload_content) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000042 virtual void SetUploadFilePath(
43 const std::string& upload_content_type,
44 const base::FilePath& file_path,
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010045 uint64 range_offset,
46 uint64 range_length,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000047 scoped_refptr<base::TaskRunner> file_task_runner) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000048 virtual void SetChunkedUpload(
49 const std::string& upload_content_type) OVERRIDE;
50 virtual void AppendChunkToUpload(const std::string& data,
51 bool is_last_chunk) OVERRIDE;
52 virtual void SetLoadFlags(int load_flags) OVERRIDE;
53 virtual int GetLoadFlags() const OVERRIDE;
54 virtual void SetReferrer(const std::string& referrer) OVERRIDE;
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000055 virtual void SetReferrerPolicy(
56 URLRequest::ReferrerPolicy referrer_policy) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000057 virtual void SetExtraRequestHeaders(
58 const std::string& extra_request_headers) OVERRIDE;
59 virtual void AddExtraRequestHeader(const std::string& header_line) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000060 virtual void SetRequestContext(
61 URLRequestContextGetter* request_context_getter) OVERRIDE;
62 virtual void SetFirstPartyForCookies(
63 const GURL& first_party_for_cookies) OVERRIDE;
64 virtual void SetURLRequestUserData(
65 const void* key,
66 const CreateDataCallback& create_data_callback) OVERRIDE;
67 virtual void SetStopOnRedirect(bool stop_on_redirect) OVERRIDE;
68 virtual void SetAutomaticallyRetryOn5xx(bool retry) OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000069 virtual void SetMaxRetriesOn5xx(int max_retries) OVERRIDE;
70 virtual int GetMaxRetriesOn5xx() const OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000071 virtual base::TimeDelta GetBackoffDelay() const OVERRIDE;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000072 virtual void SetAutomaticallyRetryOnNetworkChanges(int max_retries) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000073 virtual void SaveResponseToFileAtPath(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000074 const base::FilePath& file_path,
Torne (Richard Coles)f2477e02013-11-28 11:55:43 +000075 scoped_refptr<base::SequencedTaskRunner> file_task_runner) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000076 virtual void SaveResponseToTemporaryFile(
Torne (Richard Coles)f2477e02013-11-28 11:55:43 +000077 scoped_refptr<base::SequencedTaskRunner> file_task_runner) OVERRIDE;
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010078 virtual void SaveResponseWithWriter(
79 scoped_ptr<URLFetcherResponseWriter> response_writer) OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000080 virtual HttpResponseHeaders* GetResponseHeaders() const OVERRIDE;
81 virtual HostPortPair GetSocketAddress() const OVERRIDE;
82 virtual bool WasFetchedViaProxy() const OVERRIDE;
83 virtual void Start() OVERRIDE;
84 virtual const GURL& GetOriginalURL() const OVERRIDE;
85 virtual const GURL& GetURL() const OVERRIDE;
86 virtual const URLRequestStatus& GetStatus() const OVERRIDE;
87 virtual int GetResponseCode() const OVERRIDE;
88 virtual const ResponseCookies& GetCookies() const OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000089 virtual void ReceivedContentWasMalformed() OVERRIDE;
90 virtual bool GetResponseAsString(
91 std::string* out_response_string) const OVERRIDE;
92 virtual bool GetResponseAsFilePath(
93 bool take_ownership,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000094 base::FilePath* out_response_path) const OVERRIDE;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000095
96 static void CancelAll();
97
98 static void SetEnableInterceptionForTests(bool enabled);
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000099 static void SetIgnoreCertificateRequests(bool ignored);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000100
101 // TODO(akalin): Make these private again once URLFetcher::Create()
102 // is in net/.
103
104 static URLFetcherFactory* factory();
105
106 // Sets the factory used by the static method Create to create a URLFetcher.
107 // URLFetcher does not take ownership of |factory|. A value of NULL results
108 // in a URLFetcher being created directly.
109 //
110 // NOTE: for safety, this should only be used through ScopedURLFetcherFactory!
111 static void set_factory(URLFetcherFactory* factory);
112
113 protected:
114 // Returns the delegate.
115 URLFetcherDelegate* delegate() const;
116
117 private:
118 friend class URLFetcherTest;
119
120 // Only used by URLFetcherTest, returns the number of URLFetcher::Core objects
121 // actively running.
122 static int GetNumFetcherCores();
123
124 const scoped_refptr<URLFetcherCore> core_;
125
126 DISALLOW_COPY_AND_ASSIGN(URLFetcherImpl);
127};
128
129} // namespace net
130
131#endif // NET_URL_REQUEST_URL_FETCHER_IMPL_H_