Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/component_updater/component_patcher.h" | 5 #include "chrome/browser/component_updater/component_patcher.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | |
| 10 #include "base/file_util.h" | 11 #include "base/file_util.h" |
| 12 #include "base/files/file_path.h" | |
| 11 #include "base/json/json_file_value_serializer.h" | 13 #include "base/json/json_file_value_serializer.h" |
| 14 #include "base/memory/weak_ptr.h" | |
| 12 #include "base/values.h" | 15 #include "base/values.h" |
| 13 #include "chrome/browser/component_updater/component_patcher_operation.h" | 16 #include "chrome/browser/component_updater/component_patcher_operation.h" |
| 14 #include "chrome/browser/component_updater/component_updater_service.h" | 17 #include "chrome/browser/component_updater/component_updater_service.h" |
| 18 #include "content/public/browser/browser_thread.h" | |
| 15 | 19 |
| 16 namespace component_updater { | 20 namespace component_updater { |
| 17 | 21 |
| 18 namespace { | 22 namespace { |
| 19 | 23 |
| 20 // Deserialize the commands file (present in delta update packages). The top | 24 // Deserialize the commands file (present in delta update packages). The top |
| 21 // level must be a list. | 25 // level must be a list. |
| 22 base::ListValue* ReadCommands(const base::FilePath& unpack_path) { | 26 base::ListValue* ReadCommands(const base::FilePath& unpack_path) { |
| 23 const base::FilePath commands = | 27 const base::FilePath commands = |
| 24 unpack_path.Append(FILE_PATH_LITERAL("commands.json")); | 28 unpack_path.Append(FILE_PATH_LITERAL("commands.json")); |
| 25 if (!base::PathExists(commands)) | 29 if (!base::PathExists(commands)) |
| 26 return NULL; | 30 return NULL; |
| 27 | 31 |
| 28 JSONFileValueSerializer serializer(commands); | 32 JSONFileValueSerializer serializer(commands); |
| 29 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); | 33 scoped_ptr<base::Value> root(serializer.Deserialize(NULL, NULL)); |
| 30 | 34 |
| 31 return (root.get() && root->IsType(base::Value::TYPE_LIST)) ? | 35 return (root.get() && root->IsType(base::Value::TYPE_LIST)) ? |
| 32 static_cast<base::ListValue*>(root.release()) : NULL; | 36 static_cast<base::ListValue*>(root.release()) : NULL; |
| 33 } | 37 } |
| 34 | 38 |
| 35 } // namespace | 39 } // namespace |
| 36 | 40 |
| 37 // The patching support is not cross-platform at the moment. | 41 ComponentPatcher::ComponentPatcher( |
| 38 ComponentPatcherCrossPlatform::ComponentPatcherCrossPlatform() {} | 42 const base::FilePath& input_dir, |
| 39 | 43 const base::FilePath& unpack_dir, |
| 40 ComponentUnpacker::Error ComponentPatcherCrossPlatform::Patch( | 44 ComponentInstaller* installer, |
| 41 PatchType patch_type, | 45 bool in_process, |
| 42 const base::FilePath& input_file, | 46 scoped_refptr<base::SequencedTaskRunner> task_runner) |
| 43 const base::FilePath& patch_file, | 47 : input_dir_(input_dir), |
| 44 const base::FilePath& output_file, | 48 unpack_dir_(unpack_dir), |
| 45 int* error) { | 49 installer_(installer), |
| 46 return ComponentUnpacker::kDeltaUnsupportedCommand; | 50 in_process_(in_process), |
| 51 task_runner_(task_runner) { | |
| 47 } | 52 } |
| 48 | 53 |
| 54 ComponentPatcher::~ComponentPatcher() { | |
| 55 } | |
| 49 | 56 |
| 50 // Takes the contents of a differential component update in input_dir | 57 void ComponentPatcher::Start( |
| 51 // and produces the contents of a full component update in unpack_dir | 58 const base::Callback<void(ComponentUnpacker::Error, int)>& callback) { |
| 52 // using input_abs_path_ files that the installer knows about. | 59 callback_ = callback; |
| 53 void DifferentialUpdatePatch( | 60 task_runner_->PostTask(FROM_HERE, |
| 54 const base::FilePath& input_dir, | 61 base::Bind(&ComponentPatcher::StartPatching, |
| 55 const base::FilePath& unpack_dir, | 62 scoped_refptr<ComponentPatcher>(this))); |
| 56 ComponentPatcher* patcher, | 63 } |
| 57 ComponentInstaller* installer, | 64 |
| 58 base::Callback<void(ComponentUnpacker::Error, int)> callback) { | 65 void ComponentPatcher::StartPatching() { |
| 59 int error = 0; | 66 commands_.reset(ReadCommands(input_dir_)); |
| 60 scoped_ptr<base::ListValue> commands(ReadCommands(input_dir)); | 67 if (!commands_.get()) { |
| 61 if (!commands.get()) { | 68 DonePatching(ComponentUnpacker::kDeltaBadCommands, 0); |
| 62 callback.Run(ComponentUnpacker::kDeltaBadCommands, error); | 69 } else { |
| 70 next_command_ = commands_->begin(); | |
| 71 PatchNextFile(); | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 void ComponentPatcher::PatchNextFile() { | |
| 76 if (next_command_ == commands_->end()) { | |
| 77 DonePatching(ComponentUnpacker::kNone, 0); | |
| 63 return; | 78 return; |
| 64 } | 79 } |
| 80 if (!(*next_command_)->IsType(base::Value::TYPE_DICTIONARY)) { | |
| 81 DonePatching(ComponentUnpacker::kDeltaBadCommands, 0); | |
| 82 return; | |
| 83 } | |
| 84 base::DictionaryValue* command_args = | |
|
Sorin Jianu
2014/02/27 20:53:57
can this be a pointer to const type?
waffles
2014/02/28 00:52:43
Done.
| |
| 85 static_cast<base::DictionaryValue*>(*next_command_); | |
| 86 current_operation_ = CreateDeltaUpdateOp(*command_args); | |
| 87 if (!current_operation_) { | |
| 88 DonePatching(ComponentUnpacker::kDeltaUnsupportedCommand, 0); | |
| 89 return; | |
| 90 } | |
| 91 current_operation_->Run( | |
| 92 command_args, | |
| 93 input_dir_, | |
| 94 unpack_dir_, | |
| 95 installer_, | |
| 96 in_process_, | |
| 97 base::Bind(&ComponentPatcher::DonePatchingFile, | |
| 98 scoped_refptr<ComponentPatcher>(this)), | |
| 99 task_runner_); | |
| 100 } | |
| 65 | 101 |
| 66 for (base::ValueVector::const_iterator command = commands->begin(), | 102 void ComponentPatcher::DonePatchingFile(ComponentUnpacker::Error error, |
| 67 end = commands->end(); command != end; command++) { | 103 int extended_error) { |
| 68 if (!(*command)->IsType(base::Value::TYPE_DICTIONARY)) { | 104 if (error != ComponentUnpacker::kNone) { |
|
Sorin Jianu
2014/02/27 20:53:57
This is definitely a matter of personal taste. I p
waffles
2014/02/28 00:52:43
That's interesting. Elsewhere, we've used the patt
Sorin Jianu
2014/02/28 01:53:53
It's ok either way, and a matter of taste.
One of
| |
| 69 callback.Run(ComponentUnpacker::kDeltaBadCommands, error); | 105 DonePatching(error, extended_error); |
| 70 return; | 106 } else { |
| 71 } | 107 ++next_command_; |
| 72 base::DictionaryValue* command_args = | 108 PatchNextFile(); |
| 73 static_cast<base::DictionaryValue*>(*command); | 109 } |
| 74 scoped_ptr<DeltaUpdateOp> operation(CreateDeltaUpdateOp(command_args)); | 110 } |
| 75 if (!operation) { | |
| 76 callback.Run(ComponentUnpacker::kDeltaUnsupportedCommand, error); | |
| 77 return; | |
| 78 } | |
| 79 | 111 |
| 80 ComponentUnpacker::Error result = operation->Run( | 112 void ComponentPatcher::DonePatching(ComponentUnpacker::Error error, |
| 81 command_args, input_dir, unpack_dir, patcher, installer, &error); | 113 int extended_error) { |
| 82 if (result != ComponentUnpacker::kNone) { | 114 callback_.Run(error, extended_error); |
| 83 callback.Run(result, error); | |
| 84 return; | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 callback.Run(ComponentUnpacker::kNone, error); | |
| 89 } | 115 } |
| 90 | 116 |
| 91 } // namespace component_updater | 117 } // namespace component_updater |
| OLD | NEW |