1 // Copyright 2013 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 CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_PATCHER_OPERATION_H_
6 #define CHROME_BROWSER_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 "chrome/browser/component_updater/component_patcher.h"
16 #include "chrome/browser/component_updater/component_unpacker.h"
17 #include "content/public/browser/utility_process_host_client.h"
20 class DictionaryValue
;
23 namespace component_updater
{
25 class ComponentInstaller
;
27 class DeltaUpdateOp
: public base::RefCountedThreadSafe
<DeltaUpdateOp
> {
31 // Parses, runs, and verifies the operation. Calls |callback| with the
32 // result of the operation. The callback is called using |task_runner|.
33 void Run(const base::DictionaryValue
* command_args
,
34 const base::FilePath
& input_dir
,
35 const base::FilePath
& unpack_dir
,
36 ComponentInstaller
* installer
,
38 const ComponentUnpacker::Callback
& callback
,
39 scoped_refptr
<base::SequencedTaskRunner
> task_runner
);
42 virtual ~DeltaUpdateOp();
46 scoped_refptr
<base::SequencedTaskRunner
> GetTaskRunner();
48 std::string output_sha256_
;
49 base::FilePath output_abs_path_
;
52 friend class base::RefCountedThreadSafe
<DeltaUpdateOp
>;
54 ComponentUnpacker::Error
CheckHash();
56 // Subclasses must override DoParseArguments to parse operation-specific
57 // arguments. DoParseArguments returns DELTA_OK on success; any other code
58 // represents failure.
59 virtual ComponentUnpacker::Error
DoParseArguments(
60 const base::DictionaryValue
* command_args
,
61 const base::FilePath
& input_dir
,
62 ComponentInstaller
* installer
) = 0;
64 // Subclasses must override DoRun to actually perform the patching operation.
65 // They must call the provided callback when they have completed their
66 // operations. In practice, the provided callback is always for "DoneRunning".
67 virtual void DoRun(const ComponentUnpacker::Callback
& callback
) = 0;
69 // Callback given to subclasses for when they complete their operation.
70 // Validates the output, and posts a task to the patching operation's
72 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 class DeltaUpdateOpPatchStrategy
{
130 virtual ~DeltaUpdateOpPatchStrategy();
132 // Returns an integer to add to error codes to disambiguate their source.
133 virtual int GetErrorOffset() const = 0;
135 // Returns the "error code" that is expected in the successful install case.
136 virtual int GetSuccessCode() const = 0;
138 // Returns an IPC message that will start patching if it is sent to a
139 // UtilityProcessClient.
140 virtual scoped_ptr
<IPC::Message
> GetPatchMessage(
141 base::FilePath input_abs_path
,
142 base::FilePath patch_abs_path
,
143 base::FilePath output_abs_path
) = 0;
145 // Does the actual patching operation, and returns an error code.
146 virtual int Patch(base::FilePath input_abs_path
,
147 base::FilePath patch_abs_path
,
148 base::FilePath output_abs_path
) = 0;
151 class DeltaUpdateOpPatch
;
153 class DeltaUpdateOpPatchHost
: public content::UtilityProcessHostClient
{
155 DeltaUpdateOpPatchHost(scoped_refptr
<DeltaUpdateOpPatch
> patcher
,
156 scoped_refptr
<base::SequencedTaskRunner
> task_runner
);
158 void StartProcess(scoped_ptr
<IPC::Message
> message
);
161 virtual ~DeltaUpdateOpPatchHost();
163 void OnPatchSucceeded();
165 void OnPatchFailed(int error_code
);
167 // Overrides of content::UtilityProcessHostClient.
168 virtual bool OnMessageReceived(const IPC::Message
& message
) OVERRIDE
;
170 virtual void OnProcessCrashed(int exit_code
) OVERRIDE
;
172 scoped_refptr
<DeltaUpdateOpPatch
> patcher_
;
173 scoped_refptr
<base::SequencedTaskRunner
> task_runner_
;
176 // Both 'bsdiff' and 'courgette' operations take an existing file on disk,
177 // and a bsdiff- or Courgette-format patch file provided in the delta update
178 // package, and run bsdiff or Courgette to construct an output file in the
179 // unpacking directory.
180 class DeltaUpdateOpPatch
: public DeltaUpdateOp
{
182 explicit DeltaUpdateOpPatch(scoped_ptr
<DeltaUpdateOpPatchStrategy
> strategy
);
184 void DonePatching(ComponentUnpacker::Error error
, int error_code
);
187 virtual ~DeltaUpdateOpPatch();
189 // Overrides of DeltaUpdateOp.
190 virtual ComponentUnpacker::Error
DoParseArguments(
191 const base::DictionaryValue
* command_args
,
192 const base::FilePath
& input_dir
,
193 ComponentInstaller
* installer
) OVERRIDE
;
195 virtual void DoRun(const ComponentUnpacker::Callback
& callback
) OVERRIDE
;
197 ComponentUnpacker::Callback callback_
;
198 base::FilePath patch_abs_path_
;
199 base::FilePath input_abs_path_
;
200 scoped_ptr
<DeltaUpdateOpPatchStrategy
> strategy_
;
201 scoped_refptr
<DeltaUpdateOpPatchHost
> host_
;
203 DISALLOW_COPY_AND_ASSIGN(DeltaUpdateOpPatch
);
206 // Factory functions to create DeltaUpdateOp instances.
207 DeltaUpdateOp
* CreateDeltaUpdateOp(const base::DictionaryValue
& command
);
209 DeltaUpdateOp
* CreateDeltaUpdateOp(const std::string
& operation
);
211 } // namespace component_updater
213 #endif // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_PATCHER_OPERATION_H_