Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / components / component_updater / component_patcher.cc
blob9a429c5279a9e405d4ca690af5489a775eba33ba
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"
7 #include <string>
8 #include <vector>
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 {
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 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,
99 input_dir_,
100 unpack_dir_,
101 installer_,
102 base::Bind(&ComponentPatcher::DonePatchingFile,
103 scoped_refptr<ComponentPatcher>(this)),
104 task_runner_);
107 void ComponentPatcher::DonePatchingFile(ComponentUnpacker::Error error,
108 int extended_error) {
109 if (error != ComponentUnpacker::kNone) {
110 DonePatching(error, extended_error);
111 } else {
112 ++next_command_;
113 PatchNextFile();
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));
122 callback_.Reset();
125 } // namespace component_updater