Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 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/auto_launch_util.h" | 5 #include "chrome/installer/util/auto_launch_util.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | |
| 7 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 8 #include "base/logging.h" | 9 #include "base/logging.h" |
| 9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/string_number_conversions.h" | |
| 10 #include "base/string16.h" | 12 #include "base/string16.h" |
| 11 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 12 #include "base/win/win_util.h" | 14 #include "base/win/win_util.h" |
| 13 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 16 #include "chrome/common/chrome_version_info.h" | |
| 17 #include "chrome/installer/util/browser_distribution.h" | |
| 18 #include "chrome/installer/util/product.h" | |
| 14 #include "chrome/installer/util/util_constants.h" | 19 #include "chrome/installer/util/util_constants.h" |
| 20 #include "crypto/sha2.h" | |
| 15 | 21 |
| 16 namespace auto_launch_util { | 22 namespace auto_launch_util { |
| 17 | 23 |
| 18 // The name of the Chrome Auto-launch key under the Run key. | 24 // The prefix of the Chrome Auto-launch key under the Run key. |
| 19 const wchar_t kAutolaunchKeyValue[] = L"GoogleChromeAutoLaunch"; | 25 const wchar_t kAutolaunchKeyValue[] = L"GoogleChromeAutoLaunch"; |
| 20 | 26 |
| 21 bool WillLaunchAtLogin(const FilePath& application_path) { | 27 // A helper function that takes a |profile_path| and builds a registry key |
| 28 // name to use when deciding where to read/write the auto-launch value | |
| 29 // to/from. It takes into account the name of the profile (so that different | |
| 30 // installations of Chrome don't conflict, and so the in the future different | |
| 31 // profiles can be auto-launched (or not) separately). | |
| 32 string16 ProfileToKeyName(const FilePath& profile_path) { | |
|
grt (UTC plus 2)
2012/02/06 17:54:49
should this be changed from profile_path to a stri
| |
| 33 FilePath path; | |
| 34 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 35 if (command_line.HasSwitch(switches::kUserDataDir)) { | |
| 36 path = command_line.GetSwitchValuePath(switches::kUserDataDir); | |
|
grt (UTC plus 2)
2012/02/06 17:54:49
shouldn't the BaseName of |profile_path| be append
Finnur
2012/02/07 12:29:02
Good catch!
| |
| 37 } else { | |
| 38 // Get the path from the same source as the installer, to make sure there | |
| 39 // are no differences. | |
| 40 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | |
| 41 installer::Product product(distribution); | |
| 42 path = product.GetUserDataPath().Append(profile_path.BaseName()); | |
| 43 } | |
| 44 | |
| 45 std::string input(path.AsUTF8Unsafe()); | |
| 46 uint8 hash[16]; | |
| 47 crypto::SHA256HashString(input, hash, sizeof(hash)); | |
| 48 std::string hash_string = base::HexEncode(hash, sizeof(hash)); | |
| 49 return string16(kAutolaunchKeyValue) + | |
| 50 ASCIIToWide("_") + ASCIIToWide(hash_string); | |
| 51 } | |
| 52 | |
| 53 bool WillLaunchAtLogin(const FilePath& application_path, | |
| 54 const FilePath& profile_path) { | |
| 55 string16 key_name(ProfileToKeyName(profile_path)); | |
| 22 string16 autolaunch; | 56 string16 autolaunch; |
| 23 if (!base::win::ReadCommandFromAutoRun( | 57 if (!base::win::ReadCommandFromAutoRun( |
| 24 HKEY_CURRENT_USER, kAutolaunchKeyValue, &autolaunch)) { | 58 HKEY_CURRENT_USER, key_name, &autolaunch)) { |
| 25 return false; | 59 return false; |
| 26 } | 60 } |
| 27 | 61 |
| 28 FilePath chrome_exe(application_path); | 62 FilePath chrome_exe(application_path); |
| 29 if (chrome_exe.empty()) { | 63 if (chrome_exe.empty()) { |
| 30 if (!PathService::Get(base::DIR_EXE, &chrome_exe)) { | 64 if (!PathService::Get(base::DIR_EXE, &chrome_exe)) { |
| 31 NOTREACHED(); | 65 NOTREACHED(); |
| 32 return false; | 66 return false; |
| 33 } | 67 } |
| 34 } | 68 } |
| 35 chrome_exe = chrome_exe.Append(installer::kChromeExe); | 69 chrome_exe = chrome_exe.Append(installer::kChromeExe); |
| 36 | 70 |
| 37 return autolaunch.find(chrome_exe.value()) != string16::npos; | 71 return autolaunch.find(chrome_exe.value()) != string16::npos; |
| 38 } | 72 } |
| 39 | 73 |
| 40 void SetWillLaunchAtLogin(bool auto_launch, const FilePath& application_path) { | 74 void SetWillLaunchAtLogin(bool auto_launch, |
| 75 const FilePath& application_path, | |
| 76 const FilePath& profile_path) { | |
| 77 string16 key_name(ProfileToKeyName(profile_path)); | |
| 78 | |
| 41 // TODO(finnur): Convert this into a shortcut, instead of using the Run key. | 79 // TODO(finnur): Convert this into a shortcut, instead of using the Run key. |
| 42 if (auto_launch) { | 80 if (auto_launch) { |
| 43 FilePath path(application_path); | 81 FilePath path(application_path); |
| 44 if (path.empty()) { | 82 if (path.empty()) { |
| 45 if (!PathService::Get(base::DIR_EXE, &path)) { | 83 if (!PathService::Get(base::DIR_EXE, &path)) { |
| 46 NOTREACHED(); | 84 NOTREACHED(); |
| 47 return; | 85 return; |
| 48 } | 86 } |
| 49 } | 87 } |
| 50 string16 cmd_line = ASCIIToUTF16("\""); | 88 string16 cmd_line = ASCIIToUTF16("\""); |
| 51 cmd_line += path.value(); | 89 cmd_line += path.value(); |
| 52 cmd_line += ASCIIToUTF16("\\"); | 90 cmd_line += ASCIIToUTF16("\\"); |
| 53 cmd_line += installer::kChromeExe; | 91 cmd_line += installer::kChromeExe; |
| 54 cmd_line += ASCIIToUTF16("\" --"); | 92 cmd_line += ASCIIToUTF16("\" --"); |
| 55 cmd_line += ASCIIToUTF16(switches::kAutoLaunchAtStartup); | 93 cmd_line += ASCIIToUTF16(switches::kAutoLaunchAtStartup); |
| 94 cmd_line += ASCIIToUTF16(" --"); | |
| 95 cmd_line += ASCIIToUTF16(switches::kProfileDirectory); | |
| 96 cmd_line += ASCIIToUTF16("=\""); | |
| 97 cmd_line += profile_path.BaseName().value(); | |
| 98 cmd_line += ASCIIToUTF16("\""); | |
| 56 | 99 |
| 57 base::win::AddCommandToAutoRun( | 100 base::win::AddCommandToAutoRun( |
| 58 HKEY_CURRENT_USER, kAutolaunchKeyValue, cmd_line); | 101 HKEY_CURRENT_USER, key_name, cmd_line); |
| 59 } else { | 102 } else { |
| 60 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, kAutolaunchKeyValue); | 103 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, key_name); |
| 61 } | 104 } |
| 62 } | 105 } |
| 63 | 106 |
| 64 } // namespace auto_launch_util | 107 } // namespace auto_launch_util |
| OLD | NEW |