in_process_patcher.cc 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2019 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/patch/in_process_patcher.h"
  5. #include <utility>
  6. #include "base/callback.h"
  7. #include "base/files/file.h"
  8. #include "base/files/file_path.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "base/notreached.h"
  11. #include "components/update_client/buildflags.h"
  12. #include "courgette/courgette.h"
  13. #include "courgette/third_party/bsdiff/bsdiff.h"
  14. namespace update_client {
  15. namespace {
  16. class InProcessPatcher : public Patcher {
  17. public:
  18. InProcessPatcher() = default;
  19. // TODO(crbug.com/1349158): Remove this function once PatchPuffPatch is
  20. // implemented as this becomes obsolete.
  21. void PatchBsdiff(const base::FilePath& input_path,
  22. const base::FilePath& patch_path,
  23. const base::FilePath& output_path,
  24. PatchCompleteCallback callback) const override {
  25. base::File input_file(input_path,
  26. base::File::FLAG_OPEN | base::File::FLAG_READ);
  27. base::File patch_file(patch_path,
  28. base::File::FLAG_OPEN | base::File::FLAG_READ);
  29. base::File output_file(output_path,
  30. base::File::FLAG_CREATE | base::File::FLAG_WRITE |
  31. base::File::FLAG_WIN_EXCLUSIVE_WRITE);
  32. if (!input_file.IsValid() || !patch_file.IsValid() ||
  33. !output_file.IsValid()) {
  34. std::move(callback).Run(-1);
  35. return;
  36. }
  37. std::move(callback).Run(bsdiff::ApplyBinaryPatch(
  38. std::move(input_file), std::move(patch_file), std::move(output_file)));
  39. }
  40. // TODO(crbug.com/1349158): Remove this function once PatchPuffPatch is
  41. // implemented as this becomes obsolete.
  42. void PatchCourgette(const base::FilePath& input_path,
  43. const base::FilePath& patch_path,
  44. const base::FilePath& output_path,
  45. PatchCompleteCallback callback) const override {
  46. base::File input_file(input_path,
  47. base::File::FLAG_OPEN | base::File::FLAG_READ);
  48. base::File patch_file(patch_path,
  49. base::File::FLAG_OPEN | base::File::FLAG_READ);
  50. base::File output_file(output_path,
  51. base::File::FLAG_CREATE | base::File::FLAG_WRITE |
  52. base::File::FLAG_WIN_EXCLUSIVE_WRITE);
  53. if (!input_file.IsValid() || !patch_file.IsValid() ||
  54. !output_file.IsValid()) {
  55. std::move(callback).Run(-1);
  56. return;
  57. }
  58. std::move(callback).Run(courgette::ApplyEnsemblePatch(
  59. std::move(input_file), std::move(patch_file), std::move(output_file)));
  60. }
  61. void PatchPuffPatch(base::File input_file,
  62. base::File patch_file,
  63. base::File output_file,
  64. PatchCompleteCallback callback) const override {
  65. #if BUILDFLAG(ENABLE_PUFFIN_PATCHES)
  66. // TODO(crbug.com/1349060) once Puffin patches are fully implemented,
  67. // we should remove this #if.
  68. std::move(callback).Run(puffin::ApplyPuffPatch(
  69. std::move(input_file), std::move(patch_file), std::move(output_file)));
  70. #else
  71. NOTREACHED();
  72. #endif
  73. }
  74. protected:
  75. ~InProcessPatcher() override = default;
  76. };
  77. } // namespace
  78. InProcessPatcherFactory::InProcessPatcherFactory() = default;
  79. scoped_refptr<Patcher> InProcessPatcherFactory::Create() const {
  80. return base::MakeRefCounted<InProcessPatcher>();
  81. }
  82. InProcessPatcherFactory::~InProcessPatcherFactory() = default;
  83. } // namespace update_client