installer.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2019 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 CHROME_UPDATER_INSTALLER_H_
  5. #define CHROME_UPDATER_INSTALLER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/callback_forward.h"
  9. #include "base/files/file_path.h"
  10. #include "base/memory/scoped_refptr.h"
  11. #include "base/sequence_checker.h"
  12. #include "base/version.h"
  13. #include "build/build_config.h"
  14. #include "chrome/updater/persisted_data.h"
  15. #include "chrome/updater/update_service.h"
  16. #include "chrome/updater/updater_scope.h"
  17. #include "components/crx_file/crx_verifier.h"
  18. #include "components/update_client/update_client.h"
  19. #include "third_party/abseil-cpp/absl/types/optional.h"
  20. namespace base {
  21. class TimeDelta;
  22. }
  23. namespace updater {
  24. struct AppInfo {
  25. AppInfo(const UpdaterScope scope,
  26. const std::string& app_id,
  27. const std::string& ap,
  28. const base::Version& app_version,
  29. const base::FilePath& ecp);
  30. AppInfo(const AppInfo&);
  31. AppInfo& operator=(const AppInfo&);
  32. ~AppInfo();
  33. UpdaterScope scope;
  34. std::string app_id;
  35. std::string ap;
  36. base::Version version;
  37. base::FilePath ecp;
  38. };
  39. using AppInstallerResult = update_client::CrxInstaller::Result;
  40. using InstallProgressCallback = update_client::CrxInstaller::ProgressCallback;
  41. // Runs an app installer.
  42. // The file `server_install_data` contains additional application-specific
  43. // install configuration parameters extracted either from the update response or
  44. // the app manifest.
  45. AppInstallerResult RunApplicationInstaller(
  46. const AppInfo& app_info,
  47. const base::FilePath& installer_path,
  48. const std::string& install_args,
  49. const absl::optional<base::FilePath>& server_install_data,
  50. const base::TimeDelta& timeout,
  51. InstallProgressCallback progress_callback);
  52. // Manages the install of one application. Some of the functions of this
  53. // class are blocking and can't be invoked on the main sequence.
  54. //
  55. // If the application installer completes with success, then the following
  56. // post conditions are true: |update_client| updates persisted data in prefs,
  57. // the CRX is installed in a versioned directory in apps/app_id/version,
  58. // the application is considered to be registered for updates, and the
  59. // application installed version matches the version recorded in prefs.
  60. //
  61. // If installing the CRX fails, or the installer fails, then prefs is not
  62. // going to be updated. There will be some files left on the file system, which
  63. // are going to be cleaned up next time the installer runs.
  64. //
  65. // Install directories not matching the |pv| version are lazy-deleted.
  66. class Installer final : public update_client::CrxInstaller {
  67. public:
  68. Installer(const std::string& app_id,
  69. const std::string& install_data_index,
  70. const std::string& target_channel,
  71. const std::string& target_version_prefix,
  72. bool rollback_allowed,
  73. bool update_disabled,
  74. UpdateService::PolicySameVersionUpdate policy_same_version_update,
  75. scoped_refptr<PersistedData> persisted_data,
  76. crx_file::VerifierFormat crx_verifier_format);
  77. Installer(const Installer&) = delete;
  78. Installer& operator=(const Installer&) = delete;
  79. std::string app_id() const { return app_id_; }
  80. // Returns a CrxComponent instance that describes the current install
  81. // state of the app. Updates the values of |pv_| and the |fingerprint_| with
  82. // the persisted values in prefs.
  83. //
  84. // Callers should only invoke this function when handling a CrxDataCallback
  85. // callback from update_client::Install or from update_client::Update. This
  86. // ensure that prefs has been updated with the most recent values, including
  87. // |pv| and |fingerprint|.
  88. update_client::CrxComponent MakeCrxComponent();
  89. private:
  90. ~Installer() override;
  91. // Overrides from update_client::CrxInstaller.
  92. void OnUpdateError(int error) override;
  93. void Install(const base::FilePath& unpack_path,
  94. const std::string& public_key,
  95. std::unique_ptr<InstallParams> install_params,
  96. ProgressCallback progress_callback,
  97. Callback callback) override;
  98. bool GetInstalledFile(const std::string& file,
  99. base::FilePath* installed_file) override;
  100. bool Uninstall() override;
  101. Result InstallHelper(const base::FilePath& unpack_path,
  102. std::unique_ptr<InstallParams> install_params,
  103. ProgressCallback progress_callback);
  104. // Runs the installer code with sync primitives to allow the code to
  105. // create processes and wait for them to exit.
  106. void InstallWithSyncPrimitives(const base::FilePath& unpack_path,
  107. const std::string& public_key,
  108. std::unique_ptr<InstallParams> install_params,
  109. ProgressCallback progress_callback,
  110. Callback callback);
  111. // Deletes recursively the install paths not matching the |pv_| version.
  112. void DeleteOlderInstallPaths();
  113. // Returns an install directory matching the |pv_| version.
  114. absl::optional<base::FilePath> GetCurrentInstallDir() const;
  115. SEQUENCE_CHECKER(sequence_checker_);
  116. UpdaterScope updater_scope_;
  117. const std::string app_id_;
  118. const std::string install_data_index_;
  119. const bool rollback_allowed_;
  120. const std::string target_channel_;
  121. const std::string target_version_prefix_;
  122. const bool update_disabled_;
  123. const UpdateService::PolicySameVersionUpdate policy_same_version_update_;
  124. scoped_refptr<PersistedData> persisted_data_;
  125. const crx_file::VerifierFormat crx_verifier_format_;
  126. // These members are not updated when the installer succeeds.
  127. base::Version pv_;
  128. std::string ap_;
  129. base::FilePath checker_path_;
  130. std::string fingerprint_;
  131. };
  132. } // namespace updater
  133. #endif // CHROME_UPDATER_INSTALLER_H_