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 #include "chrome/browser/component_updater/component_patcher.h"
10 #include "base/basictypes.h"
11 #include "base/file_util.h"
12 #include "base/files/file_path.h"
13 #include "base/json/json_file_value_serializer.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/values.h"
16 #include "chrome/browser/component_updater/component_patcher_operation.h"
17 #include "chrome/browser/component_updater/component_updater_service.h"
18 #include "content/public/browser/browser_thread.h"
20 namespace component_updater
{
24 // Deserialize the commands file (present in delta update packages). The top
25 // level must be a list.
26 base::ListValue
* ReadCommands(const base::FilePath
& unpack_path
) {
27 const base::FilePath commands
=
28 unpack_path
.Append(FILE_PATH_LITERAL("commands.json"));
29 if (!base::PathExists(commands
))
32 JSONFileValueSerializer
serializer(commands
);
33 scoped_ptr
<base::Value
> root(serializer
.Deserialize(NULL
, NULL
));
35 return (root
.get() && root
->IsType(base::Value::TYPE_LIST
)) ?
36 static_cast<base::ListValue
*>(root
.release()) : NULL
;
41 ComponentPatcher::ComponentPatcher(
42 const base::FilePath
& input_dir
,
43 const base::FilePath
& unpack_dir
,
44 ComponentInstaller
* installer
,
46 scoped_refptr
<base::SequencedTaskRunner
> task_runner
)
47 : input_dir_(input_dir
),
48 unpack_dir_(unpack_dir
),
49 installer_(installer
),
50 in_process_(in_process
),
51 task_runner_(task_runner
) {
54 ComponentPatcher::~ComponentPatcher() {
57 void ComponentPatcher::Start(const ComponentUnpacker::Callback
& callback
) {
59 task_runner_
->PostTask(FROM_HERE
,
60 base::Bind(&ComponentPatcher::StartPatching
,
61 scoped_refptr
<ComponentPatcher
>(this)));
64 void ComponentPatcher::StartPatching() {
65 commands_
.reset(ReadCommands(input_dir_
));
66 if (!commands_
.get()) {
67 DonePatching(ComponentUnpacker::kDeltaBadCommands
, 0);
69 next_command_
= commands_
->begin();
74 void ComponentPatcher::PatchNextFile() {
75 if (next_command_
== commands_
->end()) {
76 DonePatching(ComponentUnpacker::kNone
, 0);
79 if (!(*next_command_
)->IsType(base::Value::TYPE_DICTIONARY
)) {
80 DonePatching(ComponentUnpacker::kDeltaBadCommands
, 0);
83 const base::DictionaryValue
* command_args
=
84 static_cast<base::DictionaryValue
*>(*next_command_
);
85 current_operation_
= CreateDeltaUpdateOp(*command_args
);
86 if (!current_operation_
) {
87 DonePatching(ComponentUnpacker::kDeltaUnsupportedCommand
, 0);
90 current_operation_
->Run(
96 base::Bind(&ComponentPatcher::DonePatchingFile
,
97 scoped_refptr
<ComponentPatcher
>(this)),
101 void ComponentPatcher::DonePatchingFile(ComponentUnpacker::Error error
,
102 int extended_error
) {
103 if (error
!= ComponentUnpacker::kNone
) {
104 DonePatching(error
, extended_error
);
111 void ComponentPatcher::DonePatching(ComponentUnpacker::Error error
,
112 int extended_error
) {
113 current_operation_
= NULL
;
114 task_runner_
->PostTask(FROM_HERE
, base::Bind(callback_
,
120 } // namespace component_updater