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
3 // found in the LICENSE file.
5 // This file declares methods that are useful for integrating Chrome in
6 // Windows shell. These methods are all static and currently part of
9 #ifndef CHROME_INSTALLER_UTIL_SHELL_UTIL_H_
10 #define CHROME_INSTALLER_UTIL_SHELL_UTIL_H_
17 #include "base/basictypes.h"
18 #include "base/files/file_path.h"
19 #include "base/logging.h"
20 #include "base/strings/string16.h"
21 #include "chrome/installer/util/work_item_list.h"
23 class BrowserDistribution
;
25 // This is a utility class that provides common shell integration methods
26 // that can be used by installer as well as Chrome.
29 // Input to any methods that make changes to OS shell.
31 CURRENT_USER
= 0x1, // Make any shell changes only at the user level
32 SYSTEM_LEVEL
= 0x2 // Make any shell changes only at the system level
35 // Chrome's default handler state for a given protocol.
42 // Typical shortcut directories. Resolved in GetShortcutPath().
43 // Also used in ShortcutLocationIsSupported().
44 enum ShortcutLocation
{
45 SHORTCUT_LOCATION_FIRST
= 0,
46 SHORTCUT_LOCATION_DESKTOP
= SHORTCUT_LOCATION_FIRST
,
47 SHORTCUT_LOCATION_QUICK_LAUNCH
,
48 SHORTCUT_LOCATION_START_MENU_ROOT
,
49 SHORTCUT_LOCATION_START_MENU_CHROME_DIR
,
50 SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR
,
51 SHORTCUT_LOCATION_TASKBAR_PINS
, // base::win::VERSION_WIN7 +
52 SHORTCUT_LOCATION_APP_SHORTCUTS
, // base::win::VERSION_WIN8 +
53 NUM_SHORTCUT_LOCATIONS
56 enum ShortcutOperation
{
57 // Create a new shortcut (overwriting if necessary).
58 SHELL_SHORTCUT_CREATE_ALWAYS
,
59 // Create the per-user shortcut only if its system-level equivalent (with
60 // the same name) is not present.
61 SHELL_SHORTCUT_CREATE_IF_NO_SYSTEM_LEVEL
,
62 // Overwrite an existing shortcut (fail if the shortcut doesn't exist).
63 // If the arguments are not specified on the new shortcut, keep the old
64 // shortcut's arguments.
65 SHELL_SHORTCUT_REPLACE_EXISTING
,
66 // Update specified properties only on an existing shortcut.
67 SHELL_SHORTCUT_UPDATE_EXISTING
,
70 // Properties for shortcuts. Properties set will be applied to
71 // the shortcut on creation/update. On update, unset properties are ignored;
72 // on create (and replaced) unset properties might have a default value (see
73 // individual property setters below for details).
74 // Callers are encouraged to use the setters provided which take care of
75 // setting |options| as desired.
76 struct ShortcutProperties
{
77 enum IndividualProperties
{
78 PROPERTIES_TARGET
= 1 << 0,
79 PROPERTIES_ARGUMENTS
= 1 << 1,
80 PROPERTIES_DESCRIPTION
= 1 << 2,
81 PROPERTIES_ICON
= 1 << 3,
82 PROPERTIES_APP_ID
= 1 << 4,
83 PROPERTIES_SHORTCUT_NAME
= 1 << 5,
84 PROPERTIES_DUAL_MODE
= 1 << 6,
87 explicit ShortcutProperties(ShellChange level_in
)
88 : level(level_in
), icon_index(0), dual_mode(false),
89 pin_to_taskbar(false), options(0U) {}
91 // Sets the target executable to launch from this shortcut.
92 // This is mandatory when creating a shortcut.
93 void set_target(const base::FilePath
& target_in
) {
95 options
|= PROPERTIES_TARGET
;
98 // Sets the arguments to be passed to |target| when launching from this
100 // The length of this string must be less than MAX_PATH.
101 void set_arguments(const base::string16
& arguments_in
) {
102 // Size restriction as per MSDN at
103 // http://msdn.microsoft.com/library/windows/desktop/bb774954.aspx.
104 DCHECK(arguments_in
.length() < MAX_PATH
);
105 arguments
= arguments_in
;
106 options
|= PROPERTIES_ARGUMENTS
;
109 // Sets the localized description of the shortcut.
110 // The length of this string must be less than MAX_PATH.
111 void set_description(const base::string16
& description_in
) {
112 // Size restriction as per MSDN at
113 // http://msdn.microsoft.com/library/windows/desktop/bb774955.aspx.
114 DCHECK(description_in
.length() < MAX_PATH
);
115 description
= description_in
;
116 options
|= PROPERTIES_DESCRIPTION
;
119 // Sets the path to the icon (icon_index set to 0).
120 // icon index unless otherwise specified in master_preferences).
121 void set_icon(const base::FilePath
& icon_in
, int icon_index_in
) {
123 icon_index
= icon_index_in
;
124 options
|= PROPERTIES_ICON
;
127 // Sets the app model id for the shortcut (Win7+).
128 void set_app_id(const base::string16
& app_id_in
) {
130 options
|= PROPERTIES_APP_ID
;
133 // Forces the shortcut's name to |shortcut_name_in|.
134 // Default: the current distribution's GetShortcutName(SHORTCUT_CHROME).
135 // The ".lnk" extension will automatically be added to this name.
136 void set_shortcut_name(const base::string16
& shortcut_name_in
) {
137 shortcut_name
= shortcut_name_in
;
138 options
|= PROPERTIES_SHORTCUT_NAME
;
141 // Sets whether this is a dual mode shortcut (Win8+).
142 // NOTE: Only the default (no arguments and default browser appid) browser
143 // shortcut in the Start menu (Start screen on Win8+) should be made dual
145 void set_dual_mode(bool dual_mode_in
) {
146 dual_mode
= dual_mode_in
;
147 options
|= PROPERTIES_DUAL_MODE
;
150 // Sets whether to pin this shortcut to the taskbar after creating it
151 // (ignored if the shortcut is only being updated).
152 // Note: This property doesn't have a mask in |options|.
153 void set_pin_to_taskbar(bool pin_to_taskbar_in
) {
154 pin_to_taskbar
= pin_to_taskbar_in
;
157 bool has_target() const {
158 return (options
& PROPERTIES_TARGET
) != 0;
161 bool has_arguments() const {
162 return (options
& PROPERTIES_ARGUMENTS
) != 0;
165 bool has_description() const {
166 return (options
& PROPERTIES_DESCRIPTION
) != 0;
169 bool has_icon() const {
170 return (options
& PROPERTIES_ICON
) != 0;
173 bool has_app_id() const {
174 return (options
& PROPERTIES_APP_ID
) != 0;
177 bool has_shortcut_name() const {
178 return (options
& PROPERTIES_SHORTCUT_NAME
) != 0;
181 bool has_dual_mode() const {
182 return (options
& PROPERTIES_DUAL_MODE
) != 0;
185 // The level to install this shortcut at (CURRENT_USER for a per-user
186 // shortcut and SYSTEM_LEVEL for an all-users shortcut).
189 base::FilePath target
;
190 base::string16 arguments
;
191 base::string16 description
;
194 base::string16 app_id
;
195 base::string16 shortcut_name
;
198 // Bitfield made of IndividualProperties. Properties set in |options| will
199 // be used to create/update the shortcut, others will be ignored on update
200 // and possibly replaced by default values on create (see individual
201 // property setters above for details on default values).
205 // Relative path of the URL Protocol registry entry (prefixed with '\').
206 static const wchar_t* kRegURLProtocol
;
208 // Relative path of DefaultIcon registry entry (prefixed with '\').
209 static const wchar_t* kRegDefaultIcon
;
211 // Relative path of "shell" registry key.
212 static const wchar_t* kRegShellPath
;
214 // Relative path of shell open command in Windows registry
215 // (i.e. \\shell\\open\\command).
216 static const wchar_t* kRegShellOpen
;
218 // Relative path of registry key under which applications need to register
219 // to control Windows Start menu links.
220 static const wchar_t* kRegStartMenuInternet
;
222 // Relative path of Classes registry entry under which file associations
223 // are added on Windows.
224 static const wchar_t* kRegClasses
;
226 // Relative path of RegisteredApplications registry entry under which
227 // we add Chrome as a Windows application
228 static const wchar_t* kRegRegisteredApplications
;
230 // The key path and key name required to register Chrome on Windows such
231 // that it can be launched from Start->Run just by name (chrome.exe).
232 static const wchar_t* kAppPathsRegistryKey
;
233 static const wchar_t* kAppPathsRegistryPathName
;
235 // Registry path that stores url associations on Vista.
236 static const wchar_t* kRegVistaUrlPrefs
;
238 // File extensions that Chrome registers itself as the default handler
239 // for when the user makes Chrome the default browser.
240 static const wchar_t* kDefaultFileAssociations
[];
242 // File extensions that Chrome registers itself as being capable of
244 static const wchar_t* kPotentialFileAssociations
[];
246 // Protocols that Chrome registers itself as the default handler for
247 // when the user makes Chrome the default browser.
248 static const wchar_t* kBrowserProtocolAssociations
[];
250 // Protocols that Chrome registers itself as being capable of handling.
251 static const wchar_t* kPotentialProtocolAssociations
[];
253 // Registry value name that is needed for ChromeHTML ProgId
254 static const wchar_t* kRegUrlProtocol
;
256 // Relative registry path from \Software\Classes\ChromeHTML to the ProgId
257 // Application definitions.
258 static const wchar_t* kRegApplication
;
260 // Registry value name for the AppUserModelId of an application.
261 static const wchar_t* kRegAppUserModelId
;
263 // Registry value name for the description of an application.
264 static const wchar_t* kRegApplicationDescription
;
266 // Registry value name for an application's name.
267 static const wchar_t* kRegApplicationName
;
269 // Registry value name for the path to an application's icon.
270 static const wchar_t* kRegApplicationIcon
;
272 // Registry value name for an application's company.
273 static const wchar_t* kRegApplicationCompany
;
275 // Relative path of ".exe" registry key.
276 static const wchar_t* kRegExePath
;
278 // Registry value name of the open verb.
279 static const wchar_t* kRegVerbOpen
;
281 // Registry value name of the opennewwindow verb.
282 static const wchar_t* kRegVerbOpenNewWindow
;
284 // Registry value name of the run verb.
285 static const wchar_t* kRegVerbRun
;
287 // Registry value name for command entries.
288 static const wchar_t* kRegCommand
;
290 // Registry value name for the DelegateExecute verb handler.
291 static const wchar_t* kRegDelegateExecute
;
293 // Registry value name for the OpenWithProgids entry for file associations.
294 static const wchar_t* kRegOpenWithProgids
;
296 // Returns true if |chrome_exe| is registered in HKLM with |suffix|.
297 // Note: This only checks one deterministic key in HKLM for |chrome_exe| and
298 // doesn't otherwise validate a full Chrome install in HKLM.
299 static bool QuickIsChromeRegisteredInHKLM(BrowserDistribution
* dist
,
300 const base::string16
& chrome_exe
,
301 const base::string16
& suffix
);
303 // Returns true if the current Windows version supports the presence of
304 // shortcuts at |location|.
305 static bool ShortcutLocationIsSupported(ShellUtil::ShortcutLocation location
);
307 // Sets |path| to the path for a shortcut at the |location| desired for the
308 // given |level| (CURRENT_USER for per-user path and SYSTEM_LEVEL for
310 // Returns false on failure.
311 static bool GetShortcutPath(ShellUtil::ShortcutLocation location
,
312 BrowserDistribution
* dist
,
314 base::FilePath
* path
);
316 // Updates shortcut in |location| (or creates it if |options| specify
317 // SHELL_SHORTCUT_CREATE_ALWAYS).
318 // |dist| gives the type of browser distribution currently in use.
319 // |properties| and |operation| affect this method as described on their
320 // invidividual definitions above.
321 // |location| may be one of SHORTCUT_LOCATION_DESKTOP,
322 // SHORTCUT_LOCATION_QUICK_LAUNCH, SHORTCUT_LOCATION_START_MENU_ROOT,
323 // SHORTCUT_LOCATION_START_MENU_CHROME_DIR, or
324 // SHORTCUT_LOCATION_START_MENU_CHROME_APPS_DIR.
325 static bool CreateOrUpdateShortcut(
326 ShellUtil::ShortcutLocation location
,
327 BrowserDistribution
* dist
,
328 const ShellUtil::ShortcutProperties
& properties
,
329 ShellUtil::ShortcutOperation operation
);
331 // Returns the string "|icon_path|,|icon_index|" (see, for example,
332 // http://msdn.microsoft.com/library/windows/desktop/dd391573.aspx).
333 static base::string16
FormatIconLocation(const base::string16
& icon_path
,
336 // This method returns the command to open URLs/files using chrome. Typically
337 // this command is written to the registry under shell\open\command key.
338 // |chrome_exe|: the full path to chrome.exe
339 static base::string16
GetChromeShellOpenCmd(const base::string16
& chrome_exe
);
341 // This method returns the command to be called by the DelegateExecute verb
342 // handler to launch chrome on Windows 8. Typically this command is written to
343 // the registry under the HKCR\Chrome\.exe\shell\(open|run)\command key.
344 // |chrome_exe|: the full path to chrome.exe
345 static base::string16
GetChromeDelegateCommand(
346 const base::string16
& chrome_exe
);
348 // Gets a mapping of all registered browser names (excluding browsers in the
349 // |dist| distribution) and their reinstall command (which usually sets
350 // browser as default).
351 // Given browsers can be registered in HKCU (as of Win7) and/or in HKLM, this
352 // method looks in both and gives precedence to values in HKCU as per the msdn
353 // standard: http://goo.gl/xjczJ.
354 static void GetRegisteredBrowsers(
355 BrowserDistribution
* dist
,
356 std::map
<base::string16
, base::string16
>* browsers
);
358 // Returns the suffix this user's Chrome install is registered with.
359 // Always returns the empty string on system-level installs.
361 // This method is meant for external methods which need to know the suffix of
362 // the current install at run-time, not for install-time decisions.
363 // There are no guarantees that this suffix will not change later:
364 // (e.g. if two user-level installs were previously installed in parallel on
365 // the same machine, both without admin rights and with no user-level install
366 // having claimed the non-suffixed HKLM registrations, they both have no
367 // suffix in their progId entries (as per the old suffix rules). If they were
368 // to both fully register (i.e. click "Make Chrome Default" and go through
369 // UAC; or upgrade to Win8 and get the automatic no UAC full registration)
370 // they would then both get a suffixed registration as per the new suffix
373 // |chrome_exe| The path to the currently installed (or running) chrome.exe.
374 static base::string16
GetCurrentInstallationSuffix(
375 BrowserDistribution
* dist
,
376 const base::string16
& chrome_exe
);
378 // Returns the application name of the program under |dist|.
379 // This application name will be suffixed as is appropriate for the current
381 // This is the name that is registered with Default Programs on Windows and
382 // that should thus be used to "make chrome default" and such.
383 static base::string16
GetApplicationName(BrowserDistribution
* dist
,
384 const base::string16
& chrome_exe
);
386 // Returns the AppUserModelId for |dist|. This identifier is unconditionally
387 // suffixed with a unique id for this user on user-level installs (in contrast
388 // to other registration entries which are suffixed as described in
389 // GetCurrentInstallationSuffix() above).
390 static base::string16
GetBrowserModelId(BrowserDistribution
* dist
,
391 bool is_per_user_install
);
393 // Returns an AppUserModelId composed of each member of |components| separated
395 // The returned appid is guaranteed to be no longer than
396 // chrome::kMaxAppModelIdLength (some of the components might have been
397 // shortened to enforce this).
398 static base::string16
BuildAppModelId(
399 const std::vector
<base::string16
>& components
);
401 // Returns true if Chrome can make itself the default browser without relying
402 // on the Windows shell to prompt the user. This is the case for versions of
403 // Windows prior to Windows 8.
404 static bool CanMakeChromeDefaultUnattended();
406 // Returns the DefaultState of Chrome for HTTP and HTTPS.
407 static DefaultState
GetChromeDefaultState();
409 // Returns the DefaultState of Chrome for |protocol|.
410 static DefaultState
GetChromeDefaultProtocolClientState(
411 const base::string16
& protocol
);
413 // Make Chrome the default browser. This function works by going through
414 // the url protocols and file associations that are related to general
415 // browsing, e.g. http, https, .html etc., and requesting to become the
416 // default handler for each. If any of these fails the operation will return
417 // false to indicate failure, which is consistent with the return value of
418 // ShellIntegration::GetDefaultBrowser.
420 // In the case of failure any successful changes will be left, however no
421 // more changes will be attempted.
422 // TODO(benwells): Attempt to undo any changes that were successfully made.
423 // http://crbug.com/83970
425 // shell_change: Defined whether to register as default browser at system
426 // level or user level. If value has ShellChange::SYSTEM_LEVEL
427 // we should be running as admin user.
428 // chrome_exe: The chrome.exe path to register as default browser.
429 // elevate_if_not_admin: On Vista if user is not admin, try to elevate for
430 // Chrome registration.
431 static bool MakeChromeDefault(BrowserDistribution
* dist
,
433 const base::string16
& chrome_exe
,
434 bool elevate_if_not_admin
);
436 // Shows and waits for the Windows 8 "How do you want to open webpages?"
437 // dialog if Chrome is not already the default HTTP/HTTPS handler. Also does
438 // XP-era registrations if Chrome is chosen or was already the default. Do
439 // not use on pre-Win8 OSes.
441 // |dist| gives the type of browser distribution currently in use.
442 // |chrome_exe| The chrome.exe path to register as default browser.
443 static bool ShowMakeChromeDefaultSystemUI(BrowserDistribution
* dist
,
444 const base::string16
& chrome_exe
);
446 // Make Chrome the default application for a protocol.
447 // chrome_exe: The chrome.exe path to register as default browser.
448 // protocol: The protocol to register as the default handler for.
449 static bool MakeChromeDefaultProtocolClient(BrowserDistribution
* dist
,
450 const base::string16
& chrome_exe
,
451 const base::string16
& protocol
);
453 // Shows and waits for the Windows 8 "How do you want to open links of this
454 // type?" dialog if Chrome is not already the default |protocol|
455 // handler. Also does XP-era registrations if Chrome is chosen or was already
456 // the default for |protocol|. Do not use on pre-Win8 OSes.
458 // |dist| gives the type of browser distribution currently in use.
459 // |chrome_exe| The chrome.exe path to register as default browser.
460 // |protocol| is the protocol being registered.
461 static bool ShowMakeChromeDefaultProtocolClientSystemUI(
462 BrowserDistribution
* dist
,
463 const base::string16
& chrome_exe
,
464 const base::string16
& protocol
);
466 // Registers Chrome as a potential default browser and handler for filetypes
468 // If Chrome is already registered, this method is a no-op.
469 // This method requires write access to HKLM (prior to Win8) so is just a
471 // If write to HKLM is required, but fails, and:
472 // - |elevate_if_not_admin| is true (and OS is Vista or above):
473 // tries to launch setup.exe with admin priviledges (by prompting the user
474 // with a UAC) to do these tasks.
475 // - |elevate_if_not_admin| is false (or OS is XP):
476 // adds the ProgId entries to HKCU. These entries will not make Chrome show
477 // in Default Programs but they are still useful because Chrome can be
478 // registered to run when the user clicks on an http link or an html file.
480 // |chrome_exe| full path to chrome.exe.
481 // |unique_suffix| Optional input. If given, this function appends the value
482 // to default browser entries names that it creates in the registry.
483 // Currently, this is only used to continue an install with the same suffix
484 // when elevating and calling setup.exe with admin privileges as described
486 // |elevate_if_not_admin| if true will make this method try alternate methods
487 // as described above. This should only be true when following a user action
488 // (e.g. "Make Chrome Default") as it allows this method to UAC.
490 // Returns true if Chrome is successfully registered (or already registered).
491 static bool RegisterChromeBrowser(BrowserDistribution
* dist
,
492 const base::string16
& chrome_exe
,
493 const base::string16
& unique_suffix
,
494 bool elevate_if_not_admin
);
496 // This method declares to Windows that Chrome is capable of handling the
497 // given protocol. This function will call the RegisterChromeBrowser function
498 // to register with Windows as capable of handling the protocol, if it isn't
499 // currently registered as capable.
500 // Declaring the capability of handling a protocol is necessary to register
501 // as the default handler for the protocol in Vista and later versions of
504 // If called by the browser and elevation is required, it will elevate by
505 // calling setup.exe which will again call this function with elevate false.
507 // |chrome_exe| full path to chrome.exe.
508 // |unique_suffix| Optional input. If given, this function appends the value
509 // to default browser entries names that it creates in the registry.
510 // |protocol| The protocol to register as being capable of handling.s
511 // |elevate_if_not_admin| if true will make this method try alternate methods
512 // as described above.
513 static bool RegisterChromeForProtocol(BrowserDistribution
* dist
,
514 const base::string16
& chrome_exe
,
515 const base::string16
& unique_suffix
,
516 const base::string16
& protocol
,
517 bool elevate_if_not_admin
);
519 // Removes installed shortcut(s) at |location|.
520 // |level|: CURRENT_USER to remove per-user shortcuts, or SYSTEM_LEVEL to
521 // remove all-users shortcuts.
522 // |target_exe|: Shortcut target exe; shortcuts will only be deleted when
523 // their target is |target_exe|.
524 // If |location| is a Chrome-specific folder, it will be deleted as well.
525 // Returns true if all shortcuts pointing to |target_exe| are successfully
526 // deleted, including the case where no such shortcuts are found.
527 static bool RemoveShortcuts(ShellUtil::ShortcutLocation location
,
528 BrowserDistribution
* dist
,
530 const base::FilePath
& target_exe
);
532 // Updates the target of all shortcuts in |location| that satisfy the
534 // - the shortcut's original target is |old_target_exe|,
535 // - the original arguments are non-empty.
536 // If the shortcut's icon points to |old_target_exe|, then it also gets
537 // redirected to |new_target_exe|.
538 // Returns true if all updates to matching shortcuts are successful, including
539 // the vacuous case where no matching shortcuts are found.
540 static bool RetargetShortcutsWithArgs(
541 ShellUtil::ShortcutLocation location
,
542 BrowserDistribution
* dist
,
544 const base::FilePath
& old_target_exe
,
545 const base::FilePath
& new_target_exe
);
547 // Sets |suffix| to the base 32 encoding of the md5 hash of this user's sid
548 // preceded by a dot.
549 // This is guaranteed to be unique on the machine and 27 characters long
550 // (including the '.').
551 // This suffix is then meant to be added to all registration that may conflict
552 // with another user-level Chrome install.
553 // Note that prior to Chrome 21, the suffix registered used to be the user's
554 // username (see GetOldUserSpecificRegistrySuffix() below). We still honor old
555 // installs registered that way, but it was wrong because some of the
556 // characters allowed in a username are not allowed in a ProgId.
557 // Returns true unless the OS call to retrieve the username fails.
558 // NOTE: Only the installer should use this suffix directly. Other callers
559 // should call GetCurrentInstallationSuffix().
560 static bool GetUserSpecificRegistrySuffix(base::string16
* suffix
);
562 // Sets |suffix| to this user's username preceded by a dot. This suffix should
563 // only be used to support legacy installs that used this suffixing
565 // Returns true unless the OS call to retrieve the username fails.
566 // NOTE: Only the installer should use this suffix directly. Other callers
567 // should call GetCurrentInstallationSuffix().
568 static bool GetOldUserSpecificRegistrySuffix(base::string16
* suffix
);
570 // Returns the base32 encoding (using the [A-Z2-7] alphabet) of |bytes|.
571 // |size| is the length of |bytes|.
572 // Note: This method does not suffix the output with '=' signs as technically
573 // required by the base32 standard for inputs that aren't a multiple of 5
575 static base::string16
ByteArrayToBase32(const uint8
* bytes
, size_t size
);
578 DISALLOW_COPY_AND_ASSIGN(ShellUtil
);
582 #endif // CHROME_INSTALLER_UTIL_SHELL_UTIL_H_