1 // 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.
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.
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.
14 #ifndef NET_URL_REQUEST_URL_FETCHER_IMPL_H_
15 #define NET_URL_REQUEST_URL_FETCHER_IMPL_H_
21 #include "base/basictypes.h"
22 #include "base/compiler_specific.h"
23 #include "net/base/net_export.h"
24 #include "net/url_request/url_fetcher.h"
28 class URLFetcherDelegate
;
29 class URLFetcherFactory
;
31 class NET_EXPORT_PRIVATE URLFetcherImpl
: public URLFetcher
{
33 // |url| is the URL to send the request to.
34 // |request_type| is the type of request to make.
35 // |d| the object that will receive the callback on fetch completion.
36 URLFetcherImpl(const GURL
& url
,
37 RequestType request_type
,
38 URLFetcherDelegate
* d
);
39 ~URLFetcherImpl() override
;
41 // URLFetcher implementation:
42 void SetUploadData(const std::string
& upload_content_type
,
43 const std::string
& upload_content
) override
;
44 void SetUploadFilePath(
45 const std::string
& upload_content_type
,
46 const base::FilePath
& file_path
,
49 scoped_refptr
<base::TaskRunner
> file_task_runner
) override
;
50 void SetUploadStreamFactory(
51 const std::string
& upload_content_type
,
52 const CreateUploadStreamCallback
& callback
) override
;
53 void SetChunkedUpload(const std::string
& upload_content_type
) override
;
54 void AppendChunkToUpload(const std::string
& data
,
55 bool is_last_chunk
) override
;
56 void SetLoadFlags(int load_flags
) override
;
57 int GetLoadFlags() const override
;
58 void SetReferrer(const std::string
& referrer
) override
;
59 void SetReferrerPolicy(URLRequest::ReferrerPolicy referrer_policy
) override
;
60 void SetExtraRequestHeaders(
61 const std::string
& extra_request_headers
) override
;
62 void AddExtraRequestHeader(const std::string
& header_line
) override
;
63 void SetRequestContext(
64 URLRequestContextGetter
* request_context_getter
) override
;
65 void SetFirstPartyForCookies(const GURL
& first_party_for_cookies
) override
;
66 void SetURLRequestUserData(
68 const CreateDataCallback
& create_data_callback
) override
;
69 void SetStopOnRedirect(bool stop_on_redirect
) override
;
70 void SetAutomaticallyRetryOn5xx(bool retry
) override
;
71 void SetMaxRetriesOn5xx(int max_retries
) override
;
72 int GetMaxRetriesOn5xx() const override
;
73 base::TimeDelta
GetBackoffDelay() const override
;
74 void SetAutomaticallyRetryOnNetworkChanges(int max_retries
) override
;
75 void SaveResponseToFileAtPath(
76 const base::FilePath
& file_path
,
77 scoped_refptr
<base::SequencedTaskRunner
> file_task_runner
) override
;
78 void SaveResponseToTemporaryFile(
79 scoped_refptr
<base::SequencedTaskRunner
> file_task_runner
) override
;
80 void SaveResponseWithWriter(
81 scoped_ptr
<URLFetcherResponseWriter
> response_writer
) override
;
82 HttpResponseHeaders
* GetResponseHeaders() const override
;
83 HostPortPair
GetSocketAddress() const override
;
84 bool WasFetchedViaProxy() const override
;
85 bool WasCached() const override
;
86 int64_t GetReceivedResponseContentLength() const override
;
87 int64_t GetTotalReceivedBytes() const override
;
88 void Start() override
;
89 const GURL
& GetOriginalURL() const override
;
90 const GURL
& GetURL() const override
;
91 const URLRequestStatus
& GetStatus() const override
;
92 int GetResponseCode() const override
;
93 const ResponseCookies
& GetCookies() const override
;
94 void ReceivedContentWasMalformed() override
;
95 bool GetResponseAsString(std::string
* out_response_string
) const override
;
96 bool GetResponseAsFilePath(bool take_ownership
,
97 base::FilePath
* out_response_path
) const override
;
99 static void CancelAll();
101 static void SetIgnoreCertificateRequests(bool ignored
);
103 // TODO(akalin): Make these private again once URLFetcher::Create()
106 static URLFetcherFactory
* factory();
108 // Sets the factory used by the static method Create to create a URLFetcher.
109 // URLFetcher does not take ownership of |factory|. A value of NULL results
110 // in a URLFetcher being created directly.
112 // NOTE: for safety, this should only be used through ScopedURLFetcherFactory!
113 static void set_factory(URLFetcherFactory
* factory
);
116 // Returns the delegate.
117 URLFetcherDelegate
* delegate() const;
120 friend class URLFetcherTest
;
122 // Only used by URLFetcherTest, returns the number of URLFetcher::Core objects
124 static int GetNumFetcherCores();
126 const scoped_refptr
<URLFetcherCore
> core_
;
128 DISALLOW_COPY_AND_ASSIGN(URLFetcherImpl
);
133 #endif // NET_URL_REQUEST_URL_FETCHER_IMPL_H_