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"
22 // A URLFetcherResponseWriter that writes into a provided buffer.
23 class UnownedStringWriter
: public net::URLFetcherResponseWriter
{
25 UnownedStringWriter(std::string
* data
) : data_(data
) {}
26 virtual ~UnownedStringWriter() {}
28 virtual int Initialize(const net::CompletionCallback
& callback
) override
{
33 virtual int Write(net::IOBuffer
* buffer
,
35 const net::CompletionCallback
& callback
) override
{
36 data_
->append(buffer
->data(), num_bytes
);
40 virtual int Finish(const net::CompletionCallback
& callback
) override
{
45 std::string
* data_
; // weak reference.
47 DISALLOW_COPY_AND_ASSIGN(UnownedStringWriter
);
52 ChromeMetadataSource::ChromeMetadataSource(
53 const std::string
& validation_data_url
,
54 net::URLRequestContextGetter
* getter
)
55 : validation_data_url_(validation_data_url
),
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());
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
)
86 fetcher(fetcher
.Pass()),
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
);
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