OLD | NEW |
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/io_buffer.h" |
10 #include "net/base/load_flags.h" | 10 #include "net/base/load_flags.h" |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 | 48 |
49 } // namespace | 49 } // namespace |
50 | 50 |
51 ChromeDownloaderImpl::ChromeDownloaderImpl(net::URLRequestContextGetter* getter) | 51 ChromeDownloaderImpl::ChromeDownloaderImpl(net::URLRequestContextGetter* getter) |
52 : getter_(getter) {} | 52 : getter_(getter) {} |
53 | 53 |
54 ChromeDownloaderImpl::~ChromeDownloaderImpl() { | 54 ChromeDownloaderImpl::~ChromeDownloaderImpl() { |
55 STLDeleteValues(&requests_); | 55 STLDeleteValues(&requests_); |
56 } | 56 } |
57 | 57 |
58 void ChromeDownloaderImpl::Download(const std::string& url, | 58 void ChromeDownloaderImpl::Download( |
59 const Callback& downloaded) const { | 59 const std::string& url, |
60 const_cast<ChromeDownloaderImpl*>(this)->DoDownload(url, downloaded); | 60 scoped_ptr<Callback> downloaded) { |
| 61 GURL resource(url); |
| 62 if (!resource.SchemeIsSecure()) { |
| 63 (*downloaded)(false, url, make_scoped_ptr(new std::string())); |
| 64 return; |
| 65 } |
| 66 |
| 67 scoped_ptr<net::URLFetcher> fetcher( |
| 68 net::URLFetcher::Create(resource, net::URLFetcher::GET, this)); |
| 69 fetcher->SetLoadFlags( |
| 70 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); |
| 71 fetcher->SetRequestContext(getter_); |
| 72 |
| 73 Request* request = new Request(url, fetcher.Pass(), downloaded.Pass()); |
| 74 request->fetcher->SaveResponseWithWriter( |
| 75 scoped_ptr<net::URLFetcherResponseWriter>( |
| 76 new UnownedStringWriter(&request->data))); |
| 77 requests_[request->fetcher.get()] = request; |
| 78 request->fetcher->Start(); |
61 } | 79 } |
62 | 80 |
63 void ChromeDownloaderImpl::OnURLFetchComplete(const net::URLFetcher* source) { | 81 void ChromeDownloaderImpl::OnURLFetchComplete(const net::URLFetcher* source) { |
64 std::map<const net::URLFetcher*, Request*>::iterator request = | 82 std::map<const net::URLFetcher*, Request*>::iterator request = |
65 requests_.find(source); | 83 requests_.find(source); |
66 DCHECK(request != requests_.end()); | 84 DCHECK(request != requests_.end()); |
67 | 85 |
68 bool ok = source->GetResponseCode() == net::HTTP_OK; | 86 bool ok = source->GetResponseCode() == net::HTTP_OK; |
69 scoped_ptr<std::string> data(new std::string()); | 87 scoped_ptr<std::string> data(new std::string()); |
70 if (ok) | 88 if (ok) |
71 data->swap(request->second->data); | 89 data->swap(request->second->data); |
72 request->second->callback(ok, request->second->url, data.release()); | 90 (*request->second->callback)(ok, request->second->url, data.Pass()); |
73 | 91 |
74 delete request->second; | 92 delete request->second; |
75 requests_.erase(request); | 93 requests_.erase(request); |
76 } | 94 } |
77 | 95 |
78 ChromeDownloaderImpl::Request::Request(const std::string& url, | 96 ChromeDownloaderImpl::Request::Request(const std::string& url, |
79 scoped_ptr<net::URLFetcher> fetcher, | 97 scoped_ptr<net::URLFetcher> fetcher, |
80 const Callback& callback) | 98 scoped_ptr<Callback> callback) |
81 : url(url), | 99 : url(url), |
82 fetcher(fetcher.Pass()), | 100 fetcher(fetcher.Pass()), |
83 callback(callback) {} | 101 callback(callback.Pass()) {} |
84 | |
85 void ChromeDownloaderImpl::DoDownload(const std::string& url, | |
86 const Callback& downloaded) { | |
87 GURL resource(url); | |
88 if (!resource.SchemeIsSecure()) { | |
89 downloaded(false, url, NULL); | |
90 return; | |
91 } | |
92 | |
93 scoped_ptr<net::URLFetcher> fetcher( | |
94 net::URLFetcher::Create(resource, net::URLFetcher::GET, this)); | |
95 fetcher->SetLoadFlags( | |
96 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES); | |
97 fetcher->SetRequestContext(getter_); | |
98 | |
99 Request* request = new Request(url, fetcher.Pass(), downloaded); | |
100 request->fetcher->SaveResponseWithWriter( | |
101 scoped_ptr<net::URLFetcherResponseWriter>( | |
102 new UnownedStringWriter(&request->data))); | |
103 requests_[request->fetcher.get()] = request; | |
104 request->fetcher->Start(); | |
105 } | |
106 | 102 |
107 } // namespace autofill | 103 } // namespace autofill |
OLD | NEW |