[Cocoa] Broker access to the PowerManagement API
authorpvollan@apple.com <pvollan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 23 Jan 2020 20:10:47 +0000 (20:10 +0000)
committerpvollan@apple.com <pvollan@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 23 Jan 2020 20:10:47 +0000 (20:10 +0000)
https://bugs.webkit.org/show_bug.cgi?id=206373
Source/WebCore:

Reviewed by Darin Adler.

Code to determine if device has battery should be executed in the UI process.

API test: WebKit.SystemHasBattery

* WebCore.xcodeproj/project.pbxproj:
* platform/cocoa/SystemBattery.h: Added.
* platform/cocoa/SystemBattery.mm: Added.
(WebCore::setSystemHasBattery):
(WebCore::systemHasBattery):
* platform/graphics/avfoundation/objc/AVAssetTrackUtilities.mm:
(WebCore::systemHasBattery): Deleted.
* testing/Internals.cpp:
(WebCore::Internals::systemHasBattery const):
* testing/Internals.h:
* testing/Internals.idl:

Source/WebKit:

<rdar://problem/34722450>

Reviewed by Darin Adler.

Send flag indicating whether device has battery to the WebContent process as part of the
WebProcess creation parameters.

* Shared/WebProcessCreationParameters.cpp:
(WebKit::WebProcessCreationParameters::encode const):
(WebKit::WebProcessCreationParameters::decode):
* Shared/WebProcessCreationParameters.h:
* UIProcess/Cocoa/WebProcessPoolCocoa.mm:
(WebKit::WebProcessPool::platformInitializeWebProcess):
* WebProcess/cocoa/WebProcessCocoa.mm:
(WebKit::WebProcess::platformInitializeWebProcess):

Tools:

Reviewed by Darin Adler.

* TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
* TestWebKitAPI/Tests/WebKit/Battery.mm: Added.
(TEST):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@254995 268f45cc-cd09-0410-ab3c-d52691b4dbfc

16 files changed:
Source/WebCore/ChangeLog
Source/WebCore/WebCore.xcodeproj/project.pbxproj
Source/WebCore/platform/cocoa/SystemBattery.h [new file with mode: 0644]
Source/WebCore/platform/cocoa/SystemBattery.mm [new file with mode: 0644]
Source/WebCore/platform/graphics/avfoundation/objc/AVAssetTrackUtilities.mm
Source/WebCore/testing/Internals.cpp
Source/WebCore/testing/Internals.h
Source/WebCore/testing/Internals.idl
Source/WebKit/ChangeLog
Source/WebKit/Shared/WebProcessCreationParameters.cpp
Source/WebKit/Shared/WebProcessCreationParameters.h
Source/WebKit/UIProcess/Cocoa/WebProcessPoolCocoa.mm
Source/WebKit/WebProcess/cocoa/WebProcessCocoa.mm
Tools/ChangeLog
Tools/TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj
Tools/TestWebKitAPI/Tests/WebKit/Battery.mm [new file with mode: 0644]

index 6898e47..d55f8a0 100644 (file)
@@ -1,3 +1,26 @@
+2020-01-23  Per Arne Vollan  <pvollan@apple.com>
+
+        [Cocoa] Broker access to the PowerManagement API
+        https://bugs.webkit.org/show_bug.cgi?id=206373
+
+        Reviewed by Darin Adler.
+
+        Code to determine if device has battery should be executed in the UI process.
+
+        API test: WebKit.SystemHasBattery
+
+        * WebCore.xcodeproj/project.pbxproj:
+        * platform/cocoa/SystemBattery.h: Added.
+        * platform/cocoa/SystemBattery.mm: Added.
+        (WebCore::setSystemHasBattery):
+        (WebCore::systemHasBattery):
+        * platform/graphics/avfoundation/objc/AVAssetTrackUtilities.mm:
+        (WebCore::systemHasBattery): Deleted.
+        * testing/Internals.cpp:
+        (WebCore::Internals::systemHasBattery const):
+        * testing/Internals.h:
+        * testing/Internals.idl:
+
 2020-01-23  Antoine Quint  <graouts@apple.com>
 
         [Web Animations] Make AnimationList ref-counted
index 5ceed00..1f3d7ea 100644 (file)
                C105DA640F3AA6B8001DD44F /* TextEncodingDetector.h in Headers */ = {isa = PBXBuildFile; fileRef = C105DA630F3AA6B8001DD44F /* TextEncodingDetector.h */; };
                C11A9ECE21403A5C00CFB20A /* SwitchingGPUClient.h in Headers */ = {isa = PBXBuildFile; fileRef = C11A9ECD21403A5C00CFB20A /* SwitchingGPUClient.h */; settings = {ATTRIBUTES = (Private, ); }; };
                C14938072234551A000CD707 /* SpeechSynthesisClient.h in Headers */ = {isa = PBXBuildFile; fileRef = C149380522342719000CD707 /* SpeechSynthesisClient.h */; settings = {ATTRIBUTES = (Private, ); }; };
