Upstream version 9.37.197.0
[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/io_buffer.h"
10 #include "net/base/load_flags.h"
11 #include "net/base/net_errors.h"
12 #include "net/http/http_status_code.h"
13 #include "net/url_request/url_fetcher.h"
14 #include "net/url_request/url_fetcher_response_writer.h"
15 #include "url/gurl.h"
16
17 namespace autofill {
18
19 namespace {
20
21 // A URLFetcherResponseWriter that writes into a provided buffer.
22 class UnownedStringWriter : public net::URLFetcherResponseWriter {
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();
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:
44   std::string* data_;  // weak reference.
45
46   DISALLOW_COPY_AND_ASSIGN(UnownedStringWriter);
47 };
48
49 }  // namespace
50
51 ChromeDownloaderImpl::ChromeDownloaderImpl(net::URLRequestContextGetter* getter)
52     : getter_(getter) {}
53
54 ChromeDownloaderImpl::~ChromeDownloaderImpl() {
55   STLDeleteValues(&requests_);
56 }
57
58 void ChromeDownloaderImpl::Download(
59     const std::string& url,
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();
79 }
80
81 void ChromeDownloaderImpl::OnURLFetchComplete(const net::URLFetcher* source) {
82   std::map<const net::URLFetcher*, Request*>::iterator request =
83       requests_.find(source);
84   DCHECK(request != requests_.end());
85
86   bool ok = source->GetResponseCode() == net::HTTP_OK;
87   scoped_ptr<std::string> data(new std::string());
88   if (ok)
89     data->swap(request->second->data);
90   (*request->second->callback)(ok, request->second->url, data.Pass());
91
92   delete request->second;
93   requests_.erase(request);
94 }
95
96 ChromeDownloaderImpl::Request::Request(const std::string& url,
97                                        scoped_ptr<net::URLFetcher> fetcher,
98                                        scoped_ptr<Callback> callback)
99     : url(url),
100       fetcher(fetcher.Pass()),
101       callback(callback.Pass()) {}
102
103 }  // namespace autofill