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