Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: chrome/installer/util/auto_launch_util.cc

Issue 9972012: Resolve the conflict that auto-launch has with the background mode feature (part 1). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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_constants.h"
14 #include "chrome/common/chrome_switches.h" 15 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/chrome_version_info.h" 16 #include "chrome/common/chrome_version_info.h"
16 #include "chrome/installer/util/browser_distribution.h" 17 #include "chrome/installer/util/browser_distribution.h"
17 #include "chrome/installer/util/product.h" 18 #include "chrome/installer/util/product.h"
18 #include "chrome/installer/util/util_constants.h" 19 #include "chrome/installer/util/util_constants.h"
19 #include "crypto/sha2.h" 20 #include "crypto/sha2.h"
20 21
21 namespace auto_launch_util { 22 namespace auto_launch_util {
22 23
23 // The prefix of the Chrome Auto-launch key under the Run key. 24 // The prefix of the Chrome Auto-launch key under the Run key.
24 const wchar_t kAutolaunchKeyValue[] = L"GoogleChromeAutoLaunch"; 25 const wchar_t kAutolaunchKeyValue[] = L"GoogleChromeAutoLaunch";
25 26
27 // We use one Run key with flags specifying which feature we want to start up.
28 // When we change our Run key we need to specify what we want to do with each
29 // flag. This lists the possible actions we can take with the flags.
30 enum FlagSetting {
31 FLAG_DISABLE, // Disable the flag.
32 FLAG_ENABLE, // Enable the flag.
33 FLAG_PRESERVE, // Preserve the value that the flag has currently.
34 };
35
26 // A helper function that takes a |profile_path| and builds a registry key 36 // 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 37 // 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 38 // 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 39 // installations of Chrome don't conflict, and so the in the future different
30 // profiles can be auto-launched (or not) separately). 40 // profiles can be auto-launched (or not) separately).
31 string16 ProfileToKeyName(const string16& profile_directory) { 41 string16 ProfileToKeyName(const string16& profile_directory) {
32 FilePath path; 42 FilePath path;
33 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 43 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
34 if (command_line.HasSwitch(switches::kUserDataDir)) { 44 if (command_line.HasSwitch(switches::kUserDataDir)) {
35 path = command_line.GetSwitchValuePath(switches::kUserDataDir); 45 path = command_line.GetSwitchValuePath(switches::kUserDataDir);
36 } else { 46 } else {
37 // Get the path from the same source as the installer, to make sure there 47 // Get the path from the same source as the installer, to make sure there
38 // are no differences. 48 // are no differences.
39 BrowserDistribution* distribution = 49 BrowserDistribution* distribution =
40 BrowserDistribution::GetSpecificDistribution( 50 BrowserDistribution::GetSpecificDistribution(
41 BrowserDistribution::CHROME_BROWSER); 51 BrowserDistribution::CHROME_BROWSER);
42 installer::Product product(distribution); 52 installer::Product product(distribution);
43 path = product.GetUserDataPath(); 53 path = product.GetUserDataPath();
44 } 54 }
45 path = path.Append(profile_directory); 55 path = path.Append(profile_directory);
46 56
47 std::string input(path.AsUTF8Unsafe()); 57 std::string input(path.AsUTF8Unsafe());
48 uint8 hash[16]; 58 uint8 hash[16];
49 crypto::SHA256HashString(input, hash, sizeof(hash)); 59 crypto::SHA256HashString(input, hash, sizeof(hash));
50 std::string hash_string = base::HexEncode(hash, sizeof(hash)); 60 std::string hash_string = base::HexEncode(hash, sizeof(hash));
51 return string16(kAutolaunchKeyValue) + 61 return string16(kAutolaunchKeyValue) +
52 ASCIIToWide("_") + ASCIIToWide(hash_string); 62 ASCIIToWide("_") + ASCIIToWide(hash_string);
53 } 63 }
54 64
55 bool WillLaunchAtLogin(const FilePath& application_path, 65 // Returns whether the Chrome executable specified in |application_path| is set
56 const string16& profile_directory) { 66 // to auto-launch at computer startup with a given |command_line_switch|.
67 // NOTE: |application_path| is optional and should be blank in most cases (as
68 // it will default to the application path of the current executable).
69 // |profile_directory| is the name of the directory (leaf, not the full path)
70 // that contains the profile that should be opened at computer startup.
71 // |command_line_switch| is the switch we are optionally interested in and, if
72 // not blank, must be present for the function to return true. If blank, it acts
73 // like a wildcard.
74 bool WillLaunchAtLoginWithSwitch(const FilePath& application_path,
75 const string16& profile_directory,
76 const std::string& command_line_switch) {
57 string16 key_name(ProfileToKeyName(profile_directory)); 77 string16 key_name(ProfileToKeyName(profile_directory));
58 string16 autolaunch; 78 string16 autolaunch;
59 if (!base::win::ReadCommandFromAutoRun( 79 if (!base::win::ReadCommandFromAutoRun(
60 HKEY_CURRENT_USER, key_name, &autolaunch)) { 80 HKEY_CURRENT_USER, key_name, &autolaunch)) {
61 return false; 81 return false;
62 } 82 }
63 83
64 FilePath chrome_exe(application_path); 84 FilePath chrome_exe(application_path);
65 if (chrome_exe.empty()) { 85 if (chrome_exe.empty()) {
66 if (!PathService::Get(base::DIR_EXE, &chrome_exe)) { 86 if (!PathService::Get(base::DIR_EXE, &chrome_exe)) {
67 NOTREACHED(); 87 NOTREACHED();
68 return false; 88 return false;
69 } 89 }
70 } 90 }
71 chrome_exe = chrome_exe.Append(installer::kChromeExe); 91 chrome_exe = chrome_exe.Append(installer::kChromeExe);
72 92
73 return autolaunch.find(chrome_exe.value()) != string16::npos; 93 if (autolaunch.find(chrome_exe.value()) == string16::npos)
94 return false;
95
96 return command_line_switch.empty() ||
97 autolaunch.find(ASCIIToUTF16(command_line_switch)) != string16::npos;
74 } 98 }
75 99
76 void SetWillLaunchAtLogin(bool auto_launch, 100 bool LaunchWithWindowAtLoginRequested(const FilePath& application_path,
77 const FilePath& application_path, 101 const string16& profile_directory) {
78 const string16& profile_directory) { 102 return WillLaunchAtLoginWithSwitch(application_path,
103 profile_directory,
104 switches::kAutoLaunchAtStartup);
105 }
106
107 bool LaunchInBackgroundAtLoginRequested(const FilePath& application_path) {
108 // Background mode isn't profile specific, but is attached to the Run key for
109 // the Default profile.
110 return WillLaunchAtLoginWithSwitch(application_path,
111 ASCIIToUTF16(chrome::kInitialProfile),
112 switches::kNoStartupWindow);
113 }
114
115 void SetWillLaunchAtLogin(const FilePath& application_path,
116 const string16& profile_directory,
117 FlagSetting auto_start_feature,
118 FlagSetting background_mode) {
79 string16 key_name(ProfileToKeyName(profile_directory)); 119 string16 key_name(ProfileToKeyName(profile_directory));
80 120
121 // Check which feature should be enabled.
122 bool auto_start =
123 auto_start_feature == FLAG_ENABLE ||
124 (auto_start_feature == FLAG_PRESERVE &&
125 WillLaunchAtLoginWithSwitch(application_path,
126 profile_directory,
127 switches::kAutoLaunchAtStartup));
128 bool in_background =
129 background_mode == FLAG_ENABLE ||
130 (background_mode == FLAG_PRESERVE &&
131 WillLaunchAtLoginWithSwitch(application_path,
132 profile_directory,
133 switches::kNoStartupWindow));
134
81 // TODO(finnur): Convert this into a shortcut, instead of using the Run key. 135 // TODO(finnur): Convert this into a shortcut, instead of using the Run key.
82 if (auto_launch) { 136 if (auto_start || in_background) {
83 FilePath path(application_path); 137 FilePath path(application_path);
84 if (path.empty()) { 138 if (path.empty()) {
85 if (!PathService::Get(base::DIR_EXE, &path)) { 139 if (!PathService::Get(base::DIR_EXE, &path)) {
86 NOTREACHED(); 140 NOTREACHED();
87 return; 141 return;
88 } 142 }
89 } 143 }
90 string16 cmd_line = ASCIIToUTF16("\""); 144 string16 cmd_line = ASCIIToUTF16("\"");
91 cmd_line += path.value(); 145 cmd_line += path.value();
92 cmd_line += ASCIIToUTF16("\\"); 146 cmd_line += ASCIIToUTF16("\\");
93 cmd_line += installer::kChromeExe; 147 cmd_line += installer::kChromeExe;
94 cmd_line += ASCIIToUTF16("\" --"); 148 cmd_line += ASCIIToUTF16("\"");
95 cmd_line += ASCIIToUTF16(switches::kAutoLaunchAtStartup);
96 149
97 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 150 if (in_background) {
98 if (command_line.HasSwitch(switches::kUserDataDir)) {
99 cmd_line += ASCIIToUTF16(" --"); 151 cmd_line += ASCIIToUTF16(" --");
100 cmd_line += ASCIIToUTF16(switches::kUserDataDir); 152 cmd_line += ASCIIToUTF16(switches::kNoStartupWindow);
153 }
154 if (auto_start) {
155 cmd_line += ASCIIToUTF16(" --");
156 cmd_line += ASCIIToUTF16(switches::kAutoLaunchAtStartup);
157
158 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
159 if (command_line.HasSwitch(switches::kUserDataDir)) {
160 cmd_line += ASCIIToUTF16(" --");
161 cmd_line += ASCIIToUTF16(switches::kUserDataDir);
162 cmd_line += ASCIIToUTF16("=\"");
163 cmd_line +=
164 command_line.GetSwitchValuePath(switches::kUserDataDir).value();
165 cmd_line += ASCIIToUTF16("\"");
166 }
167
168 cmd_line += ASCIIToUTF16(" --");
169 cmd_line += ASCIIToUTF16(switches::kProfileDirectory);
101 cmd_line += ASCIIToUTF16("=\""); 170 cmd_line += ASCIIToUTF16("=\"");
102 cmd_line += 171 cmd_line += profile_directory;
103 command_line.GetSwitchValuePath(switches::kUserDataDir).value();
104 cmd_line += ASCIIToUTF16("\""); 172 cmd_line += ASCIIToUTF16("\"");
105 } 173 }
106 174
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( 175 base::win::AddCommandToAutoRun(
114 HKEY_CURRENT_USER, key_name, cmd_line); 176 HKEY_CURRENT_USER, key_name, cmd_line);
115 } else { 177 } else {
116 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, key_name); 178 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, key_name);
117 } 179 }
118 } 180 }
119 181
182 void DisableAllAutostartFeatures(const string16& profile_directory) {
183 DisableAutoStartAtLogin(profile_directory);
184 DisableBackgroundModeAtLogin();
185 }
186
187 void EnableAutoStartAtLogin(const FilePath& application_path,
188 const string16& profile_directory) {
189 SetWillLaunchAtLogin(
190 application_path, profile_directory, FLAG_ENABLE, FLAG_PRESERVE);
191 }
192
193 void DisableAutoStartAtLogin(const string16& profile_directory) {
194 SetWillLaunchAtLogin(
195 FilePath(), profile_directory, FLAG_DISABLE, FLAG_PRESERVE);
196 }
197
198 void EnableBackgroundModeAtLogin() {
199 // Background mode isn't profile specific, but we specify the Default profile
200 // just to have a unique Run key to attach it to. FilePath is blank because
201 // this function is not called from the installer (see comments for
202 // EnableAutoStartAtLogin).
203 SetWillLaunchAtLogin(FilePath(),
204 ASCIIToUTF16(chrome::kInitialProfile),
205 FLAG_PRESERVE,
206 FLAG_ENABLE);
207 }
208
209 void DisableBackgroundModeAtLogin() {
210 SetWillLaunchAtLogin(FilePath(),
211 ASCIIToUTF16(chrome::kInitialProfile),
212 FLAG_PRESERVE,
213 FLAG_DISABLE);
214 }
215
120 } // namespace auto_launch_util 216 } // namespace auto_launch_util
OLDNEW
« chrome/installer/util/auto_launch_util.h ('K') | « chrome/installer/util/auto_launch_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698