diff options
author | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-03 20:10:53 +0000 |
---|---|---|
committer | estade@chromium.org <estade@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-02-03 20:10:53 +0000 |
commit | 1d88929e761889394c1a97e294e70932dafa5301 (patch) | |
tree | d75a1c39f49205ca7f58cd06024e1b1745e41d8c /third_party/libaddressinput | |
parent | 1247de8adb3acfbe1b26416a4e4a89aacab87388 (diff) | |
download | chromium_src-1d88929e761889394c1a97e294e70932dafa5301.zip chromium_src-1d88929e761889394c1a97e294e70932dafa5301.tar.gz chromium_src-1d88929e761889394c1a97e294e70932dafa5301.tar.bz2 |
libaddressinput - one less copy when downloading data
BUG=337679
Review URL: https://codereview.chromium.org/151133002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@248561 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'third_party/libaddressinput')
-rw-r--r-- | third_party/libaddressinput/chromium/chrome_downloader_impl.cc | 54 | ||||
-rw-r--r-- | third_party/libaddressinput/chromium/chrome_downloader_impl.h | 11 |
2 files changed, 55 insertions, 10 deletions
diff --git a/third_party/libaddressinput/chromium/chrome_downloader_impl.cc b/third_party/libaddressinput/chromium/chrome_downloader_impl.cc index baf9d96..a0a8b17 100644 --- a/third_party/libaddressinput/chromium/chrome_downloader_impl.cc +++ b/third_party/libaddressinput/chromium/chrome_downloader_impl.cc @@ -6,31 +6,70 @@ #include "base/logging.h" #include "base/memory/scoped_ptr.h" +#include "net/base/io_buffer.h" #include "net/base/load_flags.h" +#include "net/base/net_errors.h" #include "net/http/http_status_code.h" #include "net/url_request/url_fetcher.h" +#include "net/url_request/url_fetcher_response_writer.h" #include "url/gurl.h" namespace autofill { +namespace { + +// A URLFetcherResponseWriter that writes into a provided buffer. +class UnownedStringWriter : public net::URLFetcherResponseWriter { + public: + UnownedStringWriter(std::string* data) : data_(data) {} + virtual ~UnownedStringWriter() {} + + virtual int Initialize(const net::CompletionCallback& callback) OVERRIDE { + data_->clear(); + return net::OK; + } + + virtual int Write(net::IOBuffer* buffer, + int num_bytes, + const net::CompletionCallback& callback) OVERRIDE { + data_->append(buffer->data(), num_bytes); + return num_bytes; + } + + virtual int Finish(const net::CompletionCallback& callback) OVERRIDE { + return net::OK; + } + + private: + std::string* data_; // weak reference. + + DISALLOW_COPY_AND_ASSIGN(UnownedStringWriter); +}; + +} // namespace + ChromeDownloaderImpl::ChromeDownloaderImpl(net::URLRequestContextGetter* getter) : getter_(getter) {} ChromeDownloaderImpl::~ChromeDownloaderImpl() { - STLDeleteContainerPairPointers(requests_.begin(), requests_.end()); + STLDeleteValues(&requests_); } void ChromeDownloaderImpl::Download( const std::string& url, scoped_ptr<Callback> downloaded) { - net::URLFetcher* fetcher = - net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, this); + scoped_ptr<net::URLFetcher> fetcher( + net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, this)); fetcher->SetLoadFlags( net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); fetcher->SetRequestContext(getter_); - requests_[fetcher] = new Request(url, downloaded.Pass()); - fetcher->Start(); + Request* request = new Request(url, fetcher.Pass(), downloaded.Pass()); + request->fetcher->SaveResponseWithWriter( + scoped_ptr<net::URLFetcherResponseWriter>( + new UnownedStringWriter(&request->data))); + requests_[request->fetcher.get()] = request; + request->fetcher->Start(); } void ChromeDownloaderImpl::OnURLFetchComplete(const net::URLFetcher* source) { @@ -41,17 +80,18 @@ void ChromeDownloaderImpl::OnURLFetchComplete(const net::URLFetcher* source) { bool ok = source->GetResponseCode() == net::HTTP_OK; scoped_ptr<std::string> data(new std::string()); if (ok) - source->GetResponseAsString(data.get()); + data->swap(request->second->data); (*request->second->callback)(ok, request->second->url, data.Pass()); - delete request->first; delete request->second; requests_.erase(request); } ChromeDownloaderImpl::Request::Request(const std::string& url, + scoped_ptr<net::URLFetcher> fetcher, scoped_ptr<Callback> callback) : url(url), + fetcher(fetcher.Pass()), callback(callback.Pass()) {} } // namespace autofill diff --git a/third_party/libaddressinput/chromium/chrome_downloader_impl.h b/third_party/libaddressinput/chromium/chrome_downloader_impl.h index 09a9e7a..2f0bf0d 100644 --- a/third_party/libaddressinput/chromium/chrome_downloader_impl.h +++ b/third_party/libaddressinput/chromium/chrome_downloader_impl.h @@ -36,16 +36,21 @@ class ChromeDownloaderImpl : public ::i18n::addressinput::Downloader, private: struct Request { - Request(const std::string& url, scoped_ptr<Callback> callback); + Request(const std::string& url, + scoped_ptr<net::URLFetcher> fetcher, + scoped_ptr<Callback> callback); std::string url; + // The data that's received. + std::string data; + // The object that manages retrieving the data. + scoped_ptr<net::URLFetcher> fetcher; scoped_ptr<Callback> callback; }; net::URLRequestContextGetter* const getter_; // weak - // Maps from active url fetcher to request metadata. Both the key and value - // are owned. + // Maps from active url fetcher to request metadata. The value is owned. std::map<const net::URLFetcher*, Request*> requests_; DISALLOW_COPY_AND_ASSIGN(ChromeDownloaderImpl); |