+               C1692DD223D23ABD006E88F7 /* SystemBattery.mm in Sources */ = {isa = PBXBuildFile; fileRef = C1692DD123D23ABD006E88F7 /* SystemBattery.mm */; };
+               C1692DD523D23E08006E88F7 /* SystemBattery.h in Headers */ = {isa = PBXBuildFile; fileRef = C1692DD423D23AE0006E88F7 /* SystemBattery.h */; settings = {ATTRIBUTES = (Private, ); }; };
                C1E1D236203DF15400584665 /* ScreenProperties.h in Headers */ = {isa = PBXBuildFile; fileRef = C1E1D235203DF15400584665 /* ScreenProperties.h */; settings = {ATTRIBUTES = (Private, ); }; };
                C2015C0A1BE6FEB200822389 /* FontVariantBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = C2015C091BE6FE2C00822389 /* FontVariantBuilder.h */; };
                C21DF2EA1D9E4E9900F5B24C /* CSSFontVariationValue.h in Headers */ = {isa = PBXBuildFile; fileRef = C21DF2E81D9E4E9900F5B24C /* CSSFontVariationValue.h */; };
                C11A9ECD21403A5C00CFB20A /* SwitchingGPUClient.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SwitchingGPUClient.h; sourceTree = "<group>"; };
                C11A9ED22140578B00CFB20A /* SwitchingGPUClient.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = SwitchingGPUClient.cpp; sourceTree = "<group>"; };
                C149380522342719000CD707 /* SpeechSynthesisClient.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SpeechSynthesisClient.h; sourceTree = "<group>"; };
+               C1692DD123D23ABD006E88F7 /* SystemBattery.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = SystemBattery.mm; sourceTree = "<group>"; };
+               C1692DD423D23AE0006E88F7 /* SystemBattery.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SystemBattery.h; sourceTree = "<group>"; };
                C1E1D235203DF15400584665 /* ScreenProperties.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ScreenProperties.h; sourceTree = "<group>"; };
                C2015C091BE6FE2C00822389 /* FontVariantBuilder.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FontVariantBuilder.h; sourceTree = "<group>"; };
                C20F4F6421DFBE5C0070C45A /* WHLSLTypeReference.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = WHLSLTypeReference.cpp; sourceTree = "<group>"; };
                                0E7058F31BC5CCD70045A507 /* SearchPopupMenuCocoa.h */,
                                0E7058ED1BC5BC190045A507 /* SearchPopupMenuCocoa.mm */,
                                1A4A95510B4EDCFF002D8C3C /* SharedBufferCocoa.mm */,
+                               C1692DD423D23AE0006E88F7 /* SystemBattery.h */,
+                               C1692DD123D23ABD006E88F7 /* SystemBattery.mm */,
                                5D5975B119635F1100D00878 /* SystemVersion.h */,
                                5D5975B219635F1100D00878 /* SystemVersion.mm */,
                                7CC564B918BAC720001B9652 /* TelephoneNumberDetectorCocoa.cpp */,
                                5185430B1FB6784500478F95 /* SWServerToContextConnection.h in Headers */,
                                517A53461F50C17F00DCDC0A /* SWServerWorker.h in Headers */,
                                E180811716FCF9CB00B80D07 /* SynchronousLoaderClient.h in Headers */,
+                               C1692DD523D23E08006E88F7 /* SystemBattery.h in Headers */,
                                E4E8B4EC216B79E500B8834D /* SystemFontDatabaseCoreText.h in Headers */,
                                0F03C0741884695E00A5F8CA /* SystemMemory.h in Headers */,
                                5D5975B319635F1100D00878 /* SystemVersion.h in Headers */,
                                538F10A81F9022A4005102CE /* SoftLinkLibxslt.cpp in Sources */,
                                CDC8B5AA18047FF10016E685 /* SourceBufferPrivateAVFObjC.mm in Sources */,
                                A833C7CA0A2CF06B00D57664 /* SVGNames.cpp in Sources */,
+                               C1692DD223D23ABD006E88F7 /* SystemBattery.mm in Sources */,
                                6E72F54F229DCD1300B3E151 /* TemporaryANGLESetting.cpp in Sources */,
                                9759E93F14EF1CF80026A2DD /* TextTrack.cpp in Sources */,
                                9759E94214EF1CF80026A2DD /* TextTrackCue.cpp in Sources */,
