OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/component_updater/component_patcher_win.h" | |
6 | |
7 #include <string> | |
8 | |
9 #include "base/base_paths.h" | |
10 #include "base/command_line.h" | |
11 #include "base/file_util.h" | |
12 #include "base/path_service.h" | |
13 #include "base/process/kill.h" | |
14 #include "base/process/launch.h" | |
15 #include "base/strings/string_util.h" | |
16 #include "base/strings/utf_string_conversions.h" | |
17 #include "base/win/scoped_handle.h" | |
18 #include "chrome/installer/util/util_constants.h" | |
19 | |
20 namespace component_updater { | |
21 | |
22 namespace { | |
23 | |
24 std::string PatchTypeToCommandLineSwitch( | |
25 ComponentPatcher::PatchType patch_type) { | |
26 if (patch_type == ComponentPatcher::kPatchTypeCourgette) | |
27 return std::string(installer::kCourgette); | |
28 else if (patch_type == ComponentPatcher::kPatchTypeBsdiff) | |
29 return std::string(installer::kBsdiff); | |
30 else | |
31 return std::string(); | |
32 } | |
33 | |
34 // Finds the path to the setup.exe. First, it looks for the program in the | |
35 // "installer" directory. If the program is not found there, it tries to find it | |
36 // in the directory where chrome.dll lives. Returns the path to the setup.exe, | |
37 // if the path exists, otherwise it returns an an empty path. | |
38 base::FilePath FindSetupProgram() { | |
39 base::FilePath exe_dir; | |
40 if (!PathService::Get(base::DIR_MODULE, &exe_dir)) | |
41 return base::FilePath(); | |
42 | |
43 const std::string installer_dir(base::UTF16ToASCII(installer::kInstallerDir)); | |
44 const std::string setup_exe(base::UTF16ToASCII(installer::kSetupExe)); | |
45 | |
46 base::FilePath setup_path = exe_dir; | |
47 setup_path = setup_path.AppendASCII(installer_dir); | |
48 setup_path = setup_path.AppendASCII(setup_exe); | |
49 if (base::PathExists(setup_path)) | |
50 return setup_path; | |
51 | |
52 setup_path = exe_dir; | |
53 setup_path = setup_path.AppendASCII(setup_exe); | |
54 if (base::PathExists(setup_path)) | |
55 return setup_path; | |
56 | |
57 return base::FilePath(); | |
58 } | |
59 | |
60 } // namespace | |
61 | |
62 // Applies the patch to the input file. Returns kNone if the patch was | |
63 // successfully applied, kDeltaOperationFailure if the patch operation | |
64 // encountered errors, and kDeltaPatchProcessFailure if there was an error | |
65 // when running the patch code out of process. In the error case, detailed error | |
66 // information could be returned in the error parameter. | |
67 ComponentUnpacker::Error ComponentPatcherWin::Patch( | |
68 PatchType patch_type, | |
69 const base::FilePath& input_file, | |
70 const base::FilePath& patch_file, | |
71 const base::FilePath& output_file, | |
72 int* error) { | |
73 *error = 0; | |
74 | |
75 const base::FilePath exe_path = FindSetupProgram(); | |
76 if (exe_path.empty()) | |
77 return ComponentUnpacker::kDeltaPatchProcessFailure; | |
78 | |
79 const std::string patch_type_str(PatchTypeToCommandLineSwitch(patch_type)); | |
80 | |
81 CommandLine cl(CommandLine::NO_PROGRAM); | |
82 cl.AppendSwitchASCII(installer::switches::kPatch, patch_type_str.c_str()); | |
83 cl.AppendSwitchPath(installer::switches::kInputFile, input_file); | |
84 cl.AppendSwitchPath(installer::switches::kPatchFile, patch_file); | |
85 cl.AppendSwitchPath(installer::switches::kOutputFile, output_file); | |
86 | |
87 // Create the child process in a job object. The job object prevents leaving | |
88 // child processes around when the parent process exits, either gracefully or | |
89 // accidentally. | |
90 base::win::ScopedHandle job(CreateJobObject(NULL, NULL)); | |
91 if (!job || | |
92 !base::SetJobObjectLimitFlags(job, JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE)) { | |
93 *error = GetLastError(); | |
94 return ComponentUnpacker::kDeltaPatchProcessFailure; | |
95 } | |
96 | |
97 base::LaunchOptions launch_options; | |
98 launch_options.wait = true; | |
99 launch_options.job_handle = job; | |
100 launch_options.start_hidden = true; | |
101 CommandLine setup_path(exe_path); | |
102 setup_path.AppendArguments(cl, false); | |
103 | |
104 // |ph| is closed by WaitForExitCode. | |
105 base::ProcessHandle ph = base::kNullProcessHandle; | |
106 int exit_code = 0; | |
107 if (!base::LaunchProcess(setup_path, launch_options, &ph) || | |
108 !base::WaitForExitCode(ph, &exit_code)) { | |
109 *error = GetLastError(); | |
110 return ComponentUnpacker::kDeltaPatchProcessFailure; | |
111 } | |
112 | |
113 *error = exit_code; | |
114 return *error ? ComponentUnpacker::kDeltaOperationFailure : | |
115 ComponentUnpacker::kNone; | |
116 } | |
117 | |
118 } // namespace component_updater | |
OLD | NEW |