4e6c587e45a88f00db574c0bf2d9bf8c2c071f1b
[platform/framework/web/crosswalk.git] / src / third_party / libaddressinput / chromium / chrome_downloader_impl.cc
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.
4
5 #include "third_party/libaddressinput/chromium/chrome_downloader_impl.h"
6
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"
12 #include "url/gurl.h"
13
14 ChromeDownloaderImpl::ChromeDownloaderImpl(net::URLRequestContextGetter* getter)
15     : getter_(getter) {}
16
17 ChromeDownloaderImpl::~ChromeDownloaderImpl() {
18   STLDeleteContainerPairPointers(requests_.begin(), requests_.end());
19 }
20
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_);
29
30   requests_[fetcher] = new Request(url, downloaded.Pass());
31   fetcher->Start();
32 }
33
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());
38
39   bool ok = source->GetResponseCode() == net::HTTP_OK;
40   scoped_ptr<std::string> data(new std::string());
41   if (ok)
42     source->GetResponseAsString(&*data);
43   (*request->second->callback)(ok, request->second->url, data.Pass());
44
45   delete request->first;
46   delete request->second;
47   requests_.erase(request);
48 }
49
50 ChromeDownloaderImpl::Request::Request(const std::string& url,
51                                        scoped_ptr<Callback> callback)
52     : url(url),
53       callback(callback.Pass()) {}