File: app_mode_loader_mac.mm

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (276 lines) | stat: -rw-r--r-- 11,845 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// On Mac, shortcuts can't have command-line arguments. Instead, produce small
// app bundles which locate the Chromium framework and load it, passing the
// appropriate data. This is the code for such an app bundle. It should be kept
// minimal and do as little work as possible (with as much work done on
// framework side as possible).

#include <dlfcn.h>

#import <Cocoa/Cocoa.h>

#include "base/allocator/early_zone_registration_apple.h"
#include "base/apple/foundation_util.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/process/launch.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/sys_string_conversions.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#import "chrome/common/mac/app_mode_chrome_locator.h"
#include "chrome/common/mac/app_mode_common.h"

namespace {

const int kErrorReturnValue = 1;

typedef int (*StartFun)(const app_mode::ChromeAppModeInfo*);

int LoadFrameworkAndStart(int argc, char** argv) {
  base::CommandLine command_line(argc, argv);

  @autoreleasepool {
    // Get the current main bundle, i.e., that of the app loader that's running.
    NSBundle* app_bundle = NSBundle.mainBundle;
    if (!app_bundle) {
      NSLog(@"Couldn't get loader bundle");
      return kErrorReturnValue;
    }
    const base::FilePath app_mode_bundle_path =
        base::apple::NSStringToFilePath([app_bundle bundlePath]);

    // Get the bundle ID of the browser that created this app bundle.
    NSString* cr_bundle_id = base::apple::ObjCCast<NSString>(
        [app_bundle objectForInfoDictionaryKey:app_mode::kBrowserBundleIDKey]);
    if (!cr_bundle_id) {
      NSLog(@"Couldn't get browser bundle ID");
      return kErrorReturnValue;
    }

    // ** 1: Get path to outer Chrome bundle.
    base::FilePath cr_bundle_path;
    if (command_line.HasSwitch(app_mode::kLaunchedByChromeBundlePath)) {
      // If Chrome launched this app shim, and specified its bundle path on the
      // command line, use that.
      cr_bundle_path = command_line.GetSwitchValuePath(
          app_mode::kLaunchedByChromeBundlePath);
    } else {
      // Otherwise, search for a Chrome bundle to use.
      if (!app_mode::FindChromeBundle(cr_bundle_id, &cr_bundle_path)) {
        // TODO(https://crbug.com/944312): Display UI to inform the user of the
        // reason for failure.
        NSLog(@"Failed to locate browser bundle");
        return kErrorReturnValue;
      }
      if (cr_bundle_path.empty()) {
        NSLog(@"Browser bundle path unexpectedly empty");
        return kErrorReturnValue;
      }
    }

    // ** 2: Read the user data dir.
    base::FilePath user_data_dir;
    {
      // The user_data_dir for shims actually contains the app_data_path.
      // I.e. <user_data_dir>/<profile_dir>/Web Applications/_crx_extensionid/
      base::FilePath app_data_dir = base::apple::NSStringToFilePath([app_bundle
          objectForInfoDictionaryKey:app_mode::kCrAppModeUserDataDirKey]);
      user_data_dir = app_data_dir.DirName().DirName().DirName();
      NSLog(@"Using user data dir %s", user_data_dir.value().c_str());
      if (user_data_dir.empty())
        return kErrorReturnValue;
    }

    // ** 3: Read the Chrome executable, Chrome framework, and Chrome framework
    // dylib paths.
    app_mode::MojoIpczConfig mojo_ipcz_config =
        app_mode::MojoIpczConfig::kUseCommandLineFeatures;
    base::FilePath executable_path;
    base::FilePath framework_path;
    base::FilePath framework_dylib_path;
    if (command_line.HasSwitch(
            app_mode::kLaunchedByChromeFrameworkBundlePath) &&
        command_line.HasSwitch(app_mode::kLaunchedByChromeFrameworkDylibPath)) {
      // If Chrome launched this app shim, then it will specify the framework
      // path and version, as well as flags to enable or disable MojoIpcz as
      // needed. Do not populate `executable_path` (it is used to launch Chrome
      // if Chrome is not running, which is inapplicable here).
      framework_path = command_line.GetSwitchValuePath(
          app_mode::kLaunchedByChromeFrameworkBundlePath);
      framework_dylib_path = command_line.GetSwitchValuePath(
          app_mode::kLaunchedByChromeFrameworkDylibPath);
    } else {
      // Otherwise, read the version from the symbolic link in the user data
      // dir. If the version file does not exist, the version string will be
      // empty and app_mode::GetChromeBundleInfo will default to the latest
      // version, with MojoIpcz disabled.
      app_mode::ChromeConnectionConfig config;
      base::FilePath encoded_config;
      base::ReadSymbolicLink(
          user_data_dir.Append(app_mode::kRunningChromeVersionSymlinkName),
          &encoded_config);
      if (!encoded_config.empty()) {
        config =
            app_mode::ChromeConnectionConfig::DecodeFromPath(encoded_config);
        mojo_ipcz_config = config.is_mojo_ipcz_enabled
                               ? app_mode::MojoIpczConfig::kEnabled
                               : app_mode::MojoIpczConfig::kDisabled;
      }
      // If the version file does exist, it may have been left by a crashed
      // Chrome process. Ensure the process is still running.
      if (!config.framework_version.empty()) {
        NSArray* existing_chrome = [NSRunningApplication
            runningApplicationsWithBundleIdentifier:cr_bundle_id];
        if ([existing_chrome count] == 0) {
          NSLog(@"Disregarding framework version from symlink");
          config.framework_version.clear();
        } else {
          NSLog(@"Framework version from symlink %s",
                config.framework_version.c_str());
        }
      }
      if (!app_mode::GetChromeBundleInfo(
              cr_bundle_path, config.framework_version.c_str(),
              &executable_path, &framework_path, &framework_dylib_path)) {
        NSLog(@"Couldn't ready Chrome bundle info");
        return kErrorReturnValue;
      }
    }

    // Check if `executable_path` was overridden by tests via the command line.
    if (command_line.HasSwitch(app_mode::kLaunchChromeForTest)) {
      executable_path =
          command_line.GetSwitchValuePath(app_mode::kLaunchChromeForTest);
    }

    // ** 4: Read information from the Info.plist.
    // Read information about the this app shortcut from the Info.plist.
    // Don't check for null-ness on optional items.
    NSDictionary* info_plist = [app_bundle infoDictionary];
    if (!info_plist) {
      NSLog(@"Couldn't get loader Info.plist");
      return kErrorReturnValue;
    }

    const std::string app_mode_id =
        base::SysNSStringToUTF8(info_plist[app_mode::kCrAppModeShortcutIDKey]);
    if (!app_mode_id.size()) {
      NSLog(@"Couldn't get app shortcut ID");
      return kErrorReturnValue;
    }

    const std::string app_mode_name = base::SysNSStringToUTF8(
        info_plist[app_mode::kCrAppModeShortcutNameKey]);
    const std::string app_mode_url =
        base::SysNSStringToUTF8(info_plist[app_mode::kCrAppModeShortcutURLKey]);

    base::FilePath plist_user_data_dir = base::apple::NSStringToFilePath(
        info_plist[app_mode::kCrAppModeUserDataDirKey]);

    base::FilePath profile_dir = base::apple::NSStringToFilePath(
        info_plist[app_mode::kCrAppModeProfileDirKey]);

    // ** 5: Open the framework.
    StartFun ChromeAppModeStart = nullptr;
    NSLog(@"Using framework path %s", framework_path.value().c_str());
    NSLog(@"Loading framework dylib %s", framework_dylib_path.value().c_str());
    void* cr_dylib = dlopen(framework_dylib_path.value().c_str(), RTLD_LAZY);
    if (cr_dylib) {
      // Find the entry point.
      ChromeAppModeStart =
          (StartFun)dlsym(cr_dylib, APP_SHIM_ENTRY_POINT_NAME_STRING);
      if (!ChromeAppModeStart)
        NSLog(@"Couldn't get entry point: %s", dlerror());
    } else {
      NSLog(@"Couldn't load framework: %s", dlerror());
    }

    // ** 6: Fill in ChromeAppModeInfo and call into Chrome's framework.
    if (ChromeAppModeStart) {
      // Ensure that the strings pointed to by |info| outlive |info|.
      const std::string framework_path_utf8 = framework_path.AsUTF8Unsafe();
      const std::string cr_bundle_path_utf8 = cr_bundle_path.AsUTF8Unsafe();
      const std::string app_mode_bundle_path_utf8 =
          app_mode_bundle_path.AsUTF8Unsafe();
      const std::string plist_user_data_dir_utf8 =
          plist_user_data_dir.AsUTF8Unsafe();
      const std::string profile_dir_utf8 = profile_dir.AsUTF8Unsafe();
      app_mode::ChromeAppModeInfo info;
      info.argc = argc;
      info.argv = argv;
      info.chrome_framework_path = framework_path_utf8.c_str();
      info.chrome_outer_bundle_path = cr_bundle_path_utf8.c_str();
      info.app_mode_bundle_path = app_mode_bundle_path_utf8.c_str();
      info.app_mode_id = app_mode_id.c_str();
      info.app_mode_name = app_mode_name.c_str();
      info.app_mode_url = app_mode_url.c_str();
      info.user_data_dir = plist_user_data_dir_utf8.c_str();
      info.profile_dir = profile_dir_utf8.c_str();
      info.mojo_ipcz_config = mojo_ipcz_config;
      return ChromeAppModeStart(&info);
    }

    // If the shim was launched by chrome, simply quit. Chrome will detect that
    // the app shim has terminated, rebuild it (if it hadn't try to do so
    // already), and launch it again.
    if (executable_path.empty()) {
      NSLog(@"Loading Chrome failed, terminating");
      return kErrorReturnValue;
    }

    NSLog(@"Loading Chrome failed, launching Chrome with command line at %s",
          executable_path.value().c_str());
    base::CommandLine cr_command_line(executable_path);
    // The user_data_dir from the plist is actually the app data dir.
    cr_command_line.AppendSwitchPath(
        switches::kUserDataDir,
        plist_user_data_dir.DirName().DirName().DirName());
    // If the shim was launched directly (instead of by Chrome), first ask
    // Chrome to launch the app. Chrome will launch the shim again, the same
    // error might occur, after which chrome will try to regenerate the
    // shim.
    cr_command_line.AppendSwitchPath(switches::kProfileDirectory, profile_dir);
    cr_command_line.AppendSwitchASCII(switches::kAppId, app_mode_id);

    // If kLaunchChromeForTest was specified, this is a launch from a test.
    // In this case make sure to tell chrome to use a mock keychain, as
    // otherwise it might hang on startup.
    if (command_line.HasSwitch(app_mode::kLaunchChromeForTest)) {
      cr_command_line.AppendSwitch("use-mock-keychain");
    }

    // Launch the executable directly since base::mac::LaunchApplication doesn't
    // pass command line arguments if the application is already running.
    if (!base::LaunchProcess(cr_command_line, base::LaunchOptions())
             .IsValid()) {
      NSLog(@"Could not launch Chrome: %s",
            cr_command_line.GetCommandLineString().c_str());
      return kErrorReturnValue;
    }

    return 0;
  }
}

} // namespace

__attribute__((visibility("default")))
int main(int argc, char** argv) {
  // The static constructor in //base will have registered PartitionAlloc as the
  // default zone. Allow the //base instance in the main library to register it
  // as well. Otherwise we end up passing memory to free() which was allocated
  // by an unknown zone. See crbug.com/1274236 for details.
  partition_alloc::AllowDoublePartitionAllocZoneRegistration();

  base::CommandLine::Init(argc, argv);

  // Exit instead of returning to avoid the the removal of |main()| from stack
  // backtraces under tail call optimization.
  exit(LoadFrameworkAndStart(argc, argv));
}