protocol_parser.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_UPDATE_CLIENT_PROTOCOL_PARSER_H_
  5. #define COMPONENTS_UPDATE_CLIENT_PROTOCOL_PARSER_H_
  6. #include <cstdint>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <vector>
  11. #include "url/gurl.h"
  12. namespace update_client {
  13. class ProtocolParser {
  14. public:
  15. // The result of parsing one |app| entity in an update check response.
  16. struct Result {
  17. struct Manifest {
  18. struct Package {
  19. Package();
  20. Package(const Package& other);
  21. ~Package();
  22. // |fingerprint| is optional. It identifies the package, preferably
  23. // with a modified sha256 hash of the package in hex format.
  24. std::string fingerprint;
  25. // Attributes for the full update.
  26. std::string name;
  27. std::string hash_sha256;
  28. int64_t size = 0;
  29. // Attributes for the differential update.
  30. std::string namediff;
  31. std::string hashdiff_sha256;
  32. int64_t sizediff = 0;
  33. };
  34. Manifest();
  35. Manifest(const Manifest& other);
  36. ~Manifest();
  37. std::string version;
  38. std::string browser_min_version;
  39. std::vector<Package> packages;
  40. // A path within the CRX archive to an executable to run as part of the
  41. // update. The executable is typically an application installer.
  42. std::string run;
  43. // Command-line arguments for the binary specified by |run|.
  44. std::string arguments;
  45. };
  46. // Optional `data` element.
  47. struct Data {
  48. Data();
  49. Data(const Data& other);
  50. Data& operator=(const Data&);
  51. Data(const std::string& status,
  52. const std::string& name,
  53. const std::string& install_data_index,
  54. const std::string& text);
  55. ~Data();
  56. // "ok" if the server could successfully find a match for the `data`
  57. // element in the update request, or an error string otherwise.
  58. std::string status;
  59. // If `status` is "ok", this contains the `name`. The only value supported
  60. // for `name` by the server currently is "install".
  61. std::string name;
  62. // The `install_data_index` specified by the update check.
  63. std::string install_data_index;
  64. // The server data corresponding to the `name`/`install_data_index pair`.
  65. std::string text;
  66. };
  67. Result();
  68. Result(const Result& other);
  69. ~Result();
  70. std::string extension_id;
  71. // The updatecheck response status.
  72. std::string status;
  73. // App-specific additions in the updatecheck response, including the
  74. // mandatory '_' prefix (which prevents collision with formal protocol
  75. // elements).
  76. std::map<std::string, std::string> custom_attributes;
  77. // The list of fallback urls, for full and diff updates respectively.
  78. // These urls are base urls; they don't include the filename.
  79. std::vector<GURL> crx_urls;
  80. std::vector<GURL> crx_diffurls;
  81. Manifest manifest;
  82. // The server has instructed the client to set its [key] to [value] for each
  83. // key-value pair in this string.
  84. std::map<std::string, std::string> cohort_attrs;
  85. // The following are the only allowed keys in |cohort_attrs|.
  86. static const char kCohort[];
  87. static const char kCohortHint[];
  88. static const char kCohortName[];
  89. // Contains the run action returned by the server as part of an update
  90. // check response. This indicates the need to trigger the execution of
  91. // something bound to a component which is already installed.
  92. std::string action_run;
  93. // Contains the data responses corresponding to the data elements specified
  94. // in the update request.
  95. std::vector<Data> data;
  96. };
  97. static const int kNoDaystart = -1;
  98. struct Results {
  99. Results();
  100. Results(const Results& other);
  101. ~Results();
  102. // This will be >= 0, or kNoDaystart if the <daystart> tag was not present.
  103. int daystart_elapsed_seconds = kNoDaystart;
  104. // This will be >= 0, or kNoDaystart if the <daystart> tag was not present.
  105. int daystart_elapsed_days = kNoDaystart;
  106. std::vector<Result> list;
  107. };
  108. static std::unique_ptr<ProtocolParser> Create();
  109. ProtocolParser(const ProtocolParser&) = delete;
  110. ProtocolParser& operator=(const ProtocolParser&) = delete;
  111. virtual ~ProtocolParser();
  112. // Parses an update response string into Result data. Returns a bool
  113. // indicating success or failure. On success, the results are available by
  114. // calling results(). In case of success, only results corresponding to
  115. // the update check status |ok| or |noupdate| are included.
  116. // The details for any failures are available by calling errors().
  117. bool Parse(const std::string& response);
  118. const Results& results() const { return results_; }
  119. const std::string& errors() const { return errors_; }
  120. protected:
  121. ProtocolParser();
  122. // Appends parse error details to |errors_| string.
  123. void ParseError(const char* details, ...);
  124. private:
  125. virtual bool DoParse(const std::string& response, Results* results) = 0;
  126. Results results_;
  127. std::string errors_;
  128. };
  129. } // namespace update_client
  130. #endif // COMPONENTS_UPDATE_CLIENT_PROTOCOL_PARSER_H_