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

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: robert is the king of pool 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"
19 #include "base/logging.h"
18 #include "base/string16.h" 20 #include "base/string16.h"
19 #include "chrome/installer/util/work_item_list.h" 21 #include "chrome/installer/util/work_item_list.h"
20 22
21 class BrowserDistribution; 23 class BrowserDistribution;
22 class FilePath;
23
24 namespace base {
25 class DictionaryValue;
26 }
27 24
28 // This is a utility class that provides common shell integration methods 25 // This is a utility class that provides common shell integration methods
29 // that can be used by installer as well as Chrome. 26 // that can be used by installer as well as Chrome.
30 class ShellUtil { 27 class ShellUtil {
31 public: 28 public:
32 // Input to any methods that make changes to OS shell. 29 // Input to any methods that make changes to OS shell.
33 enum ShellChange { 30 enum ShellChange {
34 CURRENT_USER = 0x1, // Make any shell changes only at the user level 31 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 32 SYSTEM_LEVEL = 0x2 // Make any shell changes only at the system level
36 }; 33 };
37 34
38 enum VerifyShortcutStatus { 35 // Typical shortcut directories. Resolved in GetShortcutPath().
39 VERIFY_SHORTCUT_SUCCESS = 0, 36 enum ChromeShortcutLocation {
40 VERIFY_SHORTCUT_FAILURE_UNEXPECTED, 37 SHORTCUT_DESKTOP,
41 VERIFY_SHORTCUT_FAILURE_PATH, 38 SHORTCUT_QUICK_LAUNCH,
42 VERIFY_SHORTCUT_FAILURE_DESCRIPTION, 39 SHORTCUT_START_MENU,
43 VERIFY_SHORTCUT_FAILURE_ICON_INDEX, 40 };
41
42 enum ChromeShortcutOperation {
43 // Create a new shortcut (overwriting if necessary).
44 SHORTCUT_CREATE_ALWAYS,
45 // Create the per-user shortcut only if its system-level equivalent is not
46 // present.
47 SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL,
48 // Overwrite an existing shortcut (fail if the shortcut doesn't exist).
49 SHORTCUT_REPLACE_EXISTING,
50 // Update specified properties only on an existing shortcut.
51 SHORTCUT_UPDATE_EXISTING,
52 };
53
54 // Properties for shortcuts to chrome.exe. Properties set will be applied to
55 // the shortcut on creation/update. On update, unset properties are ignored;
56 // on create (and replaced) unset properties might have a default value (see
57 // individual property setters below for details).
58 // Callers are encouraged to use the setters provided which take care of
59 // setting |options| as desired.
60 struct ChromeShortcutProperties {
61 enum ChromeIndividualProperties {
62 PROPERTIES_CHROME_EXE = 1 << 0,
grt (UTC plus 2) 2012/10/04 14:31:22 you could add inline has_foo methods to make check
gab 2012/10/04 16:39:23 Done.
63 PROPERTIES_ARGUMENTS = 1 << 1,
64 PROPERTIES_DESCRIPTION = 1 << 2,
65 PROPERTIES_ICON = 1 << 3,
66 PROPERTIES_APP_ID = 1 << 4,
67 PROPERTIES_SHORTCUT_NAME = 1 << 5,
68 PROPERTIES_DUAL_MODE = 1 << 6,
69 };
70
71 explicit ChromeShortcutProperties(ShellChange level_in)
72 : level(level_in), dual_mode(false), pin_to_taskbar(false),
73 options(0U) {}
74
75 // Sets the chrome.exe to launch from this shortcut. This is mandatory when
76 // creating a shortcut.
77 void set_chrome_exe(const FilePath& chrome_exe_in) {
78 chrome_exe = chrome_exe_in;
79 options |= PROPERTIES_CHROME_EXE;
80 }
81
82 // Sets the arguments to be passed to |chrome_exe| when launching from this
83 // shortcut.
84 // The length of this string must be less than MAX_PATH.
85 void set_arguments(const string16& arguments_in) {
86 // Size restriction as per MSDN at
87 // http://msdn.microsoft.com/library/windows/desktop/bb774954.aspx.
88 DCHECK(arguments_in.length() < MAX_PATH);
89 arguments = arguments_in;
90 options |= PROPERTIES_ARGUMENTS;
91 }
92
93 // Sets the localized description of the shortcut.
94 // Default: the current distribution's description.
95 // The length of this string must be less than MAX_PATH.
96 void set_description(const string16& description_in) {
97 // Size restriction as per MSDN at
98 // http://msdn.microsoft.com/library/windows/desktop/bb774955.aspx.
99 DCHECK(description_in.length() < MAX_PATH);
100 description = description_in;
101 options |= PROPERTIES_DESCRIPTION;
102 }
103
104 // Sets the path to the icon (icon_index set to 0).
105 // Default: chrome.exe (icon_index defaults to the current distribution's
106 // icon index unless otherwise specified in master_preferences).
107 void set_icon(const FilePath& icon_in) {
108 icon = icon_in;
109 options |= PROPERTIES_ICON;
110 }
111
112 // Sets the app model id for the shortcut (Win7+).
113 // Default: the browser model id for the current install.
114 void set_app_id(const string16& app_id_in) {
115 app_id = app_id_in;
116 options |= PROPERTIES_APP_ID;
117 }
118
119 // Forces the shortcut's name to |shortcut_name_in|.
120 // Default: the current distribution's GetAppShortcutName().
121 // The ".lnk" extension will automatically be added to this name.
122 void set_shortcut_name(const string16& shortcut_name_in) {
123 shortcut_name = shortcut_name_in;
124 options |= PROPERTIES_SHORTCUT_NAME;
125 }
126
127 // Sets whether this is a dual mode shortcut (Win8+).
grt (UTC plus 2) 2012/10/04 14:31:22 as discussed: please add to this comment some text
gab 2012/10/04 16:39:23 Done, also made a logic change to now only set dua
128 void set_dual_mode(bool dual_mode_in) {
129 dual_mode = dual_mode_in;
130 options |= PROPERTIES_DUAL_MODE;
131 }
132
133 // Sets whether to pin this shortcut to the taskbar after creating it
134 // (ignored if the shortcut is only being updated).
135 // Note: This property doesn't have a mask in |options|.
136 void set_pin_to_taskbar(bool pin_to_taskbar_in) {
137 pin_to_taskbar = pin_to_taskbar_in;
138 }
139
140 // The level to install this shortcut at (CURRENT_USER for a per-user
141 // shortcut and SYSTEM_LEVEL for an all-users shortcut).
142 ShellChange level;
143
144 FilePath chrome_exe;
145 string16 arguments;
146 string16 description;
147 FilePath icon;
148 string16 app_id;
149 string16 shortcut_name;
150 bool dual_mode;
151 bool pin_to_taskbar;
152 // Bitfield made of IndividualProperties. Properties set in |options| will
153 // be used to create/update the shortcut, others will be ignored on update
154 // and possibly replaced by default values on create (see individual
155 // property setters above for details on default values).
156 uint32 options;
44 }; 157 };
45 158
46 // Relative path of the URL Protocol registry entry (prefixed with '\'). 159 // Relative path of the URL Protocol registry entry (prefixed with '\').
47 static const wchar_t* kRegURLProtocol; 160 static const wchar_t* kRegURLProtocol;
48 161
49 // Relative path of DefaultIcon registry entry (prefixed with '\'). 162 // Relative path of DefaultIcon registry entry (prefixed with '\').
50 static const wchar_t* kRegDefaultIcon; 163 static const wchar_t* kRegDefaultIcon;
51 164
52 // Relative path of "shell" registry key. 165 // Relative path of "shell" registry key.
53 static const wchar_t* kRegShellPath; 166 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. 248 // Registry value name for the OpenWithProgids entry for file associations.
136 static const wchar_t* kRegOpenWithProgids; 249 static const wchar_t* kRegOpenWithProgids;
137 250
138 // Returns true if |chrome_exe| is registered in HKLM with |suffix|. 251 // 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 252 // Note: This only checks one deterministic key in HKLM for |chrome_exe| and
140 // doesn't otherwise validate a full Chrome install in HKLM. 253 // doesn't otherwise validate a full Chrome install in HKLM.
141 static bool QuickIsChromeRegisteredInHKLM(BrowserDistribution* dist, 254 static bool QuickIsChromeRegisteredInHKLM(BrowserDistribution* dist,
142 const string16& chrome_exe, 255 const string16& chrome_exe,
143 const string16& suffix); 256 const string16& suffix);
144 257
145 // Creates Chrome shortcut on the Desktop. 258 // Sets |path| to the path for a shortcut at the |location| desired for the
259 // given |level| (CURRENT_USER for per-user path and SYSTEM_LEVEL for
260 // all-users path).
261 // Returns false on failure.
262 static bool GetShortcutPath(ChromeShortcutLocation location,
263 BrowserDistribution* dist,
264 ShellChange level,
265 FilePath* path);
266
267 // Updates Chrome shortcut in |location| (or creates it if |options| specify
268 // SHORTCUT_CREATE_ALWAYS).
146 // |dist| gives the type of browser distribution currently in use. 269 // |dist| gives the type of browser distribution currently in use.
147 // |chrome_exe| provides the target path information. 270 // |properties| and |operation| affect this method as described on their
148 // |description| provides the shortcut's "comment" property. 271 // invidividual definitions above.
149 // |appended_name| provides a string to be appended to the distribution name, 272 static bool CreateOrUpdateChromeShortcut(
150 // and can be the empty string. 273 ChromeShortcutLocation location,
151 // |arguments| gives a set of arguments to be passed to the executable. 274 BrowserDistribution* dist,
152 // |icon_path| provides the path to the icon file to use. 275 const ChromeShortcutProperties& properties,
153 // |icon_index| provides the index of the icon within the provided icon file. 276 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 277
183 // This method appends the Chrome icon index inside chrome.exe to the 278 // 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 279 // 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. 280 // Chrome icon that can be used as value for Windows registry keys.
186 // |chrome_exe| full path to chrome.exe. 281 // |chrome_exe| full path to chrome.exe.
187 static string16 GetChromeIcon(BrowserDistribution* dist, 282 static string16 GetChromeIcon(BrowserDistribution* dist,
188 const string16& chrome_exe); 283 const string16& chrome_exe);
189 284
190 // This method returns the command to open URLs/files using chrome. Typically 285 // 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. 286 // this command is written to the registry under shell\open\command key.
192 // |chrome_exe|: the full path to chrome.exe 287 // |chrome_exe|: the full path to chrome.exe
193 static string16 GetChromeShellOpenCmd(const string16& chrome_exe); 288 static string16 GetChromeShellOpenCmd(const string16& chrome_exe);
194 289
195 // This method returns the command to be called by the DelegateExecute verb 290 // 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 291 // 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. 292 // the registry under the HKCR\Chrome\.exe\shell\(open|run)\command key.
198 // |chrome_exe|: the full path to chrome.exe 293 // |chrome_exe|: the full path to chrome.exe
199 static string16 GetChromeDelegateCommand(const string16& chrome_exe); 294 static string16 GetChromeDelegateCommand(const string16& chrome_exe);
200 295
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 296 // Gets a mapping of all registered browser names (excluding browsers in the
223 // |dist| distribution) and their reinstall command (which usually sets 297 // |dist| distribution) and their reinstall command (which usually sets
224 // browser as default). 298 // browser as default).
225 // Given browsers can be registered in HKCU (as of Win7) and/or in HKLM, this 299 // 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 300 // method looks in both and gives precedence to values in HKCU as per the msdn
227 // standard: http://goo.gl/xjczJ. 301 // standard: http://goo.gl/xjczJ.
228 static void GetRegisteredBrowsers(BrowserDistribution* dist, 302 static void GetRegisteredBrowsers(BrowserDistribution* dist,
229 std::map<string16, string16>* browsers); 303 std::map<string16, string16>* browsers);
230 304
231 // Returns the suffix this user's Chrome install is registered with. 305 // 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. 445 // to default browser entries names that it creates in the registry.
372 // |protocol| The protocol to register as being capable of handling.s 446 // |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 447 // |elevate_if_not_admin| if true will make this method try alternate methods
374 // as described above. 448 // as described above.
375 static bool RegisterChromeForProtocol(BrowserDistribution* dist, 449 static bool RegisterChromeForProtocol(BrowserDistribution* dist,
376 const string16& chrome_exe, 450 const string16& chrome_exe,
377 const string16& unique_suffix, 451 const string16& unique_suffix,
378 const string16& protocol, 452 const string16& protocol,
379 bool elevate_if_not_admin); 453 bool elevate_if_not_admin);
380 454
381 // Remove Chrome shortcut from Desktop. 455 // Removes installed Chrome shortcut at |location|.
382 // If |shell_change| is CURRENT_USER, the shortcut is removed from the 456 // |level|: CURRENT_USER to remove the per-user shortcut and SYSTEM_LEVEL to
383 // Desktop folder of current user's profile. 457 // remove the all-users shortcut.
384 // If |shell_change| is SYSTEM_LEVEL, the shortcut is removed from the 458 // |shortcut_name|: If non-null, remove the shortcut named |shortcut_name| at
385 // Desktop folder of "All Users" profile. 459 // location; otherwise remove the default shortcut at |location|.
386 // |options|: bitfield for which the options come from ChromeShortcutOptions. 460 // If |location| is SHORTCUT_START_MENU the shortcut folder specific to |dist|
387 // Only SHORTCUT_ALTERNATE is a valid option for this function. 461 // is deleted.
388 static bool RemoveChromeDesktopShortcut(BrowserDistribution* dist, 462 // Also attempts to unpin the removed shortcut from the taskbar.
389 int shell_change, 463 // Returns true on success or if no shortcut is found at |location|.
390 uint32 options); 464 static bool RemoveChromeShortcut(ChromeShortcutLocation location,
391 465 BrowserDistribution* dist,
392 // Removes a set of existing Chrome desktop shortcuts. |appended_names| is a 466 ShellChange level,
393 // list of shortcut file names as obtained from 467 const string16* shortcut_name);
394 // ShellUtil::GetChromeShortcutName.
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 468
406 // This will remove all secondary tiles from the start screen for |dist|. 469 // This will remove all secondary tiles from the start screen for |dist|.
407 static void RemoveChromeStartScreenShortcuts(BrowserDistribution* dist, 470 static void RemoveChromeStartScreenShortcuts(BrowserDistribution* dist,
408 const string16& chrome_exe); 471 const string16& chrome_exe);
409 472
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 473 // Sets |suffix| to the base 32 encoding of the md5 hash of this user's sid
440 // preceded by a dot. 474 // preceded by a dot.
441 // This is guaranteed to be unique on the machine and 27 characters long 475 // This is guaranteed to be unique on the machine and 27 characters long
442 // (including the '.'). 476 // (including the '.').
443 // This suffix is then meant to be added to all registration that may conflict 477 // This suffix is then meant to be added to all registration that may conflict
444 // with another user-level Chrome install. 478 // with another user-level Chrome install.
445 // Note that prior to Chrome 21, the suffix registered used to be the user's 479 // Note that prior to Chrome 21, the suffix registered used to be the user's
446 // username (see GetOldUserSpecificRegistrySuffix() below). We still honor old 480 // username (see GetOldUserSpecificRegistrySuffix() below). We still honor old
447 // installs registered that way, but it was wrong because some of the 481 // installs registered that way, but it was wrong because some of the
448 // characters allowed in a username are not allowed in a ProgId. 482 // 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 499 // required by the base32 standard for inputs that aren't a multiple of 5
466 // bytes. 500 // bytes.
467 static string16 ByteArrayToBase32(const uint8* bytes, size_t size); 501 static string16 ByteArrayToBase32(const uint8* bytes, size_t size);
468 502
469 private: 503 private:
470 DISALLOW_COPY_AND_ASSIGN(ShellUtil); 504 DISALLOW_COPY_AND_ASSIGN(ShellUtil);
471 }; 505 };
472 506
473 507
474 #endif // CHROME_INSTALLER_UTIL_SHELL_UTIL_H_ 508 #endif // CHROME_INSTALLER_UTIL_SHELL_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698