OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/channel_info.h" | 5 #include "chrome/installer/util/channel_info.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string_util.h" |
8 #include "base/win/registry.h" | 9 #include "base/win/registry.h" |
9 #include "chrome/installer/util/google_update_constants.h" | 10 #include "chrome/installer/util/google_update_constants.h" |
| 11 #include "chrome/installer/util/install_util.h" |
10 #include "chrome/installer/util/util_constants.h" | 12 #include "chrome/installer/util/util_constants.h" |
11 | 13 |
12 using base::win::RegKey; | 14 using base::win::RegKey; |
13 | 15 |
14 namespace { | 16 namespace { |
15 | 17 |
16 const wchar_t kModChrome[] = L"-chrome"; | 18 const wchar_t kModChrome[] = L"-chrome"; |
17 const wchar_t kModChromeFrame[] = L"-chromeframe"; | 19 const wchar_t kModChromeFrame[] = L"-chromeframe"; |
18 // TODO(huangs): Remove by M27. | |
19 const wchar_t kModAppHostDeprecated[] = L"-apphost"; | 20 const wchar_t kModAppHostDeprecated[] = L"-apphost"; |
20 const wchar_t kModAppLauncher[] = L"-applauncher"; | 21 const wchar_t kModAppLauncherDeprecated[] = L"-applauncher"; |
21 const wchar_t kModMultiInstall[] = L"-multi"; | 22 const wchar_t kModMultiInstall[] = L"-multi"; |
22 const wchar_t kModReadyMode[] = L"-readymode"; | 23 const wchar_t kModReadyMode[] = L"-readymode"; |
23 const wchar_t kModStage[] = L"-stage:"; | 24 const wchar_t kModStage[] = L"-stage:"; |
24 const wchar_t kSfxFull[] = L"-full"; | 25 const wchar_t kSfxFull[] = L"-full"; |
25 const wchar_t kSfxMigrating[] = L"-migrating"; | 26 const wchar_t kSfxMigrating[] = L"-migrating"; |
26 const wchar_t kSfxMultiFail[] = L"-multifail"; | 27 const wchar_t kSfxMultiFail[] = L"-multifail"; |
27 | 28 |
28 const wchar_t* const kChannels[] = { | 29 const wchar_t* const kChannels[] = { |
29 installer::kChromeChannelBeta, | 30 installer::kChromeChannelBeta, |
30 installer::kChromeChannelDev, | 31 installer::kChromeChannelDev, |
31 installer::kChromeChannelStableExplicit | 32 installer::kChromeChannelStableExplicit |
32 }; | 33 }; |
33 | 34 |
34 const wchar_t* const kModifiers[] = { | 35 const wchar_t* const kModifiers[] = { |
35 kModStage, | 36 kModStage, |
36 kModMultiInstall, | 37 kModMultiInstall, |
37 kModChrome, | 38 kModChrome, |
38 kModChromeFrame, | 39 kModChromeFrame, |
39 kModAppHostDeprecated, // TODO(huangs): Remove by M27. | 40 kModAppHostDeprecated, |
40 kModAppLauncher, | 41 kModAppLauncherDeprecated, |
41 kModReadyMode, | 42 kModReadyMode, |
42 kSfxMultiFail, | 43 kSfxMultiFail, |
43 kSfxMigrating, | 44 kSfxMigrating, |
44 kSfxFull, | 45 kSfxFull, |
45 }; | 46 }; |
46 | 47 |
47 enum ModifierIndex { | 48 enum ModifierIndex { |
48 MOD_STAGE, | 49 MOD_STAGE, |
49 MOD_MULTI_INSTALL, | 50 MOD_MULTI_INSTALL, |
50 MOD_CHROME, | 51 MOD_CHROME, |
51 MOD_CHROME_FRAME, | 52 MOD_CHROME_FRAME, |
52 MOD_APP_HOST_DEPRECATED, // TODO(huangs): Remove by M27. | 53 MOD_APP_HOST_DEPRECATED, |
53 MOD_APP_LAUNCHER, | 54 MOD_APP_LAUNCHER_DEPRECATED, |
54 MOD_READY_MODE, | 55 MOD_READY_MODE, |
55 SFX_MULTI_FAIL, | 56 SFX_MULTI_FAIL, |
56 SFX_MIGRATING, | 57 SFX_MIGRATING, |
57 SFX_FULL, | 58 SFX_FULL, |
58 NUM_MODIFIERS | 59 NUM_MODIFIERS |
59 }; | 60 }; |
60 | 61 |
61 COMPILE_ASSERT(NUM_MODIFIERS == arraysize(kModifiers), | 62 COMPILE_ASSERT(NUM_MODIFIERS == arraysize(kModifiers), |
62 kModifiers_disagrees_with_ModifierIndex_comma_they_must_match_bang); | 63 kModifiers_disagrees_with_ModifierIndex_comma_they_must_match_bang); |
63 | 64 |
| 65 // To remove "-test" from "-abc-test-efg", we: |
| 66 // (1) add sentinel "-" to the end to form "-abc-test-efg-", |
| 67 // (2) search for "-test" + "-" = "-test-", |
| 68 // (3) if found, replace substring with "-" to get "-abc-efg-", |
| 69 // (4) remove trailing "-" to get "-abc-efg". |
| 70 base::string16 RemoveDeprecatedModifiers(const base::string16& value) { |
| 71 const base::string16 kModSeparator = L"-"; |
| 72 base::string16 new_value = value; |
| 73 new_value.push_back(kModSeparator[0]); // Add sentinel. |
| 74 |
| 75 ReplaceFirstSubstringAfterOffset( |
| 76 &new_value, 0, kModAppHostDeprecated + kModSeparator, kModSeparator); |
| 77 ReplaceFirstSubstringAfterOffset( |
| 78 &new_value, 0, kModAppLauncherDeprecated + kModSeparator, kModSeparator); |
| 79 |
| 80 new_value.pop_back(); // Remove sentinel. |
| 81 return new_value; |
| 82 } |
| 83 |
64 // Returns true if the modifier is found, in which case |position| holds the | 84 // Returns true if the modifier is found, in which case |position| holds the |
65 // location at which the modifier was found. The number of characters in the | 85 // location at which the modifier was found. The number of characters in the |
66 // modifier is returned in |length|, if non-NULL. | 86 // modifier is returned in |length|, if non-NULL. |
67 bool FindModifier(ModifierIndex index, | 87 bool FindModifier(ModifierIndex index, |
68 const std::wstring& ap_value, | 88 const base::string16& ap_value, |
69 std::wstring::size_type* position, | 89 base::string16::size_type* position, |
70 std::wstring::size_type* length) { | 90 base::string16::size_type* length) { |
71 DCHECK(position != NULL); | 91 DCHECK(position != NULL); |
72 std::wstring::size_type mod_position = std::wstring::npos; | 92 base::string16::size_type mod_position = base::string16::npos; |
73 std::wstring::size_type mod_length = | 93 base::string16::size_type mod_length = |
74 std::wstring::traits_type::length(kModifiers[index]); | 94 base::string16::traits_type::length(kModifiers[index]); |
75 const bool mod_takes_arg = (kModifiers[index][mod_length - 1] == L':'); | 95 const bool mod_takes_arg = (kModifiers[index][mod_length - 1] == L':'); |
76 std::wstring::size_type pos = 0; | 96 base::string16::size_type pos = 0; |
77 do { | 97 do { |
78 mod_position = ap_value.find(kModifiers[index], pos, mod_length); | 98 mod_position = ap_value.find(kModifiers[index], pos, mod_length); |
79 if (mod_position == std::wstring::npos) | 99 if (mod_position == base::string16::npos) |
80 return false; // Modifier not found. | 100 return false; // Modifier not found. |
81 pos = mod_position + mod_length; | 101 pos = mod_position + mod_length; |
82 // Modifiers that take an argument gobble up to the next separator or to the | 102 // Modifiers that take an argument gobble up to the next separator or to the |
83 // end. | 103 // end. |
84 if (mod_takes_arg) { | 104 if (mod_takes_arg) { |
85 pos = ap_value.find(L'-', pos); | 105 pos = ap_value.find(L'-', pos); |
86 if (pos == std::wstring::npos) | 106 if (pos == base::string16::npos) |
87 pos = ap_value.size(); | 107 pos = ap_value.size(); |
88 break; | 108 break; |
89 } | 109 } |
90 // Regular modifiers must be followed by '-' or the end of the string. | 110 // Regular modifiers must be followed by '-' or the end of the string. |
91 } while (pos != ap_value.size() && ap_value[pos] != L'-'); | 111 } while (pos != ap_value.size() && ap_value[pos] != L'-'); |
92 DCHECK_NE(mod_position, std::wstring::npos); | 112 DCHECK_NE(mod_position, base::string16::npos); |
93 *position = mod_position; | 113 *position = mod_position; |
94 if (length != NULL) | 114 if (length != NULL) |
95 *length = pos - mod_position; | 115 *length = pos - mod_position; |
96 return true; | 116 return true; |
97 } | 117 } |
98 | 118 |
99 bool HasModifier(ModifierIndex index, const std::wstring& ap_value) { | 119 bool HasModifier(ModifierIndex index, const base::string16& ap_value) { |
100 DCHECK(index >= 0 && index < NUM_MODIFIERS); | 120 DCHECK(index >= 0 && index < NUM_MODIFIERS); |
101 std::wstring::size_type position; | 121 base::string16::size_type position; |
102 return FindModifier(index, ap_value, &position, NULL); | 122 return FindModifier(index, ap_value, &position, NULL); |
103 } | 123 } |
104 | 124 |
105 std::wstring::size_type FindInsertionPoint(ModifierIndex index, | 125 base::string16::size_type FindInsertionPoint(ModifierIndex index, |
106 const std::wstring& ap_value) { | 126 const base::string16& ap_value) { |
107 // Return the location of the next modifier. | 127 // Return the location of the next modifier. |
108 std::wstring::size_type result; | 128 base::string16::size_type result; |
109 | 129 |
110 for (int scan = index + 1; scan < NUM_MODIFIERS; ++scan) { | 130 for (int scan = index + 1; scan < NUM_MODIFIERS; ++scan) { |
111 if (FindModifier(static_cast<ModifierIndex>(scan), ap_value, &result, NULL)) | 131 if (FindModifier(static_cast<ModifierIndex>(scan), ap_value, &result, NULL)) |
112 return result; | 132 return result; |
113 } | 133 } |
114 | 134 |
115 return ap_value.size(); | 135 return ap_value.size(); |
116 } | 136 } |
117 | 137 |
118 // Returns true if |ap_value| is modified. | 138 // Returns true if |ap_value| is modified. |
119 bool SetModifier(ModifierIndex index, bool set, std::wstring* ap_value) { | 139 bool SetModifier(ModifierIndex index, bool set, base::string16* ap_value) { |
120 DCHECK(index >= 0 && index < NUM_MODIFIERS); | 140 DCHECK(index >= 0 && index < NUM_MODIFIERS); |
121 DCHECK(ap_value); | 141 DCHECK(ap_value); |
122 std::wstring::size_type position; | 142 base::string16::size_type position; |
123 std::wstring::size_type length; | 143 base::string16::size_type length; |
124 bool have_modifier = FindModifier(index, *ap_value, &position, &length); | 144 bool have_modifier = FindModifier(index, *ap_value, &position, &length); |
125 if (set) { | 145 if (set) { |
126 if (!have_modifier) { | 146 if (!have_modifier) { |
127 ap_value->insert(FindInsertionPoint(index, *ap_value), kModifiers[index]); | 147 ap_value->insert(FindInsertionPoint(index, *ap_value), kModifiers[index]); |
128 return true; | 148 return true; |
129 } | 149 } |
130 } else { | 150 } else { |
131 if (have_modifier) { | 151 if (have_modifier) { |
132 ap_value->erase(position, length); | 152 ap_value->erase(position, length); |
133 return true; | 153 return true; |
134 } | 154 } |
135 } | 155 } |
136 return false; | 156 return false; |
137 } | 157 } |
138 | 158 |
139 } // namespace | 159 } // namespace |
140 | 160 |
141 namespace installer { | 161 namespace installer { |
142 | 162 |
143 bool ChannelInfo::Initialize(const RegKey& key) { | 163 bool ChannelInfo::Initialize(const RegKey& key) { |
144 LONG result = key.ReadValue(google_update::kRegApField, &value_); | 164 base::string16 temp_value; |
145 return result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND || | 165 LONG result = key.ReadValue(google_update::kRegApField, &temp_value); |
146 result == ERROR_INVALID_HANDLE; | 166 if (result == ERROR_SUCCESS || result == ERROR_FILE_NOT_FOUND || |
| 167 result == ERROR_INVALID_HANDLE) { |
| 168 set_value(temp_value); |
| 169 return true; |
| 170 } |
| 171 return false; |
147 } | 172 } |
148 | 173 |
149 bool ChannelInfo::Write(RegKey* key) const { | 174 bool ChannelInfo::Write(RegKey* key) const { |
150 DCHECK(key); | 175 DCHECK(key); |
151 // Google Update deletes the value when it is empty, so we may as well, too. | 176 // Google Update deletes the value when it is empty, so we may as well, too. |
152 LONG result = value_.empty() ? | 177 LONG result = value_.empty() ? |
153 key->DeleteValue(google_update::kRegApField) : | 178 key->DeleteValue(google_update::kRegApField) : |
154 key->WriteValue(google_update::kRegApField, value_.c_str()); | 179 key->WriteValue(google_update::kRegApField, value_.c_str()); |
155 if (result != ERROR_SUCCESS) { | 180 if (result != ERROR_SUCCESS) { |
156 LOG(ERROR) << "Failed writing channel info; result: " << result; | 181 LOG(ERROR) << "Failed writing channel info; result: " << result; |
157 return false; | 182 return false; |
158 } | 183 } |
159 return true; | 184 return true; |
160 } | 185 } |
161 | 186 |
162 bool ChannelInfo::GetChannelName(std::wstring* channel_name) const { | 187 void ChannelInfo::set_value(const base::string16& value) { |
| 188 value_ = RemoveDeprecatedModifiers(value); |
| 189 } |
| 190 |
| 191 bool ChannelInfo::GetChannelName(base::string16* channel_name) const { |
163 DCHECK(channel_name); | 192 DCHECK(channel_name); |
164 if (value_.empty()) { | 193 if (value_.empty()) { |
165 channel_name->erase(); | 194 channel_name->erase(); |
166 return true; | 195 return true; |
167 } else { | 196 } else { |
168 for (const wchar_t* const* scan = &kChannels[0], | 197 for (const wchar_t* const* scan = &kChannels[0], |
169 *const* end = &kChannels[arraysize(kChannels)]; scan != end; | 198 *const* end = &kChannels[arraysize(kChannels)]; scan != end; |
170 ++scan) { | 199 ++scan) { |
171 if (value_.find(*scan) != std::wstring::npos) { | 200 if (value_.find(*scan) != base::string16::npos) { |
172 // Report channels with "stable" in them as stable (empty string). | 201 // Report channels with "stable" in them as stable (empty string). |
173 if (*scan == installer::kChromeChannelStableExplicit) | 202 if (*scan == installer::kChromeChannelStableExplicit) |
174 channel_name->erase(); | 203 channel_name->erase(); |
175 else | 204 else |
176 channel_name->assign(*scan); | 205 channel_name->assign(*scan); |
177 return true; | 206 return true; |
178 } | 207 } |
179 } | 208 } |
180 // There may be modifiers present. Strip them off and see if we're left | 209 // There may be modifiers present. Strip them off and see if we're left |
181 // with the empty string (stable channel). | 210 // with the empty string (stable channel). |
182 std::wstring tmp_value = value_; | 211 base::string16 tmp_value = value_; |
183 for (int i = 0; i != NUM_MODIFIERS; ++i) { | 212 for (int i = 0; i != NUM_MODIFIERS; ++i) { |
184 SetModifier(static_cast<ModifierIndex>(i), false, &tmp_value); | 213 SetModifier(static_cast<ModifierIndex>(i), false, &tmp_value); |
185 } | 214 } |
186 if (tmp_value.empty()) { | 215 if (tmp_value.empty()) { |
187 channel_name->erase(); | 216 channel_name->erase(); |
188 return true; | 217 return true; |
189 } | 218 } |
190 } | 219 } |
191 | 220 |
192 return false; | 221 return false; |
193 } | 222 } |
194 | 223 |
195 bool ChannelInfo::IsChrome() const { | 224 bool ChannelInfo::IsChrome() const { |
196 return HasModifier(MOD_CHROME, value_); | 225 return HasModifier(MOD_CHROME, value_); |
197 } | 226 } |
198 | 227 |
199 bool ChannelInfo::SetChrome(bool value) { | 228 bool ChannelInfo::SetChrome(bool value) { |
200 return SetModifier(MOD_CHROME, value, &value_); | 229 return SetModifier(MOD_CHROME, value, &value_); |
201 } | 230 } |
202 | 231 |
203 bool ChannelInfo::IsChromeFrame() const { | 232 bool ChannelInfo::IsChromeFrame() const { |
204 return HasModifier(MOD_CHROME_FRAME, value_); | 233 return HasModifier(MOD_CHROME_FRAME, value_); |
205 } | 234 } |
206 | 235 |
207 bool ChannelInfo::SetChromeFrame(bool value) { | 236 bool ChannelInfo::SetChromeFrame(bool value) { |
208 return SetModifier(MOD_CHROME_FRAME, value, &value_); | 237 return SetModifier(MOD_CHROME_FRAME, value, &value_); |
209 } | 238 } |
210 | 239 |
211 bool ChannelInfo::IsAppLauncher() const { | 240 bool ChannelInfo::IsAppLauncher() const { |
212 return HasModifier(MOD_APP_LAUNCHER, value_); | 241 // -apphost and -applauncher have been deprecated. |
| 242 return false; |
213 } | 243 } |
214 | 244 |
215 bool ChannelInfo::SetAppLauncher(bool value) { | 245 bool ChannelInfo::SetAppLauncher(bool value) { |
216 // Unconditionally remove -apphost since it has been deprecated. | 246 // Unconditionally remove -apphost and -applauncher since they have been |
| 247 // deprecated. |
217 bool changed_app_host = SetModifier(MOD_APP_HOST_DEPRECATED, false, &value_); | 248 bool changed_app_host = SetModifier(MOD_APP_HOST_DEPRECATED, false, &value_); |
218 bool changed_app_launcher = SetModifier(MOD_APP_LAUNCHER, value, &value_); | 249 bool changed_app_launcher = SetModifier(MOD_APP_LAUNCHER_DEPRECATED, |
| 250 false, &value_); |
219 return changed_app_host || changed_app_launcher; | 251 return changed_app_host || changed_app_launcher; |
220 } | 252 } |
221 | 253 |
222 bool ChannelInfo::IsMultiInstall() const { | 254 bool ChannelInfo::IsMultiInstall() const { |
223 return HasModifier(MOD_MULTI_INSTALL, value_); | 255 return HasModifier(MOD_MULTI_INSTALL, value_); |
224 } | 256 } |
225 | 257 |
226 bool ChannelInfo::SetMultiInstall(bool value) { | 258 bool ChannelInfo::SetMultiInstall(bool value) { |
227 return SetModifier(MOD_MULTI_INSTALL, value, &value_); | 259 return SetModifier(MOD_MULTI_INSTALL, value, &value_); |
228 } | 260 } |
229 | 261 |
230 bool ChannelInfo::IsReadyMode() const { | 262 bool ChannelInfo::IsReadyMode() const { |
231 return HasModifier(MOD_READY_MODE, value_); | 263 return HasModifier(MOD_READY_MODE, value_); |
232 } | 264 } |
233 | 265 |
234 bool ChannelInfo::SetReadyMode(bool value) { | 266 bool ChannelInfo::SetReadyMode(bool value) { |
235 return SetModifier(MOD_READY_MODE, value, &value_); | 267 return SetModifier(MOD_READY_MODE, value, &value_); |
236 } | 268 } |
237 | 269 |
238 bool ChannelInfo::SetStage(const wchar_t* stage) { | 270 bool ChannelInfo::SetStage(const wchar_t* stage) { |
239 std::wstring::size_type position; | 271 base::string16::size_type position; |
240 std::wstring::size_type length; | 272 base::string16::size_type length; |
241 bool have_modifier = FindModifier(MOD_STAGE, value_, &position, &length); | 273 bool have_modifier = FindModifier(MOD_STAGE, value_, &position, &length); |
242 if (stage != NULL && *stage != L'\0') { | 274 if (stage != NULL && *stage != L'\0') { |
243 std::wstring stage_str(kModStage); | 275 base::string16 stage_str(kModStage); |
244 stage_str.append(stage); | 276 stage_str.append(stage); |
245 if (!have_modifier) { | 277 if (!have_modifier) { |
246 value_.insert(FindInsertionPoint(MOD_STAGE, value_), stage_str); | 278 value_.insert(FindInsertionPoint(MOD_STAGE, value_), stage_str); |
247 return true; | 279 return true; |
248 } | 280 } |
249 if (value_.compare(position, length, stage_str) != 0) { | 281 if (value_.compare(position, length, stage_str) != 0) { |
250 value_.replace(position, length, stage_str); | 282 value_.replace(position, length, stage_str); |
251 return true; | 283 return true; |
252 } | 284 } |
253 } else { | 285 } else { |
254 if (have_modifier) { | 286 if (have_modifier) { |
255 value_.erase(position, length); | 287 value_.erase(position, length); |
256 return true; | 288 return true; |
257 } | 289 } |
258 } | 290 } |
259 return false; | 291 return false; |
260 } | 292 } |
261 | 293 |
262 std::wstring ChannelInfo::GetStage() const { | 294 base::string16 ChannelInfo::GetStage() const { |
263 std::wstring::size_type position; | 295 base::string16::size_type position; |
264 std::wstring::size_type length; | 296 base::string16::size_type length; |
265 | 297 |
266 if (FindModifier(MOD_STAGE, value_, &position, &length)) { | 298 if (FindModifier(MOD_STAGE, value_, &position, &length)) { |
267 // Return the portion after the prefix. | 299 // Return the portion after the prefix. |
268 std::wstring::size_type pfx_length = | 300 base::string16::size_type pfx_length = |
269 std::wstring::traits_type::length(kModStage); | 301 base::string16::traits_type::length(kModStage); |
270 DCHECK_LE(pfx_length, length); | 302 DCHECK_LE(pfx_length, length); |
271 return value_.substr(position + pfx_length, length - pfx_length); | 303 return value_.substr(position + pfx_length, length - pfx_length); |
272 } | 304 } |
273 return std::wstring(); | 305 return base::string16(); |
274 } | 306 } |
275 | 307 |
276 bool ChannelInfo::HasFullSuffix() const { | 308 bool ChannelInfo::HasFullSuffix() const { |
277 return HasModifier(SFX_FULL, value_); | 309 return HasModifier(SFX_FULL, value_); |
278 } | 310 } |
279 | 311 |
280 bool ChannelInfo::SetFullSuffix(bool value) { | 312 bool ChannelInfo::SetFullSuffix(bool value) { |
281 return SetModifier(SFX_FULL, value, &value_); | 313 return SetModifier(SFX_FULL, value, &value_); |
282 } | 314 } |
283 | 315 |
(...skipping 18 matching lines...) Expand all Loading... |
302 | 334 |
303 for (int scan = 0; scan < NUM_MODIFIERS; ++scan) { | 335 for (int scan = 0; scan < NUM_MODIFIERS; ++scan) { |
304 ModifierIndex index = static_cast<ModifierIndex>(scan); | 336 ModifierIndex index = static_cast<ModifierIndex>(scan); |
305 modified = SetModifier(index, false, &value_) || modified; | 337 modified = SetModifier(index, false, &value_) || modified; |
306 } | 338 } |
307 | 339 |
308 return modified; | 340 return modified; |
309 } | 341 } |
310 | 342 |
311 } // namespace installer | 343 } // namespace installer |
OLD | NEW |