puffin_patcher.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "components/update_client/puffin_patcher.h"
  5. #include <string>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/callback.h"
  9. #include "base/files/file_path.h"
  10. #include "base/files/file_util.h"
  11. #include "base/memory/scoped_refptr.h"
  12. #include "base/sequence_checker.h"
  13. #include "base/task/task_runner.h"
  14. #include "base/threading/sequenced_task_runner_handle.h"
  15. #include "components/update_client/component_patcher_operation.h"
  16. #include "components/update_client/patcher.h"
  17. #include "components/update_client/update_client.h"
  18. #include "components/update_client/update_client_errors.h"
  19. #include "third_party/puffin/puffin/src/include/puffin/puffpatch.h"
  20. namespace update_client {
  21. PuffinPatcher::PuffinPatcher(
  22. base::File old_crx_file,
  23. base::File puff_patch_file,
  24. base::File new_crx_output,
  25. scoped_refptr<Patcher> patcher,
  26. base::OnceCallback<void(UnpackerError, int)> callback)
  27. : old_crx_file_(std::move(old_crx_file)),
  28. puff_patch_file_(std::move(puff_patch_file)),
  29. new_crx_output_file_(std::move(new_crx_output)),
  30. patcher_(patcher),
  31. callback_(std::move(callback)) {}
  32. PuffinPatcher::~PuffinPatcher() = default;
  33. void PuffinPatcher::Patch(
  34. base::File old_crx_file,
  35. base::File puff_patch_file,
  36. base::File new_crx_output,
  37. scoped_refptr<Patcher> patcher,
  38. base::OnceCallback<void(UnpackerError, int)> callback) {
  39. scoped_refptr<PuffinPatcher> puffin_patcher =
  40. base::WrapRefCounted(new PuffinPatcher(
  41. std::move(old_crx_file), std::move(puff_patch_file),
  42. std::move(new_crx_output), patcher, std::move(callback)));
  43. base::SequencedTaskRunnerHandle::Get()->PostTask(
  44. FROM_HERE, base::BindOnce(&PuffinPatcher::StartPatching, puffin_patcher));
  45. }
  46. void PuffinPatcher::StartPatching() {
  47. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  48. if (!old_crx_file_.IsValid()) {
  49. DonePatching(UnpackerError::kInvalidParams, 0);
  50. } else if (!puff_patch_file_.IsValid()) {
  51. DonePatching(UnpackerError::kInvalidParams, 0);
  52. } else if (!new_crx_output_file_.IsValid()) {
  53. DonePatching(UnpackerError::kInvalidParams, 0);
  54. } else {
  55. PatchCrx();
  56. }
  57. }
  58. void PuffinPatcher::PatchCrx() {
  59. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  60. patcher_->PatchPuffPatch(std::move(old_crx_file_),
  61. std::move(puff_patch_file_),
  62. std::move(new_crx_output_file_),
  63. base::BindOnce(&PuffinPatcher::DonePatch, this));
  64. }
  65. void PuffinPatcher::DonePatch(int extended_error) {
  66. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  67. if (extended_error != puffin::P_OK) {
  68. DonePatching(UnpackerError::kDeltaOperationFailure, extended_error);
  69. } else {
  70. DonePatching(UnpackerError::kNone, 0);
  71. }
  72. }
  73. void PuffinPatcher::DonePatching(UnpackerError error, int extended_error) {
  74. DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  75. DETACH_FROM_SEQUENCE(sequence_checker_);
  76. std::move(callback_).Run(error, extended_error);
  77. }
  78. } // namespace update_client