diff --git a/Source/WebCore/platform/cocoa/SystemBattery.h b/Source/WebCore/platform/cocoa/SystemBattery.h
new file mode 100644 (file)
index 0000000..5ada89e
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+* Copyright (C) 2020 Apple Inc. All rights reserved.
+*
+* Redistribution and use in source and binary forms, with or without
+* modification, are permitted provided that the following conditions
+* are met:
+* 1. Redistributions of source code must retain the above copyright
+*    notice, this list of conditions and the following disclaimer.
+* 2. Redistributions in binary form must reproduce the above copyright
+*    notice, this list of conditions and the following disclaimer in the
+*    documentation and/or other materials provided with the distribution.
+*
+* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+* THE POSSIBILITY OF SUCH DAMAGE.
+*/
+
+#pragma once
+
+namespace WebCore {
+
+WEBCORE_EXPORT void setSystemHasBattery(bool);
+WEBCORE_EXPORT bool systemHasBattery();
+
+}
diff --git a/Source/WebCore/platform/cocoa/SystemBattery.mm b/Source/WebCore/platform/cocoa/SystemBattery.mm
new file mode 100644 (file)
index 0000000..e633dbb
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2020 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "config.h"
+#import "SystemBattery.h"
+
+#import <pal/spi/cocoa/IOPSLibSPI.h>
+
+namespace WebCore {
+
+static Optional<bool> hasBattery;
+
+void setSystemHasBattery(bool battery)
+{
+    hasBattery = battery;
+}
+
+bool systemHasBattery()
+{
+    if (hasBattery.hasValue())
+        return *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;
+}
+
+}
index e82f8a8..50679cd 100644 (file)
 #if ENABLE(VIDEO) && USE(AVFOUNDATION)
 
 #import "FourCC.h"
+#import "SystemBattery.h"
+#import "VideoToolboxSoftLink.h"
 #import <AVFoundation/AVAssetTrack.h>
-#import <pal/spi/cocoa/IOPSLibSPI.h>
 
 #import <pal/cf/CoreMediaSoftLink.h>
