xref: /aosp_15_r20/external/cronet/base/base_paths_ios.mm (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1// Copyright 2023 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::PathProviderIOS which replaces base::PathProviderPosix for iOS
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/path_service.h"
17
18namespace base {
19
20bool PathProviderIOS(int key, base::FilePath* result) {
21  switch (key) {
22    case base::FILE_EXE:
23      *result = base::apple::internal::GetExecutablePath();
24      return true;
25
26    case base::DIR_APP_DATA: {
27      base::FilePath path;
28      if (!base::apple::GetUserDirectory(NSApplicationSupportDirectory,
29                                         &path)) {
30        return false;
31      }
32
33      // On iOS, this directory does not exist unless it is created explicitly.
34      if (!base::PathExists(path) && !base::CreateDirectory(path)) {
35        return false;
36      }
37
38      *result = path;
39      return true;
40    }
41
42    case base::DIR_SRC_TEST_DATA_ROOT:
43    case base::DIR_OUT_TEST_DATA_ROOT:
44      // On iOS, there is no access to source root, nor build dir,
45      // however, the necessary resources are packaged into the
46      // test app as assets.
47      [[fallthrough]];
48
49    case base::DIR_ASSETS:
50      // On iOS, the resources are located at the root of the framework bundle.
51      *result = base::apple::FrameworkBundlePath();
52#if BUILDFLAG(IS_IOS_MACCATALYST)
53      // When running in the catalyst environment (i.e. building an iOS app
54      // to run on macOS), the bundles have the same structure as macOS, so
55      // the resources are in the "Contents/Resources" sub-directory.
56      *result = result->Append(FILE_PATH_LITERAL("Contents"))
57                    .Append(FILE_PATH_LITERAL("Resources"));
58#endif  // BUILDFLAG(IS_IOS_MACCATALYST)
59      return true;
60
61    case base::DIR_CACHE:
62      return base::apple::GetUserDirectory(NSCachesDirectory, result);
63
64    default:
65      return false;
66  }
67}
68
69}  // namespace base
70