1/*
2 *  Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11// This file only exists because various iOS and macOS system APIs are only
12// available from Objective-C. See unixfilesystem.cc for the only use
13// (enforced by a lack of a header file).
14
15#import <Foundation/NSPathUtilities.h>
16#import <Foundation/NSProcessInfo.h>
17#include <string.h>
18
19#include "webrtc/base/checks.h"
20#include "webrtc/base/common.h"
21#include "webrtc/base/pathutils.h"
22
23// Return a new[]'d |char*| copy of the UTF8 representation of |s|.
24// Caller owns the returned memory and must use delete[] on it.
25static char* copyString(NSString* s) {
26  const char* utf8 = [s UTF8String];
27  size_t len = strlen(utf8) + 1;
28  char* copy = new char[len];
29  // This uses a new[] + strcpy (instead of strdup) because the
30  // receiver expects to be able to delete[] the returned pointer
31  // (instead of free()ing it).
32  strcpy(copy, utf8);
33  return copy;
34}
35
36// Return a (leaked) copy of a directory name suitable for application data.
37char* AppleDataDirectory() {
38  NSArray* paths = NSSearchPathForDirectoriesInDomains(
39      NSApplicationSupportDirectory, NSUserDomainMask, YES);
40  RTC_DCHECK([paths count] == 1);
41  return copyString([paths objectAtIndex:0]);
42}
43
44// Return a (leaked) copy of a directory name suitable for use as a $TEMP.
45char* AppleTempDirectory() {
46  return copyString(NSTemporaryDirectory());
47}
48
49// Return the binary's path.
50void AppleAppName(rtc::Pathname* path) {
51  NSProcessInfo* pInfo = [NSProcessInfo processInfo];
52  NSString* argv0 = [[pInfo arguments] objectAtIndex:0];
53  path->SetPathname([argv0 UTF8String]);
54}
55