Chromium Code Reviews| 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/auto_launch_util.h" | 5 #include "chrome/installer/util/auto_launch_util.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/win/win_util.h" | 13 #include "base/win/win_util.h" |
| 14 #include "chrome/common/chrome_switches.h" | 14 #include "chrome/common/chrome_switches.h" |
| 15 #include "chrome/common/chrome_version_info.h" | 15 #include "chrome/common/chrome_version_info.h" |
| 16 #include "chrome/installer/util/browser_distribution.h" | 16 #include "chrome/installer/util/browser_distribution.h" |
| 17 #include "chrome/installer/util/product.h" | 17 #include "chrome/installer/util/product.h" |
| 18 #include "chrome/installer/util/util_constants.h" | 18 #include "chrome/installer/util/util_constants.h" |
| 19 #include "crypto/sha2.h" | 19 #include "crypto/sha2.h" |
| 20 | 20 |
| 21 namespace auto_launch_util { | 21 namespace auto_launch_util { |
| 22 | 22 |
| 23 // The prefix of the Chrome Auto-launch key under the Run key. | 23 // The prefix of the Chrome Auto-launch key under the Run key. |
| 24 const wchar_t kAutolaunchKeyValue[] = L"GoogleChromeAutoLaunch"; | 24 const wchar_t kAutolaunchKeyValue[] = L"GoogleChromeAutoLaunch"; |
| 25 | 25 |
| 26 // We use one Run key with flags specifying which feature we want to start up. | |
| 27 // When we change our Run key we need to specify what we want to do with each | |
| 28 // flag. This lists the possible actions we can take with the flags. | |
| 29 enum FlagSetting { | |
| 30 FLAG_DISABLE, // Disable the flag. | |
| 31 FLAG_ENABLE, // Enable the flag. | |
| 32 FLAG_PRESERVE, // Preserve the value that the flag has currently. | |
| 33 }; | |
| 34 | |
| 26 // A helper function that takes a |profile_path| and builds a registry key | 35 // 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 | 36 // 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 | 37 // 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 | 38 // installations of Chrome don't conflict, and so the in the future different |
| 30 // profiles can be auto-launched (or not) separately). | 39 // profiles can be auto-launched (or not) separately). |
| 31 string16 ProfileToKeyName(const string16& profile_directory) { | 40 string16 ProfileToKeyName(const string16& profile_directory) { |
| 32 FilePath path; | 41 FilePath path; |
| 33 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 42 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 34 if (command_line.HasSwitch(switches::kUserDataDir)) { | 43 if (command_line.HasSwitch(switches::kUserDataDir)) { |
| 35 path = command_line.GetSwitchValuePath(switches::kUserDataDir); | 44 path = command_line.GetSwitchValuePath(switches::kUserDataDir); |
| 36 } else { | 45 } else { |
| 37 // Get the path from the same source as the installer, to make sure there | 46 // Get the path from the same source as the installer, to make sure there |
| 38 // are no differences. | 47 // are no differences. |
| 39 BrowserDistribution* distribution = | 48 BrowserDistribution* distribution = |
| 40 BrowserDistribution::GetSpecificDistribution( | 49 BrowserDistribution::GetSpecificDistribution( |
| 41 BrowserDistribution::CHROME_BROWSER); | 50 BrowserDistribution::CHROME_BROWSER); |
| 42 installer::Product product(distribution); | 51 installer::Product product(distribution); |
| 43 path = product.GetUserDataPath(); | 52 path = product.GetUserDataPath(); |
| 44 } | 53 } |
| 45 path = path.Append(profile_directory); | 54 path = path.Append(profile_directory); |
| 46 | 55 |
| 47 std::string input(path.AsUTF8Unsafe()); | 56 std::string input(path.AsUTF8Unsafe()); |
| 48 uint8 hash[16]; | 57 uint8 hash[16]; |
| 49 crypto::SHA256HashString(input, hash, sizeof(hash)); | 58 crypto::SHA256HashString(input, hash, sizeof(hash)); |
| 50 std::string hash_string = base::HexEncode(hash, sizeof(hash)); | 59 std::string hash_string = base::HexEncode(hash, sizeof(hash)); |
| 51 return string16(kAutolaunchKeyValue) + | 60 return string16(kAutolaunchKeyValue) + |
| 52 ASCIIToWide("_") + ASCIIToWide(hash_string); | 61 ASCIIToWide("_") + ASCIIToWide(hash_string); |
| 53 } | 62 } |
| 54 | 63 |
| 55 bool WillLaunchAtLogin(const FilePath& application_path, | 64 bool WillLaunchAtLoginWithSwitch(const FilePath& application_path, |
| 56 const string16& profile_directory) { | 65 const string16& profile_directory, |
| 66 const std::string& command_line_switch) { | |
| 57 string16 key_name(ProfileToKeyName(profile_directory)); | 67 string16 key_name(ProfileToKeyName(profile_directory)); |
| 58 string16 autolaunch; | 68 string16 autolaunch; |
| 59 if (!base::win::ReadCommandFromAutoRun( | 69 if (!base::win::ReadCommandFromAutoRun( |
| 60 HKEY_CURRENT_USER, key_name, &autolaunch)) { | 70 HKEY_CURRENT_USER, key_name, &autolaunch)) { |
| 61 return false; | 71 return false; |
| 62 } | 72 } |
| 63 | 73 |
| 64 FilePath chrome_exe(application_path); | 74 FilePath chrome_exe(application_path); |
| 65 if (chrome_exe.empty()) { | 75 if (chrome_exe.empty()) { |
| 66 if (!PathService::Get(base::DIR_EXE, &chrome_exe)) { | 76 if (!PathService::Get(base::DIR_EXE, &chrome_exe)) { |
| 67 NOTREACHED(); | 77 NOTREACHED(); |
| 68 return false; | 78 return false; |
| 69 } | 79 } |
| 70 } | 80 } |
| 71 chrome_exe = chrome_exe.Append(installer::kChromeExe); | 81 chrome_exe = chrome_exe.Append(installer::kChromeExe); |
| 72 | 82 |
| 73 return autolaunch.find(chrome_exe.value()) != string16::npos; | 83 if (autolaunch.find(chrome_exe.value()) == string16::npos) |
| 84 return false; | |
| 85 | |
| 86 return command_line_switch.empty() || | |
| 87 autolaunch.find(ASCIIToUTF16(command_line_switch)) != string16::npos; | |
| 74 } | 88 } |
| 75 | 89 |
| 76 void SetWillLaunchAtLogin(bool auto_launch, | 90 void SetWillLaunchAtLogin(const FilePath& application_path, |
| 77 const FilePath& application_path, | 91 const string16& profile_directory, |
| 78 const string16& profile_directory) { | 92 FlagSetting auto_start_feature, |
| 93 FlagSetting background_mode) { | |
| 79 string16 key_name(ProfileToKeyName(profile_directory)); | 94 string16 key_name(ProfileToKeyName(profile_directory)); |
| 80 | 95 |
| 96 // Check which feature should be enabled. | |
| 97 bool auto_start = | |
| 98 auto_start_feature == FLAG_ENABLE || | |
| 99 (auto_start_feature == FLAG_PRESERVE && | |
| 100 WillLaunchAtLoginWithSwitch(application_path, | |
| 101 profile_directory, | |
| 102 switches::kAutoLaunchAtStartup)); | |
| 103 bool in_background = | |
| 104 background_mode == FLAG_ENABLE || | |
| 105 (background_mode == FLAG_PRESERVE && | |
| 106 WillLaunchAtLoginWithSwitch(application_path, | |
| 107 profile_directory, | |
| 108 switches::kNoStartupWindow)); | |
| 109 | |
| 81 // TODO(finnur): Convert this into a shortcut, instead of using the Run key. | 110 // TODO(finnur): Convert this into a shortcut, instead of using the Run key. |
| 82 if (auto_launch) { | 111 if (auto_start || in_background) { |
| 83 FilePath path(application_path); | 112 FilePath path(application_path); |
| 84 if (path.empty()) { | 113 if (path.empty()) { |
| 85 if (!PathService::Get(base::DIR_EXE, &path)) { | 114 if (!PathService::Get(base::DIR_EXE, &path)) { |
| 86 NOTREACHED(); | 115 NOTREACHED(); |
| 87 return; | 116 return; |
| 88 } | 117 } |
| 89 } | 118 } |
| 90 string16 cmd_line = ASCIIToUTF16("\""); | 119 string16 cmd_line = ASCIIToUTF16("\""); |
| 91 cmd_line += path.value(); | 120 cmd_line += path.value(); |
| 92 cmd_line += ASCIIToUTF16("\\"); | 121 cmd_line += ASCIIToUTF16("\\"); |
| 93 cmd_line += installer::kChromeExe; | 122 cmd_line += installer::kChromeExe; |
| 94 cmd_line += ASCIIToUTF16("\" --"); | 123 cmd_line += ASCIIToUTF16("\""); |
| 95 cmd_line += ASCIIToUTF16(switches::kAutoLaunchAtStartup); | |
| 96 | 124 |
| 97 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 125 if (in_background) { |
| 98 if (command_line.HasSwitch(switches::kUserDataDir)) { | |
| 99 cmd_line += ASCIIToUTF16(" --"); | 126 cmd_line += ASCIIToUTF16(" --"); |
| 100 cmd_line += ASCIIToUTF16(switches::kUserDataDir); | 127 cmd_line += ASCIIToUTF16(switches::kNoStartupWindow); |
| 128 } | |
| 129 if (auto_start) { | |
| 130 cmd_line += ASCIIToUTF16(" --"); | |
| 131 cmd_line += ASCIIToUTF16(switches::kAutoLaunchAtStartup); | |
| 132 | |
| 133 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
| 134 if (command_line.HasSwitch(switches::kUserDataDir)) { | |
| 135 cmd_line += ASCIIToUTF16(" --"); | |
| 136 cmd_line += ASCIIToUTF16(switches::kUserDataDir); | |
| 137 cmd_line += ASCIIToUTF16("=\""); | |
| 138 cmd_line += | |
| 139 command_line.GetSwitchValuePath(switches::kUserDataDir).value(); | |
| 140 cmd_line += ASCIIToUTF16("\""); | |
| 141 } | |
| 142 | |
| 143 cmd_line += ASCIIToUTF16(" --"); | |
| 144 cmd_line += ASCIIToUTF16(switches::kProfileDirectory); | |
| 101 cmd_line += ASCIIToUTF16("=\""); | 145 cmd_line += ASCIIToUTF16("=\""); |
| 102 cmd_line += | 146 cmd_line += profile_directory; |
| 103 command_line.GetSwitchValuePath(switches::kUserDataDir).value(); | |
| 104 cmd_line += ASCIIToUTF16("\""); | 147 cmd_line += ASCIIToUTF16("\""); |
| 105 } | 148 } |
| 106 | 149 |
| 107 cmd_line += ASCIIToUTF16(" --"); | |
| 108 cmd_line += ASCIIToUTF16(switches::kProfileDirectory); | |
| 109 cmd_line += ASCIIToUTF16("=\""); | |
| 110 cmd_line += profile_directory; | |
| 111 cmd_line += ASCIIToUTF16("\""); | |
| 112 | |
| 113 base::win::AddCommandToAutoRun( | 150 base::win::AddCommandToAutoRun( |
| 114 HKEY_CURRENT_USER, key_name, cmd_line); | 151 HKEY_CURRENT_USER, key_name, cmd_line); |
| 115 } else { | 152 } else { |
| 116 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, key_name); | 153 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, key_name); |
| 117 } | 154 } |
| 118 } | 155 } |
| 119 | 156 |
| 157 void DisableAllAutostartFeatures(const string16& profile_directory) { | |
| 158 DisableAutoStartAtLogin(profile_directory); | |
| 159 DisableBackgroundModeAtLogin(profile_directory); | |
| 160 } | |
| 161 | |
| 162 void EnableAutoStartAtLogin(const FilePath& application_path, | |
| 163 const string16& profile_directory) { | |
| 164 SetWillLaunchAtLogin( | |
| 165 application_path, profile_directory, FLAG_ENABLE, FLAG_PRESERVE); | |
| 166 } | |
| 167 | |
| 168 void DisableAutoStartAtLogin(const string16& profile_directory) { | |
| 169 SetWillLaunchAtLogin( | |
| 170 FilePath(), profile_directory, FLAG_DISABLE, FLAG_PRESERVE); | |
| 171 } | |
| 172 | |
| 173 void EnableBackgroundModeAtLogin(const FilePath& application_path, | |
| 174 const string16& profile_directory) { | |
| 175 SetWillLaunchAtLogin( | |
| 176 application_path, profile_directory, FLAG_PRESERVE, FLAG_ENABLE); | |
| 177 } | |
| 178 | |
| 179 void DisableBackgroundModeAtLogin(const string16& profile_directory) { | |
|
rpetterson
2012/04/17 01:02:11
I'm concerned this won't have the effect you're lo
Finnur
2012/04/17 12:27:45
If you look at SetWillLaunchAtLogin you'll see tha
| |
| 180 SetWillLaunchAtLogin( | |
| 181 FilePath(), profile_directory, FLAG_PRESERVE, FLAG_DISABLE); | |
| 182 } | |
| 183 | |
| 120 } // namespace auto_launch_util | 184 } // namespace auto_launch_util |
| OLD | NEW |