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

Side by Side 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: checks Created 6 years, 10 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « third_party/libaddressinput/chromium/chrome_downloader_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 #include "third_party/libaddressinput/chromium/chrome_downloader_impl.h" 5 #include "third_party/libaddressinput/chromium/chrome_downloader_impl.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "net/base/io_buffer.h"
9 #include "net/base/load_flags.h" 10 #include "net/base/load_flags.h"
11 #include "net/base/net_errors.h"
10 #include "net/http/http_status_code.h" 12 #include "net/http/http_status_code.h"
11 #include "net/url_request/url_fetcher.h" 13 #include "net/url_request/url_fetcher.h"
14 #include "net/url_request/url_fetcher_response_writer.h"
12 #include "url/gurl.h" 15 #include "url/gurl.h"
13 16
14 namespace autofill { 17 namespace autofill {
15 18
19 namespace {
20
21 // A URLFetcherResponseWriter that writes into a provided buffer.
22 class UnownedStringWriter : public net::URLFetcherResponseWriter {
Dan Beam 2014/02/01 00:53:33 nit: BufferedStringWriter maybe?
Evan Stade 2014/02/01 01:02:16 I like unowned better, I'm not sure what "Buffered
23 public:
24 UnownedStringWriter(std::string* data) : data_(data) {}
25 virtual ~UnownedStringWriter() {}
26
27 virtual int Initialize(const net::CompletionCallback& callback) OVERRIDE {
28 data_->clear();
Dan Beam 2014/02/01 00:53:33 should we be calling the CompletionCallbacks from
Evan Stade 2014/02/01 01:02:16 no, we should not, because these operations are al
29 return net::OK;
30 }
31
32 virtual int Write(net::IOBuffer* buffer,
33 int num_bytes,
34 const net::CompletionCallback& callback) OVERRIDE {
35 data_->append(buffer->data(), num_bytes);
36 return num_bytes;
37 }
38
39 virtual int Finish(const net::CompletionCallback& callback) OVERRIDE {
40 return net::OK;
41 }
42
43 private:
Dan Beam 2014/02/01 00:53:33 maybe add this somewhere: // Cleared on initial
Evan Stade 2014/02/01 01:02:16 I think that's easy enough to see in code; a comme
44 std::string* data_; // weak reference.
45
46 DISALLOW_COPY_AND_ASSIGN(UnownedStringWriter);
47 };
48
49 } // namespace
50
16 ChromeDownloaderImpl::ChromeDownloaderImpl(net::URLRequestContextGetter* getter) 51 ChromeDownloaderImpl::ChromeDownloaderImpl(net::URLRequestContextGetter* getter)
17 : getter_(getter) {} 52 : getter_(getter) {}
18 53
19 ChromeDownloaderImpl::~ChromeDownloaderImpl() { 54 ChromeDownloaderImpl::~ChromeDownloaderImpl() {
20 STLDeleteContainerPairPointers(requests_.begin(), requests_.end()); 55 STLDeleteValues(&requests_);
21 } 56 }
22 57
23 void ChromeDownloaderImpl::Download( 58 void ChromeDownloaderImpl::Download(
24 const std::string& url, 59 const std::string& url,
25 scoped_ptr<Callback> downloaded) { 60 scoped_ptr<Callback> downloaded) {
26 net::URLFetcher* fetcher = 61 scoped_ptr<net::URLFetcher> fetcher(
27 net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, this); 62 net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, this));
28 fetcher->SetLoadFlags( 63 fetcher->SetLoadFlags(
29 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); 64 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES);
30 fetcher->SetRequestContext(getter_); 65 fetcher->SetRequestContext(getter_);
31 66
32 requests_[fetcher] = new Request(url, downloaded.Pass()); 67 Request* request = new Request(url, fetcher.Pass(), downloaded.Pass());
33 fetcher->Start(); 68 request->fetcher->SaveResponseWithWriter(
69 scoped_ptr<net::URLFetcherResponseWriter>(
70 new UnownedStringWriter(&request->data)));
71 requests_[request->fetcher.get()] = request;
72 request->fetcher->Start();
34 } 73 }
35 74
36 void ChromeDownloaderImpl::OnURLFetchComplete(const net::URLFetcher* source) { 75 void ChromeDownloaderImpl::OnURLFetchComplete(const net::URLFetcher* source) {
37 std::map<const net::URLFetcher*, Request*>::iterator request = 76 std::map<const net::URLFetcher*, Request*>::iterator request =
38 requests_.find(source); 77 requests_.find(source);
39 DCHECK(request != requests_.end()); 78 DCHECK(request != requests_.end());
40 79
41 bool ok = source->GetResponseCode() == net::HTTP_OK; 80 bool ok = source->GetResponseCode() == net::HTTP_OK;
42 scoped_ptr<std::string> data(new std::string()); 81 scoped_ptr<std::string> data(new std::string());
43 if (ok) 82 if (ok)
44 source->GetResponseAsString(data.get()); 83 data->swap(request->second->data);
45 (*request->second->callback)(ok, request->second->url, data.Pass()); 84 (*request->second->callback)(ok, request->second->url, data.Pass());
46 85
47 delete request->first;
48 delete request->second; 86 delete request->second;
49 requests_.erase(request); 87 requests_.erase(request);
50 } 88 }
51 89
52 ChromeDownloaderImpl::Request::Request(const std::string& url, 90 ChromeDownloaderImpl::Request::Request(const std::string& url,
91 scoped_ptr<net::URLFetcher> fetcher,
53 scoped_ptr<Callback> callback) 92 scoped_ptr<Callback> callback)
54 : url(url), 93 : url(url),
94 fetcher(fetcher.Pass()),
55 callback(callback.Pass()) {} 95 callback(callback.Pass()) {}
56 96
57 } // namespace autofill 97 } // namespace autofill
OLDNEW
« 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