Refactor views app list services to allow more code sharing
[chromium-blink-merge.git] / chrome / browser / component_updater / component_patcher.cc
blob78064300c121716ef05550db72132716a2cfb7dc
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"
7 #include <string>
8 #include <vector>
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 {
22 namespace {
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))
30 return NULL;
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;
39 } // namespace
41 ComponentPatcher::ComponentPatcher(
42 const base::FilePath& input_dir,
43 const base::FilePath& unpack_dir,
44 ComponentInstaller* installer,
45 bool in_process,
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) {
58 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);
68 } else {
69 next_command_ = commands_->begin();
70 PatchNextFile();
74 void ComponentPatcher::PatchNextFile() {
75 if (next_command_ == commands_->end()) {
76 DonePatching(ComponentUnpacker::kNone, 0);
77 return;
79 if (!(*next_command_)->IsType(base::Value::TYPE_DICTIONARY)) {
80 DonePatching(ComponentUnpacker::kDeltaBadCommands, 0);
81 return;
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);
88 return;
90 current_operation_->Run(
91 command_args,
92 input_dir_,
93 unpack_dir_,
94 installer_,
95 in_process_,
96 base::Bind(&ComponentPatcher::DonePatchingFile,
97 scoped_refptr<ComponentPatcher>(this)),
98 task_runner_);
101 void ComponentPatcher::DonePatchingFile(ComponentUnpacker::Error error,
102 int extended_error) {
103 if (error != ComponentUnpacker::kNone) {
104 DonePatching(error, extended_error);
105 } else {
106 ++next_command_;
107 PatchNextFile();
111 void ComponentPatcher::DonePatching(ComponentUnpacker::Error error,
112 int extended_error) {
113 current_operation_ = NULL;
114 task_runner_->PostTask(FROM_HERE, base::Bind(callback_,
115 error,
116 extended_error));
117 callback_.Reset();
120 } // namespace component_updater