From bff02013c91cb2ef10db6dbcdce7cc8936d1fdbd Mon Sep 17 00:00:00 2001 From: Nick Bourdakos Date: Tue, 12 Aug 2025 13:50:56 -0700 Subject: [PATCH] Add implementation for log file path PiperOrigin-RevId: 794256202 --- internal/platform/implementation/apple/BUILD | 12 ++++ .../implementation/apple/GNCDevicePaths.h | 36 ++++++++++++ .../implementation/apple/GNCDevicePaths.m | 46 +++++++++++++++ .../implementation/apple/device_info.mm | 56 ++----------------- 4 files changed, 98 insertions(+), 52 deletions(-) create mode 100644 internal/platform/implementation/apple/GNCDevicePaths.h create mode 100644 internal/platform/implementation/apple/GNCDevicePaths.m diff --git a/internal/platform/implementation/apple/BUILD b/internal/platform/implementation/apple/BUILD index 415d6371..7feba254 100644 --- a/internal/platform/implementation/apple/BUILD +++ b/internal/platform/implementation/apple/BUILD @@ -77,6 +77,7 @@ objc_library( aspect_hints = ["//tools/build_defs/swift:no_module"], features = ["-layering_check"], deps = [ + ":GNCDevicePaths", ":comm", ":Platform_cc", ":Shared", @@ -102,6 +103,7 @@ objc_library( "//internal/platform/implementation/apple/Mediums", "//internal/platform/implementation/shared:file", "//internal/platform/implementation/apple/Log:GNCLogger", + "//internal/platform:cancellation_flag", ] + select({ "@platforms//os:platform_ios": [ "//third_party/apple_frameworks:UIKit", @@ -225,6 +227,16 @@ objc_library( ], ) +objc_library( + name = "GNCDevicePaths", + srcs = ["GNCDevicePaths.m"], + hdrs = ["GNCDevicePaths.h"], + deps = [ + "//internal/platform/implementation/apple/Log:GNCLogger", + "//third_party/apple_frameworks:Foundation", + ], +) + cc_library( name = "Platform_cc", srcs = [ diff --git a/internal/platform/implementation/apple/GNCDevicePaths.h b/internal/platform/implementation/apple/GNCDevicePaths.h new file mode 100644 index 00000000..e3dab1cb --- /dev/null +++ b/internal/platform/implementation/apple/GNCDevicePaths.h @@ -0,0 +1,36 @@ +// Copyright 2025 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import + +NS_ASSUME_NONNULL_BEGIN + +#ifdef __cplusplus +extern "C" { +#endif + +// Returns the path to the LocalAppData directory for the given app path. +NSURL *_Nullable GNCLocalAppDataPath(); + +// Returns the path to the log directory. +NSURL *_Nullable GNCLogPath(); + +// Returns the path to the crash dump directory. +NSURL *_Nullable GNCCrashDumpPath(); + +#ifdef __cplusplus +} // extern "C" +#endif + +NS_ASSUME_NONNULL_END diff --git a/internal/platform/implementation/apple/GNCDevicePaths.m b/internal/platform/implementation/apple/GNCDevicePaths.m new file mode 100644 index 00000000..b9f6af0d --- /dev/null +++ b/internal/platform/implementation/apple/GNCDevicePaths.m @@ -0,0 +1,46 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#import "internal/platform/implementation/apple/GNCDevicePaths.h" + +#import "internal/platform/implementation/apple/Log/GNCLogger.h" + +NS_ASSUME_NONNULL_BEGIN + +NSURL *_Nullable GNCLocalAppDataPath() { + NSFileManager *manager = [NSFileManager defaultManager]; + + NSError *error = nil; + NSURL *applicationSupportURL = [manager URLForDirectory:NSApplicationSupportDirectory + inDomain:NSUserDomainMask + appropriateForURL:nil + create:YES + error:&error]; + if (!applicationSupportURL) { + GNCLoggerError(@"Failed to get application support path: %@", error); + return nil; + } + + return applicationSupportURL; +} + +NSURL *_Nullable GNCLogPath() { + return [GNCLocalAppDataPath() URLByAppendingPathComponent:@"Google/Nearby/Sharing/Logs"]; +} + +NSURL *_Nullable GNCCrashDumpPath() { + return [GNCLocalAppDataPath() URLByAppendingPathComponent:@"Google/Nearby/Sharing/CrashDumps"]; +} + +NS_ASSUME_NONNULL_END diff --git a/internal/platform/implementation/apple/device_info.mm b/internal/platform/implementation/apple/device_info.mm index 6e3fdc02..08dc3536 100644 --- a/internal/platform/implementation/apple/device_info.mm +++ b/internal/platform/implementation/apple/device_info.mm @@ -28,6 +28,7 @@ #include "internal/base/file_path.h" #include "internal/platform/implementation/device_info.h" +#import "internal/platform/implementation/apple/GNCDevicePaths.h" #import "internal/platform/implementation/apple/Log/GNCLogger.h" namespace nearby { @@ -96,20 +97,7 @@ std::optional DeviceInfo::GetDownloadPath() const { } std::optional DeviceInfo::GetLocalAppDataPath() const { - NSFileManager *manager = [NSFileManager defaultManager]; - - NSError *error = nil; - NSURL *applicationSupportURL = [manager URLForDirectory:NSApplicationSupportDirectory - inDomain:NSUserDomainMask - appropriateForURL:nil - create:YES - error:&error]; - if (!applicationSupportURL) { - GNCLoggerError(@"Failed to get application support path: %@", error); - return std::nullopt; - } - - return FilePath(absl::string_view([applicationSupportURL.path cString])); + return FilePath(absl::string_view([GNCLocalAppDataPath().path cString])); } std::optional DeviceInfo::GetCommonAppDataPath() const { return GetLocalAppDataPath(); } @@ -119,47 +107,11 @@ std::optional DeviceInfo::GetTemporaryPath() const { } std::optional DeviceInfo::GetLogPath() const { - NSFileManager *manager = [NSFileManager defaultManager]; - - NSError *error = nil; - NSURL *applicationSupportURL = [manager URLForDirectory:NSApplicationSupportDirectory - inDomain:NSUserDomainMask - appropriateForURL:nil - create:YES - error:&error]; - if (!applicationSupportURL) { - GNCLoggerError(@"Failed to get application support path: %@", error); - return std::nullopt; - } - - // TODO(b/276937308): This should not hard-code Nearby Share's log directory, but this matches the - // current Windows implmementation. - NSURL *logsURL = - [applicationSupportURL URLByAppendingPathComponent:@"Google/Nearby/Sharing/Logs"]; - - return FilePath(absl::string_view([logsURL.path cString])); + return FilePath(absl::string_view([GNCLogPath().path cString])); } std::optional DeviceInfo::GetCrashDumpPath() const { - NSFileManager *manager = [NSFileManager defaultManager]; - - NSError *error = nil; - NSURL *applicationSupportURL = [manager URLForDirectory:NSApplicationSupportDirectory - inDomain:NSUserDomainMask - appropriateForURL:nil - create:YES - error:&error]; - if (!applicationSupportURL) { - GNCLoggerError(@"Failed to get application support path: %@", error); - return std::nullopt; - } - - // TODO(b/276937308): This should not hard-code Nearby Share's crash dump directory, but this - // matches the current Windows implmementation. - NSURL *crashDumpsURL = - [applicationSupportURL URLByAppendingPathComponent:@"Google/Nearby/Sharing/CrashDumps"]; - - return FilePath(absl::string_view([crashDumpsURL.path cString])); + return FilePath(absl::string_view([GNCCrashDumpPath().path cString])); } bool DeviceInfo::IsScreenLocked() const { return false; }