component_patcher.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. // Component updates can be either differential updates or full updates.
  5. // Full updates come in CRX format; differential updates come in CRX-style
  6. // archives, but have a different magic number. They contain "commands.json", a
  7. // list of commands for the patcher to follow. The patcher uses these commands,
  8. // the other files in the archive, and the files from the existing installation
  9. // of the component to create the contents of a full update, which is then
  10. // installed normally.
  11. // Component updates are specified by the 'codebasediff' attribute of an
  12. // updatecheck response:
  13. // <updatecheck codebase="http://example.com/extension_1.2.3.4.crx"
  14. // hash="12345" size="9854" status="ok" version="1.2.3.4"
  15. // prodversionmin="2.0.143.0"
  16. // codebasediff="http://example.com/diff_1.2.3.4.crx"
  17. // hashdiff="123" sizediff="101"
  18. // fp="1.123"/>
  19. // The component updater will attempt a differential update if it is available
  20. // and allowed to, and fall back to a full update if it fails.
  21. //
  22. // After installation (diff or full), the component updater records "fp", the
  23. // fingerprint of the installed files, to later identify the existing files to
  24. // the server so that a proper differential update can be provided next cycle.
  25. #ifndef COMPONENTS_UPDATE_CLIENT_COMPONENT_PATCHER_H_
  26. #define COMPONENTS_UPDATE_CLIENT_COMPONENT_PATCHER_H_
  27. #include "base/callback_forward.h"
  28. #include "base/memory/ref_counted.h"
  29. #include "base/values.h"
  30. #include "components/update_client/component_unpacker.h"
  31. #include "third_party/abseil-cpp/absl/types/optional.h"
  32. // TODO(crbug.com/1349158): Remove this class once Puffin patches are fully
  33. // implemented.
  34. namespace base {
  35. class FilePath;
  36. }
  37. namespace update_client {
  38. class CrxInstaller;
  39. class DeltaUpdateOp;
  40. class Patcher;
  41. enum class UnpackerError;
  42. // The type of a patch file.
  43. enum PatchType {
  44. kPatchTypeUnknown,
  45. kPatchTypeCourgette,
  46. kPatchTypeBsdiff,
  47. };
  48. // Encapsulates a task for applying a differential update to a component.
  49. class ComponentPatcher : public base::RefCountedThreadSafe<ComponentPatcher> {
  50. public:
  51. using Callback = base::OnceCallback<void(UnpackerError, int)>;
  52. // Takes an unpacked differential CRX (|input_dir|) and a component installer,
  53. // and sets up the class to create a new (non-differential) unpacked CRX.
  54. // If |in_process| is true, patching will be done completely within the
  55. // existing process. Otherwise, some steps of patching may be done
  56. // out-of-process.
  57. ComponentPatcher(const base::FilePath& input_dir,
  58. const base::FilePath& unpack_dir,
  59. scoped_refptr<CrxInstaller> installer,
  60. scoped_refptr<Patcher> patcher);
  61. ComponentPatcher(const ComponentPatcher&) = delete;
  62. ComponentPatcher& operator=(const ComponentPatcher&) = delete;
  63. // Starts patching files. This member function returns immediately, after
  64. // posting a task to do the patching. When patching has been completed,
  65. // |callback| will be called with the error codes if any error codes were
  66. // encountered.
  67. void Start(Callback callback);
  68. private:
  69. friend class base::RefCountedThreadSafe<ComponentPatcher>;
  70. virtual ~ComponentPatcher();
  71. void StartPatching();
  72. void PatchNextFile();
  73. void DonePatchingFile(UnpackerError error, int extended_error);
  74. void DonePatching(UnpackerError error, int extended_error);
  75. const base::FilePath input_dir_;
  76. const base::FilePath unpack_dir_;
  77. scoped_refptr<CrxInstaller> installer_;
  78. scoped_refptr<Patcher> patcher_;
  79. Callback callback_;
  80. absl::optional<base::Value::List> commands_;
  81. base::Value::List::const_iterator next_command_;
  82. scoped_refptr<DeltaUpdateOp> current_operation_;
  83. };
  84. } // namespace update_client
  85. #endif // COMPONENTS_UPDATE_CLIENT_COMPONENT_PATCHER_H_