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