relauncher.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Copyright (c) 2016 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_BROWSER_RELAUNCHER_H_
  5. #define ELECTRON_SHELL_BROWSER_RELAUNCHER_H_
  6. // relauncher implements main browser application relaunches across platforms.
  7. // When a browser wants to relaunch itself, it can't simply fork off a new
  8. // process and exec a new browser from within. That leaves open a window
  9. // during which two browser applications might be running concurrently. If
  10. // that happens, each will wind up with a distinct Dock icon, which is
  11. // especially bad if the user expected the Dock icon to be persistent by
  12. // choosing Keep in Dock from the icon's contextual menu.
  13. //
  14. // relauncher approaches this problem by introducing an intermediate
  15. // process (the "relauncher") in between the original browser ("parent") and
  16. // replacement browser ("relaunched"). The helper executable is used for the
  17. // relauncher process; because it's an LSUIElement, it doesn't get a Dock
  18. // icon and isn't visible as a running application at all. The parent will
  19. // start a relauncher process, giving it the "writer" side of a pipe that it
  20. // retains the "reader" end of. When the relauncher starts up, it will
  21. // establish a kqueue to wait for the parent to exit, and will then write to
  22. // the pipe. The parent, upon reading from the pipe, is free to exit. When the
  23. // relauncher is notified via its kqueue that the parent has exited, it
  24. // proceeds, launching the relaunched process. The handshake to synchronize
  25. // the parent with the relauncher is necessary to avoid races: the relauncher
  26. // needs to be sure that it's monitoring the parent and not some other process
  27. // in light of PID reuse, so the parent must remain alive long enough for the
  28. // relauncher to set up its kqueue.
  29. #include "base/command_line.h"
  30. #if BUILDFLAG(IS_WIN)
  31. #include "base/process/process_handle.h"
  32. #endif
  33. namespace content {
  34. struct MainFunctionParams;
  35. }
  36. namespace relauncher {
  37. using CharType = base::CommandLine::CharType;
  38. using StringType = base::CommandLine::StringType;
  39. using StringVector = base::CommandLine::StringVector;
  40. // Relaunches the application using the helper application associated with the
  41. // currently running instance of Chrome in the parent browser process as the
  42. // executable for the relauncher process. |argv| is an argv-style vector of
  43. // command line arguments of the form normally passed to execv. argv[0] is
  44. // also the path to the relaunched process. Because the relauncher process
  45. // will ultimately launch the relaunched process via Launch Services, argv[0]
  46. // may be either a pathname to an executable file or a pathname to an .app
  47. // bundle directory. The caller should exit soon after RelaunchApp returns
  48. // successfully. Returns true on success, although some failures can occur
  49. // after this function returns true if, for example, they occur within the
  50. // relauncher process. Returns false when the relaunch definitely failed.
  51. bool RelaunchApp(const StringVector& argv);
  52. // Identical to RelaunchApp, but uses |helper| as the path to the relauncher
  53. // process, and allows additional arguments to be supplied to the relauncher
  54. // process in relauncher_args. Unlike argv[0], |helper| must be a pathname to
  55. // an executable file. The helper path given must be from the same version of
  56. // Chrome as the running parent browser process, as there are no guarantees
  57. // that the parent and relauncher processes from different versions will be
  58. // able to communicate with one another. This variant can be useful to
  59. // relaunch the same version of Chrome from another location, using that
  60. // location's helper.
  61. bool RelaunchAppWithHelper(const base::FilePath& helper,
  62. const StringVector& relauncher_args,
  63. const StringVector& argv);
  64. // The entry point from ChromeMain into the relauncher process.
  65. int RelauncherMain(const content::MainFunctionParams& main_parameters);
  66. namespace internal {
  67. #if BUILDFLAG(IS_POSIX)
  68. // The "magic" file descriptor that the relauncher process' write side of the
  69. // pipe shows up on. Chosen to avoid conflicting with stdin, stdout, and
  70. // stderr.
  71. extern const int kRelauncherSyncFD;
  72. #endif
  73. // The "type" argument identifying a relauncher process ("--type=relauncher").
  74. extern const CharType* kRelauncherTypeArg;
  75. // The argument separating arguments intended for the relauncher process from
  76. // those intended for the relaunched process. "---" is chosen instead of "--"
  77. // because CommandLine interprets "--" as meaning "end of switches", but
  78. // for many purposes, the relauncher process' CommandLine ought to interpret
  79. // arguments intended for the relaunched process, to get the correct settings
  80. // for such things as logging and the user-data-dir in case it affects crash
  81. // reporting.
  82. extern const CharType* kRelauncherArgSeparator;
  83. #if BUILDFLAG(IS_WIN)
  84. StringType GetWaitEventName(base::ProcessId pid);
  85. StringType ArgvToCommandLineString(const StringVector& argv);
  86. #endif
  87. // In the relauncher process, performs the necessary synchronization steps
  88. // with the parent by setting up a kqueue to watch for it to exit, writing a
  89. // byte to the pipe, and then waiting for the exit notification on the kqueue.
  90. // If anything fails, this logs a message and returns immediately. In those
  91. // situations, it can be assumed that something went wrong with the parent
  92. // process and the best recovery approach is to attempt relaunch anyway.
  93. void RelauncherSynchronizeWithParent();
  94. int LaunchProgram(const StringVector& relauncher_args,
  95. const StringVector& argv);
  96. } // namespace internal
  97. } // namespace relauncher
  98. #endif // ELECTRON_SHELL_BROWSER_RELAUNCHER_H_