| 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( | 58 void ChromeDownloaderImpl::Download(const std::string& url, | 
| 59     const std::string& url, | 59                                     const Callback& downloaded) const { | 
| 60     scoped_ptr<Callback> downloaded) { | 60   const_cast<ChromeDownloaderImpl*>(this)->DoDownload(url, 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(); |  | 
| 79 } | 61 } | 
| 80 | 62 | 
| 81 void ChromeDownloaderImpl::OnURLFetchComplete(const net::URLFetcher* source) { | 63 void ChromeDownloaderImpl::OnURLFetchComplete(const net::URLFetcher* source) { | 
| 82   std::map<const net::URLFetcher*, Request*>::iterator request = | 64   std::map<const net::URLFetcher*, Request*>::iterator request = | 
| 83       requests_.find(source); | 65       requests_.find(source); | 
| 84   DCHECK(request != requests_.end()); | 66   DCHECK(request != requests_.end()); | 
| 85 | 67 | 
| 86   bool ok = source->GetResponseCode() == net::HTTP_OK; | 68   bool ok = source->GetResponseCode() == net::HTTP_OK; | 
| 87   scoped_ptr<std::string> data(new std::string()); | 69   scoped_ptr<std::string> data(new std::string()); | 
| 88   if (ok) | 70   if (ok) | 
| 89     data->swap(request->second->data); | 71     data->swap(request->second->data); | 
| 90   (*request->second->callback)(ok, request->second->url, data.Pass()); | 72   request->second->callback(ok, request->second->url, data.release()); | 
| 91 | 73 | 
| 92   delete request->second; | 74   delete request->second; | 
| 93   requests_.erase(request); | 75   requests_.erase(request); | 
| 94 } | 76 } | 
| 95 | 77 | 
| 96 ChromeDownloaderImpl::Request::Request(const std::string& url, | 78 ChromeDownloaderImpl::Request::Request(const std::string& url, | 
| 97                                        scoped_ptr<net::URLFetcher> fetcher, | 79                                        scoped_ptr<net::URLFetcher> fetcher, | 
| 98                                        scoped_ptr<Callback> callback) | 80                                        const Callback& callback) | 
| 99     : url(url), | 81     : url(url), | 
| 100       fetcher(fetcher.Pass()), | 82       fetcher(fetcher.Pass()), | 
| 101       callback(callback.Pass()) {} | 83       callback(callback) {} | 
|  | 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 } | 
| 102 | 106 | 
| 103 }  // namespace autofill | 107 }  // namespace autofill | 
| OLD | NEW | 
|---|