|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 3 | + * SPDX-License-Identifier: GPL-2.0-or-later |
| 4 | + */ |
| 5 | + |
| 6 | +#include "macsandboxpersistentaccess.h" |
| 7 | + |
| 8 | +#include <QLoggingCategory> |
| 9 | +#include <QString> |
| 10 | + |
| 11 | +#import <Foundation/Foundation.h> |
| 12 | + |
| 13 | +Q_LOGGING_CATEGORY(lcMacSandboxPersistent, "nextcloud.common.mac.sandbox.persistent", QtInfoMsg) |
| 14 | + |
| 15 | +namespace OCC { |
| 16 | +namespace Utility { |
| 17 | + |
| 18 | +class MacSandboxPersistentAccess::Impl |
| 19 | +{ |
| 20 | +public: |
| 21 | + explicit Impl(const QByteArray &bookmarkData) |
| 22 | + : _nsUrl(nullptr) |
| 23 | + , _hasAccess(false) |
| 24 | + , _isStale(false) |
| 25 | + { |
| 26 | + if (bookmarkData.isEmpty()) { |
| 27 | + qCWarning(lcMacSandboxPersistent) << "Empty bookmark data provided"; |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + @autoreleasepool { |
| 32 | + NSData *nsBookmarkData = [NSData dataWithBytes:bookmarkData.constData() |
| 33 | + length:bookmarkData.size()]; |
| 34 | + if (!nsBookmarkData) { |
| 35 | + qCWarning(lcMacSandboxPersistent) << "Failed to create NSData from bookmark data"; |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + BOOL isStale = NO; |
| 40 | + NSError *error = nil; |
| 41 | + _nsUrl = [[NSURL URLByResolvingBookmarkData:nsBookmarkData |
| 42 | + options:NSURLBookmarkResolutionWithSecurityScope |
| 43 | + relativeToURL:nil |
| 44 | + bookmarkDataIsStale:&isStale |
| 45 | + error:&error] retain]; |
| 46 | + |
| 47 | + if (error) { |
| 48 | + qCWarning(lcMacSandboxPersistent) << "Failed to resolve bookmark data:" |
| 49 | + << QString::fromNSString([error localizedDescription]); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + if (!_nsUrl) { |
| 54 | + qCWarning(lcMacSandboxPersistent) << "Resolved URL is nil"; |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (isStale) { |
| 59 | + _isStale = true; |
| 60 | + qCWarning(lcMacSandboxPersistent) << "Bookmark data is stale for path:" |
| 61 | + << QString::fromNSString([_nsUrl path]) |
| 62 | + << "- the bookmark should be recreated"; |
| 63 | + } |
| 64 | + |
| 65 | + _hasAccess = [_nsUrl startAccessingSecurityScopedResource]; |
| 66 | + |
| 67 | + if (_hasAccess) { |
| 68 | + qCInfo(lcMacSandboxPersistent) << "Successfully started persistent access to security-scoped resource:" |
| 69 | + << QString::fromNSString([_nsUrl path]); |
| 70 | + } else { |
| 71 | + qCWarning(lcMacSandboxPersistent) << "Failed to start persistent access to security-scoped resource:" |
| 72 | + << QString::fromNSString([_nsUrl path]); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + ~Impl() |
| 78 | + { |
| 79 | + @autoreleasepool { |
| 80 | + if (_hasAccess && _nsUrl) { |
| 81 | + [_nsUrl stopAccessingSecurityScopedResource]; |
| 82 | + qCDebug(lcMacSandboxPersistent) << "Stopped persistent access to security-scoped resource"; |
| 83 | + _hasAccess = false; |
| 84 | + } |
| 85 | + |
| 86 | + if (_nsUrl) { |
| 87 | + [_nsUrl release]; |
| 88 | + _nsUrl = nullptr; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Non-copyable |
| 94 | + Impl(const Impl &) = delete; |
| 95 | + Impl &operator=(const Impl &) = delete; |
| 96 | + |
| 97 | + [[nodiscard]] bool hasAccess() const { return _hasAccess; } |
| 98 | + [[nodiscard]] bool isStale() const { return _isStale; } |
| 99 | + |
| 100 | +private: |
| 101 | + NSURL *_nsUrl; |
| 102 | + bool _hasAccess; |
| 103 | + bool _isStale; |
| 104 | +}; |
| 105 | + |
| 106 | +MacSandboxPersistentAccess::MacSandboxPersistentAccess(const QByteArray &bookmarkData) |
| 107 | + : _impl(std::make_unique<Impl>(bookmarkData)) |
| 108 | +{ |
| 109 | +} |
| 110 | + |
| 111 | +MacSandboxPersistentAccess::~MacSandboxPersistentAccess() = default; |
| 112 | + |
| 113 | +MacSandboxPersistentAccess::MacSandboxPersistentAccess(MacSandboxPersistentAccess &&) noexcept = default; |
| 114 | + |
| 115 | +MacSandboxPersistentAccess &MacSandboxPersistentAccess::operator=(MacSandboxPersistentAccess &&) noexcept = default; |
| 116 | + |
| 117 | +std::unique_ptr<MacSandboxPersistentAccess> MacSandboxPersistentAccess::createFromBookmarkData(const QByteArray &bookmarkData) |
| 118 | +{ |
| 119 | + if (bookmarkData.isEmpty()) { |
| 120 | + return nullptr; |
| 121 | + } |
| 122 | + return std::unique_ptr<MacSandboxPersistentAccess>(new MacSandboxPersistentAccess(bookmarkData)); |
| 123 | +} |
| 124 | + |
| 125 | +bool MacSandboxPersistentAccess::isValid() const |
| 126 | +{ |
| 127 | + return _impl && _impl->hasAccess(); |
| 128 | +} |
| 129 | + |
| 130 | +bool MacSandboxPersistentAccess::isStale() const |
| 131 | +{ |
| 132 | + return _impl && _impl->isStale(); |
| 133 | +} |
| 134 | + |
| 135 | +} // namespace Utility |
| 136 | +} // namespace OCC |
0 commit comments