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 "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"
21 // A URLFetcherResponseWriter that writes into a provided buffer.
22 class UnownedStringWriter
: public net::URLFetcherResponseWriter
{
24 UnownedStringWriter(std::string
* data
) : data_(data
) {}
25 virtual ~UnownedStringWriter() {}
27 virtual int Initialize(const net::CompletionCallback
& callback
) override
{
32 virtual int Write(net::IOBuffer
* buffer
,
34 const net::CompletionCallback
& callback
) override
{
35 data_
->append(buffer
->data(), num_bytes
);
39 virtual int Finish(const net::CompletionCallback
& callback
) override
{
44 std::string
* data_
; // weak reference.
46 DISALLOW_COPY_AND_ASSIGN(UnownedStringWriter
);
51 ChromeMetadataSource::ChromeMetadataSource(
52 const std::string
& validation_data_url
,
53 net::URLRequestContextGetter
* getter
)
54 : validation_data_url_(validation_data_url
),
57 ChromeMetadataSource::~ChromeMetadataSource() {
58 STLDeleteValues(&requests_
);
61 void ChromeMetadataSource::Get(const std::string
& key
,
62 const Callback
& downloaded
) const {
63 const_cast<ChromeMetadataSource
*>(this)->Download(key
, downloaded
);
66 void ChromeMetadataSource::OnURLFetchComplete(const net::URLFetcher
* source
) {
67 std::map
<const net::URLFetcher
*, Request
*>::iterator request
=
68 requests_
.find(source
);
69 DCHECK(request
!= requests_
.end());
71 bool ok
= source
->GetResponseCode() == net::HTTP_OK
;
72 scoped_ptr
<std::string
> data(new std::string());
74 data
->swap(request
->second
->data
);
75 request
->second
->callback(ok
, request
->second
->key
, data
.release());
77 delete request
->second
;
78 requests_
.erase(request
);
81 ChromeMetadataSource::Request::Request(const std::string
& key
,
82 scoped_ptr
<net::URLFetcher
> fetcher
,
83 const Callback
& callback
)
85 fetcher(fetcher
.Pass()),
88 void ChromeMetadataSource::Download(const std::string
& key
,
89 const Callback
& downloaded
) {
90 GURL
resource(validation_data_url_
+ key
);
91 if (!resource
.SchemeIsCryptographic()) {
92 downloaded(false, key
, NULL
);
96 scoped_ptr
<net::URLFetcher
> fetcher
=
97 net::URLFetcher::Create(resource
, net::URLFetcher::GET
, this);
98 fetcher
->SetLoadFlags(
99 net::LOAD_DO_NOT_SEND_COOKIES
| net::LOAD_DO_NOT_SAVE_COOKIES
);
100 fetcher
->SetRequestContext(getter_
);
102 Request
* request
= new Request(key
, fetcher
.Pass(), downloaded
);
103 request
->fetcher
->SaveResponseWithWriter(
104 scoped_ptr
<net::URLFetcherResponseWriter
>(
105 new UnownedStringWriter(&request
->data
)));
106 requests_
[request
->fetcher
.get()] = request
;
107 request
->fetcher
->Start();
110 } // namespace autofill