chrome_metadata_source.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. #ifndef THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_METADATA_SOURCE_H_
  5. #define THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_METADATA_SOURCE_H_
  6. #include <list>
  7. #include <memory>
  8. #include <string>
  9. #include "base/macros.h"
  10. #include "base/memory/scoped_refptr.h"
  11. #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
  12. namespace network {
  13. class SharedURLLoaderFactory;
  14. class SimpleURLLoader;
  15. }
  16. namespace autofill {
  17. // A class for downloading rules to let libaddressinput validate international
  18. // addresses.
  19. class ChromeMetadataSource : public ::i18n::addressinput::Source {
  20. public:
  21. ChromeMetadataSource(
  22. const std::string& validation_data_url,
  23. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory);
  24. virtual ~ChromeMetadataSource();
  25. // ::i18n::addressinput::Source:
  26. virtual void Get(const std::string& key,
  27. const Callback& downloaded) const override;
  28. private:
  29. struct Request {
  30. Request(const std::string& key,
  31. std::unique_ptr<network::SimpleURLLoader> loader,
  32. const Callback& callback);
  33. std::string key;
  34. // The data that's received.
  35. std::string data;
  36. // The object that manages retrieving the data.
  37. std::unique_ptr<network::SimpleURLLoader> loader;
  38. const Callback& callback;
  39. };
  40. using RequestList = std::list<std::unique_ptr<Request>>;
  41. // Non-const method actually implementing Get().
  42. void Download(const std::string& key, const Callback& downloaded);
  43. void OnSimpleLoaderComplete(RequestList::iterator it,
  44. std::unique_ptr<std::string> response_body);
  45. const std::string validation_data_url_;
  46. scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
  47. // Holds all pending requests and their URL loaders.
  48. RequestList requests_;
  49. DISALLOW_COPY_AND_ASSIGN(ChromeMetadataSource);
  50. };
  51. } // namespace autofill
  52. #endif // THIRD_PARTY_LIBADDRESSINPUT_CHROMIUM_CHROME_METADATA_SOURCE_H_