| File: | Source/WebKit/UIProcess/EndowmentStateTracker.mm |
| Warning: | line 81, column 49 Call argument for parameter 'aString' is unretained and unsafe |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | /* |
| 2 | * Copyright (C) 2020 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "EndowmentStateTracker.h" |
| 28 | |
| 29 | #if PLATFORM(IOS_FAMILY)(defined 1 && 1) |
| 30 | |
| 31 | #import "Logging.h" |
| 32 | #import "RunningBoardServicesSPI.h" |
| 33 | #include <wtf/NeverDestroyed.h> |
| 34 | #include <wtf/RunLoop.h> |
| 35 | #include <wtf/TZoneMallocInlines.h> |
| 36 | |
| 37 | namespace WebKit { |
| 38 | |
| 39 | static NSString* visibilityEndowment = @"com.apple.frontboard.visibility"; |
| 40 | static NSString* userfacingEndowment = @"com.apple.launchservices.userfacing"; |
| 41 | |
| 42 | static RBSProcessHandle *handleForPID(pid_t pid) |
| 43 | { |
| 44 | RBSProcessIdentifier *processIdentifier = [RBSProcessIdentifier identifierWithPid:pid]; |
| 45 | if (!processIdentifier) { |
| 46 | RELEASE_LOG_ERROR(ProcessSuspension, "handleForPID: Failed to construct RBSProcessIdentifier from PID %d", pid)clang diagnostic push
clang diagnostic ignored "-Wunknown-warning-option" clang diagnostic ignored "-Wunsafe-buffer-usage" clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call" clang diagnostic ignored "-Wunsafe-buffer-usage-in-format-attr-call" [[clang ::suppress]] __extension__({ os_log_t _log_tmp = (WebKit2LogProcessSuspension .osLogChannel); os_log_type_t _type_tmp = OS_LOG_TYPE_ERROR; if (os_log_type_enabled(_log_tmp, _type_tmp)) { __extension__({ clang diagnostic push clang diagnostic ignored "-Wvla" clang diagnostic error "-Wformat" _Static_assert(__builtin_constant_p ("handleForPID: Failed to construct RBSProcessIdentifier from PID %d" ), "format/label/description argument must be a string constant" ); __attribute__((section("__TEXT,__oslogstring,cstring_literals" ),internal_linkage)) static const char _os_fmt_str[] __asm("LOS_LOG24" ) = "handleForPID: Failed to construct RBSProcessIdentifier from PID %d" ; uint8_t _Alignas(16) __attribute__((uninitialized)) _os_fmt_buf [__builtin_os_log_format_buffer_size("handleForPID: Failed to construct RBSProcessIdentifier from PID %d" , pid)]; _os_log_error_impl(&__dso_handle, _log_tmp, _type_tmp , _os_fmt_str, (uint8_t *)__builtin_os_log_format(_os_fmt_buf , "handleForPID: Failed to construct RBSProcessIdentifier from PID %d" , pid), (uint32_t)sizeof(_os_fmt_buf)) clang diagnostic pop ; }); } }) clang diagnostic pop ; |
| 47 | return nilnullptr; |
| 48 | } |
| 49 | |
| 50 | NSError *error = nilnullptr; |
| 51 | RBSProcessHandle *processHandle = [RBSProcessHandle handleForIdentifier:processIdentifier error:&error]; |
| 52 | if (!processHandle) { |
| 53 | RELEASE_LOG_ERROR(ProcessSuspension, "endowmentsForPid: Failed to get RBSProcessHandle for process with PID %d, error: %{public}@", pid, error)clang diagnostic push
clang diagnostic ignored "-Wunknown-warning-option" clang diagnostic ignored "-Wunsafe-buffer-usage" clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call" clang diagnostic ignored "-Wunsafe-buffer-usage-in-format-attr-call" [[clang ::suppress]] __extension__({ os_log_t _log_tmp = (WebKit2LogProcessSuspension .osLogChannel); os_log_type_t _type_tmp = OS_LOG_TYPE_ERROR; if (os_log_type_enabled(_log_tmp, _type_tmp)) { __extension__({ clang diagnostic push clang diagnostic ignored "-Wvla" clang diagnostic error "-Wformat" _Static_assert(__builtin_constant_p ("endowmentsForPid: Failed to get RBSProcessHandle for process with PID %d, error: %{public}@" ), "format/label/description argument must be a string constant" ); __attribute__((section("__TEXT,__oslogstring,cstring_literals" ),internal_linkage)) static const char _os_fmt_str[] __asm("LOS_LOG25" ) = "endowmentsForPid: Failed to get RBSProcessHandle for process with PID %d, error: %{public}@" ; uint8_t _Alignas(16) __attribute__((uninitialized)) _os_fmt_buf [__builtin_os_log_format_buffer_size("endowmentsForPid: Failed to get RBSProcessHandle for process with PID %d, error: %{public}@" , pid, error)]; _os_log_error_impl(&__dso_handle, _log_tmp , _type_tmp, _os_fmt_str, (uint8_t *)__builtin_os_log_format( _os_fmt_buf, "endowmentsForPid: Failed to get RBSProcessHandle for process with PID %d, error: %{public}@" , pid, error), (uint32_t)sizeof(_os_fmt_buf)) clang diagnostic pop ; }); } }) clang diagnostic pop ; |
| 54 | return nilnullptr; |
| 55 | } |
| 56 | |
| 57 | return processHandle; |
| 58 | } |
| 59 | |
| 60 | static NSSet<RBSProcessEndowmentInfo *> *endowmentInfosForHandle(RBSProcessHandle *processHandle) |
| 61 | { |
| 62 | if (!processHandle) |
| 63 | return nilnullptr; |
| 64 | |
| 65 | RBSProcessState *state = processHandle.currentState; |
| 66 | if (state.taskState != RBSTaskStateRunningScheduled) { |
| 67 | RELEASE_LOG_ERROR(ProcessSuspension, "endowmentInfosForHandle: Process with PID %d is not running", processHandle.pid)clang diagnostic push
clang diagnostic ignored "-Wunknown-warning-option" clang diagnostic ignored "-Wunsafe-buffer-usage" clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call" clang diagnostic ignored "-Wunsafe-buffer-usage-in-format-attr-call" [[clang ::suppress]] __extension__({ os_log_t _log_tmp = (WebKit2LogProcessSuspension .osLogChannel); os_log_type_t _type_tmp = OS_LOG_TYPE_ERROR; if (os_log_type_enabled(_log_tmp, _type_tmp)) { __extension__({ clang diagnostic push clang diagnostic ignored "-Wvla" clang diagnostic error "-Wformat" _Static_assert(__builtin_constant_p ("endowmentInfosForHandle: Process with PID %d is not running" ), "format/label/description argument must be a string constant" ); __attribute__((section("__TEXT,__oslogstring,cstring_literals" ),internal_linkage)) static const char _os_fmt_str[] __asm("LOS_LOG26" ) = "endowmentInfosForHandle: Process with PID %d is not running" ; uint8_t _Alignas(16) __attribute__((uninitialized)) _os_fmt_buf [__builtin_os_log_format_buffer_size("endowmentInfosForHandle: Process with PID %d is not running" , processHandle.pid)]; _os_log_error_impl(&__dso_handle, _log_tmp , _type_tmp, _os_fmt_str, (uint8_t *)__builtin_os_log_format( _os_fmt_buf, "endowmentInfosForHandle: Process with PID %d is not running" , processHandle.pid), (uint32_t)sizeof(_os_fmt_buf)) clang diagnostic pop ; }); } }) clang diagnostic pop ; |
| 68 | return nilnullptr; |
| 69 | } |
| 70 | |
| 71 | return state.endowmentInfos; |
| 72 | } |
| 73 | |
| 74 | WTF_MAKE_TZONE_ALLOCATED_IMPL(EndowmentStateTracker)::bmalloc::api::HeapRef EndowmentStateTracker::s_heapRef; const TZoneSpecification EndowmentStateTracker::s_heapSpec = { & EndowmentStateTracker::s_heapRef, TZoneSpecification::encodeSize <EndowmentStateTracker>(), TZoneSpecification::encodeAlignment <EndowmentStateTracker>(), TZoneSpecification::encodeCategory <EndowmentStateTracker>(), ::bmalloc::api::compactAllocationMode <EndowmentStateTracker>(), TZoneSpecification::encodeDescriptor <EndowmentStateTracker>(), }; void* EndowmentStateTracker ::operatorNewSlow(size_t size) { if constexpr (::bmalloc::api ::compactAllocationMode<EndowmentStateTracker>() == CompactAllocationMode ::Compact) return ::bmalloc::api::tzoneAllocateCompactSlow(size , s_heapSpec); return ::bmalloc::api::tzoneAllocateNonCompactSlow (size, s_heapSpec); } using __makeBtzoneMallocedInlineMacroSemicolonifier __attribute__((unused)) = int; |
| 75 | |
| 76 | inline auto EndowmentStateTracker::stateFromEndowmentInfos(NSSet *endowmentInfos) -> State |
| 77 | { |
| 78 | State state; |
| 79 | for (RBSProcessEndowmentInfo *info in endowmentInfos) { |
| 80 | NSString *endowmentNamespace = info.endowmentNamespace; |
| 81 | if ([endowmentNamespace isEqualToString:userfacingEndowment]) |
Call argument for parameter 'aString' is unretained and unsafe | |
| 82 | state.isUserFacing = true; |
| 83 | else if ([endowmentNamespace isEqualToString:visibilityEndowment]) { |
| 84 | state.isVisible = true; |
| 85 | if (NSString *environment = info.environment) |
| 86 | state.visibilityEnvironments.add(String(environment)); |
| 87 | } |
| 88 | } |
| 89 | return state; |
| 90 | } |
| 91 | |
| 92 | bool EndowmentStateTracker::isApplicationForeground(pid_t pid) |
| 93 | { |
| 94 | return stateForHandle(handleForPID(pid)).isVisible; |
| 95 | } |
| 96 | |
| 97 | auto EndowmentStateTracker::stateForHandle(RBSProcessHandle *processHandle) -> State |
| 98 | { |
| 99 | if (RetainPtr endowmentInfos = endowmentInfosForHandle(processHandle)) |
| 100 | return stateFromEndowmentInfos(endowmentInfos.get()); |
| 101 | |
| 102 | // Assume foreground when unable to determine state to maintain pre-existing behavior |
| 103 | // and to avoid not rendering anything when we fail. |
| 104 | return State { true, true, { } }; |
| 105 | } |
| 106 | |
| 107 | EndowmentStateTracker& EndowmentStateTracker::singleton() |
| 108 | { |
| 109 | static auto tracker = NeverDestroyed<EndowmentStateTracker>(); |
| 110 | return tracker; |
| 111 | } |
| 112 | |
| 113 | void EndowmentStateTracker::registerMonitorIfNecessary() |
| 114 | { |
| 115 | if (m_processMonitor) |
| 116 | return; |
| 117 | |
| 118 | m_processMonitor = [RBSProcessMonitor monitorWithConfiguration:[this] (id<RBSProcessMonitorConfiguring> config) { |
| 119 | [config setPredicates:@[[RBSProcessPredicate predicateMatchingHandle:[RBSProcessHandle currentProcess]]]]; |
| 120 | |
| 121 | RBSProcessStateDescriptor *stateDescriptor = [RBSProcessStateDescriptor descriptor]; |
| 122 | stateDescriptor.endowmentNamespaces = @[visibilityEndowment, userfacingEndowment]; |
| 123 | stateDescriptor.values = RBSProcessStateValueEndowmentInfos; |
| 124 | [config setStateDescriptor:stateDescriptor]; |
| 125 | |
| 126 | [config setUpdateHandler:[this] (RBSProcessMonitor * _Nonnull monitor, RBSProcessHandle * _Nonnull process, RBSProcessStateUpdate * _Nonnull update) mutable { |
| 127 | RunLoop::mainSingleton().dispatch([this, state = stateFromEndowmentInfos(update.state.endowmentInfos)]() mutable { |
| 128 | setState(WTF::move(state)); |
| 129 | }); |
| 130 | }]; |
| 131 | }]; |
| 132 | } |
| 133 | |
| 134 | void EndowmentStateTracker::addClient(EndowmentStateTrackerClient& client) |
| 135 | { |
| 136 | m_clients.add(client); |
| 137 | registerMonitorIfNecessary(); |
| 138 | } |
| 139 | |
| 140 | void EndowmentStateTracker::removeClient(EndowmentStateTrackerClient& client) |
| 141 | { |
| 142 | m_clients.remove(client); |
| 143 | } |
| 144 | |
| 145 | auto EndowmentStateTracker::ensureState() const -> const State& |
| 146 | { |
| 147 | if (!m_state) |
| 148 | m_state = stateForHandle([RBSProcessHandle currentProcess]); |
| 149 | return *m_state; |
| 150 | } |
| 151 | |
| 152 | void EndowmentStateTracker::setState(State&& state) |
| 153 | { |
| 154 | bool isUserFacingChanged = !m_state || m_state->isUserFacing != state.isUserFacing; |
| 155 | bool isVisibleChanged = !m_state || m_state->isVisible != state.isVisible; |
| 156 | bool visibilityEnvironmentsChanged = !m_state || m_state->visibilityEnvironments != state.visibilityEnvironments; |
| 157 | if (!isUserFacingChanged && !isVisibleChanged && !visibilityEnvironmentsChanged) |
| 158 | return; |
| 159 | |
| 160 | m_state = WTF::move(state); |
| 161 | |
| 162 | RELEASE_LOG(ViewState, "%p - EndowmentStateTracker::setState() isUserFacing: %{public}s isVisible: %{public}s visibilityEnvironmentCount: %u", this, m_state->isUserFacing ? "true" : "false", m_state->isVisible ? "true" : "false", m_state->visibilityEnvironments.size())clang diagnostic push
clang diagnostic ignored "-Wunknown-warning-option" clang diagnostic ignored "-Wunsafe-buffer-usage" clang diagnostic ignored "-Wunsafe-buffer-usage-in-libc-call" clang diagnostic ignored "-Wunsafe-buffer-usage-in-format-attr-call" [[clang ::suppress]] __extension__({ os_log_t _log_tmp = (WebKit2LogViewState .osLogChannel); os_log_type_t _type_tmp = (OS_LOG_TYPE_DEFAULT ); if (os_log_type_enabled(_log_tmp, _type_tmp)) { __extension__ ({ clang diagnostic push clang diagnostic ignored "-Wvla" clang diagnostic error "-Wformat" _Static_assert(__builtin_constant_p ("%p - EndowmentStateTracker::setState() isUserFacing: %{public}s isVisible: %{public}s visibilityEnvironmentCount: %u" ), "format/label/description argument must be a string constant" ); __attribute__((section("__TEXT,__oslogstring,cstring_literals" ),internal_linkage)) static const char _os_fmt_str[] __asm("LOS_LOG27" ) = "%p - EndowmentStateTracker::setState() isUserFacing: %{public}s isVisible: %{public}s visibilityEnvironmentCount: %u" ; uint8_t _Alignas(16) __attribute__((uninitialized)) _os_fmt_buf [__builtin_os_log_format_buffer_size("%p - EndowmentStateTracker::setState() isUserFacing: %{public}s isVisible: %{public}s visibilityEnvironmentCount: %u" , this, m_state->isUserFacing ? "true" : "false", m_state-> isVisible ? "true" : "false", m_state->visibilityEnvironments .size())]; _os_log_impl(&__dso_handle, _log_tmp, _type_tmp , _os_fmt_str, (uint8_t *)__builtin_os_log_format(_os_fmt_buf , "%p - EndowmentStateTracker::setState() isUserFacing: %{public}s isVisible: %{public}s visibilityEnvironmentCount: %u" , this, m_state->isUserFacing ? "true" : "false", m_state-> isVisible ? "true" : "false", m_state->visibilityEnvironments .size()), (uint32_t)sizeof(_os_fmt_buf)) clang diagnostic pop ; }); } }) clang diagnostic pop ; |
| 163 | |
| 164 | m_clients.forEach([&](auto& client) { |
| 165 | if (isUserFacingChanged) |
| 166 | client.isUserFacingChanged(m_state->isUserFacing); |
| 167 | if (isVisibleChanged) |
| 168 | client.isVisibleChanged(m_state->isVisible); |
| 169 | if (visibilityEnvironmentsChanged) |
| 170 | client.visibilityEndowmentEnvironmentsChanged(m_state->visibilityEnvironments); |
| 171 | }); |
| 172 | } |
| 173 | |
| 174 | #if ENABLE(ENDOWMENT_BASED_APPLICATION_STATE_TRACKING)(defined ENABLE_ENDOWMENT_BASED_APPLICATION_STATE_TRACKING && ENABLE_ENDOWMENT_BASED_APPLICATION_STATE_TRACKING) |
| 175 | void EndowmentStateTracker::setStateForTesting(bool isUserFacing, bool isVisible) |
| 176 | { |
| 177 | setState(State { isUserFacing, isVisible }); |
| 178 | } |
| 179 | #endif |
| 180 | |
| 181 | } |
| 182 | |
| 183 | #endif // PLATFORM(IOS_FAMILY) |