setup_util.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
1// Copyright (c) 2012 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// This file declares util functions for setup project.
6
7#ifndef CHROME_INSTALLER_SETUP_SETUP_UTIL_H_
8#define CHROME_INSTALLER_SETUP_SETUP_UTIL_H_
9
10#include <windows.h>
11
12#include "base/basictypes.h"
13#include "base/string16.h"
14#include "base/win/scoped_handle.h"
15#include "chrome/installer/util/browser_distribution.h"
16#include "chrome/installer/util/util_constants.h"
17
18class CommandLine;
19class FilePath;
20class Version;
21
22namespace installer {
23
24class InstallationState;
25class InstallerState;
26class ProductState;
27
28// Apply a diff patch to source file. First tries to apply it using courgette
29// since it checks for courgette header and fails quickly. If that fails
30// tries to apply the patch using regular bsdiff. Returns status code.
31// The installer stage is updated if |installer_state| is non-NULL.
32int ApplyDiffPatch(const FilePath& src,
33                   const FilePath& patch,
34                   const FilePath& dest,
35                   const InstallerState* installer_state);
36
37// Find the version of Chrome from an install source directory.
38// Chrome_path should contain at least one version folder.
39// Returns the maximum version found or NULL if no version is found.
40Version* GetMaxVersionFromArchiveDir(const FilePath& chrome_path);
41
42// Spawns a new process that waits for a specified amount of time before
43// attempting to delete |path|.  This is useful for setup to delete the
44// currently running executable or a file that we cannot close right away but
45// estimate that it will be possible after some period of time.
46// Returns true if a new process was started, false otherwise.  Note that
47// given the nature of this function, it is not possible to know if the
48// delete operation itself succeeded.
49bool DeleteFileFromTempProcess(const FilePath& path,
50                               uint32 delay_before_delete_ms);
51
52// Returns true and populates |setup_exe| with the path to an existing product
53// installer if one is found that is newer than the currently running installer
54// (|installer_version|).
55bool GetExistingHigherInstaller(const InstallationState& original_state,
56                                bool system_install,
57                                const Version& installer_version,
58                                FilePath* setup_exe);
59
60// Invokes the pre-existing |setup_exe| to handle the current operation (as
61// dictated by |command_line|). An installerdata file, if specified, is first
62// unconditionally copied into place so that it will be in effect in case the
63// invoked |setup_exe| runs the newly installed product prior to exiting.
64// Returns true if |setup_exe| was launched, false otherwise.
65bool DeferToExistingInstall(const FilePath& setup_exe,
66                            const CommandLine& command_line,
67                            const InstallerState& installer_state,
68                            const FilePath& temp_path,
69                            InstallStatus* install_status);
70
71// This class will enable the privilege defined by |privilege_name| on the
72// current process' token. The privilege will be disabled upon the
73// ScopedTokenPrivilege's destruction (unless it was already enabled when the
74// ScopedTokenPrivilege object was constructed).
75// Some privileges might require admin rights to be enabled (check is_enabled()
76// to know whether |privilege_name| was successfully enabled).
77class ScopedTokenPrivilege {
78 public:
79  explicit ScopedTokenPrivilege(const wchar_t* privilege_name);
80  ~ScopedTokenPrivilege();
81
82  // Always returns true unless the privilege could not be enabled.
83  bool is_enabled() const { return is_enabled_; }
84
85 private:
86  // Always true unless the privilege could not be enabled.
87  bool is_enabled_;
88
89  // A scoped handle to the current process' token. This will be closed
90  // preemptively should enabling the privilege fail in the constructor.
91  base::win::ScopedHandle token_;
92
93  // The previous state of the privilege this object is responsible for. As set
94  // by AdjustTokenPrivileges() upon construction.
95  TOKEN_PRIVILEGES previous_privileges_;
96
97  DISALLOW_IMPLICIT_CONSTRUCTORS(ScopedTokenPrivilege);
98};
99
100}  // namespace installer
101
102#endif  // CHROME_INSTALLER_SETUP_SETUP_UTIL_H_
103