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