Chromium Code Reviews| Index: chrome/browser/component_updater/component_patcher.cc |
| diff --git a/chrome/browser/component_updater/component_patcher.cc b/chrome/browser/component_updater/component_patcher.cc |
| index 59f283aaf175801e3bf9cbe9729f297135e6a5a6..0ac0656b781bddc34a8c34fdac68be419a4cd92c 100644 |
| --- a/chrome/browser/component_updater/component_patcher.cc |
| +++ b/chrome/browser/component_updater/component_patcher.cc |
| @@ -7,11 +7,15 @@ |
| #include <string> |
| #include <vector> |
| +#include "base/basictypes.h" |
| #include "base/file_util.h" |
| +#include "base/files/file_path.h" |
| #include "base/json/json_file_value_serializer.h" |
| +#include "base/memory/weak_ptr.h" |
| #include "base/values.h" |
| #include "chrome/browser/component_updater/component_patcher_operation.h" |
| #include "chrome/browser/component_updater/component_updater_service.h" |
| +#include "content/public/browser/browser_thread.h" |
| namespace component_updater { |
| @@ -34,58 +38,85 @@ base::ListValue* ReadCommands(const base::FilePath& unpack_path) { |
| } // namespace |
| -// The patching support is not cross-platform at the moment. |
| -ComponentPatcherCrossPlatform::ComponentPatcherCrossPlatform() {} |
| +ComponentPatcher::ComponentPatcher( |
| + const base::FilePath& input_dir, |
| + const base::FilePath& unpack_dir, |
| + ComponentInstaller* installer, |
| + bool in_process, |
| + scoped_refptr<base::SequencedTaskRunner> task_runner) |
| + : input_dir_(input_dir), |
| + unpack_dir_(unpack_dir), |
| + installer_(installer), |
| + in_process_(in_process), |
| + ptr_factory_(this), |
| + task_runner_(task_runner) { |
| +} |
| -ComponentUnpacker::Error ComponentPatcherCrossPlatform::Patch( |
| - PatchType patch_type, |
| - const base::FilePath& input_file, |
| - const base::FilePath& patch_file, |
| - const base::FilePath& output_file, |
| - int* error) { |
| - return ComponentUnpacker::kDeltaUnsupportedCommand; |
| +ComponentPatcher::~ComponentPatcher() { |
| } |
| +void ComponentPatcher::Start( |
| + const base::Callback<void(ComponentUnpacker::Error, int)>& callback) { |
| + callback_ = callback; |
| + commands_.reset(ReadCommands(input_dir_)); |
| + if (!commands_.get()) { |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(callback_, ComponentUnpacker::kDeltaBadCommands, 0)); |
| + } else { |
| + next_command_ = commands_->begin(); |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&ComponentPatcher::PatchNextFile, GetWeakPtr())); |
| + } |
| +} |
| -// Takes the contents of a differential component update in input_dir |
| -// and produces the contents of a full component update in unpack_dir |
| -// using input_abs_path_ files that the installer knows about. |
| -void DifferentialUpdatePatch( |
| - const base::FilePath& input_dir, |
| - const base::FilePath& unpack_dir, |
| - ComponentPatcher* patcher, |
| - ComponentInstaller* installer, |
| - base::Callback<void(ComponentUnpacker::Error, int)> callback) { |
| - int error = 0; |
| - scoped_ptr<base::ListValue> commands(ReadCommands(input_dir)); |
| - if (!commands.get()) { |
| - callback.Run(ComponentUnpacker::kDeltaBadCommands, error); |
| +void ComponentPatcher::PatchNextFile() { |
| + if (next_command_ == commands_->end()) { |
| + callback_.Run(ComponentUnpacker::kNone, 0); |
|
Sorin Jianu
2014/02/03 20:57:57
Why is this callback called directly and the ones
waffles
2014/02/07 01:00:59
Done.
|
| + return; |
| + } |
| + if (!(*next_command_)->IsType(base::Value::TYPE_DICTIONARY)) { |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(callback_, ComponentUnpacker::kDeltaBadCommands, 0)); |
| return; |
| } |
| + base::DictionaryValue* command_args = |
| + static_cast<base::DictionaryValue*>(*next_command_); |
| + current_operation_.reset(CreateDeltaUpdateOp(command_args)); |
| + if (!current_operation_) { |
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(callback_, ComponentUnpacker::kDeltaUnsupportedCommand, 0)); |
| + return; |
| + } |
| + current_operation_->Run( |
| + command_args, |
| + input_dir_, |
| + unpack_dir_, |
| + installer_, |
| + in_process_, |
| + base::Bind(&ComponentPatcher::DonePatchingFile, GetWeakPtr()), |
| + task_runner_); |
| +} |
| - for (base::ValueVector::const_iterator command = commands->begin(), |
| - end = commands->end(); command != end; command++) { |
| - if (!(*command)->IsType(base::Value::TYPE_DICTIONARY)) { |
| - callback.Run(ComponentUnpacker::kDeltaBadCommands, error); |
| - return; |
| - } |
| - base::DictionaryValue* command_args = |
| - static_cast<base::DictionaryValue*>(*command); |
| - scoped_ptr<DeltaUpdateOp> operation(CreateDeltaUpdateOp(command_args)); |
| - if (!operation) { |
| - callback.Run(ComponentUnpacker::kDeltaUnsupportedCommand, error); |
| - return; |
| - } |
| - |
| - ComponentUnpacker::Error result = operation->Run( |
| - command_args, input_dir, unpack_dir, patcher, installer, &error); |
| - if (result != ComponentUnpacker::kNone) { |
| - callback.Run(result, error); |
| - return; |
| - } |
| +void ComponentPatcher::DonePatchingFile(ComponentUnpacker::Error error, |
| + int extended_error) { |
| + if (error != ComponentUnpacker::kNone) { |
| + callback_.Run(error, extended_error); |
| + } else if (next_command_ == commands_->end()) { |
|
Sorin Jianu
2014/02/03 20:57:57
This is the if statement that could be eliminated,
waffles
2014/02/07 01:00:59
Done.
|
| + callback_.Run(ComponentUnpacker::kNone, 0); |
| + } else { |
| + next_command_++; |
|
Sorin Jianu
2014/02/03 20:57:57
could use preincrement.
waffles
2014/02/07 01:00:59
Done.
|
| + task_runner_->PostTask( |
| + FROM_HERE, |
| + base::Bind(&ComponentPatcher::PatchNextFile, GetWeakPtr())); |
| } |
| +} |
| - callback.Run(ComponentUnpacker::kNone, error); |
| +base::WeakPtr<ComponentPatcher> ComponentPatcher::GetWeakPtr() { |
| + return ptr_factory_.GetWeakPtr(); |
| } |
| } // namespace component_updater |