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 #include "components/component_updater/component_patcher.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/file_util.h"
13 #include "base/files/file_path.h"
14 #include "base/json/json_file_value_serializer.h"
15 #include "base/location.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/values.h"
18 #include "components/component_updater/component_patcher_operation.h"
19 #include "components/component_updater/component_updater_service.h"
21 namespace component_updater
{
25 // Deserialize the commands file (present in delta update packages). The top
26 // level must be a list.
27 base::ListValue
* ReadCommands(const base::FilePath
& unpack_path
) {
28 const base::FilePath commands
=
29 unpack_path
.Append(FILE_PATH_LITERAL("commands.json"));
30 if (!base::PathExists(commands
))
33 JSONFileValueSerializer
serializer(commands
);
34 scoped_ptr
<base::Value
> root(serializer
.Deserialize(NULL
, NULL
));
36 return (root
.get() && root
->IsType(base::Value::TYPE_LIST
))
37 ? static_cast<base::ListValue
*>(root
.release())
43 ComponentPatcher::ComponentPatcher(
44 const base::FilePath
& input_dir
,
45 const base::FilePath
& unpack_dir
,
46 ComponentInstaller
* installer
,
47 scoped_refptr
<OutOfProcessPatcher
> out_of_process_patcher
,
48 scoped_refptr
<base::SequencedTaskRunner
> task_runner
)
49 : input_dir_(input_dir
),
50 unpack_dir_(unpack_dir
),
51 installer_(installer
),
52 out_of_process_patcher_(out_of_process_patcher
),
53 task_runner_(task_runner
) {
56 ComponentPatcher::~ComponentPatcher() {
59 void ComponentPatcher::Start(const ComponentUnpacker::Callback
& callback
) {
61 task_runner_
->PostTask(FROM_HERE
,
62 base::Bind(&ComponentPatcher::StartPatching
,
63 scoped_refptr
<ComponentPatcher
>(this)));
66 void ComponentPatcher::StartPatching() {
67 commands_
.reset(ReadCommands(input_dir_
));
68 if (!commands_
.get()) {
69 DonePatching(ComponentUnpacker::kDeltaBadCommands
, 0);
71 next_command_
= commands_
->begin();
76 void ComponentPatcher::PatchNextFile() {
77 if (next_command_
== commands_
->end()) {
78 DonePatching(ComponentUnpacker::kNone
, 0);
81 if (!(*next_command_
)->IsType(base::Value::TYPE_DICTIONARY
)) {
82 DonePatching(ComponentUnpacker::kDeltaBadCommands
, 0);
85 const base::DictionaryValue
* command_args
=
86 static_cast<base::DictionaryValue
*>(*next_command_
);
88 std::string operation
;
89 if (command_args
->GetString(kOp
, &operation
)) {
91 CreateDeltaUpdateOp(operation
, out_of_process_patcher_
);
94 if (!current_operation_
.get()) {
95 DonePatching(ComponentUnpacker::kDeltaUnsupportedCommand
, 0);
98 current_operation_
->Run(command_args
,
102 base::Bind(&ComponentPatcher::DonePatchingFile
,
103 scoped_refptr
<ComponentPatcher
>(this)),
107 void ComponentPatcher::DonePatchingFile(ComponentUnpacker::Error error
,
108 int extended_error
) {
109 if (error
!= ComponentUnpacker::kNone
) {
110 DonePatching(error
, extended_error
);
117 void ComponentPatcher::DonePatching(ComponentUnpacker::Error error
,
118 int extended_error
) {
119 current_operation_
= NULL
;
120 task_runner_
->PostTask(FROM_HERE
,
121 base::Bind(callback_
, error
, extended_error
));
125 } // namespace component_updater