xref: /aosp_15_r20/external/cronet/base/base_paths_mac.mm (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1// Copyright 2012 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// Defines base::PathProviderMac which replaces base::PathProviderPosix for Mac
6// in base/path_service.cc.
7
8#import <Foundation/Foundation.h>
9
10#include "base/apple/bundle_locations.h"
11#include "base/apple/foundation_util.h"
12#include "base/base_paths.h"
13#include "base/base_paths_apple.h"
14#include "base/files/file_path.h"
15#include "base/files/file_util.h"
16#include "base/notreached.h"
17#include "base/path_service.h"
18
19namespace base {
20
21bool PathProviderMac(int key, base::FilePath* result) {
22  switch (key) {
23    case base::FILE_EXE:
24      *result = base::apple::internal::GetExecutablePath();
25      return true;
26    case base::FILE_MODULE:
27      return base::apple::internal::GetModulePathForAddress(
28          result, reinterpret_cast<const void*>(&base::PathProviderMac));
29    case base::DIR_APP_DATA: {
30      bool success =
31          base::apple::GetUserDirectory(NSApplicationSupportDirectory, result);
32      return success;
33    }
34    case base::DIR_SRC_TEST_DATA_ROOT:
35      // Go through PathService to catch overrides.
36      if (!PathService::Get(base::FILE_EXE, result)) {
37        return false;
38      }
39
40      // Start with the executable's directory.
41      *result = result->DirName();
42
43      if (base::apple::AmIBundled()) {
44        // The bundled app executables (Chromium, TestShell, etc) live five
45        // levels down, eg:
46        // src/xcodebuild/{Debug|Release}/Chromium.app/Contents/MacOS/Chromium
47        *result = result->DirName().DirName().DirName().DirName().DirName();
48      } else {
49        // Unit tests execute two levels deep from the source root, eg:
50        // src/xcodebuild/{Debug|Release}/base_unittests
51        *result = result->DirName().DirName();
52      }
53      return true;
54    case base::DIR_USER_DESKTOP:
55      return base::apple::GetUserDirectory(NSDesktopDirectory, result);
56    case base::DIR_ASSETS:
57      if (!base::apple::AmIBundled()) {
58        return PathService::Get(base::DIR_MODULE, result);
59      }
60      *result = base::apple::FrameworkBundlePath().Append(
61          FILE_PATH_LITERAL("Resources"));
62      return true;
63    case base::DIR_CACHE:
64      return base::apple::GetUserDirectory(NSCachesDirectory, result);
65    default:
66      return false;
67  }
68}
69
70}  // namespace base
71