OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file contains URLFetcher, a wrapper around net::URLRequest that handles | 5 // This file contains URLFetcher, a wrapper around URLRequest that handles |
6 // low-level details like thread safety, ref counting, and incremental buffer | 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 | 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. | 8 // URL and don't care about all the nitty-gritty details. |
9 // | 9 // |
10 // NOTE(willchan): Only one "IO" thread is supported for URLFetcher. This is a | 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" | 11 // temporary situation. We will work on allowing support for multiple "io" |
12 // threads per process. | 12 // threads per process. |
13 | 13 |
14 #ifndef CONTENT_COMMON_NET_URL_FETCHER_IMPL_H_ | 14 #ifndef NET_URL_REQUEST_URL_FETCHER_IMPL_H_ |
15 #define CONTENT_COMMON_NET_URL_FETCHER_IMPL_H_ | 15 #define NET_URL_REQUEST_URL_FETCHER_IMPL_H_ |
16 #pragma once | 16 #pragma once |
17 | 17 |
18 #include "base/basictypes.h" | 18 #include "base/basictypes.h" |
19 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
20 #include "content/common/content_export.h" | 20 #include "net/base/net_export.h" |
21 #include "content/public/common/url_fetcher.h" | 21 #include "net/url_request/url_fetcher.h" |
22 | 22 |
23 namespace net { | 23 namespace net { |
24 class URLFetcherCore; | 24 class URLFetcherCore; |
25 class URLFetcherDelegate; | 25 class URLFetcherDelegate; |
26 class URLFetcherFactory; | 26 class URLFetcherFactory; |
27 } // namespace net | |
28 | 27 |
29 class CONTENT_EXPORT URLFetcherImpl : public net::URLFetcher { | 28 // TODO(akalin): Remove NET_EXPORT once URLFetcher::Create is in net/. |
| 29 class NET_EXPORT URLFetcherImpl : public URLFetcher { |
30 public: | 30 public: |
31 // |url| is the URL to send the request to. | 31 // |url| is the URL to send the request to. |
32 // |request_type| is the type of request to make. | 32 // |request_type| is the type of request to make. |
33 // |d| the object that will receive the callback on fetch completion. | 33 // |d| the object that will receive the callback on fetch completion. |
34 URLFetcherImpl(const GURL& url, | 34 URLFetcherImpl(const GURL& url, |
35 RequestType request_type, | 35 RequestType request_type, |
36 net::URLFetcherDelegate* d); | 36 URLFetcherDelegate* d); |
37 virtual ~URLFetcherImpl(); | 37 virtual ~URLFetcherImpl(); |
38 | 38 |
39 // net::URLFetcher implementation: | 39 // URLFetcher implementation: |
40 virtual void SetUploadData(const std::string& upload_content_type, | 40 virtual void SetUploadData(const std::string& upload_content_type, |
41 const std::string& upload_content) OVERRIDE; | 41 const std::string& upload_content) OVERRIDE; |
42 virtual void SetChunkedUpload( | 42 virtual void SetChunkedUpload( |
43 const std::string& upload_content_type) OVERRIDE; | 43 const std::string& upload_content_type) OVERRIDE; |
44 virtual void AppendChunkToUpload(const std::string& data, | 44 virtual void AppendChunkToUpload(const std::string& data, |
45 bool is_last_chunk) OVERRIDE; | 45 bool is_last_chunk) OVERRIDE; |
46 virtual void SetLoadFlags(int load_flags) OVERRIDE; | 46 virtual void SetLoadFlags(int load_flags) OVERRIDE; |
47 virtual int GetLoadFlags() const OVERRIDE; | 47 virtual int GetLoadFlags() const OVERRIDE; |
48 virtual void SetReferrer(const std::string& referrer) OVERRIDE; | 48 virtual void SetReferrer(const std::string& referrer) OVERRIDE; |
49 virtual void SetExtraRequestHeaders( | 49 virtual void SetExtraRequestHeaders( |
50 const std::string& extra_request_headers) OVERRIDE; | 50 const std::string& extra_request_headers) OVERRIDE; |
51 virtual void AddExtraRequestHeader(const std::string& header_line) OVERRIDE; | 51 virtual void AddExtraRequestHeader(const std::string& header_line) OVERRIDE; |
52 virtual void GetExtraRequestHeaders( | 52 virtual void GetExtraRequestHeaders( |
53 net::HttpRequestHeaders* headers) const OVERRIDE; | 53 HttpRequestHeaders* headers) const OVERRIDE; |
54 virtual void SetRequestContext( | 54 virtual void SetRequestContext( |
55 net::URLRequestContextGetter* request_context_getter) OVERRIDE; | 55 URLRequestContextGetter* request_context_getter) OVERRIDE; |
56 virtual void SetFirstPartyForCookies( | 56 virtual void SetFirstPartyForCookies( |
57 const GURL& first_party_for_cookies) OVERRIDE; | 57 const GURL& first_party_for_cookies) OVERRIDE; |
58 virtual void SetURLRequestUserData( | 58 virtual void SetURLRequestUserData( |
59 const void* key, | 59 const void* key, |
60 const CreateDataCallback& create_data_callback) OVERRIDE; | 60 const CreateDataCallback& create_data_callback) OVERRIDE; |
61 virtual void SetStopOnRedirect(bool stop_on_redirect) OVERRIDE; | 61 virtual void SetStopOnRedirect(bool stop_on_redirect) OVERRIDE; |
62 virtual void SetAutomaticallyRetryOn5xx(bool retry) OVERRIDE; | 62 virtual void SetAutomaticallyRetryOn5xx(bool retry) OVERRIDE; |
63 virtual void SetMaxRetries(int max_retries) OVERRIDE; | 63 virtual void SetMaxRetries(int max_retries) OVERRIDE; |
64 virtual int GetMaxRetries() const OVERRIDE; | 64 virtual int GetMaxRetries() const OVERRIDE; |
65 virtual base::TimeDelta GetBackoffDelay() const OVERRIDE; | 65 virtual base::TimeDelta GetBackoffDelay() const OVERRIDE; |
66 virtual void SaveResponseToFileAtPath( | 66 virtual void SaveResponseToFileAtPath( |
67 const FilePath& file_path, | 67 const FilePath& file_path, |
68 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; | 68 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; |
69 virtual void SaveResponseToTemporaryFile( | 69 virtual void SaveResponseToTemporaryFile( |
70 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; | 70 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; |
71 virtual net::HttpResponseHeaders* GetResponseHeaders() const OVERRIDE; | 71 virtual HttpResponseHeaders* GetResponseHeaders() const OVERRIDE; |
72 virtual net::HostPortPair GetSocketAddress() const OVERRIDE; | 72 virtual HostPortPair GetSocketAddress() const OVERRIDE; |
73 virtual bool WasFetchedViaProxy() const OVERRIDE; | 73 virtual bool WasFetchedViaProxy() const OVERRIDE; |
74 virtual void Start() OVERRIDE; | 74 virtual void Start() OVERRIDE; |
75 virtual const GURL& GetOriginalURL() const OVERRIDE; | 75 virtual const GURL& GetOriginalURL() const OVERRIDE; |
76 virtual const GURL& GetURL() const OVERRIDE; | 76 virtual const GURL& GetURL() const OVERRIDE; |
77 virtual const net::URLRequestStatus& GetStatus() const OVERRIDE; | 77 virtual const URLRequestStatus& GetStatus() const OVERRIDE; |
78 virtual int GetResponseCode() const OVERRIDE; | 78 virtual int GetResponseCode() const OVERRIDE; |
79 virtual const net::ResponseCookies& GetCookies() const OVERRIDE; | 79 virtual const ResponseCookies& GetCookies() const OVERRIDE; |
80 virtual bool FileErrorOccurred( | 80 virtual bool FileErrorOccurred( |
81 base::PlatformFileError* out_error_code) const OVERRIDE; | 81 base::PlatformFileError* out_error_code) const OVERRIDE; |
82 virtual void ReceivedContentWasMalformed() OVERRIDE; | 82 virtual void ReceivedContentWasMalformed() OVERRIDE; |
83 virtual bool GetResponseAsString( | 83 virtual bool GetResponseAsString( |
84 std::string* out_response_string) const OVERRIDE; | 84 std::string* out_response_string) const OVERRIDE; |
85 virtual bool GetResponseAsFilePath( | 85 virtual bool GetResponseAsFilePath( |
86 bool take_ownership, | 86 bool take_ownership, |
87 FilePath* out_response_path) const OVERRIDE; | 87 FilePath* out_response_path) const OVERRIDE; |
88 | 88 |
89 static void CancelAll(); | 89 static void CancelAll(); |
90 | 90 |
| 91 static void SetEnableInterceptionForTests(bool enabled); |
| 92 |
| 93 // TODO(akalin): Make these private again once URLFetcher::Create() |
| 94 // is in net/. |
| 95 |
| 96 static URLFetcherFactory* factory(); |
| 97 |
| 98 // Sets the factory used by the static method Create to create a URLFetcher. |
| 99 // URLFetcher does not take ownership of |factory|. A value of NULL results |
| 100 // in a URLFetcher being created directly. |
| 101 // |
| 102 // NOTE: for safety, this should only be used through ScopedURLFetcherFactory! |
| 103 static void set_factory(URLFetcherFactory* factory); |
| 104 |
91 protected: | 105 protected: |
92 // Returns the delegate. | 106 // Returns the delegate. |
93 net::URLFetcherDelegate* delegate() const; | 107 URLFetcherDelegate* delegate() const; |
94 | 108 |
95 private: | 109 private: |
96 friend class ScopedURLFetcherFactory; | |
97 friend class TestURLFetcher; | |
98 friend class URLFetcherTest; | 110 friend class URLFetcherTest; |
99 | 111 |
100 // Only used by URLFetcherTest, returns the number of URLFetcher::Core objects | 112 // Only used by URLFetcherTest, returns the number of URLFetcher::Core objects |
101 // actively running. | 113 // actively running. |
102 static int GetNumFetcherCores(); | 114 static int GetNumFetcherCores(); |
103 | 115 |
104 static net::URLFetcherFactory* factory(); | 116 const scoped_refptr<URLFetcherCore> core_; |
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(net::URLFetcherFactory* factory); | |
112 | |
113 const scoped_refptr<net::URLFetcherCore> core_; | |
114 | 117 |
115 DISALLOW_COPY_AND_ASSIGN(URLFetcherImpl); | 118 DISALLOW_COPY_AND_ASSIGN(URLFetcherImpl); |
116 }; | 119 }; |
117 | 120 |
118 #endif // CONTENT_COMMON_NET_URL_FETCHER_IMPL_H_ | 121 } // namespace net |
| 122 |
| 123 #endif // NET_URL_REQUEST_URL_FETCHER_IMPL_H_ |
OLD | NEW |