puffin_patcher.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright 2022 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 puffdiff updates come in *.puff
  6. // files, with a different magic number. They describe a bsdiff diff generated
  7. // by Puffin. The patcher takes the previous version of this binary, applies
  8. // puffpatch and generates the newest version of the CRX.
  9. //
  10. // The component updater attempts a differential update if it is available
  11. // and allowed to, and fall back to a full update if it fails.
  12. //
  13. // After installation (diff or full), the component updater records "fp", the
  14. // fingerprint of the installed files, to later identify the existing files to
  15. // the server so that a proper differential update can be provided next cycle.
  16. #ifndef COMPONENTS_UPDATE_CLIENT_PUFFIN_PATCHER_H_
  17. #define COMPONENTS_UPDATE_CLIENT_PUFFIN_PATCHER_H_
  18. #include "base/callback_forward.h"
  19. #include "base/files/file.h"
  20. #include "base/memory/ref_counted.h"
  21. #include "base/sequence_checker.h"
  22. #include "base/values.h"
  23. #include "components/update_client/component_unpacker.h"
  24. namespace base {
  25. class File;
  26. }
  27. namespace update_client {
  28. class Patcher;
  29. // Encapsulates a task for applying a differential update to a component.
  30. class PuffinPatcher : public base::RefCountedThreadSafe<PuffinPatcher> {
  31. public:
  32. PuffinPatcher(const PuffinPatcher&) = delete;
  33. PuffinPatcher& operator=(const PuffinPatcher&) = delete;
  34. // Takes a full CRX `old_crx_file` and a Puffin puffpatch file
  35. // `puff_patch_file`, and sets up the class to create a new full
  36. // If `in_process` is true, patching is done completely within the
  37. // existing process. Otherwise, some steps of patching may be done
  38. // out-of-process. Then, it starts patching files.
  39. //
  40. // This static function returns immediately, after posting a task
  41. // to do the patching. When patching has been completed,
  42. // `callback` is called with the error codes if any error codes were
  43. // encountered.
  44. static void Patch(base::File old_crx_file,
  45. base::File puff_patch_file,
  46. base::File new_crx_output,
  47. scoped_refptr<Patcher> patcher,
  48. base::OnceCallback<void(UnpackerError, int)> callback);
  49. private:
  50. friend class base::RefCountedThreadSafe<PuffinPatcher>;
  51. PuffinPatcher(base::File old_crx_file,
  52. base::File puff_patch_file,
  53. base::File new_crx_output,
  54. scoped_refptr<Patcher> patcher,
  55. base::OnceCallback<void(UnpackerError, int)> callback);
  56. virtual ~PuffinPatcher();
  57. void StartPatching();
  58. void PatchCrx();
  59. void DonePatch(int extended_error);
  60. void DonePatching(UnpackerError error, int extended_error);
  61. base::File old_crx_file_;
  62. base::File puff_patch_file_;
  63. base::File new_crx_output_file_;
  64. scoped_refptr<Patcher> patcher_;
  65. base::OnceCallback<void(UnpackerError, int)> callback_;
  66. SEQUENCE_CHECKER(sequence_checker_);
  67. };
  68. } // namespace update_client
  69. #endif // COMPONENTS_UPDATE_CLIENT_PUFFIN_PATCHER_H_