component_installer.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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_COMPONENT_UPDATER_COMPONENT_INSTALLER_H_
  5. #define COMPONENTS_COMPONENT_UPDATER_COMPONENT_INSTALLER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/callback_forward.h"
  11. #include "base/files/file_path.h"
  12. #include "base/memory/ref_counted.h"
  13. #include "base/task/task_traits.h"
  14. #include "base/threading/thread_checker.h"
  15. #include "base/values.h"
  16. #include "base/version.h"
  17. #include "components/update_client/update_client.h"
  18. #include "third_party/abseil-cpp/absl/types/optional.h"
  19. namespace base {
  20. class SequencedTaskRunner;
  21. class SingleThreadTaskRunner;
  22. } // namespace base
  23. namespace component_updater {
  24. struct ComponentRegistration;
  25. class ComponentUpdateService;
  26. using RegisterCallback = base::OnceCallback<bool(const ComponentRegistration&)>;
  27. // Components should use a ComponentInstaller by defining a class that
  28. // implements the members of ComponentInstallerPolicy, and then registering a
  29. // ComponentInstaller that has been constructed with an instance of that
  30. // class.
  31. class ComponentInstallerPolicy {
  32. public:
  33. virtual ~ComponentInstallerPolicy();
  34. // Verifies that a working installation resides within the directory specified
  35. // by |install_dir|. |install_dir| is of the form <base directory>/<version>.
  36. // |manifest| should have been read from the manifest file in |install_dir|.
  37. // |manifest| is a DICTIONARY base::Value. Called only from a thread belonging
  38. // to a blocking thread pool. The implementation of this function must be
  39. // efficient since the function can be called when Chrome starts.
  40. virtual bool VerifyInstallation(const base::Value& manifest,
  41. const base::FilePath& install_dir) const = 0;
  42. // Returns true if the component supports a group policy to enable updates.
  43. // Called once during component registration from the UI thread.
  44. virtual bool SupportsGroupPolicyEnabledComponentUpdates() const = 0;
  45. // Returns true if the network communication related to this component
  46. // must be encrypted.
  47. virtual bool RequiresNetworkEncryption() const = 0;
  48. // OnCustomInstall is called during the installation process. Components that
  49. // require custom installation operations should implement them here.
  50. // Returns a failure result if a custom operation failed, and
  51. // update_client::InstallError::NONE otherwise. |manifest| is a DICTIONARY
  52. // base::Value. Called only from a thread belonging to a blocking thread pool.
  53. virtual update_client::CrxInstaller::Result OnCustomInstall(
  54. const base::Value& manifest,
  55. const base::FilePath& install_dir) = 0;
  56. // OnCustomUninstall is called during the unregister (uninstall) process.
  57. // Components that require custom uninstallation operations should implement
  58. // them here.
  59. // Called only from a thread belonging to a blocking thread pool.
  60. virtual void OnCustomUninstall() = 0;
  61. // ComponentReady is called in two cases:
  62. // 1) After an installation is successfully completed.
  63. // 2) During component registration if the component is already installed.
  64. // In both cases the install is verified before this is called. This method
  65. // is guaranteed to be called before any observers of the component are
  66. // notified of a successful install, and is meant to support follow-on work
  67. // such as updating paths elsewhere in Chrome. Called on the UI thread.
  68. // |version| is the version of the component.
  69. // |install_dir| is the path to the install directory for this version.
  70. // |manifest| is the manifest for this version of the component, and is a
  71. // DICTIONARY base::Value.
  72. virtual void ComponentReady(const base::Version& version,
  73. const base::FilePath& install_dir,
  74. base::Value manifest) = 0;
  75. // Returns a relative path that will be appended to the component updater
  76. // root directories to find the data for this particular component.
  77. virtual base::FilePath GetRelativeInstallDir() const = 0;
  78. // Returns the component's SHA2 hash as raw bytes.
  79. virtual void GetHash(std::vector<uint8_t>* hash) const = 0;
  80. // Returns the human-readable name of the component.
  81. virtual std::string GetName() const = 0;
  82. // Returns a container of name-value pairs representing arbitrary,
  83. // installer-defined metadata.
  84. // The installer metadata may be used in the update checks for this component.
  85. // A compatible server may use these attributes to negotiate special update
  86. // rules when issuing an update response.
  87. // Valid values for the name part of an attribute match
  88. // ^[-_a-zA-Z0-9]{1,256}$ and valid values the value part of an attribute
  89. // match ^[-.,;+_=$a-zA-Z0-9]{0,256}$ .
  90. virtual update_client::InstallerAttributes GetInstallerAttributes() const = 0;
  91. };
  92. // Defines the installer for Chrome components. The behavior of this class is
  93. // controlled by an instance of ComponentInstallerPolicy, at construction time.
  94. class ComponentInstaller final : public update_client::CrxInstaller {
  95. public:
  96. ComponentInstaller(
  97. std::unique_ptr<ComponentInstallerPolicy> installer_policy,
  98. scoped_refptr<update_client::ActionHandler> action_handler = nullptr);
  99. ComponentInstaller(const ComponentInstaller&) = delete;
  100. ComponentInstaller& operator=(const ComponentInstaller&) = delete;
  101. // Registers the component for update checks and installs.
  102. // |cus| provides the registration logic.
  103. // The passed |callback| will be called once the initial check for installed
  104. // versions is done and the component has been registered.
  105. // Registration tasks will be done with a priority of |task_priority|. Some
  106. // components may affect user-visible features, hence a default of
  107. // USER_VISIBLE.
  108. void Register(
  109. ComponentUpdateService* cus,
  110. base::OnceClosure callback,
  111. base::TaskPriority task_priority = base::TaskPriority::USER_VISIBLE);
  112. // Registers the component for update checks and installs.
  113. // |register_callback| is called to do the registration.
  114. // |callback| is called when registration finishes.
  115. // Registration tasks will be done with a priority of |task_priority|. Some
  116. // components may affect user-visible features, hence a default of
  117. // USER_VISIBLE.
  118. void Register(
  119. RegisterCallback register_callback,
  120. base::OnceClosure callback,
  121. base::TaskPriority task_priority = base::TaskPriority::USER_VISIBLE);
  122. // Overrides from update_client::CrxInstaller.
  123. void OnUpdateError(int error) override;
  124. void Install(const base::FilePath& unpack_path,
  125. const std::string& public_key,
  126. std::unique_ptr<InstallParams> install_params,
  127. ProgressCallback progress_callback,
  128. Callback callback) override;
  129. bool GetInstalledFile(const std::string& file,
  130. base::FilePath* installed_file) override;
  131. // Only user-level component installations can be uninstalled.
  132. bool Uninstall() override;
  133. private:
  134. struct RegistrationInfo : base::RefCountedThreadSafe<RegistrationInfo> {
  135. RegistrationInfo();
  136. RegistrationInfo(const RegistrationInfo&) = delete;
  137. RegistrationInfo& operator=(const RegistrationInfo&) = delete;
  138. base::FilePath install_dir;
  139. base::Version version;
  140. std::string fingerprint;
  141. absl::optional<base::Value> manifest;
  142. private:
  143. friend class base::RefCountedThreadSafe<RegistrationInfo>;
  144. ~RegistrationInfo();
  145. };
  146. ~ComponentInstaller() override;
  147. // If there is a installation of the component set up alongside Chrome's
  148. // files (as opposed to in the user data directory), sets current_* to the
  149. // values associated with that installation and returns true; otherwise,
  150. // returns false.
  151. bool FindPreinstallation(const base::FilePath& root,
  152. scoped_refptr<RegistrationInfo> registration_info);
  153. update_client::CrxInstaller::Result InstallHelper(
  154. const base::FilePath& unpack_path,
  155. base::Value* manifest,
  156. base::Version* version,
  157. base::FilePath* install_path);
  158. void StartRegistration(scoped_refptr<RegistrationInfo> registration_info);
  159. void FinishRegistration(scoped_refptr<RegistrationInfo> registration_info,
  160. RegisterCallback register_callback,
  161. base::OnceClosure callback);
  162. void ComponentReady(base::Value manifest);
  163. void UninstallOnTaskRunner();
  164. THREAD_CHECKER(thread_checker_);
  165. base::FilePath current_install_dir_;
  166. base::Version current_version_;
  167. std::string current_fingerprint_;
  168. std::unique_ptr<ComponentInstallerPolicy> installer_policy_;
  169. scoped_refptr<update_client::ActionHandler> action_handler_;
  170. scoped_refptr<base::SequencedTaskRunner> task_runner_;
  171. // Posts responses back to the main thread.
  172. scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
  173. };
  174. } // namespace component_updater
  175. #endif // COMPONENTS_COMPONENT_UPDATER_COMPONENT_INSTALLER_H_