OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 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. |
| 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_util.h" |
| 14 #include "base/string_util.h" |
| 15 #include "chrome/installer/util/util_constants.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 std::string PatchTypeToCommandLineSwitch( |
| 20 ComponentPatcher::PatchType patch_type) { |
| 21 if (patch_type == ComponentPatcher::kPatchTypeCourgette) |
| 22 return std::string(installer::kCourgette); |
| 23 else if (patch_type == ComponentPatcher::kPatchTypeBsdiff) |
| 24 return std::string(installer::kBsdiff); |
| 25 else |
| 26 return std::string(); |
| 27 } |
| 28 |
| 29 // Finds the path to the setup.exe. First, it looks for the program in the |
| 30 // "installer" directory. If the program is not found there, it tries to find it |
| 31 // in the directory where chrome.dll lives. Returns the path to the setup.exe, |
| 32 // if the path exists, otherwise it returns an an empty path. |
| 33 base::FilePath FindSetupProgram() { |
| 34 base::FilePath exe_dir; |
| 35 if (!PathService::Get(base::DIR_MODULE, &exe_dir)) |
| 36 return base::FilePath(); |
| 37 |
| 38 const std::string installer_dir(WideToASCII(installer::kInstallerDir)); |
| 39 const std::string setup_exe(WideToASCII(installer::kSetupExe)); |
| 40 |
| 41 base::FilePath setup_path = exe_dir; |
| 42 setup_path = setup_path.AppendASCII(installer_dir); |
| 43 setup_path = setup_path.AppendASCII(setup_exe); |
| 44 if (file_util::PathExists(setup_path)) |
| 45 return setup_path; |
| 46 |
| 47 setup_path = exe_dir; |
| 48 setup_path = setup_path.AppendASCII(setup_exe); |
| 49 if (file_util::PathExists(setup_path)) |
| 50 return setup_path; |
| 51 |
| 52 return base::FilePath(); |
| 53 } |
| 54 |
| 55 } // namespace |
| 56 |
| 57 // Applies the patch to the input file. Returns kNone if the patch was |
| 58 // successfully applied, kDeltaOperationFailure if the patch operation |
| 59 // encountered errors, and kDeltaPatchProcessFailure if there was an error |
| 60 // when running the patch code out of process. In the error case, detailed error |
| 61 // information could be returned in the error parameter. |
| 62 ComponentUnpacker::Error ComponentPatcherWin::Patch( |
| 63 PatchType patch_type, |
| 64 const base::FilePath& input_file, |
| 65 const base::FilePath& patch_file, |
| 66 const base::FilePath& output_file, |
| 67 int* error) { |
| 68 *error = 0; |
| 69 |
| 70 const base::FilePath exe_path = FindSetupProgram(); |
| 71 if (exe_path.empty()) |
| 72 return ComponentUnpacker::kDeltaPatchProcessFailure; |
| 73 |
| 74 const std::string patch_type_str(PatchTypeToCommandLineSwitch(patch_type)); |
| 75 |
| 76 CommandLine cl(CommandLine::NO_PROGRAM); |
| 77 cl.AppendSwitchASCII(installer::switches::kPatch, patch_type_str.c_str()); |
| 78 cl.AppendSwitchPath(installer::switches::kInputFile, input_file); |
| 79 cl.AppendSwitchPath(installer::switches::kPatchFile, patch_file); |
| 80 cl.AppendSwitchPath(installer::switches::kOutputFile, output_file); |
| 81 |
| 82 base::LaunchOptions launch_options; |
| 83 launch_options.wait = true; |
| 84 launch_options.start_hidden = true; |
| 85 CommandLine setup_path(exe_path); |
| 86 setup_path.AppendArguments(cl, false); |
| 87 |
| 88 base::ProcessHandle ph; |
| 89 int exit_code = 0; |
| 90 if (!base::LaunchProcess(setup_path, launch_options, &ph) || |
| 91 !base::WaitForExitCode(ph, &exit_code)) |
| 92 return ComponentUnpacker::kDeltaPatchProcessFailure; |
| 93 |
| 94 *error = exit_code; |
| 95 return *error ? ComponentUnpacker::kDeltaOperationFailure : |
| 96 ComponentUnpacker::kNone; |
| 97 } |
| 98 |
OLD | NEW |