OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 net::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_H_ | 14 #ifndef CONTENT_COMMON_URL_FETCHER_H_ |
15 #define CONTENT_COMMON_NET_URL_FETCHER_H_ | 15 #define CONTENT_COMMON_URL_FETCHER_H_ |
16 #pragma once | 16 #pragma once |
17 | 17 |
18 #include <string> | 18 #include <string> |
19 #include <vector> | 19 #include <vector> |
20 | 20 |
21 #include "base/memory/ref_counted.h" | 21 #include "base/memory/ref_counted.h" |
22 #include "base/message_loop.h" | 22 #include "base/message_loop.h" |
23 #include "base/platform_file.h" | 23 #include "base/platform_file.h" |
24 #include "base/time.h" | 24 #include "base/time.h" |
25 | 25 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 Delegate* d) = 0; | 110 Delegate* d) = 0; |
111 | 111 |
112 protected: | 112 protected: |
113 virtual ~Factory() {} | 113 virtual ~Factory() {} |
114 }; | 114 }; |
115 | 115 |
116 // |url| is the URL to send the request to. | 116 // |url| is the URL to send the request to. |
117 // |request_type| is the type of request to make. | 117 // |request_type| is the type of request to make. |
118 // |d| the object that will receive the callback on fetch completion. | 118 // |d| the object that will receive the callback on fetch completion. |
119 URLFetcher(const GURL& url, RequestType request_type, Delegate* d); | 119 URLFetcher(const GURL& url, RequestType request_type, Delegate* d); |
120 | |
121 virtual ~URLFetcher(); | 120 virtual ~URLFetcher(); |
122 | 121 |
123 // Sets the factory used by the static method Create to create a URLFetcher. | |
124 // URLFetcher does not take ownership of |factory|. A value of NULL results | |
125 // in a URLFetcher being created directly. | |
126 #if defined(UNIT_TEST) | |
127 static void set_factory(Factory* factory) { | |
128 factory_ = factory; | |
129 } | |
130 #endif | |
131 | |
132 // Normally interception is disabled for URLFetcher, but you can use this | 122 // Normally interception is disabled for URLFetcher, but you can use this |
133 // to enable it for tests. Also see the set_factory method for another way | 123 // to enable it for tests. Also see ScopedURLFetcherFactory for another way |
134 // of testing code that uses an URLFetcher. | 124 // of testing code that uses an URLFetcher. |
135 static void enable_interception_for_tests(bool enabled) { | 125 static void enable_interception_for_tests(bool enabled) { |
136 g_interception_enabled = enabled; | 126 g_interception_enabled = enabled; |
137 } | 127 } |
138 | 128 |
139 // Creates a URLFetcher, ownership returns to the caller. If there is no | 129 // Creates a URLFetcher, ownership returns to the caller. If there is no |
140 // Factory (the default) this creates and returns a new URLFetcher. See the | 130 // Factory (the default) this creates and returns a new URLFetcher. See the |
141 // constructor for a description of the args. |id| may be used during testing | 131 // constructor for a description of the args. |id| may be used during testing |
142 // to identify who is creating the URLFetcher. | 132 // to identify who is creating the URLFetcher. |
143 static URLFetcher* Create(int id, const GURL& url, RequestType request_type, | 133 static URLFetcher* Create(int id, const GURL& url, RequestType request_type, |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 // Return a reference to the string data fetched. Response type must | 278 // Return a reference to the string data fetched. Response type must |
289 // be STRING, or this will CHECK. This method exists to support the | 279 // be STRING, or this will CHECK. This method exists to support the |
290 // old signiture to OnURLFetchComplete(), and will be removed as part | 280 // old signiture to OnURLFetchComplete(), and will be removed as part |
291 // of crbug.com/83592 . | 281 // of crbug.com/83592 . |
292 virtual const std::string& GetResponseStringRef() const; | 282 virtual const std::string& GetResponseStringRef() const; |
293 | 283 |
294 virtual void SetResponseDestinationForTesting(ResponseDestinationType); | 284 virtual void SetResponseDestinationForTesting(ResponseDestinationType); |
295 virtual ResponseDestinationType GetResponseDestinationForTesting() const; | 285 virtual ResponseDestinationType GetResponseDestinationForTesting() const; |
296 | 286 |
297 private: | 287 private: |
| 288 friend class ScopedURLFetcherFactory; |
| 289 friend class TestURLFetcher; |
298 friend class URLFetcherTest; | 290 friend class URLFetcherTest; |
299 friend class TestURLFetcher; | |
300 | 291 |
301 // Only used by URLFetcherTest, returns the number of URLFetcher::Core objects | 292 // Only used by URLFetcherTest, returns the number of URLFetcher::Core objects |
302 // actively running. | 293 // actively running. |
303 static int GetNumFetcherCores(); | 294 static int GetNumFetcherCores(); |
304 | 295 |
| 296 static Factory* factory() { return factory_; } |
| 297 |
| 298 // Sets the factory used by the static method Create to create a URLFetcher. |
| 299 // URLFetcher does not take ownership of |factory|. A value of NULL results |
| 300 // in a URLFetcher being created directly. |
| 301 // |
| 302 // NOTE: for safety, this should only be used through ScopedURLFetcherFactory! |
| 303 static void set_factory(Factory* factory) { |
| 304 factory_ = factory; |
| 305 } |
| 306 |
305 class Core; | 307 class Core; |
306 scoped_refptr<Core> core_; | 308 scoped_refptr<Core> core_; |
307 | 309 |
308 static Factory* factory_; | 310 static Factory* factory_; |
309 | 311 |
310 // Back-off time delay. 0 by default. | 312 // Back-off time delay. 0 by default. |
311 base::TimeDelta backoff_delay_; | 313 base::TimeDelta backoff_delay_; |
312 | 314 |
313 static bool g_interception_enabled; | 315 static bool g_interception_enabled; |
314 | 316 |
315 DISALLOW_COPY_AND_ASSIGN(URLFetcher); | 317 DISALLOW_COPY_AND_ASSIGN(URLFetcher); |
316 }; | 318 }; |
317 | 319 |
318 #endif // CONTENT_COMMON_NET_URL_FETCHER_H_ | 320 #endif // CONTENT_COMMON_URL_FETCHER_H_ |
OLD | NEW |