summaryrefslogtreecommitdiff
path: root/chromium/components/update_client/component_patcher_operation.h
blob: 0649ff959b496da2fede7c07ba1784d9694aa835 (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_UPDATE_CLIENT_COMPONENT_PATCHER_OPERATION_H_
#define COMPONENTS_UPDATE_CLIENT_COMPONENT_PATCHER_OPERATION_H_

#include <string>

#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "components/update_client/component_patcher.h"
#include "components/update_client/component_unpacker.h"

namespace base {
class DictionaryValue;
}  // namespace base

namespace update_client {

extern const char kOp[];
extern const char kBsdiff[];
extern const char kCourgette[];
extern const char kInput[];
extern const char kPatch[];

class CrxInstaller;
class Patcher;
enum class UnpackerError;

class DeltaUpdateOp : public base::RefCountedThreadSafe<DeltaUpdateOp> {
 public:
  DeltaUpdateOp();

  // Parses, runs, and verifies the operation. Calls |callback| with the
  // result of the operation. The callback is called using |task_runner|.
  void Run(const base::DictionaryValue* command_args,
           const base::FilePath& input_dir,
           const base::FilePath& unpack_dir,
           scoped_refptr<CrxInstaller> installer,
           ComponentPatcher::Callback callback);

 protected:
  virtual ~DeltaUpdateOp();

  std::string output_sha256_;
  base::FilePath output_abs_path_;

 private:
  friend class base::RefCountedThreadSafe<DeltaUpdateOp>;

  UnpackerError CheckHash();

  // Subclasses must override DoParseArguments to parse operation-specific
  // arguments. DoParseArguments returns DELTA_OK on success; any other code
  // represents failure.
  virtual UnpackerError DoParseArguments(
      const base::DictionaryValue* command_args,
      const base::FilePath& input_dir,
      scoped_refptr<CrxInstaller> installer) = 0;

  // Subclasses must override DoRun to actually perform the patching operation.
  // They must call the provided callback when they have completed their
  // operations. In practice, the provided callback is always for "DoneRunning".
  virtual void DoRun(ComponentPatcher::Callback callback) = 0;

  // Callback given to subclasses for when they complete their operation.
  // Validates the output, and posts a task to the patching operation's
  // callback.
  void DoneRunning(UnpackerError error, int extended_error);

  ComponentPatcher::Callback callback_;

  DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOp);
};

// A 'copy' operation takes a file currently residing on the disk and moves it
// into the unpacking directory: this represents "no change" in the file being
// installed.
class DeltaUpdateOpCopy : public DeltaUpdateOp {
 public:
  DeltaUpdateOpCopy();

 private:
  ~DeltaUpdateOpCopy() override;

  // Overrides of DeltaUpdateOp.
  UnpackerError DoParseArguments(
      const base::DictionaryValue* command_args,
      const base::FilePath& input_dir,
      scoped_refptr<CrxInstaller> installer) override;

  void DoRun(ComponentPatcher::Callback callback) override;

  base::FilePath input_abs_path_;

  DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpCopy);
};

// A 'create' operation takes a full file that was sent in the delta update
// archive and moves it into the unpacking directory: this represents the
// addition of a new file, or a file so different that no bandwidth could be
// saved by transmitting a differential update.
class DeltaUpdateOpCreate : public DeltaUpdateOp {
 public:
  DeltaUpdateOpCreate();

 private:
  ~DeltaUpdateOpCreate() override;

  // Overrides of DeltaUpdateOp.
  UnpackerError DoParseArguments(
      const base::DictionaryValue* command_args,
      const base::FilePath& input_dir,
      scoped_refptr<CrxInstaller> installer) override;

  void DoRun(ComponentPatcher::Callback callback) override;

  base::FilePath patch_abs_path_;

  DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpCreate);
};

// Both 'bsdiff' and 'courgette' operations take an existing file on disk,
// and a bsdiff- or Courgette-format patch file provided in the delta update
// package, and run bsdiff or Courgette to construct an output file in the
// unpacking directory.
class DeltaUpdateOpPatch : public DeltaUpdateOp {
 public:
  DeltaUpdateOpPatch(const std::string& operation,
                     scoped_refptr<Patcher> patcher);

 private:
  ~DeltaUpdateOpPatch() override;

  // Overrides of DeltaUpdateOp.
  UnpackerError DoParseArguments(
      const base::DictionaryValue* command_args,
      const base::FilePath& input_dir,
      scoped_refptr<CrxInstaller> installer) override;

  void DoRun(ComponentPatcher::Callback callback) override;

  // |success_code| is the code that indicates a successful patch.
  // |result| is the code the patching operation returned.
  void DonePatching(ComponentPatcher::Callback callback, int result);

  std::string operation_;
  scoped_refptr<Patcher> patcher_;
  base::FilePath patch_abs_path_;
  base::FilePath input_abs_path_;

  DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpPatch);
};

DeltaUpdateOp* CreateDeltaUpdateOp(const std::string& operation,
                                   scoped_refptr<Patcher> patcher);

}  // namespace update_client

#endif  // COMPONENTS_UPDATE_CLIENT_COMPONENT_PATCHER_OPERATION_H_