Roll src/third_party/WebKit 8139f33:52503da (svn 201975:201976)
[chromium-blink-merge.git] / third_party / libaddressinput / chromium / chrome_metadata_source.cc
blob0c99aad4b51d60418457dbf7c226ebd8362d82bc
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_metadata_source.h"
7 #include "base/logging.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/stl_util.h"
10 #include "net/base/io_buffer.h"
11 #include "net/base/load_flags.h"
12 #include "net/base/net_errors.h"
13 #include "net/http/http_status_code.h"
14 #include "net/url_request/url_fetcher.h"
15 #include "net/url_request/url_fetcher_response_writer.h"
16 #include "url/gurl.h"
18 namespace autofill {
20 namespace {
22 // A URLFetcherResponseWriter that writes into a provided buffer.
23 class UnownedStringWriter : public net::URLFetcherResponseWriter {
24 public:
25 UnownedStringWriter(std::string* data) : data_(data) {}
26 virtual ~UnownedStringWriter() {}
28 virtual int Initialize(const net::CompletionCallback& callback) override {
29 data_->clear();
30 return net::OK;
33 virtual int Write(net::IOBuffer* buffer,
34 int num_bytes,
35 const net::CompletionCallback& callback) override {
36 data_->append(buffer->data(), num_bytes);
37 return num_bytes;
40 virtual int Finish(const net::CompletionCallback& callback) override {
41 return net::OK;
44 private:
45 std::string* data_; // weak reference.
47 DISALLOW_COPY_AND_ASSIGN(UnownedStringWriter);
50 } // namespace
52 ChromeMetadataSource::ChromeMetadataSource(
53 const std::string& validation_data_url,
54 net::URLRequestContextGetter* getter)
55 : validation_data_url_(validation_data_url),
56 getter_(getter) {}
58 ChromeMetadataSource::~ChromeMetadataSource() {
59 STLDeleteValues(&requests_);
62 void ChromeMetadataSource::Get(const std::string& key,
63 const Callback& downloaded) const {
64 const_cast<ChromeMetadataSource*>(this)->Download(key, downloaded);
67 void ChromeMetadataSource::OnURLFetchComplete(const net::URLFetcher* source) {
68 std::map<const net::URLFetcher*, Request*>::iterator request =
69 requests_.find(source);
70 DCHECK(request != requests_.end());
72 bool ok = source->GetResponseCode() == net::HTTP_OK;
73 scoped_ptr<std::string> data(new std::string());
74 if (ok)
75 data->swap(request->second->data);
76 request->second->callback(ok, request->second->key, data.release());
78 delete request->second;
79 requests_.erase(request);
82 ChromeMetadataSource::Request::Request(const std::string& key,
83 scoped_ptr<net::URLFetcher> fetcher,
84 const Callback& callback)
85 : key(key),
86 fetcher(fetcher.Pass()),
87 callback(callback) {}
89 void ChromeMetadataSource::Download(const std::string& key,
90 const Callback& downloaded) {
91 GURL resource(validation_data_url_ + key);
92 if (!resource.SchemeIsCryptographic()) {
93 downloaded(false, key, NULL);
94 return;
97 scoped_ptr<net::URLFetcher> fetcher =
98 net::URLFetcher::Create(resource, net::URLFetcher::GET, this);
99 fetcher->SetLoadFlags(
100 net::LOAD_DO_NOT_SEND_COOKIES | net::LOAD_DO_NOT_SAVE_COOKIES);
101 fetcher->SetRequestContext(getter_);
103 Request* request = new Request(key, fetcher.Pass(), downloaded);
104 request->fetcher->SaveResponseWithWriter(
105 scoped_ptr<net::URLFetcherResponseWriter>(
106 new UnownedStringWriter(&request->data)));
107 requests_[request->fetcher.get()] = request;
108 request->fetcher->Start();
111 } // namespace autofill