Title: [259827] trunk/Source/WebCore
Revision
259827
Author
[email protected]
Date
2020-04-09 14:04:46 -0700 (Thu, 09 Apr 2020)

Log Message

[Cocoa] The function WebCore::systemHasBattery() should cache the result.
https://bugs.webkit.org/show_bug.cgi?id=210296
<rdar://problem/61331536>

Reviewed by Darin Adler.

The function WebCore::systemHasBattery() should cache the result, since the return value of this function
will be the same on a specific device.

No new tests, since there is no change in behavior.

* platform/cocoa/SystemBattery.mm:
(WebCore::systemHasBattery):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (259826 => 259827)


--- trunk/Source/WebCore/ChangeLog	2020-04-09 20:51:59 UTC (rev 259826)
+++ trunk/Source/WebCore/ChangeLog	2020-04-09 21:04:46 UTC (rev 259827)
@@ -1,3 +1,19 @@
+2020-04-09  Per Arne Vollan  <[email protected]>
+
+        [Cocoa] The function WebCore::systemHasBattery() should cache the result.
+        https://bugs.webkit.org/show_bug.cgi?id=210296
+        <rdar://problem/61331536>
+
+        Reviewed by Darin Adler.
+
+        The function WebCore::systemHasBattery() should cache the result, since the return value of this function
+        will be the same on a specific device.
+
+        No new tests, since there is no change in behavior.
+
+        * platform/cocoa/SystemBattery.mm:
+        (WebCore::systemHasBattery):
+
 2020-04-09  Keith Rollin  <[email protected]>
 
         Set ENTITLEMENTS_REQUIRED=NO for some Xcode build targets

Modified: trunk/Source/WebCore/platform/cocoa/SystemBattery.mm (259826 => 259827)


--- trunk/Source/WebCore/platform/cocoa/SystemBattery.mm	2020-04-09 20:51:59 UTC (rev 259826)
+++ trunk/Source/WebCore/platform/cocoa/SystemBattery.mm	2020-04-09 21:04:46 UTC (rev 259827)
@@ -39,22 +39,25 @@
 
 bool systemHasBattery()
 {
-    if (hasBattery.hasValue())
-        return *hasBattery;
+    if (!hasBattery.hasValue()) {
+        hasBattery = [] {
+            RetainPtr<CFTypeRef> powerSourcesInfo = adoptCF(IOPSCopyPowerSourcesInfo());
+            if (!powerSourcesInfo)
+                return false;
+            RetainPtr<CFArrayRef> powerSourcesList = adoptCF(IOPSCopyPowerSourcesList(powerSourcesInfo.get()));
+            if (!powerSourcesList)
+                return false;
+            for (CFIndex i = 0, count = CFArrayGetCount(powerSourcesList.get()); i < count; ++i) {
+                CFDictionaryRef description = IOPSGetPowerSourceDescription(powerSourcesInfo.get(), CFArrayGetValueAtIndex(powerSourcesList.get(), i));
+                CFTypeRef value = CFDictionaryGetValue(description, CFSTR(kIOPSTypeKey));
+                if (!value || CFEqual(value, CFSTR(kIOPSInternalBatteryType)))
+                    return true;
+            }
+            return false;
+        }();
+    }
 
-    RetainPtr<CFTypeRef> powerSourcesInfo = adoptCF(IOPSCopyPowerSourcesInfo());
-    if (!powerSourcesInfo)
-        return false;
-    RetainPtr<CFArrayRef> powerSourcesList = adoptCF(IOPSCopyPowerSourcesList(powerSourcesInfo.get()));
-    if (!powerSourcesList)
-        return false;
-    for (CFIndex i = 0, count = CFArrayGetCount(powerSourcesList.get()); i < count; ++i) {
-        CFDictionaryRef description = IOPSGetPowerSourceDescription(powerSourcesInfo.get(), CFArrayGetValueAtIndex(powerSourcesList.get(), i));
-        CFTypeRef value = CFDictionaryGetValue(description, CFSTR(kIOPSTypeKey));
-        if (!value || CFEqual(value, CFSTR(kIOPSInternalBatteryType)))
-            return true;
-    }
-    return false;
+    return *hasBattery;
 }
 
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to