summaryrefslogtreecommitdiff
path: root/chromium/components/update_client/puffin_patcher.cc
blob: 0d9405c8ac5a1be19364fb2a0ff14e11aecf6b16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/update_client/puffin_patcher.h"

#include <string>
#include <utility>

#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/memory/scoped_refptr.h"
#include "base/sequence_checker.h"
#include "base/task/task_runner.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "components/update_client/component_patcher_operation.h"
#include "components/update_client/patcher.h"
#include "components/update_client/update_client.h"
#include "components/update_client/update_client_errors.h"
#include "third_party/puffin/puffin/src/include/puffin/puffpatch.h"

namespace update_client {

PuffinPatcher::PuffinPatcher(
    base::File old_crx_file,
    base::File puff_patch_file,
    base::File new_crx_output,
    scoped_refptr<Patcher> patcher,
    base::OnceCallback<void(UnpackerError, int)> callback)
    : old_crx_file_(std::move(old_crx_file)),
      puff_patch_file_(std::move(puff_patch_file)),
      new_crx_output_file_(std::move(new_crx_output)),
      patcher_(patcher),
      callback_(std::move(callback)) {}

PuffinPatcher::~PuffinPatcher() = default;

void PuffinPatcher::Patch(
    base::File old_crx_file,
    base::File puff_patch_file,
    base::File new_crx_output,
    scoped_refptr<Patcher> patcher,
    base::OnceCallback<void(UnpackerError, int)> callback) {
  scoped_refptr<PuffinPatcher> puffin_patcher =
      base::WrapRefCounted(new PuffinPatcher(
          std::move(old_crx_file), std::move(puff_patch_file),
          std::move(new_crx_output), patcher, std::move(callback)));
  base::SequencedTaskRunnerHandle::Get()->PostTask(
      FROM_HERE, base::BindOnce(&PuffinPatcher::StartPatching, puffin_patcher));
}

void PuffinPatcher::StartPatching() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (!old_crx_file_.IsValid()) {
    DonePatching(UnpackerError::kInvalidParams, 0);
  } else if (!puff_patch_file_.IsValid()) {
    DonePatching(UnpackerError::kInvalidParams, 0);
  } else if (!new_crx_output_file_.IsValid()) {
    DonePatching(UnpackerError::kInvalidParams, 0);
  } else {
    PatchCrx();
  }
}

void PuffinPatcher::PatchCrx() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  patcher_->PatchPuffPatch(std::move(old_crx_file_),
                           std::move(puff_patch_file_),
                           std::move(new_crx_output_file_),
                           base::BindOnce(&PuffinPatcher::DonePatch, this));
}

void PuffinPatcher::DonePatch(int extended_error) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  if (extended_error != puffin::P_OK) {
    DonePatching(UnpackerError::kDeltaOperationFailure, extended_error);
  } else {
    DonePatching(UnpackerError::kNone, 0);
  }
}

void PuffinPatcher::DonePatching(UnpackerError error, int extended_error) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DETACH_FROM_SEQUENCE(sequence_checker_);
  std::move(callback_).Run(error, extended_error);
}

}  // namespace update_client