relauncher.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 SHELL_BROWSER_RELAUNCHER_H_
  5. #define 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 <string>
  30. #include <vector>
  31. #include "base/command_line.h"
  32. #if defined(OS_WIN)
  33. #include "base/process/process_handle.h"
  34. #endif
  35. namespace content {
  36. struct MainFunctionParams;
  37. }
  38. namespace relauncher {
  39. using CharType = base::CommandLine::CharType;
  40. using StringType = base::CommandLine::StringType;
  41. using StringVector = base::CommandLine::StringVector;
  42. // Relaunches the application using the helper application associated with the
  43. // currently running instance of Chrome in the parent browser process as the
  44. // executable for the relauncher process. |args| is an argv-style vector of
  45. // command line arguments of the form normally passed to execv. args[0] is
  46. // also the path to the relaunched process. Because the relauncher process
  47. // will ultimately launch the relaunched process via Launch Services, args[0]
  48. // may be either a pathname to an executable file or a pathname to an .app
  49. // bundle directory. The caller should exit soon after RelaunchApp returns
  50. // successfully. Returns true on success, although some failures can occur
  51. // after this function returns true if, for example, they occur within the
  52. // relauncher process. Returns false when the relaunch definitely failed.
  53. bool RelaunchApp(const StringVector& argv);
  54. // Identical to RelaunchApp, but uses |helper| as the path to the relauncher
  55. // process, and allows additional arguments to be supplied to the relauncher
  56. // process in relauncher_args. Unlike args[0], |helper| must be a pathname to
  57. // an executable file. The helper path given must be from the same version of
  58. // Chrome as the running parent browser process, as there are no guarantees
  59. // that the parent and relauncher processes from different versions will be
  60. // able to communicate with one another. This variant can be useful to
  61. // relaunch the same version of Chrome from another location, using that
  62. // location's helper.
  63. bool RelaunchAppWithHelper(const base::FilePath& helper,
  64. const StringVector& relauncher_args,
  65. const StringVector& args);
  66. // The entry point from ChromeMain into the relauncher process.
  67. int RelauncherMain(const content::MainFunctionParams& main_parameters);
  68. namespace internal {
  69. #if defined(OS_POSIX)
  70. // The "magic" file descriptor that the relauncher process' write side of the
  71. // pipe shows up on. Chosen to avoid conflicting with stdin, stdout, and
  72. // stderr.
  73. extern const int kRelauncherSyncFD;
  74. #endif
  75. // The "type" argument identifying a relauncher process ("--type=relauncher").
  76. extern const CharType* kRelauncherTypeArg;
  77. // The argument separating arguments intended for the relauncher process from
  78. // those intended for the relaunched process. "---" is chosen instead of "--"
  79. // because CommandLine interprets "--" as meaning "end of switches", but
  80. // for many purposes, the relauncher process' CommandLine ought to interpret
  81. // arguments intended for the relaunched process, to get the correct settings
  82. // for such things as logging and the user-data-dir in case it affects crash
  83. // reporting.
  84. extern const CharType* kRelauncherArgSeparator;
  85. #if defined(OS_WIN)
  86. StringType GetWaitEventName(base::ProcessId pid);
  87. StringType ArgvToCommandLineString(const StringVector& argv);
  88. #endif
  89. // In the relauncher process, performs the necessary synchronization steps
  90. // with the parent by setting up a kqueue to watch for it to exit, writing a
  91. // byte to the pipe, and then waiting for the exit notification on the kqueue.
  92. // If anything fails, this logs a message and returns immediately. In those
  93. // situations, it can be assumed that something went wrong with the parent
  94. // process and the best recovery approach is to attempt relaunch anyway.
  95. void RelauncherSynchronizeWithParent();
  96. int LaunchProgram(const StringVector& relauncher_args,
  97. const StringVector& argv);
  98. } // namespace internal
  99. } // namespace relauncher
  100. #endif // SHELL_BROWSER_RELAUNCHER_H_