Update app list background color.
[chromium-blink-merge.git] / components / update_client / component_patcher.cc
blob3077bc4f40b5a24ebe1f8a7ca71a87d285862c79
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/update_client/component_patcher.h"
7 #include <string>
8 #include <vector>
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.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/update_client/component_patcher_operation.h"
19 #include "components/update_client/update_client.h"
21 namespace update_client {
23 namespace {
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))
31 return NULL;
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())
38 : NULL;
41 } // namespace
43 ComponentPatcher::ComponentPatcher(
44 const base::FilePath& input_dir,
45 const base::FilePath& unpack_dir,
46 scoped_refptr<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) {
60 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);
70 } else {
71 next_command_ = commands_->begin();
72 PatchNextFile();
76 void ComponentPatcher::PatchNextFile() {
77 if (next_command_ == commands_->end()) {
78 DonePatching(ComponentUnpacker::kNone, 0);
79 return;
81 if (!(*next_command_)->IsType(base::Value::TYPE_DICTIONARY)) {
82 DonePatching(ComponentUnpacker::kDeltaBadCommands, 0);
83 return;
85 const base::DictionaryValue* command_args =
86 static_cast<base::DictionaryValue*>(*next_command_);
88 std::string operation;
89 if (command_args->GetString(kOp, &operation)) {
90 current_operation_ =
91 CreateDeltaUpdateOp(operation, out_of_process_patcher_);
94 if (!current_operation_.get()) {
95 DonePatching(ComponentUnpacker::kDeltaUnsupportedCommand, 0);
96 return;
98 current_operation_->Run(command_args, input_dir_, unpack_dir_, installer_,
99 base::Bind(&ComponentPatcher::DonePatchingFile,
100 scoped_refptr<ComponentPatcher>(this)),
101 task_runner_);
104 void ComponentPatcher::DonePatchingFile(ComponentUnpacker::Error error,
105 int extended_error) {
106 if (error != ComponentUnpacker::kNone) {
107 DonePatching(error, extended_error);
108 } else {
109 ++next_command_;
110 PatchNextFile();
114 void ComponentPatcher::DonePatching(ComponentUnpacker::Error error,
115 int extended_error) {
116 current_operation_ = NULL;
117 task_runner_->PostTask(FROM_HERE,
118 base::Bind(callback_, error, extended_error));
119 callback_.Reset();
122 } // namespace update_client