Background state not being tracked correctly for PDFs on iOS
authorbarraclough@apple.com <barraclough@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 4 Dec 2015 23:09:37 +0000 (23:09 +0000)
committerbarraclough@apple.com <barraclough@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Fri, 4 Dec 2015 23:09:37 +0000 (23:09 +0000)
https://bugs.webkit.org/show_bug.cgi?id=151886

Reviewed by Anders Carlson.

The problem here is that when viewing a PDF we don't have an ApplicationStateTracker.
(While we do have a content view - which normally holds the ApplicationStateTracker -
the content view is not in a window, and only has an ApplicationStateTracker while in a
window). For now, let's give the WKPDFView an ApplicationStateTracker of its very own.
In the future we may want to refactor ownership of the ApplicationStateTracker up to
the WKWebView.

* UIProcess/API/Cocoa/WKWebView.mm:
(-[WKWebView _isBackground]):
    - Added, checks background state of content/PDF view.
* UIProcess/API/Cocoa/WKWebViewInternal.h:
    - expose _isBackground to PageClientImplIOS.
* UIProcess/ApplicationStateTracker.h:
(WebKit::ApplicationStateTracker::isInBackground):
* UIProcess/ApplicationStateTracker.mm:
(WebKit::isBackgroundState):
(WebKit::ApplicationStateTracker::ApplicationStateTracker):
    - generalize WKContentView -> UIView, so this may now also be a WKPDFView.
* UIProcess/ios/PageClientImplIOS.mm:
(WebKit::PageClientImpl::isViewVisible):
    - check background state via the WKWebView.
* UIProcess/ios/WKPDFView.h:
* UIProcess/ios/WKPDFView.mm:
(-[WKPDFView willMoveToWindow:]):
(-[WKPDFView didMoveToWindow]):
(-[WKPDFView isBackground]):
(-[WKPDFView _applicationDidEnterBackground]):
(-[WKPDFView _applicationWillEnterForeground]):
    - added methods to initialize ApplicationStateTracker, access background state, & callbacks to the page proxy.

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

Source/WebKit2/ChangeLog
Source/WebKit2/UIProcess/API/Cocoa/WKWebView.mm
Source/WebKit2/UIProcess/API/Cocoa/WKWebViewInternal.h
Source/WebKit2/UIProcess/ApplicationStateTracker.h
Source/WebKit2/UIProcess/ApplicationStateTracker.mm
Source/WebKit2/UIProcess/ios/PageClientImplIOS.mm
Source/WebKit2/UIProcess/ios/WKPDFView.h
Source/WebKit2/UIProcess/ios/WKPDFView.mm

index 11fcb81..45e8978 100644 (file)
@@ -1,3 +1,40 @@
+2015-12-04  Gavin Barraclough  <barraclough@apple.com>
+
+        Background state not being tracked correctly for PDFs on iOS
+        https://bugs.webkit.org/show_bug.cgi?id=151886
+
+        Reviewed by Anders Carlson.
+
+        The problem here is that when viewing a PDF we don't have an ApplicationStateTracker.
+        (While we do have a content view - which normally holds the ApplicationStateTracker -
+        the content view is not in a window, and only has an ApplicationStateTracker while in a
+        window). For now, let's give the WKPDFView an ApplicationStateTracker of its very own.
+        In the future we may want to refactor ownership of the ApplicationStateTracker up to
+        the WKWebView.
+
+        * UIProcess/API/Cocoa/WKWebView.mm:
+        (-[WKWebView _isBackground]):
+            - Added, checks background state of content/PDF view.
+        * UIProcess/API/Cocoa/WKWebViewInternal.h:
+            - expose _isBackground to PageClientImplIOS.
+        * UIProcess/ApplicationStateTracker.h:
+        (WebKit::ApplicationStateTracker::isInBackground):
+        * UIProcess/ApplicationStateTracker.mm:
+        (WebKit::isBackgroundState):
+        (WebKit::ApplicationStateTracker::ApplicationStateTracker):
+            - generalize WKContentView -> UIView, so this may now also be a WKPDFView.
+        * UIProcess/ios/PageClientImplIOS.mm:
+        (WebKit::PageClientImpl::isViewVisible):
+            - check background state via the WKWebView.
+        * UIProcess/ios/WKPDFView.h:
+        * UIProcess/ios/WKPDFView.mm:
+        (-[WKPDFView willMoveToWindow:]):
+        (-[WKPDFView didMoveToWindow]):
+        (-[WKPDFView isBackground]):
+        (-[WKPDFView _applicationDidEnterBackground]):
+        (-[WKPDFView _applicationWillEnterForeground]):
+            - added methods to initialize ApplicationStateTracker, access background state, & callbacks to the page proxy.
+
 2015-12-04  Antti Koivisto  <antti@apple.com>
 
         Rename WebResourceLoadScheduler to WebLoaderStrategy
index 5fa1ef2..32b3f72 100644 (file)
@@ -750,6 +750,15 @@ static WKErrorCode callbackErrorCode(WebKit::CallbackBase::Error error)
 #pragma mark iOS-specific methods
 
 #if PLATFORM(IOS)
