OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/installer/util/installation_state.h" | 5 #include "chrome/installer/util/installation_state.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "base/version.h" | 9 #include "base/version.h" |
10 #include "base/win/registry.h" | 10 #include "base/win/registry.h" |
11 #include "chrome/installer/util/google_update_constants.h" | 11 #include "chrome/installer/util/google_update_constants.h" |
12 #include "chrome/installer/util/package_properties.h" | 12 |
| 13 namespace { |
| 14 |
| 15 CommandLine MakeUninstallCommand(const std::wstring& setup_path, |
| 16 const std::wstring& uninstall_arguments) { |
| 17 bool no_program = setup_path.empty(); |
| 18 |
| 19 // Return a bunch of nothingness. |
| 20 if (no_program && uninstall_arguments.empty()) |
| 21 return CommandLine(CommandLine::NO_PROGRAM); |
| 22 |
| 23 // Form a full command line string. |
| 24 std::wstring command; |
| 25 command.append(1, L'"') |
| 26 .append(no_program ? L"foo.exe" : setup_path) |
| 27 .append(L"\" ") |
| 28 .append(uninstall_arguments); |
| 29 |
| 30 // If we have a program name, return this complete command line. |
| 31 if (!no_program) |
| 32 return CommandLine::FromString(command); |
| 33 |
| 34 // Otherwise, make a new instance with the arguments but no program. |
| 35 CommandLine result(CommandLine::NO_PROGRAM); |
| 36 result.AppendArguments(CommandLine::FromString(command), false); |
| 37 return result; |
| 38 } |
| 39 |
| 40 } // namespace |
13 | 41 |
14 namespace installer { | 42 namespace installer { |
15 | 43 |
16 ProductState::ProductState() { | 44 ProductState::ProductState() |
| 45 : uninstall_command_(CommandLine::NO_PROGRAM) { |
17 } | 46 } |
18 | 47 |
19 void ProductState::Initialize(bool system_install, | 48 bool ProductState::Initialize(bool system_install, |
20 const std::wstring& version_key, | 49 BrowserDistribution::Type type) { |
21 const std::wstring& state_key) { | 50 return Initialize(system_install, |
| 51 BrowserDistribution::GetSpecificDistribution(type)); |
| 52 } |
| 53 |
| 54 bool ProductState::Initialize(bool system_install, |
| 55 BrowserDistribution* distribution) { |
| 56 const std::wstring version_key(distribution->GetVersionKey()); |
| 57 const std::wstring state_key(distribution->GetStateKey()); |
22 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | 58 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; |
23 base::win::RegKey key(root_key, version_key.c_str(), KEY_QUERY_VALUE); | 59 base::win::RegKey key(root_key, version_key.c_str(), KEY_QUERY_VALUE); |
24 std::wstring version_str; | 60 std::wstring version_str; |
25 if (key.ReadValue(google_update::kRegVersionField, &version_str) | 61 if (key.ReadValue(google_update::kRegVersionField, &version_str) |
26 == ERROR_SUCCESS) { | 62 == ERROR_SUCCESS) { |
27 version_.reset(Version::GetVersionFromString(WideToASCII(version_str))); | 63 version_.reset(Version::GetVersionFromString(WideToASCII(version_str))); |
28 if (version_.get() != NULL) { | 64 if (version_.get() != NULL) { |
29 // The product is installed. Check for the channel value (absent if not | 65 // The product is installed. |
30 // installed/managed by Google Update). | 66 if (key.ReadValue(google_update::kRegOldVersionField, &version_str) |
31 if ((key.Open(root_key, state_key.c_str(), KEY_QUERY_VALUE) != | 67 == ERROR_SUCCESS) { |
32 ERROR_SUCCESS) || !channel_.Initialize(key)) { | 68 old_version_.reset( |
33 channel_.set_value(std::wstring()); | 69 Version::GetVersionFromString(WideToASCII(version_str))); |
| 70 } else { |
| 71 old_version_.reset(); |
| 72 } |
| 73 if (key.ReadValue(google_update::kRegRenameCmdField, &rename_cmd_) |
| 74 != ERROR_SUCCESS) |
| 75 rename_cmd_.clear(); |
| 76 // Read from the ClientState key. |
| 77 channel_.set_value(std::wstring()); |
| 78 uninstall_command_ = CommandLine(CommandLine::NO_PROGRAM); |
| 79 msi_ = false; |
| 80 multi_install_ = false; |
| 81 if (key.Open(root_key, state_key.c_str(), KEY_QUERY_VALUE) |
| 82 == ERROR_SUCCESS) { |
| 83 std::wstring setup_path; |
| 84 std::wstring uninstall_arguments; |
| 85 // "ap" will be absent if not managed by Google Update. |
| 86 channel_.Initialize(key); |
| 87 // "UninstallString" will be absent for the multi-installer package. |
| 88 key.ReadValue(kUninstallStringField, &setup_path); |
| 89 // "UninstallArguments" will be absent for the multi-installer package. |
| 90 key.ReadValue(kUninstallArgumentsField, &uninstall_arguments); |
| 91 uninstall_command_ = |
| 92 MakeUninstallCommand(setup_path, uninstall_arguments); |
| 93 // "msi" may be absent, 0 or 1 |
| 94 DWORD dw_value = 0; |
| 95 msi_ = (key.ReadValueDW(google_update::kRegMSIField, &dw_value) |
| 96 == ERROR_SUCCESS) && (dw_value != 0); |
| 97 // Multi-install is implied or is derived from the command-line. |
| 98 if (distribution->GetType() == BrowserDistribution::CHROME_BINARIES) |
| 99 multi_install_ = true; |
| 100 else |
| 101 multi_install_ = uninstall_command_.HasSwitch( |
| 102 switches::kMultiInstall); |
34 } | 103 } |
35 } | 104 } |
36 } else { | 105 } else { |
37 version_.reset(); | 106 version_.reset(); |
38 } | 107 } |
| 108 return version_.get() != NULL; |
| 109 } |
| 110 |
| 111 FilePath ProductState::GetSetupPath() const { |
| 112 return uninstall_command_.GetProgram(); |
39 } | 113 } |
40 | 114 |
41 const Version& ProductState::version() const { | 115 const Version& ProductState::version() const { |
42 DCHECK(version_.get() != NULL); | 116 DCHECK(version_.get() != NULL); |
43 return *version_; | 117 return *version_; |
44 } | 118 } |
45 | 119 |
46 void ProductState::CopyFrom(const ProductState& other) { | 120 ProductState& ProductState::CopyFrom(const ProductState& other) { |
47 channel_.set_value(other.channel_.value()); | 121 channel_.set_value(other.channel_.value()); |
48 if (other.version_.get() == NULL) | 122 version_.reset(other.version_.get() == NULL ? NULL : other.version_->Clone()); |
49 version_.reset(); | 123 old_version_.reset( |
50 else | 124 other.old_version_.get() == NULL ? NULL : other.old_version_->Clone()); |
51 version_.reset(other.version_->Clone()); | 125 rename_cmd_ = other.rename_cmd_; |
| 126 uninstall_command_ = other.uninstall_command_; |
| 127 msi_ = other.msi_; |
| 128 multi_install_ = other.multi_install_; |
| 129 |
| 130 return *this; |
52 } | 131 } |
53 | 132 |
54 InstallationState::InstallationState() { | 133 InstallationState::InstallationState() { |
55 } | 134 } |
56 | 135 |
57 // static | 136 // static |
58 int InstallationState::IndexFromDistType(BrowserDistribution::Type type) { | 137 int InstallationState::IndexFromDistType(BrowserDistribution::Type type) { |
59 COMPILE_ASSERT(BrowserDistribution::CHROME_BROWSER == CHROME_BROWSER_INDEX, | 138 COMPILE_ASSERT(BrowserDistribution::CHROME_BROWSER == CHROME_BROWSER_INDEX, |
60 unexpected_chrome_browser_distribution_value_); | 139 unexpected_chrome_browser_distribution_value_); |
61 COMPILE_ASSERT(BrowserDistribution::CHROME_FRAME == CHROME_FRAME_INDEX, | 140 COMPILE_ASSERT(BrowserDistribution::CHROME_FRAME == CHROME_FRAME_INDEX, |
62 unexpected_chrome_frame_distribution_value_); | 141 unexpected_chrome_frame_distribution_value_); |
| 142 COMPILE_ASSERT(BrowserDistribution::CHROME_BINARIES == CHROME_BINARIES_INDEX, |
| 143 unexpected_chrome_frame_distribution_value_); |
63 DCHECK(type == BrowserDistribution::CHROME_BROWSER || | 144 DCHECK(type == BrowserDistribution::CHROME_BROWSER || |
64 type == BrowserDistribution::CHROME_FRAME); | 145 type == BrowserDistribution::CHROME_FRAME || |
| 146 type == BrowserDistribution::CHROME_BINARIES); |
65 return type; | 147 return type; |
66 } | 148 } |
67 | 149 |
68 void InstallationState::Initialize(const MasterPreferences& prefs) { | 150 void InstallationState::Initialize() { |
69 BrowserDistribution* distribution; | 151 BrowserDistribution* distribution; |
70 | 152 |
71 distribution = BrowserDistribution::GetSpecificDistribution( | 153 distribution = BrowserDistribution::GetSpecificDistribution( |
72 BrowserDistribution::CHROME_BROWSER, prefs); | 154 BrowserDistribution::CHROME_BROWSER); |
73 InitializeProduct(false, distribution, &user_products_[CHROME_BROWSER_INDEX]); | 155 user_products_[CHROME_BROWSER_INDEX].Initialize(false, distribution); |
74 InitializeProduct(true, distribution, | 156 system_products_[CHROME_BROWSER_INDEX].Initialize(true, distribution); |
75 &system_products_[CHROME_BROWSER_INDEX]); | |
76 | 157 |
77 distribution = BrowserDistribution::GetSpecificDistribution( | 158 distribution = BrowserDistribution::GetSpecificDistribution( |
78 BrowserDistribution::CHROME_FRAME, prefs); | 159 BrowserDistribution::CHROME_FRAME); |
79 InitializeProduct(false, distribution, &user_products_[CHROME_FRAME_INDEX]); | 160 user_products_[CHROME_FRAME_INDEX].Initialize(false, distribution); |
80 InitializeProduct(true, distribution, &system_products_[CHROME_FRAME_INDEX]); | 161 system_products_[CHROME_FRAME_INDEX].Initialize(true, distribution); |
81 | 162 |
82 ActivePackageProperties package_properties; | 163 distribution = BrowserDistribution::GetSpecificDistribution( |
83 InitializeMultiPackage(false, package_properties, | 164 BrowserDistribution::CHROME_BINARIES); |
84 &user_products_[MULTI_PACKAGE_INDEX]); | 165 user_products_[CHROME_BINARIES_INDEX].Initialize(false, distribution); |
85 InitializeMultiPackage(true, package_properties, | 166 system_products_[CHROME_BINARIES_INDEX].Initialize(true, distribution); |
86 &system_products_[MULTI_PACKAGE_INDEX]); | |
87 } | |
88 | |
89 // static | |
90 void InstallationState::InitializeProduct(bool system_install, | |
91 BrowserDistribution* distribution, | |
92 ProductState* product) { | |
93 product->Initialize(system_install, distribution->GetVersionKey(), | |
94 distribution->GetStateKey()); | |
95 } | |
96 | |
97 // static | |
98 void InstallationState::InitializeMultiPackage(bool system_install, | |
99 PackageProperties& properties, | |
100 ProductState* product) { | |
101 product->Initialize(system_install, properties.GetVersionKey(), | |
102 properties.GetStateKey()); | |
103 } | |
104 | |
105 const ProductState* InstallationState::GetMultiPackageState( | |
106 bool system_install) const { | |
107 const ProductState& product_state = | |
108 (system_install ? system_products_ : user_products_)[MULTI_PACKAGE_INDEX]; | |
109 return product_state.version_.get() == NULL ? NULL : &product_state; | |
110 } | 167 } |
111 | 168 |
112 const ProductState* InstallationState::GetProductState( | 169 const ProductState* InstallationState::GetProductState( |
113 bool system_install, | 170 bool system_install, |
114 BrowserDistribution::Type type) const { | 171 BrowserDistribution::Type type) const { |
115 const ProductState& product_state = (system_install ? system_products_ : | 172 const ProductState& product_state = (system_install ? system_products_ : |
116 user_products_)[IndexFromDistType(type)]; | 173 user_products_)[IndexFromDistType(type)]; |
117 return product_state.version_.get() == NULL ? NULL : &product_state; | 174 return product_state.version_.get() == NULL ? NULL : &product_state; |
118 } | 175 } |
119 | 176 |
120 } // namespace installer | 177 } // namespace installer |
OLD | NEW |