-#import "VideoToolboxSoftLink.h"
 
 namespace WebCore {
 
-static bool systemHasBattery()
-{
-    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;
-}
-
 static Vector<FourCC> contentTypesToCodecs(const Vector<ContentType>& contentTypes)
 {
     Vector<FourCC> codecs;
index eae3033..9e0586f 100644 (file)
 #endif
 
 #if PLATFORM(COCOA)
-#import <wtf/spi/darwin/SandboxSPI.h>
+#include "SystemBattery.h"
+#include <wtf/spi/darwin/SandboxSPI.h>
 #endif
 
 using JSC::CallData;
@@ -5426,4 +5427,13 @@ String Internals::systemColorForCSSValue(const String& cssValue, bool useDarkMod
     return RenderTheme::singleton().systemColor(id, options).cssText();
 }
 
+bool Internals::systemHasBattery() const
+{
+#if PLATFORM(COCOA)
+    return WebCore::systemHasBattery();
+#else
+    return false;
+#endif
+}
+
 } // namespace WebCore
index 09d2495..752de7a 100644 (file)
@@ -928,6 +928,8 @@ public:
 
     String systemColorForCSSValue(const String& cssValue, bool useDarkModeAppearance, bool useElevatedUserInterfaceLevel);
 
+    bool systemHasBattery() const;
+
 private:
     explicit Internals(Document&);
     Document* contextDocument() const;
index 3493872..f5b3a3a 100644 (file)
@@ -835,4 +835,6 @@ enum CompositingPolicy {
     boolean hasSandboxMachLookupAccessToXPCServiceName(DOMString process, DOMString service);
     
     DOMString systemColorForCSSValue(DOMString cssValue, boolean useDarkModeAppearance, boolean useElevatedUserInterfaceLevel);
+
+    boolean systemHasBattery();
 };
index f78984e..756aece 100644 (file)
@@ -1,3 +1,23 @@
+2020-01-23  Per Arne Vollan  <pvollan@apple.com>
+
+        [Cocoa] Broker access to the PowerManagement API
+        https://bugs.webkit.org/show_bug.cgi?id=206373
+        <rdar://problem/34722450>
+
+        Reviewed by Darin Adler.
+
+        Send flag indicating whether device has battery to the WebContent process as part of the
+        WebProcess creation parameters.
+
+        * Shared/WebProcessCreationParameters.cpp:
+        (WebKit::WebProcessCreationParameters::encode const):
+        (WebKit::WebProcessCreationParameters::decode):
+        * Shared/WebProcessCreationParameters.h:
+        * UIProcess/Cocoa/WebProcessPoolCocoa.mm:
+        (WebKit::WebProcessPool::platformInitializeWebProcess):
+        * WebProcess/cocoa/WebProcessCocoa.mm:
+        (WebKit::WebProcess::platformInitializeWebProcess):
+
 2020-01-23  Ryan Haddad  <ryanhaddad@apple.com>
 
         Unreviewed, rolling out r254990.
index 3d35dc7..bb9ae45 100644 (file)
@@ -166,6 +166,7 @@ void WebProcessCreationParameters::encode(IPC::Encoder& encoder) const
 #if PLATFORM(COCOA)
     encoder << neHelperExtensionHandle;
     encoder << neSessionManagerExtensionHandle;
+    encoder << systemHasBattery;
 #endif
 
 #if PLATFORM(IOS_FAMILY)
@@ -433,6 +434,12 @@ bool WebProcessCreationParameters::decode(IPC::Decoder& decoder, WebProcessCreat
     if (!neSessionManagerExtensionHandle)
         return false;
     parameters.neSessionManagerExtensionHandle = WTFMove(*neSessionManagerExtensionHandle);
+
+    Optional<bool> systemHasBattery;
+    decoder >> systemHasBattery;
+    if (!systemHasBattery)
+        return false;
+    parameters.systemHasBattery = WTFMove(*systemHasBattery);
 #endif
 
 #if PLATFORM(IOS_FAMILY)
index b4302d6..d7aefee 100644 (file)
@@ -208,6 +208,7 @@ struct WebProcessCreationParameters {
 #if PLATFORM(COCOA)
     Optional<SandboxExtension::Handle> neHelperExtensionHandle;
     Optional<SandboxExtension::Handle> neSessionManagerExtensionHandle;
+    bool systemHasBattery { false };
 #endif
 
 #if PLATFORM(IOS_FAMILY)
index a26f984..5b6e066 100644 (file)
@@ -81,6 +81,7 @@ SOFT_LINK_CLASS(WebContentAnalysis, WebFilterEvaluator);
 #endif
 
 #if PLATFORM(COCOA)
+#import <WebCore/SystemBattery.h>
 #import <pal/spi/cocoa/NEFilterSourceSPI.h>
 
 SOFT_LINK_FRAMEWORK_OPTIONAL(NetworkExtension);
@@ -343,6 +344,7 @@ void WebProcessPool::platformInitializeWebProcess(const WebProcessProxy& process
 #endif
         parameters.neSessionManagerExtensionHandle = WTFMove(handle);
     }
+    parameters.systemHasBattery = systemHasBattery();
 #endif
     
 #if PLATFORM(IOS)
index 2f83d8c..21e4598 100644 (file)
 
 #if PLATFORM(COCOA)
 #import <WebCore/NetworkExtensionContentFilter.h>
+#import <WebCore/SystemBattery.h>
 #endif
 
 #if HAVE(CSCHECKFIXDISABLE)
@@ -258,6 +259,7 @@ void WebProcess::platformInitializeWebProcess(WebProcessCreationParameters& para
     if (parameters.neSessionManagerExtensionHandle)
         SandboxExtension::consumePermanently(*parameters.neSessionManagerExtensionHandle);
     NetworkExtensionContentFilter::setHasConsumedSandboxExtensions(parameters.neHelperExtensionHandle.hasValue() && parameters.neSessionManagerExtensionHandle.hasValue());
+    setSystemHasBattery(parameters.systemHasBattery);
 #endif
 
 #if PLATFORM(IOS_FAMILY)
index 27bcbd5..0d12f6f 100644 (file)
@@ -1,3 +1,14 @@
+2020-01-23  Per Arne Vollan  <pvollan@apple.com>
+
+        [Cocoa] Broker access to the PowerManagement API
+        https://bugs.webkit.org/show_bug.cgi?id=206373
+
+        Reviewed by Darin Adler.
+
+        * TestWebKitAPI/TestWebKitAPI.xcodeproj/project.pbxproj:
+        * TestWebKitAPI/Tests/WebKit/Battery.mm: Added.
+        (TEST):
+
 2020-01-23  Jonathan Bedard  <jbedard@apple.com>
 
         Fix iOS 13.2 iphoneos build
index 0fd71fe..5badd91 100644 (file)
                C0ADBE9612FCA79B00D2C129 /* simple-form.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = C0ADBE8412FCA6B600D2C129 /* simple-form.html */; };
                C0BD669F131D3CFF00E18F2A /* ResponsivenessTimerDoesntFireEarly_Bundle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C0BD669E131D3CFF00E18F2A /* ResponsivenessTimerDoesntFireEarly_Bundle.cpp */; };
                C0C5D3C61459912900A802A6 /* GetBackingScaleFactor_Bundle.mm in Sources */ = {isa = PBXBuildFile; fileRef = C0C5D3BD14598B6F00A802A6 /* GetBackingScaleFactor_Bundle.mm */; };
+               C1692DCA23D10DAE006E88F7 /* Battery.mm in Sources */ = {isa = PBXBuildFile; fileRef = C1692DC923D10DAE006E88F7 /* Battery.mm */; };
                C20F88A72295B96700D610FA /* CoreText.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C20F88A62295B96700D610FA /* CoreText.framework */; };
                C22FA32B228F8708009D7988 /* TextWidth.mm in Sources */ = {isa = PBXBuildFile; fileRef = C22FA32A228F8708009D7988 /* TextWidth.mm */; };
                C22FA32D228F8AEB009D7988 /* TextWidth.html in Copy Resources */ = {isa = PBXBuildFile; fileRef = C22FA32C228F877A009D7988 /* TextWidth.html */; };
                C0BD669E131D3CFF00E18F2A /* ResponsivenessTimerDoesntFireEarly_Bundle.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ResponsivenessTimerDoesntFireEarly_Bundle.cpp; sourceTree = "<group>"; };
                C0C5D3BC14598B6F00A802A6 /* GetBackingScaleFactor.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = GetBackingScaleFactor.mm; sourceTree = "<group>"; };
                C0C5D3BD14598B6F00A802A6 /* GetBackingScaleFactor_Bundle.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = GetBackingScaleFactor_Bundle.mm; sourceTree = "<group>"; };
+               C1692DC923D10DAE006E88F7 /* Battery.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = Battery.mm; sourceTree = "<group>"; };
                C1D8EE212028E8E3008EB141 /* WebProcessTerminate.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = WebProcessTerminate.mm; sourceTree = "<group>"; };
                C20F88A62295B96700D610FA /* CoreText.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreText.framework; path = System/Library/Frameworks/CoreText.framework; sourceTree = SDKROOT; };
                C22FA32A228F8708009D7988 /* TextWidth.mm */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.objcpp; path = TextWidth.mm; sourceTree = "<group>"; };
                0F139E741A423A4600F590F5 /* cocoa */ = {
                        isa = PBXGroup;
                        children = (
+                               C1692DC923D10DAE006E88F7 /* Battery.mm */,
                                0F139E751A423A5300F590F5 /* WeakObjCPtr.mm */,
                        );
                        name = cocoa;
                                0711DF52226A95FC003DD2F7 /* AVFoundationSoftLinkTest.mm in Sources */,
                                7CCE7EB51A411A7E00447C4C /* BackForwardList.mm in Sources */,
                                1C7FEB20207C0F2E00D23278 /* BackgroundColor.mm in Sources */,
+                               C1692DCA23D10DAE006E88F7 /* Battery.mm in Sources */,
                                374B7A601DF36EEE00ACCB6C /* BundleEditingDelegate.mm in Sources */,
                                7A89BB67233164320042CB1E /* BundleFormDelegate.mm in Sources */,
                                A13EBBB11B87438000097110 /* BundleParameters.mm in Sources */,
diff --git a/Tools/TestWebKitAPI/Tests/WebKit/Battery.mm b/Tools/TestWebKitAPI/Tests/WebKit/Battery.mm
new file mode 100644 (file)
index 0000000..d19edbe
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2020 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
+ * THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+
+#if WK_HAVE_C_SPI
+
+#import "PlatformUtilities.h"
+#import "TestWKWebView.h"
+#import <WebCore/SystemBattery.h>
+
+TEST(WebKit, SystemHasBattery)
+{
+    auto configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    WKRetainPtr<WKContextRef> context = adoptWK(TestWebKitAPI::Util::createContextForInjectedBundleTest("InternalsInjectedBundleTest"));
+    configuration.get().processPool = (WKProcessPool *)context.get();
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 300, 300) configuration:configuration.get() addToWindow:YES]);
+
+    auto hasBattery = [&] {
+        return [webView stringByEvaluatingJavaScript:@"window.internals.systemHasBattery()"].boolValue;
+    };
+
+    ASSERT_TRUE(hasBattery() == WebCore::systemHasBattery());
+}
+
+#endif // WK_HAVE_C_SPI