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#ifndef THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_METADATA_SOURCE_H_
6#define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_METADATA_SOURCE_H_
7
8#include <map>
9#include <string>
10
11#include "base/memory/scoped_vector.h"
12#include "net/url_request/url_fetcher_delegate.h"
13#include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
14
15namespace net {
16class URLFetcher;
17class URLRequestContextGetter;
18}
19
20namespace autofill {
21
22// A class for downloading rules to let libaddressinput validate international
23// addresses.
24class ChromeMetadataSource : public ::i18n::addressinput::Source,
25                             public net::URLFetcherDelegate {
26 public:
27  ChromeMetadataSource(const std::string& validation_data_url,
28                       net::URLRequestContextGetter* getter);
29  virtual ~ChromeMetadataSource();
30
31  // ::i18n::addressinput::Source:
32  virtual void Get(const std::string& key,
33                   const Callback& downloaded) const OVERRIDE;
34
35  // net::URLFetcherDelegate:
36  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
37
38 private:
39  struct Request {
40    Request(const std::string& key,
41            scoped_ptr<net::URLFetcher> fetcher,
42            const Callback& callback);
43
44    std::string key;
45    // The data that's received.
46    std::string data;
47    // The object that manages retrieving the data.
48    scoped_ptr<net::URLFetcher> fetcher;
49    const Callback& callback;
50  };
51
52  // Non-const method actually implementing Get().
53  void Download(const std::string& key, const Callback& downloaded);
54
55  const std::string validation_data_url_;
56  net::URLRequestContextGetter* const getter_;  // weak
57
58  // Maps from active URL fetcher to request metadata. The value is owned.
59  std::map<const net::URLFetcher*, Request*> requests_;
60
61  DISALLOW_COPY_AND_ASSIGN(ChromeMetadataSource);
62};
63
64}  // namespace autofill
65
66#endif  // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_METADATA_SOURCE_H_
67