| File: | Source/WebKit/UIProcess/EndowmentStateTracker.mm |
| Warning: | line 86, column 27 Call argument 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, "handleForPID: 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 ("handleForPID: 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" ) = "handleForPID: 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("handleForPID: 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, "handleForPID: 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 | 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; |
| 61 | |
| 62 | inline auto EndowmentStateTracker::stateFromProcessState(RBSProcessState *processState) -> State |
| 63 | { |
| 64 | State state; |
| 65 | #if ENABLE(ENDOWMENT_BASED_APPLICATION_STATE_TRACKING)(defined ENABLE_ENDOWMENT_BASED_APPLICATION_STATE_TRACKING && ENABLE_ENDOWMENT_BASED_APPLICATION_STATE_TRACKING) |
| 66 | for (RBSProcessEndowmentInfo *info in processState.endowmentInfos) { |
| 67 | NSString *endowmentNamespace = info.endowmentNamespace; |
| 68 | if ([endowmentNamespace isEqualToString:userfacingEndowment]) |
| 69 | state.isUserFacing = true; |
| 70 | else if ([endowmentNamespace isEqualToString:visibilityEndowment]) { |
| 71 | state.isVisible = true; |
| 72 | if (NSString *environment = info.environment) |
| 73 | state.visibilityEnvironments.add(String(environment)); |
| 74 | } |
| 75 | } |
| 76 | #else |
| 77 | NSSet<NSString *> *endowmentNamespaces = processState.endowmentNamespaces; |
| 78 | state.isUserFacing = [endowmentNamespaces containsObject:userfacingEndowment]; |
| 79 | state.isVisible = [endowmentNamespaces containsObject:visibilityEndowment]; |
| 80 | #endif |
| 81 | return state; |
| 82 | } |
| 83 | |
| 84 | bool EndowmentStateTracker::isApplicationForeground(pid_t pid) |
| 85 | { |
| 86 | return stateForHandle(handleForPID(pid)).isVisible; |
Call argument is unretained and unsafe | |
| 87 | } |
| 88 | |
| 89 | auto EndowmentStateTracker::stateForHandle(RBSProcessHandle *processHandle) -> State |
| 90 | { |
| 91 | if (processHandle) { |
| 92 | RBSProcessState *processState = processHandle.currentState; |
| 93 | if (processState.taskState == RBSTaskStateRunningScheduled) |
| 94 | return stateFromProcessState(processState); |
| 95 | RELEASE_LOG_ERROR(ProcessSuspension, "stateForHandle: 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 ("stateForHandle: 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" ) = "stateForHandle: Process with PID %d is not running"; uint8_t _Alignas(16) __attribute__((uninitialized)) _os_fmt_buf[__builtin_os_log_format_buffer_size ("stateForHandle: 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 , "stateForHandle: Process with PID %d is not running", processHandle .pid), (uint32_t)sizeof(_os_fmt_buf)) clang diagnostic pop ; } ); } }) clang diagnostic pop ; |
| 96 | } |
| 97 | |
| 98 | // Assume foreground when unable to determine state to maintain pre-existing behavior |
| 99 | // and to avoid not rendering anything when we fail. |
| 100 | return State { true, true, { } }; |
| 101 | } |
| 102 | |
| 103 | EndowmentStateTracker& EndowmentStateTracker::singleton() |
| 104 | { |
| 105 | static auto tracker = NeverDestroyed<EndowmentStateTracker>(); |
| 106 | return tracker; |
| 107 | } |
| 108 | |
| 109 | void EndowmentStateTracker::registerMonitorIfNecessary() |
| 110 | { |
| 111 | if (m_processMonitor) |
| 112 | return; |
| 113 | |
| 114 | m_processMonitor = [RBSProcessMonitor monitorWithConfiguration:[this] (id<RBSProcessMonitorConfiguring> config) { |
| 115 | [config setPredicates:@[[RBSProcessPredicate predicateMatchingHandle:[RBSProcessHandle currentProcess]]]]; |
| 116 | |
| 117 | RBSProcessStateDescriptor *stateDescriptor = [RBSProcessStateDescriptor descriptor]; |
| 118 | stateDescriptor.endowmentNamespaces = @[visibilityEndowment, userfacingEndowment]; |
| 119 | #if ENABLE(ENDOWMENT_BASED_APPLICATION_STATE_TRACKING)(defined ENABLE_ENDOWMENT_BASED_APPLICATION_STATE_TRACKING && ENABLE_ENDOWMENT_BASED_APPLICATION_STATE_TRACKING) |
| 120 | stateDescriptor.values = RBSProcessStateValueEndowmentInfos; |
| 121 | #endif |
| 122 | [config setStateDescriptor:stateDescriptor]; |
| 123 | |
| 124 | [config setUpdateHandler:[this] (RBSProcessMonitor * _Nonnull monitor, RBSProcessHandle * _Nonnull process, RBSProcessStateUpdate * _Nonnull update) mutable { |
| 125 | RunLoop::mainSingleton().dispatch([this, state = stateFromProcessState(update.state)]() mutable { |
| 126 | setState(WTF::move(state)); |
| 127 | }); |
| 128 | }]; |
| 129 | }]; |
| 130 | } |
| 131 | |
| 132 | void EndowmentStateTracker::addClient(EndowmentStateTrackerClient& client) |
| 133 | { |
| 134 | m_clients.add(client); |
| 135 | registerMonitorIfNecessary(); |
| 136 | } |
| 137 | |
| 138 | void EndowmentStateTracker::removeClient(EndowmentStateTrackerClient& client) |
| 139 | { |
| 140 | m_clients.remove(client); |
| 141 | } |
| 142 | |
| 143 | auto EndowmentStateTracker::ensureState() const -> const State& |
| 144 | { |
| 145 | if (!m_state) |
| 146 | m_state = stateForHandle([RBSProcessHandle currentProcess]); |
| 147 | return *m_state; |
| 148 | } |
| 149 | |
| 150 | void EndowmentStateTracker::setState(State&& state) |
| 151 | { |
| 152 | bool isUserFacingChanged = !m_state || m_state->isUserFacing != state.isUserFacing; |
| 153 | bool isVisibleChanged = !m_state || m_state->isVisible != state.isVisible; |
| 154 | bool visibilityEnvironmentsChanged = !m_state || m_state->visibilityEnvironments != state.visibilityEnvironments; |
| 155 | if (!isUserFacingChanged && !isVisibleChanged && !visibilityEnvironmentsChanged) |
| 156 | return; |
| 157 | |
| 158 | m_state = WTF::move(state); |
| 159 | |
| 160 | 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 ; |
| 161 | |
| 162 | m_clients.forEach([&](auto& client) { |
| 163 | if (isUserFacingChanged) |
| 164 | client.isUserFacingChanged(m_state->isUserFacing); |
| 165 | if (isVisibleChanged) |
| 166 | client.isVisibleChanged(m_state->isVisible); |
| 167 | if (visibilityEnvironmentsChanged) |
| 168 | client.visibilityEndowmentEnvironmentsChanged(m_state->visibilityEnvironments); |
| 169 | }); |
| 170 | } |
| 171 | |
| 172 | #if ENABLE(ENDOWMENT_BASED_APPLICATION_STATE_TRACKING)(defined ENABLE_ENDOWMENT_BASED_APPLICATION_STATE_TRACKING && ENABLE_ENDOWMENT_BASED_APPLICATION_STATE_TRACKING) |
| 173 | void EndowmentStateTracker::setStateForTesting(bool isUserFacing, bool isVisible) |
| 174 | { |
| 175 | setState(State { isUserFacing, isVisible }); |
| 176 | } |
| 177 | #endif |
| 178 | |
| 179 | } |
| 180 | |
| 181 | #endif // PLATFORM(IOS_FAMILY) |