utils.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_UTILS_H_
  5. #define COMPONENTS_UPDATE_CLIENT_UTILS_H_
  6. #include <string>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/callback_forward.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "components/update_client/update_client.h"
  12. class GURL;
  13. namespace base {
  14. class FilePath;
  15. class Value;
  16. }
  17. namespace update_client {
  18. class Component;
  19. struct CrxComponent;
  20. // Defines a name-value pair that represents an installer attribute.
  21. // Installer attributes are component-specific metadata, which may be serialized
  22. // in an update check request.
  23. using InstallerAttribute = std::pair<std::string, std::string>;
  24. // Returns true if the |component| contains a valid differential update url.
  25. bool HasDiffUpdate(const Component& component);
  26. // Returns true if the |status_code| represents a server error 5xx.
  27. bool IsHttpServerError(int status_code);
  28. // Deletes the file and its directory, if the directory is empty. If the
  29. // parent directory is not empty, the function ignores deleting the directory.
  30. // Returns true if the file and the empty directory are deleted.
  31. bool DeleteFileAndEmptyParentDirectory(const base::FilePath& filepath);
  32. // Returns the component id of the |component|. The component id is either the
  33. // app_id, if the member is set, or a string value derived from the public
  34. // key hash with a format similar with the format of an extension id.
  35. std::string GetCrxComponentID(const CrxComponent& component);
  36. // Returns a CRX id from a public key hash.
  37. std::string GetCrxIdFromPublicKeyHash(const std::vector<uint8_t>& pk_hash);
  38. // Returns true if the actual SHA-256 hash of the |filepath| matches the
  39. // |expected_hash|.
  40. bool VerifyFileHash256(const base::FilePath& filepath,
  41. const std::string& expected_hash);
  42. // Returns true if the |brand| parameter matches ^[a-zA-Z]{4}?$ .
  43. bool IsValidBrand(const std::string& brand);
  44. // Returns true if the name part of the |attr| parameter matches
  45. // ^[-_a-zA-Z0-9]{1,256}$ and the value part of the |attr| parameter
  46. // matches ^[-.,;+_=$a-zA-Z0-9]{0,256}$ .
  47. bool IsValidInstallerAttribute(const InstallerAttribute& attr);
  48. // Removes the unsecure urls in the |urls| parameter.
  49. void RemoveUnsecureUrls(std::vector<GURL>* urls);
  50. // Adapter function for the old definitions of CrxInstaller::Install until the
  51. // component installer code is migrated to use a Result instead of bool.
  52. CrxInstaller::Result InstallFunctionWrapper(
  53. base::OnceCallback<bool()> callback);
  54. // Deserializes the CRX manifest. The top level must be a dictionary.
  55. // Returns a base::Value object of type dictionary on success, or another type
  56. // on failure.
  57. base::Value ReadManifest(const base::FilePath& unpack_path);
  58. // Converts a custom, specific installer error (and optionally extended error)
  59. // to an installer result.
  60. template <typename T>
  61. CrxInstaller::Result ToInstallerResult(const T& error, int extended_error = 0) {
  62. static_assert(std::is_enum<T>::value,
  63. "Use an enum class to define custom installer errors");
  64. return CrxInstaller::Result(
  65. static_cast<int>(update_client::InstallError::CUSTOM_ERROR_BASE) +
  66. static_cast<int>(error),
  67. extended_error);
  68. }
  69. } // namespace update_client
  70. #endif // COMPONENTS_UPDATE_CLIENT_UTILS_H_