component_patcher_operation.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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_COMPONENT_PATCHER_OPERATION_H_
  5. #define COMPONENTS_UPDATE_CLIENT_COMPONENT_PATCHER_OPERATION_H_
  6. #include <string>
  7. #include "base/callback.h"
  8. #include "base/files/file_path.h"
  9. #include "base/memory/ref_counted.h"
  10. #include "base/values.h"
  11. #include "components/update_client/component_patcher.h"
  12. #include "components/update_client/component_unpacker.h"
  13. // TODO(crbug.com/1349158): Remove this file once Puffin patches are fully
  14. // implemented.
  15. namespace update_client {
  16. extern const char kOp[];
  17. extern const char kBsdiff[];
  18. extern const char kCourgette[];
  19. extern const char kInput[];
  20. extern const char kPatch[];
  21. class CrxInstaller;
  22. class Patcher;
  23. enum class UnpackerError;
  24. class DeltaUpdateOp : public base::RefCountedThreadSafe<DeltaUpdateOp> {
  25. public:
  26. DeltaUpdateOp();
  27. DeltaUpdateOp(const DeltaUpdateOp&) = delete;
  28. DeltaUpdateOp& operator=(const DeltaUpdateOp&) = delete;
  29. // Parses, runs, and verifies the operation. Calls |callback| with the
  30. // result of the operation. The callback is called using |task_runner|.
  31. void Run(const base::Value::Dict& command_args,
  32. const base::FilePath& input_dir,
  33. const base::FilePath& unpack_dir,
  34. scoped_refptr<CrxInstaller> installer,
  35. ComponentPatcher::Callback callback);
  36. protected:
  37. virtual ~DeltaUpdateOp();
  38. std::string output_sha256_;
  39. base::FilePath output_abs_path_;
  40. private:
  41. friend class base::RefCountedThreadSafe<DeltaUpdateOp>;
  42. UnpackerError CheckHash();
  43. // Subclasses must override DoParseArguments to parse operation-specific
  44. // arguments. DoParseArguments returns DELTA_OK on success; any other code
  45. // represents failure.
  46. virtual UnpackerError DoParseArguments(
  47. const base::Value::Dict& command_args,
  48. const base::FilePath& input_dir,
  49. scoped_refptr<CrxInstaller> installer) = 0;
  50. // Subclasses must override DoRun to actually perform the patching operation.
  51. // They must call the provided callback when they have completed their
  52. // operations. In practice, the provided callback is always for "DoneRunning".
  53. virtual void DoRun(ComponentPatcher::Callback callback) = 0;
  54. // Callback given to subclasses for when they complete their operation.
  55. // Validates the output, and posts a task to the patching operation's
  56. // callback.
  57. void DoneRunning(UnpackerError error, int extended_error);
  58. ComponentPatcher::Callback callback_;
  59. };
  60. // A 'copy' operation takes a file currently residing on the disk and moves it
  61. // into the unpacking directory: this represents "no change" in the file being
  62. // installed.
  63. class DeltaUpdateOpCopy : public DeltaUpdateOp {
  64. public:
  65. DeltaUpdateOpCopy();
  66. DeltaUpdateOpCopy(const DeltaUpdateOpCopy&) = delete;
  67. DeltaUpdateOpCopy& operator=(const DeltaUpdateOpCopy&) = delete;
  68. private:
  69. ~DeltaUpdateOpCopy() override;
  70. // Overrides of DeltaUpdateOp.
  71. UnpackerError DoParseArguments(
  72. const base::Value::Dict& command_args,
  73. const base::FilePath& input_dir,
  74. scoped_refptr<CrxInstaller> installer) override;
  75. void DoRun(ComponentPatcher::Callback callback) override;
  76. base::FilePath input_abs_path_;
  77. };
  78. // A 'create' operation takes a full file that was sent in the delta update
  79. // archive and moves it into the unpacking directory: this represents the
  80. // addition of a new file, or a file so different that no bandwidth could be
  81. // saved by transmitting a differential update.
  82. class DeltaUpdateOpCreate : public DeltaUpdateOp {
  83. public:
  84. DeltaUpdateOpCreate();
  85. DeltaUpdateOpCreate(const DeltaUpdateOpCreate&) = delete;
  86. DeltaUpdateOpCreate& operator=(const DeltaUpdateOpCreate&) = delete;
  87. private:
  88. ~DeltaUpdateOpCreate() override;
  89. // Overrides of DeltaUpdateOp.
  90. UnpackerError DoParseArguments(
  91. const base::Value::Dict& command_args,
  92. const base::FilePath& input_dir,
  93. scoped_refptr<CrxInstaller> installer) override;
  94. void DoRun(ComponentPatcher::Callback callback) override;
  95. base::FilePath patch_abs_path_;
  96. };
  97. // Both 'bsdiff' and 'courgette' operations take an existing file on disk,
  98. // and a bsdiff- or Courgette-format patch file provided in the delta update
  99. // package, and run bsdiff or Courgette to construct an output file in the
  100. // unpacking directory.
  101. class DeltaUpdateOpPatch : public DeltaUpdateOp {
  102. public:
  103. DeltaUpdateOpPatch(const std::string& operation,
  104. scoped_refptr<Patcher> patcher);
  105. DeltaUpdateOpPatch(const DeltaUpdateOpPatch&) = delete;
  106. DeltaUpdateOpPatch& operator=(const DeltaUpdateOpPatch&) = delete;
  107. private:
  108. ~DeltaUpdateOpPatch() override;
  109. // Overrides of DeltaUpdateOp.
  110. UnpackerError DoParseArguments(
  111. const base::Value::Dict& command_args,
  112. const base::FilePath& input_dir,
  113. scoped_refptr<CrxInstaller> installer) override;
  114. void DoRun(ComponentPatcher::Callback callback) override;
  115. // |success_code| is the code that indicates a successful patch.
  116. // |result| is the code the patching operation returned.
  117. void DonePatching(ComponentPatcher::Callback callback, int result);
  118. std::string operation_;
  119. scoped_refptr<Patcher> patcher_;
  120. base::FilePath patch_abs_path_;
  121. base::FilePath input_abs_path_;
  122. };
  123. DeltaUpdateOp* CreateDeltaUpdateOp(const std::string& operation,
  124. scoped_refptr<Patcher> patcher);
  125. } // namespace update_client
  126. #endif // COMPONENTS_UPDATE_CLIENT_COMPONENT_PATCHER_OPERATION_H_