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