summaryrefslogtreecommitdiffstats
path: root/components/update_client/component_patcher.cc
blob: 29ce7747d9b8a9b15661fb64c985eb0ae74271bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/update_client/component_patcher.h"

#include <string>
#include <vector>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/json/json_file_value_serializer.h"
#include "base/location.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "components/update_client/component_patcher_operation.h"
#include "components/update_client/update_client.h"

namespace update_client {

namespace {

// Deserialize the commands file (present in delta update packages). The top
// level must be a list.
base::ListValue* ReadCommands(const base::FilePath& unpack_path) {
  const base::FilePath commands =
      unpack_path.Append(FILE_PATH_LITERAL("commands.json"));
  if (!base::PathExists(commands))
    return NULL;

  JSONFileValueDeserializer deserializer(commands);
  scoped_ptr<base::Value> root(deserializer.Deserialize(NULL, NULL));

  return (root.get() && root->IsType(base::Value::TYPE_LIST))
             ? static_cast<base::ListValue*>(root.release())
             : NULL;
}

}  // namespace

ComponentPatcher::ComponentPatcher(
    const base::FilePath& input_dir,
    const base::FilePath& unpack_dir,
    scoped_refptr<ComponentInstaller> installer,
    scoped_refptr<OutOfProcessPatcher> out_of_process_patcher,
    scoped_refptr<base::SequencedTaskRunner> task_runner)
    : input_dir_(input_dir),
      unpack_dir_(unpack_dir),
      installer_(installer),
      out_of_process_patcher_(out_of_process_patcher),
      task_runner_(task_runner) {
}

ComponentPatcher::~ComponentPatcher() {
}

void ComponentPatcher::Start(const ComponentUnpacker::Callback& callback) {
  callback_ = callback;
  task_runner_->PostTask(FROM_HERE,
                         base::Bind(&ComponentPatcher::StartPatching,
                                    scoped_refptr<ComponentPatcher>(this)));
}

void ComponentPatcher::StartPatching() {
  commands_.reset(ReadCommands(input_dir_));
  if (!commands_.get()) {
    DonePatching(ComponentUnpacker::kDeltaBadCommands, 0);
  } else {
    next_command_ = commands_->begin();
    PatchNextFile();
  }
}

void ComponentPatcher::PatchNextFile() {
  if (next_command_ == commands_->end()) {
    DonePatching(ComponentUnpacker::kNone, 0);
    return;
  }
  if (!(*next_command_)->IsType(base::Value::TYPE_DICTIONARY)) {
    DonePatching(ComponentUnpacker::kDeltaBadCommands, 0);
    return;
  }
  const base::DictionaryValue* command_args =
      static_cast<base::DictionaryValue*>(*next_command_);

  std::string operation;
  if (command_args->GetString(kOp, &operation)) {
    current_operation_ =
        CreateDeltaUpdateOp(operation, out_of_process_patcher_);
  }

  if (!current_operation_.get()) {
    DonePatching(ComponentUnpacker::kDeltaUnsupportedCommand, 0);
    return;
  }
  current_operation_->Run(command_args, input_dir_, unpack_dir_, installer_,
                          base::Bind(&ComponentPatcher::DonePatchingFile,
                                     scoped_refptr<ComponentPatcher>(this)),
                          task_runner_);
}

void ComponentPatcher::DonePatchingFile(ComponentUnpacker::Error error,
                                        int extended_error) {
  if (error != ComponentUnpacker::kNone) {
    DonePatching(error, extended_error);
  } else {
    ++next_command_;
    PatchNextFile();
  }
}

void ComponentPatcher::DonePatching(ComponentUnpacker::Error error,
                                    int extended_error) {
  current_operation_ = NULL;
  task_runner_->PostTask(FROM_HERE,
                         base::Bind(callback_, error, extended_error));
  callback_.Reset();
}

}  // namespace update_client