summaryrefslogtreecommitdiffstats
path: root/net
diff options
context:
space:
mode:
authorakalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-11 05:32:23 +0000
committerakalin@chromium.org <akalin@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-05-11 05:32:23 +0000
commit10c2d69eef670a8870a53c5a0afee74810d416d6 (patch)
treec7b102852b40953ebb1da5ecce5f2a9cab0eccdb /net
parent8623b0f992684cf2bca8fc27f733ad1c31cdd8f8 (diff)
downloadchromium_src-10c2d69eef670a8870a53c5a0afee74810d416d6.zip
chromium_src-10c2d69eef670a8870a53c5a0afee74810d416d6.tar.gz
chromium_src-10c2d69eef670a8870a53c5a0afee74810d416d6.tar.bz2
Move URLFetcherDelegate to net/ and split URLFetcher between net/ and content/
Temporarily make a shim content::URLFetcherDelegate class. A future CL will replace all instances of content::URLFetcherDelegate with net::URLFetcherDelegate and remove that class. Move most URLFetcher methods to net::URLFetcher, except for the static methods (which will be handled in a future CL) and AssociateWithRenderView, which is content-specific. Replace all instances of content::URLFetcher* in the URLFetcherDelegate callbacks with net::URLFetcher* (except for rlz, which required its own special hack). BUG=118220 TEST= TBR=mnissler@chromium.org,jhawkins@chromium.org,cpu@chromium.org,estade@chromium.org,sky@chromium.org,joth@chromium.org,satish@chromium.org Review URL: https://chromiumcodereview.appspot.com/10386063 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@136514 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r--net/net.gyp4
-rw-r--r--net/url_request/url_fetcher.cc11
-rw-r--r--net/url_request/url_fetcher.h216
-rw-r--r--net/url_request/url_fetcher_delegate.cc24
-rw-r--r--net/url_request/url_fetcher_delegate.h65
5 files changed, 320 insertions, 0 deletions
diff --git a/net/net.gyp b/net/net.gyp
index bd82c8e..d055469 100644
--- a/net/net.gyp
+++ b/net/net.gyp
@@ -715,6 +715,10 @@
'udp/udp_socket_win.cc',
'udp/udp_socket_win.h',
'url_request/fraudulent_certificate_reporter.h',
+ 'url_request/url_fetcher.cc',
+ 'url_request/url_fetcher.h',
+ 'url_request/url_fetcher_delegate.cc',
+ 'url_request/url_fetcher_delegate.h',
'url_request/url_request.cc',
'url_request/url_request.h',
'url_request/url_request_about_job.cc',
diff --git a/net/url_request/url_fetcher.cc b/net/url_request/url_fetcher.cc
new file mode 100644
index 0000000..27f32fa
--- /dev/null
+++ b/net/url_request/url_fetcher.cc
@@ -0,0 +1,11 @@
+// Copyright (c) 2012 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.
+
+#include "net/url_request/url_fetcher.h"
+
+namespace net {
+
+URLFetcher::~URLFetcher() {}
+
+} // namespace net
diff --git a/net/url_request/url_fetcher.h b/net/url_request/url_fetcher.h
new file mode 100644
index 0000000..b5e7e55
--- /dev/null
+++ b/net/url_request/url_fetcher.h
@@ -0,0 +1,216 @@
+// Copyright (c) 2012 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 NET_URL_REQUEST_URL_FETCHER_H_
+#define NET_URL_REQUEST_URL_FETCHER_H_
+#pragma once
+
+#include <string>
+#include <vector>
+
+#include "base/memory/ref_counted.h"
+#include "base/platform_file.h"
+#include "net/base/net_export.h"
+
+class FilePath;
+class GURL;
+
+namespace base {
+class MessageLoopProxy;
+class TimeDelta;
+}
+
+namespace net {
+class HostPortPair;
+class HttpRequestHeaders;
+class HttpResponseHeaders;
+class URLFetcherDelegate;
+class URLRequestContextGetter;
+class URLRequestStatus;
+typedef std::vector<std::string> ResponseCookies;
+
+// To use this class, create an instance with the desired URL and a pointer to
+// the object to be notified when the URL has been loaded:
+// URLFetcher* fetcher = URLFetcher::Create("http://www.google.com",
+// URLFetcher::GET, this);
+//
+// Then, optionally set properties on this object, like the request context or
+// extra headers:
+// fetcher->set_extra_request_headers("X-Foo: bar");
+//
+// Finally, start the request:
+// fetcher->Start();
+//
+//
+// The object you supply as a delegate must inherit from
+// URLFetcherDelegate; when the fetch is completed,
+// OnURLFetchComplete() will be called with a pointer to the URLFetcher. From
+// that point until the original URLFetcher instance is destroyed, you may use
+// accessor methods to see the result of the fetch. You should copy these
+// objects if you need them to live longer than the URLFetcher instance. If the
+// URLFetcher instance is destroyed before the callback happens, the fetch will
+// be canceled and no callback will occur.
+//
+// You may create the URLFetcher instance on any thread; OnURLFetchComplete()
+// will be called back on the same thread you use to create the instance.
+//
+//
+// NOTE: By default URLFetcher requests are NOT intercepted, except when
+// interception is explicitly enabled in tests.
+class NET_EXPORT URLFetcher {
+ public:
+ // Imposible http response code. Used to signal that no http response code
+ // was received.
+ enum ResponseCode {
+ RESPONSE_CODE_INVALID = -1
+ };
+
+ enum RequestType {
+ GET,
+ POST,
+ HEAD,
+ DELETE_REQUEST, // DELETE is already taken on Windows.
+ // <winnt.h> defines a DELETE macro.
+ PUT,
+ };
+
+ virtual ~URLFetcher();
+
+ // Sets data only needed by POSTs. All callers making POST requests should
+ // call this before the request is started. |upload_content_type| is the MIME
+ // type of the content, while |upload_content| is the data to be sent (the
+ // Content-Length header value will be set to the length of this data).
+ virtual void SetUploadData(const std::string& upload_content_type,
+ const std::string& upload_content) = 0;
+
+ // Indicates that the POST data is sent via chunked transfer encoding.
+ // This may only be called before calling Start().
+ // Use AppendChunkToUpload() to give the data chunks after calling Start().
+ virtual void SetChunkedUpload(const std::string& upload_content_type) = 0;
+
+ // Adds the given bytes to a request's POST data transmitted using chunked
+ // transfer encoding.
+ // This method should be called ONLY after calling Start().
+ virtual void AppendChunkToUpload(const std::string& data,
+ bool is_last_chunk) = 0;
+
+ // Set one or more load flags as defined in net/base/load_flags.h. Must be
+ // called before the request is started.
+ virtual void SetLoadFlags(int load_flags) = 0;
+
+ // Returns the current load flags.
+ virtual int GetLoadFlags() const = 0;
+
+ // The referrer URL for the request. Must be called before the request is
+ // started.
+ virtual void SetReferrer(const std::string& referrer) = 0;
+
+ // Set extra headers on the request. Must be called before the request
+ // is started.
+ // This replaces the entire extra request headers.
+ virtual void SetExtraRequestHeaders(
+ const std::string& extra_request_headers) = 0;
+
+ // Add header (with format field-name ":" [ field-value ]) to the request
+ // headers. Must be called before the request is started.
+ // This appends the header to the current extra request headers.
+ virtual void AddExtraRequestHeader(const std::string& header_line) = 0;
+
+ virtual void GetExtraRequestHeaders(
+ HttpRequestHeaders* headers) const = 0;
+
+ // Set the URLRequestContext on the request. Must be called before the
+ // request is started.
+ virtual void SetRequestContext(
+ URLRequestContextGetter* request_context_getter) = 0;
+
+ // If |retry| is false, 5xx responses will be propagated to the observer,
+ // if it is true URLFetcher will automatically re-execute the request,
+ // after backoff_delay() elapses. URLFetcher has it set to true by default.
+ virtual void SetAutomaticallyRetryOn5xx(bool retry) = 0;
+
+ virtual void SetMaxRetries(int max_retries) = 0;
+ virtual int GetMaxRetries() const = 0;
+
+ // Returns the back-off delay before the request will be retried,
+ // when a 5xx response was received.
+ virtual base::TimeDelta GetBackoffDelay() const = 0;
+
+ // By default, the response is saved in a string. Call this method to save the
+ // response to a file instead. Must be called before Start().
+ // |file_message_loop_proxy| will be used for all file operations.
+ // To save to a temporary file, use SaveResponseToTemporaryFile().
+ // The created file is removed when the URLFetcher is deleted unless you
+ // take ownership by calling GetResponseAsFilePath().
+ virtual void SaveResponseToFileAtPath(
+ const FilePath& file_path,
+ scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) = 0;
+
+ // By default, the response is saved in a string. Call this method to save the
+ // response to a temporary file instead. Must be called before Start().
+ // |file_message_loop_proxy| will be used for all file operations.
+ // The created file is removed when the URLFetcher is deleted unless you
+ // take ownership by calling GetResponseAsFilePath().
+ virtual void SaveResponseToTemporaryFile(
+ scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) = 0;
+
+ // Retrieve the response headers from the request. Must only be called after
+ // the OnURLFetchComplete callback has run.
+ virtual HttpResponseHeaders* GetResponseHeaders() const = 0;
+
+ // Retrieve the remote socket address from the request. Must only
+ // be called after the OnURLFetchComplete callback has run and if
+ // the request has not failed.
+ virtual HostPortPair GetSocketAddress() const = 0;
+
+ // Returns true if the request was delivered through a proxy. Must only
+ // be called after the OnURLFetchComplete callback has run and the request
+ // has not failed.
+ virtual bool WasFetchedViaProxy() const = 0;
+
+ // Start the request. After this is called, you may not change any other
+ // settings.
+ virtual void Start() = 0;
+
+ // Return the URL that we were asked to fetch.
+ virtual const GURL& GetOriginalURL() const = 0;
+
+ // Return the URL that this fetcher is processing.
+ virtual const GURL& GetURL() const = 0;
+
+ // The status of the URL fetch.
+ virtual const URLRequestStatus& GetStatus() const = 0;
+
+ // The http response code received. Will return RESPONSE_CODE_INVALID
+ // if an error prevented any response from being received.
+ virtual int GetResponseCode() const = 0;
+
+ // Cookies recieved.
+ virtual const ResponseCookies& GetCookies() const = 0;
+
+ // Return true if any file system operation failed. If so, set |error_code|
+ // to the error code. File system errors are only possible if user called
+ // SaveResponseToTemporaryFile().
+ virtual bool FileErrorOccurred(
+ base::PlatformFileError* out_error_code) const = 0;
+
+ // Reports that the received content was malformed.
+ virtual void ReceivedContentWasMalformed() = 0;
+
+ // Get the response as a string. Return false if the fetcher was not
+ // set to store the response as a string.
+ virtual bool GetResponseAsString(std::string* out_response_string) const = 0;
+
+ // Get the path to the file containing the response body. Returns false
+ // if the response body was not saved to a file. If take_ownership is
+ // true, caller takes responsibility for the file, and it will not
+ // be removed once the URLFetcher is destroyed. User should not take
+ // ownership more than once, or call this method after taking ownership.
+ virtual bool GetResponseAsFilePath(bool take_ownership,
+ FilePath* out_response_path) const = 0;
+};
+
+} // namespace net
+
+#endif // NET_URL_REQUEST_URL_FETCHER_H_
diff --git a/net/url_request/url_fetcher_delegate.cc b/net/url_request/url_fetcher_delegate.cc
new file mode 100644
index 0000000..1cc58e7
--- /dev/null
+++ b/net/url_request/url_fetcher_delegate.cc
@@ -0,0 +1,24 @@
+// Copyright (c) 2012 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.
+
+#include "net/url_request/url_fetcher_delegate.h"
+
+namespace net {
+
+void URLFetcherDelegate::OnURLFetchDownloadProgress(
+ const URLFetcher* source, int64 current, int64 total) {}
+
+void URLFetcherDelegate::OnURLFetchDownloadData(
+ const URLFetcher* source, scoped_ptr<std::string> download_data) {}
+
+void URLFetcherDelegate::OnURLFetchUploadProgress(
+ const URLFetcher* source, int64 current, int64 total) {}
+
+bool URLFetcherDelegate::ShouldSendDownloadData() {
+ return false;
+}
+
+URLFetcherDelegate::~URLFetcherDelegate() {}
+
+} // namespace net
diff --git a/net/url_request/url_fetcher_delegate.h b/net/url_request/url_fetcher_delegate.h
new file mode 100644
index 0000000..5069cb6
--- /dev/null
+++ b/net/url_request/url_fetcher_delegate.h
@@ -0,0 +1,65 @@
+// Copyright (c) 2012 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 NET_URL_REQUEST_URL_FETCHER_DELEGATE_H_
+#define NET_URL_REQUEST_URL_FETCHER_DELEGATE_H_
+#pragma once
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/memory/scoped_ptr.h"
+#include "net/base/net_export.h"
+
+namespace net {
+
+class URLFetcher;
+
+// A delegate interface for users of URLFetcher.
+class NET_EXPORT URLFetcherDelegate {
+ public:
+ // This will be called when the URL has been fetched, successfully or not.
+ // Use accessor methods on |source| to get the results.
+ virtual void OnURLFetchComplete(const URLFetcher* source) = 0;
+
+ // This will be called when some part of the response is read. |current|
+ // denotes the number of bytes received up to the call, and |total| is the
+ // expected total size of the response (or -1 if not determined).
+ virtual void OnURLFetchDownloadProgress(const URLFetcher* source,
+ int64 current, int64 total);
+
+ // This will be called when some part of the response is read.
+ // |download_data| contains the current bytes received since the last call.
+ // This will be called after ShouldSendDownloadData() and only if the latter
+ // returns true.
+ virtual void OnURLFetchDownloadData(const URLFetcher* source,
+ scoped_ptr<std::string> download_data);
+
+ // This indicates if OnURLFetchDownloadData should be called.
+ // This will be called before OnURLFetchDownloadData is called, and only if
+ // this returns true.
+ // Default implementation is false.
+ virtual bool ShouldSendDownloadData();
+
+ // This will be called when uploading of POST or PUT requests proceeded.
+ // |current| denotes the number of bytes sent so far, and |total| is the
+ // total size of uploading data (or -1 if chunked upload is enabled).
+ virtual void OnURLFetchUploadProgress(const URLFetcher* source,
+ int64 current, int64 total);
+
+ // TODO(akalin): Remove this hack once rlz is updated to use
+ // net::URLFetcher{,Delegate}.
+#ifdef RLZ_LIB_FINANCIAL_PING_H_
+ struct content {
+ typedef ::net::URLFetcher URLFetcher;
+ };
+#endif
+
+ protected:
+ virtual ~URLFetcherDelegate();
+};
+
+} // namespace net
+
+#endif // NET_URL_REQUEST_URL_FETCHER_DELEGATE_H_