summaryrefslogtreecommitdiffstats
path: root/chrome/browser/component_updater/component_patcher_win.cc
blob: 9e38c0cf10862ca1440b0a610e7a42765b40f318 (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
// Copyright (c) 2013 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 "chrome/browser/component_updater/component_patcher_win.h"

#include <string>

#include "base/base_paths.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/strings/string_util.h"
#include "base/win/scoped_handle.h"
#include "chrome/installer/util/util_constants.h"

namespace component_updater {

namespace {

std::string PatchTypeToCommandLineSwitch(
    ComponentPatcher::PatchType patch_type) {
  if (patch_type == ComponentPatcher::kPatchTypeCourgette)
    return std::string(installer::kCourgette);
  else if (patch_type == ComponentPatcher::kPatchTypeBsdiff)
    return std::string(installer::kBsdiff);
  else
    return std::string();
}

// Finds the path to the setup.exe. First, it looks for the program in the
// "installer" directory. If the program is not found there, it tries to find it
// in the directory where chrome.dll lives. Returns the path to the setup.exe,
// if the path exists, otherwise it returns an an empty path.
base::FilePath FindSetupProgram() {
  base::FilePath exe_dir;
  if (!PathService::Get(base::DIR_MODULE, &exe_dir))
    return base::FilePath();

  const std::string installer_dir(WideToASCII(installer::kInstallerDir));
  const std::string setup_exe(WideToASCII(installer::kSetupExe));

  base::FilePath setup_path = exe_dir;
  setup_path = setup_path.AppendASCII(installer_dir);
  setup_path = setup_path.AppendASCII(setup_exe);
  if (base::PathExists(setup_path))
    return setup_path;

  setup_path = exe_dir;
  setup_path = setup_path.AppendASCII(setup_exe);
  if (base::PathExists(setup_path))
    return setup_path;

  return base::FilePath();
}

}  // namespace

// Applies the patch to the input file. Returns kNone if the patch was
// successfully applied, kDeltaOperationFailure if the patch operation
// encountered errors, and kDeltaPatchProcessFailure if there was an error
// when running the patch code out of process. In the error case, detailed error
// information could be returned in the error parameter.
ComponentUnpacker::Error ComponentPatcherWin::Patch(
    PatchType patch_type,
    const base::FilePath& input_file,
    const base::FilePath& patch_file,
    const base::FilePath& output_file,
    int* error) {
  *error = 0;

  const base::FilePath exe_path = FindSetupProgram();
  if (exe_path.empty())
    return ComponentUnpacker::kDeltaPatchProcessFailure;

  const std::string patch_type_str(PatchTypeToCommandLineSwitch(patch_type));

  CommandLine cl(CommandLine::NO_PROGRAM);
  cl.AppendSwitchASCII(installer::switches::kPatch, patch_type_str.c_str());
  cl.AppendSwitchPath(installer::switches::kInputFile, input_file);
  cl.AppendSwitchPath(installer::switches::kPatchFile, patch_file);
  cl.AppendSwitchPath(installer::switches::kOutputFile, output_file);

  // Create the child process in a job object. The job object prevents leaving
  // child processes around when the parent process exits, either gracefully or
  // accidentally.
  base::win::ScopedHandle job(CreateJobObject(NULL, NULL));
  if (!job ||
      !base::SetJobObjectLimitFlags(job, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)) {
    *error = GetLastError();
    return ComponentUnpacker::kDeltaPatchProcessFailure;
  }

  base::LaunchOptions launch_options;
  launch_options.wait = true;
  launch_options.job_handle = job;
  launch_options.start_hidden = true;
  CommandLine setup_path(exe_path);
  setup_path.AppendArguments(cl, false);

  // |ph| is closed by WaitForExitCode.
  base::ProcessHandle ph = base::kNullProcessHandle;
  int exit_code = 0;
  if (!base::LaunchProcess(setup_path, launch_options, &ph) ||
      !base::WaitForExitCode(ph, &exit_code)) {
    *error = GetLastError();
    return ComponentUnpacker::kDeltaPatchProcessFailure;
  }

  *error = exit_code;
  return *error ? ComponentUnpacker::kDeltaOperationFailure :
                  ComponentUnpacker::kNone;
}

}  // namespace component_updater