Avi Drissman | bfdd3a48 | 2023-01-25 19:17:16 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_MAC_LAUNCH_APPLICATION_H_ |
| 6 | #define BASE_MAC_LAUNCH_APPLICATION_H_ |
| 7 | |
| 8 | #import <AppKit/AppKit.h> |
| 9 | |
| 10 | #include <string> |
Victor Hugo Vianna Silva | ab2c6ac | 2025-03-18 19:52:34 | [diff] [blame] | 11 | #include <variant> |
Avi Drissman | bfdd3a48 | 2023-01-25 19:17:16 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
| 14 | #include "base/base_export.h" |
| 15 | #include "base/command_line.h" |
| 16 | #include "base/files/file_path.h" |
| 17 | #include "base/functional/callback_forward.h" |
Avi Drissman | bfdd3a48 | 2023-01-25 19:17:16 | [diff] [blame] | 18 | |
| 19 | // Launches an application. |
| 20 | // |
| 21 | // What makes this different from `LaunchProcess()` in /base/process/launch.h? |
| 22 | // That code creates a sub-process, which is useful for utility processes and |
| 23 | // the like, but inappropriate for independent applications. |
| 24 | // `LaunchApplication()` below, on the other hand, launches an app in the way |
| 25 | // that the Finder or Dock would launch an app. |
| 26 | |
| 27 | namespace base::mac { |
| 28 | |
| 29 | struct LaunchApplicationOptions { |
| 30 | bool activate = true; |
| 31 | bool create_new_instance = false; |
| 32 | bool prompt_user_if_needed = false; |
Marijn Kruisselbrink | 88fa5d5f | 2023-08-09 18:27:56 | [diff] [blame] | 33 | |
| 34 | // When this option is set to true, a private SPI is used to launch the app |
| 35 | // "invisibly". Apps launched this way do not show up as running. |
| 36 | // Note that opening URLs in an already running hidden-in-background app |
| 37 | // appears to always cause the app to transition to foreground, even if we've |
| 38 | // requested a background launch. |
| 39 | bool hidden_in_background = false; |
Avi Drissman | bfdd3a48 | 2023-01-25 19:17:16 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | using LaunchApplicationCallback = |
Avi Drissman | 4173dee | 2023-05-05 18:06:45 | [diff] [blame] | 43 | base::OnceCallback<void(NSRunningApplication*, NSError*)>; |
Avi Drissman | bfdd3a48 | 2023-01-25 19:17:16 | [diff] [blame] | 44 | |
| 45 | using CommandLineArgs = |
Victor Hugo Vianna Silva | ab2c6ac | 2025-03-18 19:52:34 | [diff] [blame] | 46 | std::variant<std::monostate, CommandLine, std::vector<std::string>>; |
Avi Drissman | bfdd3a48 | 2023-01-25 19:17:16 | [diff] [blame] | 47 | |
| 48 | // Launches the specified application. |
| 49 | // - `app_bundle_path`: the location of the application to launch |
| 50 | // - `command_line_args`: the command line arguments to pass to the |
| 51 | // application if the app isn't already running (the default-constructed |
| 52 | // monostate alternative means no arguments) |
| 53 | // - Note: The application to launch is specified as `app_bundle_path`, so |
| 54 | // if `base::CommandLine` is used to provide command line arguments, its |
| 55 | // first argument will be ignored |
| 56 | // - `url_specs`: the URLs for the application to open (an empty vector is OK) |
| 57 | // - `options`: options to modify the launch |
| 58 | // - `callback`: the result callback |
| 59 | // |
| 60 | // When the launch is complete, `callback` is called on the main thread. If the |
Avi Drissman | 4173dee | 2023-05-05 18:06:45 | [diff] [blame] | 61 | // launch succeeded, it will be called with an `NSRunningApplication*` and the |
| 62 | // `NSError*` will be nil. If the launch failed, it will be called with an |
| 63 | // `NSError*`, and the `NSRunningApplication*` will be nil. |
Avi Drissman | bfdd3a48 | 2023-01-25 19:17:16 | [diff] [blame] | 64 | BASE_EXPORT void LaunchApplication(const FilePath& app_bundle_path, |
| 65 | const CommandLineArgs& command_line_args, |
| 66 | const std::vector<std::string>& url_specs, |
| 67 | LaunchApplicationOptions options, |
| 68 | LaunchApplicationCallback callback); |
| 69 | |
| 70 | } // namespace base::mac |
| 71 | |
| 72 | #endif // BASE_MAC_LAUNCH_APPLICATION_H_ |