Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Unified Diff: third_party/libaddressinput/chromium/chrome_downloader_impl.cc

Issue 151133002: libaddressinput - one less copy when downloading data (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: re-up Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/libaddressinput/chromium/chrome_downloader_impl.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/libaddressinput/chromium/chrome_downloader_impl.cc
diff --git a/third_party/libaddressinput/chromium/chrome_downloader_impl.cc b/third_party/libaddressinput/chromium/chrome_downloader_impl.cc
index baf9d96a83d66157a82d422758571e1afad9a816..a0a8b172f0617645241c13e9ac9a3b69a2d65354 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
« no previous file with comments | « third_party/libaddressinput/chromium/chrome_downloader_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698