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