xref: /aosp_15_r20/external/cronet/base/mac/launch_application.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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>
11 #include <vector>
12 
13 #include "base/base_export.h"
14 #include "base/command_line.h"
15 #include "base/files/file_path.h"
16 #include "base/functional/callback_forward.h"
17 #include "third_party/abseil-cpp/absl/types/variant.h"
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;
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;
40 };
41 
42 using LaunchApplicationCallback =
43     base::OnceCallback<void(NSRunningApplication*, NSError*)>;
44 
45 using CommandLineArgs =
46     absl::variant<absl::monostate, CommandLine, std::vector<std::string>>;
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
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.
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_
73