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
3 // found in the LICENSE file.
5 #include "third_party/libaddressinput/chromium/chrome_downloader_impl.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "net/base/load_flags.h"
10 #include "net/http/http_status_code.h"
11 #include "net/url_request/url_fetcher.h"
14 ChromeDownloaderImpl::ChromeDownloaderImpl(net::URLRequestContextGetter* getter)
17 ChromeDownloaderImpl::~ChromeDownloaderImpl() {
18 STLDeleteContainerPairPointers(requests_.begin(), requests_.end());
21 void ChromeDownloaderImpl::Download(
22 const std::string& url,
23 scoped_ptr<Callback> downloaded) {
24 net::URLFetcher* fetcher =
25 net::URLFetcher::Create(GURL(url), net::URLFetcher::GET, this);
26 fetcher->SetLoadFlags(
27 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES);
28 fetcher->SetRequestContext(getter_);
30 requests_[fetcher] = new Request(url, downloaded.Pass());
34 void ChromeDownloaderImpl::OnURLFetchComplete(const net::URLFetcher* source) {
35 std::map<const net::URLFetcher*, Request*>::iterator request =
36 requests_.find(source);
37 DCHECK(request != requests_.end());
39 bool ok = source->GetResponseCode() == net::HTTP_OK;
40 scoped_ptr<std::string> data(new std::string());
42 source->GetResponseAsString(&*data);
43 (*request->second->callback)(ok, request->second->url, data.Pass());
45 delete request->first;
46 delete request->second;
47 requests_.erase(request);
50 ChromeDownloaderImpl::Request::Request(const std::string& url,
51 scoped_ptr<Callback> callback)
53 callback(callback.Pass()) {}