1 // Copyright 2014 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.
5 #ifndef COMPONENTS_UPDATE_CLIENT_COMPONENT_PATCHER_OPERATION_H_
6 #define COMPONENTS_UPDATE_CLIENT_COMPONENT_PATCHER_OPERATION_H_
10 #include "base/callback.h"
11 #include "base/compiler_specific.h"
12 #include "base/files/file_path.h"
13 #include "base/macros.h"
14 #include "base/memory/ref_counted.h"
15 #include "components/update_client/component_unpacker.h"
18 class DictionaryValue
;
21 namespace update_client
{
23 extern const char kOp
[];
24 extern const char kBsdiff
[];
25 extern const char kCourgette
[];
26 extern const char kInput
[];
27 extern const char kPatch
[];
29 class ComponentInstaller
;
31 class DeltaUpdateOp
: public base::RefCountedThreadSafe
<DeltaUpdateOp
> {
35 // Parses, runs, and verifies the operation. Calls |callback| with the
36 // result of the operation. The callback is called using |task_runner|.
37 void Run(const base::DictionaryValue
* command_args
,
38 const base::FilePath
& input_dir
,
39 const base::FilePath
& unpack_dir
,
40 const scoped_refptr
<ComponentInstaller
>& installer
,
41 const ComponentUnpacker::Callback
& callback
,
42 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
);
45 virtual ~DeltaUpdateOp();
47 scoped_refptr
<base::SequencedTaskRunner
> GetTaskRunner();
49 std::string output_sha256_
;
50 base::FilePath output_abs_path_
;
53 friend class base::RefCountedThreadSafe
<DeltaUpdateOp
>;
55 ComponentUnpacker::Error
CheckHash();
57 // Subclasses must override DoParseArguments to parse operation-specific
58 // arguments. DoParseArguments returns DELTA_OK on success; any other code
59 // represents failure.
60 virtual ComponentUnpacker::Error
DoParseArguments(
61 const base::DictionaryValue
* command_args
,
62 const base::FilePath
& input_dir
,
63 const scoped_refptr
<ComponentInstaller
>& installer
) = 0;
65 // Subclasses must override DoRun to actually perform the patching operation.
66 // They must call the provided callback when they have completed their
67 // operations. In practice, the provided callback is always for "DoneRunning".
68 virtual void DoRun(const ComponentUnpacker::Callback
& callback
) = 0;
70 // Callback given to subclasses for when they complete their operation.
71 // Validates the output, and posts a task to the patching operation's
73 void DoneRunning(ComponentUnpacker::Error error
, int extended_error
);
75 ComponentUnpacker::Callback callback_
;
76 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
78 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOp
);
81 // A 'copy' operation takes a file currently residing on the disk and moves it
82 // into the unpacking directory: this represents "no change" in the file being
84 class DeltaUpdateOpCopy
: public DeltaUpdateOp
{
89 ~DeltaUpdateOpCopy() override
;
91 // Overrides of DeltaUpdateOp.
92 ComponentUnpacker::Error
DoParseArguments(
93 const base::DictionaryValue
* command_args
,
94 const base::FilePath
& input_dir
,
95 const scoped_refptr
<ComponentInstaller
>& installer
) override
;
97 void DoRun(const ComponentUnpacker::Callback
& callback
) override
;
99 base::FilePath input_abs_path_
;
101 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpCopy
);
104 // A 'create' operation takes a full file that was sent in the delta update
105 // archive and moves it into the unpacking directory: this represents the
106 // addition of a new file, or a file so different that no bandwidth could be
107 // saved by transmitting a differential update.
108 class DeltaUpdateOpCreate
: public DeltaUpdateOp
{
110 DeltaUpdateOpCreate();
113 ~DeltaUpdateOpCreate() override
;
115 // Overrides of DeltaUpdateOp.
116 ComponentUnpacker::Error
DoParseArguments(
117 const base::DictionaryValue
* command_args
,
118 const base::FilePath
& input_dir
,
119 const scoped_refptr
<ComponentInstaller
>& installer
) override
;
121 void DoRun(const ComponentUnpacker::Callback
& callback
) override
;
123 base::FilePath patch_abs_path_
;
125 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpCreate
);
128 // An interface an embedder may fulfill to enable out-of-process patching.
129 class OutOfProcessPatcher
130 : public base::RefCountedThreadSafe
<OutOfProcessPatcher
> {
133 const std::string
& operation
,
134 scoped_refptr
<base::SequencedTaskRunner
> task_runner
,
135 const base::FilePath
& input_abs_path
,
136 const base::FilePath
& patch_abs_path
,
137 const base::FilePath
& output_abs_path
,
138 base::Callback
<void(int result
)> callback
) = 0;
141 friend class base::RefCountedThreadSafe
<OutOfProcessPatcher
>;
143 virtual ~OutOfProcessPatcher() {}
146 // Both 'bsdiff' and 'courgette' operations take an existing file on disk,
147 // and a bsdiff- or Courgette-format patch file provided in the delta update
148 // package, and run bsdiff or Courgette to construct an output file in the
149 // unpacking directory.
150 class DeltaUpdateOpPatch
: public DeltaUpdateOp
{
152 // |out_of_process_patcher| may be NULL.
153 DeltaUpdateOpPatch(const std::string
& operation
,
154 scoped_refptr
<OutOfProcessPatcher
> out_of_process_patcher
);
157 ~DeltaUpdateOpPatch() override
;
159 // Overrides of DeltaUpdateOp.
160 ComponentUnpacker::Error
DoParseArguments(
161 const base::DictionaryValue
* command_args
,
162 const base::FilePath
& input_dir
,
163 const scoped_refptr
<ComponentInstaller
>& installer
) override
;
165 void DoRun(const ComponentUnpacker::Callback
& callback
) override
;
167 // |success_code| is the code that indicates a successful patch.
168 // |result| is the code the patching operation returned.
169 void DonePatching(const ComponentUnpacker::Callback
& callback
, int result
);
171 std::string operation_
;
172 scoped_refptr
<OutOfProcessPatcher
> out_of_process_patcher_
;
173 base::FilePath patch_abs_path_
;
174 base::FilePath input_abs_path_
;
176 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpPatch
);
179 DeltaUpdateOp
* CreateDeltaUpdateOp(
180 const std::string
& operation
,
181 const scoped_refptr
<OutOfProcessPatcher
>& out_of_process_patcher
);
183 } // namespace update_client
185 #endif // COMPONENTS_UPDATE_CLIENT_COMPONENT_PATCHER_OPERATION_H_