+
+- (BOOL)_isBackground
+{
+    if ([self _isDisplayingPDF])
+        return [(WKPDFView *)_customContentView isBackground];
+
+    return [_contentView isBackground];
+}
+
 - (void)setFrame:(CGRect)frame
 {
     CGRect oldFrame = self.frame;
index afa398c..63cbef6 100644 (file)
@@ -111,6 +111,8 @@ struct PrintInfo;
 - (void)_navigationGestureDidBegin;
 - (void)_navigationGestureDidEnd;
 
+@property (nonatomic, readonly) BOOL _isBackground;
+
 @property (nonatomic, readonly) WKWebViewContentProviderRegistry *_contentProviderRegistry;
 
 @property (nonatomic, readonly) WKSelectionGranularity _selectionGranularity;
index 8ab6686..5af49df 100644 (file)
 #import <wtf/WeakPtr.h>
 
 OBJC_CLASS BKSApplicationStateMonitor;
-OBJC_CLASS WKContentView;
+OBJC_CLASS UIView;
 
 namespace WebKit {
 
 class ApplicationStateTracker {
 public:
-    ApplicationStateTracker(WKContentView *, SEL didEnterBackgroundSelector, SEL willEnterForegroundSelector);
+    ApplicationStateTracker(UIView *, SEL didEnterBackgroundSelector, SEL willEnterForegroundSelector);
     ~ApplicationStateTracker();
 
     bool isInBackground() const { return m_isInBackground; }
@@ -48,7 +48,7 @@ private:
     void applicationDidEnterBackground();
     void applicationWillEnterForeground();
 
-    WeakObjCPtr<WKContentView> m_view;
+    WeakObjCPtr<UIView> m_view;
     SEL m_didEnterBackgroundSelector;
     SEL m_willEnterForegroundSelector;
 
index 8be5be2..ada6af8 100644 (file)
@@ -31,7 +31,6 @@
 #import "AssertionServicesSPI.h"
 #import "SandboxUtilities.h"
 #import "UIKitSPI.h"
-#import "WKContentView.h"
 #import <WebCore/SecuritySPI.h>
 #import <wtf/NeverDestroyed.h>
 #import <wtf/ObjcRuntimeExtras.h>
@@ -74,7 +73,7 @@ static bool isBackgroundState(BKSApplicationState state)
     }
 }
 
-ApplicationStateTracker::ApplicationStateTracker(WKContentView *view, SEL didEnterBackgroundSelector, SEL willEnterForegroundSelector)
+ApplicationStateTracker::ApplicationStateTracker(UIView *view, SEL didEnterBackgroundSelector, SEL willEnterForegroundSelector)
     : m_view(view)
     , m_didEnterBackgroundSelector(didEnterBackgroundSelector)
     , m_willEnterForegroundSelector(willEnterForegroundSelector)
index a099036..8cd5bbc 100644 (file)
@@ -172,7 +172,7 @@ bool PageClientImpl::isViewFocused()
 
 bool PageClientImpl::isViewVisible()
 {
-    if (isViewInWindow() && !m_contentView.isBackground)
+    if (isViewInWindow() && !m_webView._isBackground)
         return true;
     
     if ([m_webView _isShowingVideoPictureInPicture])
index 3cd9900..36d37f7 100644 (file)
@@ -34,6 +34,7 @@
 
 @property (nonatomic, readonly) NSString *suggestedFilename;
 @property (nonatomic, readonly) CGPDFDocumentRef pdfDocument;
+@property (nonatomic, readonly) BOOL isBackground;
 
 @end
 
index 8df3ef1..51f84c6 100644 (file)
@@ -30,6 +30,7 @@
 
 #import "APIFindClient.h"
 #import "APIUIClient.h"
+#import "ApplicationStateTracker.h"
 #import "CorePDFSPI.h"
 #import "SessionState.h"
 #import "UIKitSPI.h"
@@ -46,6 +47,7 @@
 #import <wtf/Vector.h>
 
 using namespace WebCore;
+using namespace WebKit;
 
 const CGFloat pdfPageMargin = 8;
 const CGFloat pdfMinimumZoomScale = 1;
@@ -108,6 +110,8 @@ typedef struct {
     dispatch_queue_t _findQueue;
 
     RetainPtr<UIWKSelectionAssistant> _webSelectionAssistant;
+
+    std::unique_ptr<ApplicationStateTracker> _applicationStateTracker;
 }
 
 - (instancetype)web_initWithFrame:(CGRect)frame webView:(WKWebView *)webView
@@ -844,6 +848,47 @@ static NSStringCompareOptions stringCompareOptions(_WKFindOptions options)
     return NO;
 }
 
+- (void)willMoveToWindow:(UIWindow *)newWindow
+{
+    if (newWindow)
+        return;
+    
+    ASSERT(self.window);
+    ASSERT(_applicationStateTracker);
+    _applicationStateTracker = nullptr;
+}
+
+- (void)didMoveToWindow
+{
+    if (!self.window)
+        return;
+
+    ASSERT(!_applicationStateTracker);
+    _applicationStateTracker = std::make_unique<ApplicationStateTracker>(self, @selector(_applicationDidEnterBackground), @selector(_applicationWillEnterForeground));
+}
+
+- (BOOL)isBackground
+{
+    if (!_applicationStateTracker)
+        return YES;
+
+    return _applicationStateTracker->isInBackground();
+}
+
+- (void)_applicationDidEnterBackground
+{
+    _webView->_page->applicationDidEnterBackground();
+    _webView->_page->viewStateDidChange(ViewState::AllFlags & ~ViewState::IsInWindow);
+}
+
+- (void)_applicationWillEnterForeground
+{
+    _webView->_page->applicationWillEnterForeground();
+    if (auto drawingArea = _webView->_page->drawingArea())
+        drawingArea->hideContentUntilAnyUpdate();
+    _webView->_page->viewStateDidChange(ViewState::AllFlags & ~ViewState::IsInWindow, true, WebPageProxy::ViewStateChangeDispatchMode::Immediate);
+}
+
 @end
 
 #endif /* PLATFORM(IOS) */