template_url_parser.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_PARSER_H_
  5. #define COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_PARSER_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/callback.h"
  10. class SearchTermsData;
  11. class TemplateURL;
  12. namespace data_decoder {
  13. class DataDecoder;
  14. }
  15. // TemplateURLParser, as the name implies, handling reading of TemplateURLs
  16. // from OpenSearch description documents.
  17. class TemplateURLParser {
  18. public:
  19. // A ParameterFilter is called for every URL paramter encountered during
  20. // Parse(). It passes the parameter key as the first argument and the value
  21. // as the second. The callback should return true if the parameter should be
  22. // kept, and false if it should be discarded.
  23. using ParameterFilter =
  24. base::RepeatingCallback<bool(const std::string&, const std::string&)>;
  25. using ParseCallback = base::OnceCallback<void(std::unique_ptr<TemplateURL>)>;
  26. TemplateURLParser(const TemplateURLParser&) = delete;
  27. TemplateURLParser& operator=(const TemplateURLParser&) = delete;
  28. // Decodes the chunk of data representing a TemplateURL, creates the
  29. // TemplateURL, and calls the |completion_callback| with the result. A null
  30. // value is provided if the data does not describe a valid TemplateURL, the
  31. // URLs referenced do not point to valid http/https resources, or for some
  32. // other reason we do not support the described TemplateURL.
  33. // |parameter_filter| can be used if you want to filter some parameters out
  34. // of the URL. For example, when importing from another browser, we remove
  35. // any parameter identifying that browser. If set to null, the URL is not
  36. // modified.
  37. static void Parse(const SearchTermsData* search_terms_data,
  38. const std::string& data,
  39. const ParameterFilter& parameter_filter,
  40. ParseCallback completion_callback);
  41. // The same as Parse(), but it allows the caller to manage the lifetime of
  42. // the DataDecoder service. The |data_decoder| must be kept alive until the
  43. // |completion_callback| is called.
  44. static void ParseWithDataDecoder(data_decoder::DataDecoder* data_decoder,
  45. const SearchTermsData* search_terms_data,
  46. const std::string& data,
  47. const ParameterFilter& parameter_filter,
  48. ParseCallback completion_callback);
  49. private:
  50. // No one should create one of these.
  51. TemplateURLParser();
  52. };
  53. #endif // COMPONENTS_SEARCH_ENGINES_TEMPLATE_URL_PARSER_H_