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_COMPONENT_UPDATER_COMPONENT_PATCHER_OPERATION_H_
6 #define COMPONENTS_COMPONENT_UPDATER_COMPONENT_PATCHER_OPERATION_H_
10 #include "base/basictypes.h"
11 #include "base/callback.h"
12 #include "base/compiler_specific.h"
13 #include "base/files/file_path.h"
14 #include "base/memory/ref_counted.h"
15 #include "components/component_updater/component_unpacker.h"
18 class DictionaryValue
;
21 namespace component_updater
{
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 ComponentInstaller
* installer
,
41 const ComponentUnpacker::Callback
& callback
,
42 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 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 virtual ~DeltaUpdateOpCopy();
91 // Overrides of DeltaUpdateOp.
92 virtual ComponentUnpacker::Error
DoParseArguments(
93 const base::DictionaryValue
* command_args
,
94 const base::FilePath
& input_dir
,
95 ComponentInstaller
* installer
) OVERRIDE
;
97 virtual 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 virtual ~DeltaUpdateOpCreate();
115 // Overrides of DeltaUpdateOp.
116 virtual ComponentUnpacker::Error
DoParseArguments(
117 const base::DictionaryValue
* command_args
,
118 const base::FilePath
& input_dir
,
119 ComponentInstaller
* installer
) OVERRIDE
;
121 virtual 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
> {
132 virtual void Patch(const std::string
& operation
,
133 scoped_refptr
<base::SequencedTaskRunner
> task_runner
,
134 base::FilePath
& input_abs_path
,
135 base::FilePath
& patch_abs_path
,
136 base::FilePath
& output_abs_path
,
137 base::Callback
<void(int result
)> callback
) = 0;
140 friend class base::RefCountedThreadSafe
<OutOfProcessPatcher
>;
142 virtual ~OutOfProcessPatcher() {}
145 // Both 'bsdiff' and 'courgette' operations take an existing file on disk,
146 // and a bsdiff- or Courgette-format patch file provided in the delta update
147 // package, and run bsdiff or Courgette to construct an output file in the
148 // unpacking directory.
149 class DeltaUpdateOpPatch
: public DeltaUpdateOp
{
151 // |out_of_process_patcher| may be NULL.
152 DeltaUpdateOpPatch(const std::string
& operation
,
153 scoped_refptr
<OutOfProcessPatcher
> out_of_process_patcher
);
156 virtual ~DeltaUpdateOpPatch();
158 // Overrides of DeltaUpdateOp.
159 virtual ComponentUnpacker::Error
DoParseArguments(
160 const base::DictionaryValue
* command_args
,
161 const base::FilePath
& input_dir
,
162 ComponentInstaller
* installer
) OVERRIDE
;
164 virtual void DoRun(const ComponentUnpacker::Callback
& callback
) OVERRIDE
;
166 // |success_code| is the code that indicates a successful patch.
167 // |result| is the code the patching operation returned.
168 void DonePatching(const ComponentUnpacker::Callback
& callback
, int result
);
170 std::string operation_
;
171 scoped_refptr
<OutOfProcessPatcher
> out_of_process_patcher_
;
172 base::FilePath patch_abs_path_
;
173 base::FilePath input_abs_path_
;
175 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpPatch
);
178 DeltaUpdateOp
* CreateDeltaUpdateOp(
179 const std::string
& operation
,
180 scoped_refptr
<OutOfProcessPatcher
> out_of_process_patcher
);
182 } // namespace component_updater
184 #endif // COMPONENTS_COMPONENT_UPDATER_COMPONENT_PATCHER_OPERATION_H_