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