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

Side by Side Diff: chrome/installer/util/shell_util.h

Issue 10836247: Refactor ShellUtil shortcut code -- single multi-purpose methods as opposed to many slighlty diffe… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: adress comments about shell_util interface Created 8 years, 2 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 // This file declares methods that are useful for integrating Chrome in 5 // This file declares methods that are useful for integrating Chrome in
6 // Windows shell. These methods are all static and currently part of 6 // Windows shell. These methods are all static and currently part of
7 // ShellUtil class. 7 // ShellUtil class.
8 8
9 #ifndef CHROME_INSTALLER_UTIL_SHELL_UTIL_H_ 9 #ifndef CHROME_INSTALLER_UTIL_SHELL_UTIL_H_
10 #define CHROME_INSTALLER_UTIL_SHELL_UTIL_H_ 10 #define CHROME_INSTALLER_UTIL_SHELL_UTIL_H_
11 11
12 #include <windows.h> 12 #include <windows.h>
13 13
14 #include <map> 14 #include <map>
15 #include <vector> 15 #include <vector>
16 16
17 #include "base/basictypes.h" 17 #include "base/basictypes.h"
18 #include "base/file_path.h"
18 #include "base/string16.h" 19 #include "base/string16.h"
19 #include "chrome/installer/util/work_item_list.h" 20 #include "chrome/installer/util/work_item_list.h"
20 21
21 class BrowserDistribution; 22 class BrowserDistribution;
22 class FilePath;
23
24 namespace base {
25 class DictionaryValue;
26 }
27 23
28 // This is a utility class that provides common shell integration methods 24 // This is a utility class that provides common shell integration methods
29 // that can be used by installer as well as Chrome. 25 // that can be used by installer as well as Chrome.
30 class ShellUtil { 26 class ShellUtil {
31 public: 27 public:
32 // Input to any methods that make changes to OS shell. 28 // Input to any methods that make changes to OS shell.
33 enum ShellChange { 29 enum ShellChange {
34 CURRENT_USER = 0x1, // Make any shell changes only at the user level 30 CURRENT_USER = 0x1, // Make any shell changes only at the user level
35 SYSTEM_LEVEL = 0x2 // Make any shell changes only at the system level 31 SYSTEM_LEVEL = 0x2 // Make any shell changes only at the system level
36 }; 32 };
37 33
38 enum VerifyShortcutStatus { 34 // Typical shortcut directories. Resolved in GetShortcutPath().
39 VERIFY_SHORTCUT_SUCCESS = 0, 35 enum ChromeShortcutLocation {
40 VERIFY_SHORTCUT_FAILURE_UNEXPECTED, 36 SHORTCUT_DESKTOP,
41 VERIFY_SHORTCUT_FAILURE_PATH, 37 SHORTCUT_QUICK_LAUNCH,
42 VERIFY_SHORTCUT_FAILURE_DESCRIPTION, 38 SHORTCUT_START_MENU,
43 VERIFY_SHORTCUT_FAILURE_ICON_INDEX, 39 };
40
41 enum ChromeShortcutOperation {
42 // Create a new shortcut (overwriting if necessary).
43 SHORTCUT_CREATE_ALWAYS,
44 // Create the per-user shortcut only if its system-level equivalent is not
45 // present.
46 SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL,
47 // Overwrite an existing shortcut (fail if the shortcut doesn't exist).
48 SHORTCUT_REPLACE_EXISTING,
49 // Update specified properties only on an existing shortcut.
50 SHORTCUT_UPDATE_EXISTING,
51 };
52
53 // Properties for shortcuts to chrome.exe. Properties set will be applied to
54 // the shortcut on creation/update. On update, unset properties are ignored;
55 // on create unset properties might have a default value (see individual
56 // property setters below for details).
57 // Callers are encouraged to use the setters provided which take care of
58 // setting |options| as desired.
59 struct ChromeShortcutProperties {
60 enum ChromeIndividualProperties {
61 PROPERTIES_CHROME_EXE = 1 << 0,
62 PROPERTIES_ARGUMENTS = 1 << 1,
63 PROPERTIES_DESCRIPTION = 1 << 2,
64 PROPERTIES_ICON = 1 << 3,
65 PROPERTIES_APP_ID = 1 << 4,
66 PROPERTIES_SHORTCUT_NAME = 1 << 5,
67 PROPERTIES_DUAL_MODE = 1 << 6,
68 };
69
70 explicit ChromeShortcutProperties(ShellChange level_in)
71 : level(level_in), dual_mode(false), pin_to_taskbar(false),
72 options(0U) {}
73
74 // Sets the chrome.exe to launch from this shortcut. This is mandatory when
75 // creating a shortcut.
76 void set_chrome_exe(const FilePath& chrome_exe_in) {
77 chrome_exe = chrome_exe_in;
78 options |= PROPERTIES_CHROME_EXE;
79 }
80
81 // Sets the arguments to be passed to |chrome_exe| when launching from this
82 // shortcut.
83 // The length of this string must be less than MAX_PATH.
84 void set_arguments(const string16& arguments_in) {
85 arguments = arguments_in;
86 options |= PROPERTIES_ARGUMENTS;
87 }
88
89 // Sets the localized description of the shortcut.
90 // Default for chrome.exe: the current distribution's description.
robertshield 2012/10/04 00:25:23 Isn't this in fact the default for SHORTCUT_CREATE
gab 2012/10/04 04:10:32 Whoops forgot "for chrome.exe" on this one, remove
91 // The length of this string must be less than MAX_PATH.
robertshield 2012/10/04 00:25:23 Where is this MAX_PATH restriction enforced? Shoul
gab 2012/10/04 04:10:32 MAX_PATH is enforced in base::win::shortcut.h, I b
92 void set_description(const string16& description_in) {
93 description = description_in;
94 options |= PROPERTIES_DESCRIPTION;
95 }
96
97 // Sets the path to the icon (icon_index set to 0).
98 // Default: chrome.exe (icon_index defaults to the current distribution's
99 // icon index unless otherwise specified in master_preferences).
100 void set_icon(const FilePath& icon_in) {
101 icon = icon_in;
102 options |= PROPERTIES_ICON;
103 }
104
105 // Sets the app model id for the shortcut (Win7+).
106 // Default: the browser model id for the current install.
107 void set_app_id(const string16& app_id_in) {
108 app_id = app_id_in;
109 options |= PROPERTIES_APP_ID;
110 }
111
112 // Forces the shortcut's name to |shortcut_name_in|.
113 // Default: the current distribution's GetAppShortcutName().
114 // The ".lnk" extension will automatically be added to this name.
115 void set_shortcut_name(const string16& shortcut_name_in) {
116 shortcut_name = shortcut_name_in;
117 options |= PROPERTIES_SHORTCUT_NAME;
118 }
119
120 // Sets whether this is a dual mode shortcut (Win8+).
121 void set_dual_mode(bool dual_mode_in) {
122 dual_mode = dual_mode_in;
123 options |= PROPERTIES_DUAL_MODE;
124 }
125
126 // Sets whether to pin this shortcut to the taskbar after creating it
127 // (ignored if the shortcut is only being updated).
128 // Note: This property doesn't have a mask in |options|.
129 void set_pin_to_taskbar(bool pin_to_taskbar_in) {
130 pin_to_taskbar = pin_to_taskbar_in;
131 }
132
133 // The level to install this shortcut at (CURRENT_USER for a per-user
134 // shortcut and SYSTEM_LEVEL for an all-users shortcut).
135 ShellChange level;
136
137 FilePath chrome_exe;
138 string16 arguments;
139 string16 description;
140 FilePath icon;
141 string16 app_id;
142 string16 shortcut_name;
143 bool dual_mode;
144 bool pin_to_taskbar;
145 // Bitfield made of IndividualProperties. Properties set in |options| will
146 // be used to create/update the shortcut, others will be ignored on update
147 // and possibly replaced by default values on create (see individual
148 // property setters above for details on default values).
149 uint32 options;
44 }; 150 };
45 151
46 // Relative path of the URL Protocol registry entry (prefixed with '\'). 152 // Relative path of the URL Protocol registry entry (prefixed with '\').
47 static const wchar_t* kRegURLProtocol; 153 static const wchar_t* kRegURLProtocol;
48 154
49 // Relative path of DefaultIcon registry entry (prefixed with '\'). 155 // Relative path of DefaultIcon registry entry (prefixed with '\').
50 static const wchar_t* kRegDefaultIcon; 156 static const wchar_t* kRegDefaultIcon;
51 157
52 // Relative path of "shell" registry key. 158 // Relative path of "shell" registry key.
53 static const wchar_t* kRegShellPath; 159 static const wchar_t* kRegShellPath;
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 // Registry value name for the OpenWithProgids entry for file associations. 241 // Registry value name for the OpenWithProgids entry for file associations.
136 static const wchar_t* kRegOpenWithProgids; 242 static const wchar_t* kRegOpenWithProgids;
137 243
138 // Returns true if |chrome_exe| is registered in HKLM with |suffix|. 244 // Returns true if |chrome_exe| is registered in HKLM with |suffix|.
139 // Note: This only checks one deterministic key in HKLM for |chrome_exe| and 245 // Note: This only checks one deterministic key in HKLM for |chrome_exe| and
140 // doesn't otherwise validate a full Chrome install in HKLM. 246 // doesn't otherwise validate a full Chrome install in HKLM.
141 static bool QuickIsChromeRegisteredInHKLM(BrowserDistribution* dist, 247 static bool QuickIsChromeRegisteredInHKLM(BrowserDistribution* dist,
142 const string16& chrome_exe, 248 const string16& chrome_exe,
143 const string16& suffix); 249 const string16& suffix);
144 250
145 // Creates Chrome shortcut on the Desktop. 251 // Sets |path| to the path for a shortcut at the |location| desired for the
252 // given |level| (CURRENT_USER for per-user path and SYSTEM_LEVEL for
253 // all-users path).
254 // Returns false on failure.
255 static bool GetShortcutPath(ChromeShortcutLocation location,
256 BrowserDistribution* dist,
257 ShellChange level,
258 FilePath* path);
259
260 // Updates Chrome shortcut in |location| (or creates it if |options| specify
261 // SHORTCUT_CREATE_ALWAYS).
146 // |dist| gives the type of browser distribution currently in use. 262 // |dist| gives the type of browser distribution currently in use.
147 // |chrome_exe| provides the target path information. 263 // |properties| and |operation| affect this method as described on their
148 // |description| provides the shortcut's "comment" property. 264 // invidividual definitions above.
149 // |appended_name| provides a string to be appended to the distribution name, 265 static bool CreateOrUpdateChromeShortcut(
150 // and can be the empty string. 266 ChromeShortcutLocation location,
151 // |arguments| gives a set of arguments to be passed to the executable. 267 BrowserDistribution* dist,
152 // |icon_path| provides the path to the icon file to use. 268 const ChromeShortcutProperties& properties,
153 // |icon_index| provides the index of the icon within the provided icon file. 269 ChromeShortcutOperation operation);
154 // If |shell_change| is CURRENT_USER, the shortcut is created in the
155 // Desktop folder of current user's profile.
156 // If |shell_change| is SYSTEM_LEVEL, the shortcut is created in the
157 // Desktop folder of the "All Users" profile.
158 // |options|: bitfield for which the options come from ChromeShortcutOptions.
159 // Returns true iff the method causes a shortcut to be created / updated.
160 static bool CreateChromeDesktopShortcut(BrowserDistribution* dist,
161 const string16& chrome_exe,
162 const string16& description,
163 const string16& appended_name,
164 const string16& arguments,
165 const string16& icon_path,
166 int icon_index,
167 ShellChange shell_change,
168 uint32 options);
169
170 // Create Chrome shortcut on Quick Launch Bar.
171 // If shell_change is CURRENT_USER, the shortcut is created in the
172 // Quick Launch folder of current user's profile.
173 // If shell_change is SYSTEM_LEVEL, the shortcut is created in the
174 // Quick Launch folder of "Default User" profile. This will make sure
175 // that this shortcut will be seen by all the new users logging into the
176 // system.
177 // |options|: bitfield for which the options come from ChromeShortcutOptions.
178 static bool CreateChromeQuickLaunchShortcut(BrowserDistribution* dist,
179 const string16& chrome_exe,
180 int shell_change,
181 uint32 options);
182 270
183 // This method appends the Chrome icon index inside chrome.exe to the 271 // This method appends the Chrome icon index inside chrome.exe to the
184 // chrome.exe path passed in as input, to generate the full path for 272 // chrome.exe path passed in as input, to generate the full path for
185 // Chrome icon that can be used as value for Windows registry keys. 273 // Chrome icon that can be used as value for Windows registry keys.
186 // |chrome_exe| full path to chrome.exe. 274 // |chrome_exe| full path to chrome.exe.
187 static string16 GetChromeIcon(BrowserDistribution* dist, 275 static string16 GetChromeIcon(BrowserDistribution* dist,
188 const string16& chrome_exe); 276 const string16& chrome_exe);
189 277
190 // This method returns the command to open URLs/files using chrome. Typically 278 // This method returns the command to open URLs/files using chrome. Typically
191 // this command is written to the registry under shell\open\command key. 279 // this command is written to the registry under shell\open\command key.
192 // |chrome_exe|: the full path to chrome.exe 280 // |chrome_exe|: the full path to chrome.exe
193 static string16 GetChromeShellOpenCmd(const string16& chrome_exe); 281 static string16 GetChromeShellOpenCmd(const string16& chrome_exe);
194 282
195 // This method returns the command to be called by the DelegateExecute verb 283 // This method returns the command to be called by the DelegateExecute verb
196 // handler to launch chrome on Windows 8. Typically this command is written to 284 // handler to launch chrome on Windows 8. Typically this command is written to
197 // the registry under the HKCR\Chrome\.exe\shell\(open|run)\command key. 285 // the registry under the HKCR\Chrome\.exe\shell\(open|run)\command key.
198 // |chrome_exe|: the full path to chrome.exe 286 // |chrome_exe|: the full path to chrome.exe
199 static string16 GetChromeDelegateCommand(const string16& chrome_exe); 287 static string16 GetChromeDelegateCommand(const string16& chrome_exe);
200 288
201 // Returns the localized name of Chrome shortcut in |shortcut|. If
202 // |appended_name| is not empty, it is included in the shortcut name. If
203 // |alternate| is true, a second localized text that is better suited for
204 // certain scenarios is used.
205 static bool GetChromeShortcutName(BrowserDistribution* dist,
206 bool alternate,
207 const string16& appended_name,
208 string16* shortcut);
209
210 // Gets the desktop path for the current user or all users (if system_level
211 // is true) and returns it in 'path' argument. Return true if successful,
212 // otherwise returns false.
213 static bool GetDesktopPath(bool system_level, FilePath* path);
214
215 // Gets the Quick Launch shortcuts path for the current user and
216 // returns it in 'path' argument. Return true if successful, otherwise
217 // returns false. If system_level is true this function returns the path
218 // to Default Users Quick Launch shortcuts path. Adding a shortcut to Default
219 // User's profile only affects any new user profiles (not existing ones).
220 static bool GetQuickLaunchPath(bool system_level, FilePath* path);
221
222 // Gets a mapping of all registered browser names (excluding browsers in the 289 // Gets a mapping of all registered browser names (excluding browsers in the
223 // |dist| distribution) and their reinstall command (which usually sets 290 // |dist| distribution) and their reinstall command (which usually sets
224 // browser as default). 291 // browser as default).
225 // Given browsers can be registered in HKCU (as of Win7) and/or in HKLM, this 292 // Given browsers can be registered in HKCU (as of Win7) and/or in HKLM, this
226 // method looks in both and gives precedence to values in HKCU as per the msdn 293 // method looks in both and gives precedence to values in HKCU as per the msdn
227 // standard: http://goo.gl/xjczJ. 294 // standard: http://goo.gl/xjczJ.
228 static void GetRegisteredBrowsers(BrowserDistribution* dist, 295 static void GetRegisteredBrowsers(BrowserDistribution* dist,
229 std::map<string16, string16>* browsers); 296 std::map<string16, string16>* browsers);
230 297
231 // Returns the suffix this user's Chrome install is registered with. 298 // Returns the suffix this user's Chrome install is registered with.
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 // to default browser entries names that it creates in the registry. 438 // to default browser entries names that it creates in the registry.
372 // |protocol| The protocol to register as being capable of handling.s 439 // |protocol| The protocol to register as being capable of handling.s
373 // |elevate_if_not_admin| if true will make this method try alternate methods 440 // |elevate_if_not_admin| if true will make this method try alternate methods
374 // as described above. 441 // as described above.
375 static bool RegisterChromeForProtocol(BrowserDistribution* dist, 442 static bool RegisterChromeForProtocol(BrowserDistribution* dist,
376 const string16& chrome_exe, 443 const string16& chrome_exe,
377 const string16& unique_suffix, 444 const string16& unique_suffix,
378 const string16& protocol, 445 const string16& protocol,
379 bool elevate_if_not_admin); 446 bool elevate_if_not_admin);
380 447
381 // Remove Chrome shortcut from Desktop. 448 // Removes installed Chrome shortcut at |location|.
382 // If |shell_change| is CURRENT_USER, the shortcut is removed from the 449 // |level|: CURRENT_USER to remove the per-user shortcut and SYSTEM_LEVEL to
383 // Desktop folder of current user's profile. 450 // remove the all-users shortcut.
384 // If |shell_change| is SYSTEM_LEVEL, the shortcut is removed from the 451 // |shortcut_name|: If non-null, remove the shortcut named |shortcut_name| at
385 // Desktop folder of "All Users" profile. 452 // location; otherwise remove the default shortcut at |location|.
386 // |options|: bitfield for which the options come from ChromeShortcutOptions. 453 // If |location| is SHORTCUT_START_MENU the shortcut folder specific to |dist|
387 // Only SHORTCUT_ALTERNATE is a valid option for this function. 454 // is deleted.
388 static bool RemoveChromeDesktopShortcut(BrowserDistribution* dist, 455 // Also attempts to unpin the removed shortcut from the taskbar.
389 int shell_change, 456 // Returns true unless an unexpected failure occurs (i.e. returns true even if
390 uint32 options); 457 // no shortcut is found at |location|).
robertshield 2012/10/04 00:25:23 For this last sentence, how about: "Returns true o
gab 2012/10/04 04:10:32 Sure.
391 458 static bool RemoveChromeShortcut(ChromeShortcutLocation location,
392 // Removes a set of existing Chrome desktop shortcuts. |appended_names| is a 459 BrowserDistribution* dist,
393 // list of shortcut file names as obtained from 460 ShellChange level,
394 // ShellUtil::GetChromeShortcutName. 461 const string16* shortcut_name);
395 static bool RemoveChromeDesktopShortcutsWithAppendedNames(
396 const std::vector<string16>& appended_names);
397
398 // Remove Chrome shortcut from Quick Launch Bar.
399 // If shell_change is CURRENT_USER, the shortcut is removed from
400 // the Quick Launch folder of current user's profile.
401 // If shell_change is SYSTEM_LEVEL, the shortcut is removed from
402 // the Quick Launch folder of "Default User" profile.
403 static bool RemoveChromeQuickLaunchShortcut(BrowserDistribution* dist,
404 int shell_change);
405 462
406 // This will remove all secondary tiles from the start screen for |dist|. 463 // This will remove all secondary tiles from the start screen for |dist|.
407 static void RemoveChromeStartScreenShortcuts(BrowserDistribution* dist, 464 static void RemoveChromeStartScreenShortcuts(BrowserDistribution* dist,
408 const string16& chrome_exe); 465 const string16& chrome_exe);
409 466
410 enum ChromeShortcutOptions {
411 SHORTCUT_NO_OPTIONS = 0,
412 // Set DualMode property for Windows 8 Metro-enabled shortcuts.
413 SHORTCUT_DUAL_MODE = 1 << 0,
414 // Create a new shortcut (overwriting if necessary).
415 SHORTCUT_CREATE_ALWAYS = 1 << 1,
416 // Use an alternate application name for the shortcut (e.g. "The Internet").
417 // This option is only applied to the Desktop shortcut.
418 SHORTCUT_ALTERNATE = 1 << 2,
419 };
420
421 // Updates shortcut (or creates a new shortcut) at destination given by
422 // shortcut to a target given by chrome_exe. The arguments are given by
423 // |arguments| for the target and icon is set based on |icon_path| and
424 // |icon_index|. If create_new is set to true, the function will create a new
425 // shortcut if it doesn't exist.
426 // |options|: bitfield for which the options come from ChromeShortcutOptions.
427 // If SHORTCUT_CREATE_ALWAYS is not set in |options|, only specified (non-
428 // null) properties on an existing shortcut will be modified. If the shortcut
429 // does not exist, this method is a no-op and returns false.
430 static bool UpdateChromeShortcut(BrowserDistribution* dist,
431 const string16& chrome_exe,
432 const string16& shortcut,
433 const string16& arguments,
434 const string16& description,
435 const string16& icon_path,
436 int icon_index,
437 uint32 options);
438
439 // Sets |suffix| to the base 32 encoding of the md5 hash of this user's sid 467 // Sets |suffix| to the base 32 encoding of the md5 hash of this user's sid
440 // preceded by a dot. 468 // preceded by a dot.
441 // This is guaranteed to be unique on the machine and 27 characters long 469 // This is guaranteed to be unique on the machine and 27 characters long
442 // (including the '.'). 470 // (including the '.').
443 // This suffix is then meant to be added to all registration that may conflict 471 // This suffix is then meant to be added to all registration that may conflict
444 // with another user-level Chrome install. 472 // with another user-level Chrome install.
445 // Note that prior to Chrome 21, the suffix registered used to be the user's 473 // Note that prior to Chrome 21, the suffix registered used to be the user's
446 // username (see GetOldUserSpecificRegistrySuffix() below). We still honor old 474 // username (see GetOldUserSpecificRegistrySuffix() below). We still honor old
447 // installs registered that way, but it was wrong because some of the 475 // installs registered that way, but it was wrong because some of the
448 // characters allowed in a username are not allowed in a ProgId. 476 // characters allowed in a username are not allowed in a ProgId.
(...skipping 16 matching lines...) Expand all
465 // required by the base32 standard for inputs that aren't a multiple of 5 493 // required by the base32 standard for inputs that aren't a multiple of 5
466 // bytes. 494 // bytes.
467 static string16 ByteArrayToBase32(const uint8* bytes, size_t size); 495 static string16 ByteArrayToBase32(const uint8* bytes, size_t size);
468 496
469 private: 497 private:
470 DISALLOW_COPY_AND_ASSIGN(ShellUtil); 498 DISALLOW_COPY_AND_ASSIGN(ShellUtil);
471 }; 499 };
472 500
473 501
474 #endif // CHROME_INSTALLER_UTIL_SHELL_UTIL_H_ 502 #endif // CHROME_INSTALLER_UTIL_SHELL_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698