<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[186648] trunk/Source/WebKit2</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/186648">186648</a></dd>
<dt>Author</dt> <dd>andersca@apple.com</dd>
<dt>Date</dt> <dd>2015-07-09 16:32:15 -0700 (Thu, 09 Jul 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>SafariViewController loads partial webpage or entirely blank webpage
https://bugs.webkit.org/show_bug.cgi?id=146815
rdar://problem/21734757

Reviewed by Dan Bernstein.

* Platform/spi/ios/UIKitSPI.h:
Add _hostProcessIdentifier getter.

* UIProcess/ApplicationStateTracker.h:
Change ApplicationStateTracker to hold on to a WKContentView instead of a UIView.

* UIProcess/ApplicationStateTracker.mm:
(WebKit::applicationType):
Add a helper function that returns the type of application the web view is hosted by (regular
application, view service or extension).

(WebKit::ApplicationStateTracker::ApplicationStateTracker):
Register notifications and update m_isInBackground. For regular applications, just listen for UIApplication notifications.
For view services, walk the view controller hierarchy and find the service view controller and listen for notifications and use
BKS to find the background state of the host application. For extensions, just use BKSApplicationStateMonitor directly.

(WebKit::isViewService): Deleted.

* UIProcess/ios/WKContentView.mm:
(-[WKContentView willMoveToWindow:]):
If we're moving out of a window, null out the state tracker.

(-[WKContentView didMoveToWindow]):
If we moved to a window, create a new state tracker.

(-[WKContentView isBackground]):
Handle a null application state.

(-[WKContentView _commonInitializationWithProcessPool:configuration:]): Deleted.
Don't create the application state here.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2PlatformspiiosUIKitSPIh">trunk/Source/WebKit2/Platform/spi/ios/UIKitSPI.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessApplicationStateTrackerh">trunk/Source/WebKit2/UIProcess/ApplicationStateTracker.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessApplicationStateTrackermm">trunk/Source/WebKit2/UIProcess/ApplicationStateTracker.mm</a></li>
<li><a href="#trunkSourceWebKit2UIProcessiosWKContentViewmm">trunk/Source/WebKit2/UIProcess/ios/WKContentView.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (186647 => 186648)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2015-07-09 23:29:33 UTC (rev 186647)
+++ trunk/Source/WebKit2/ChangeLog        2015-07-09 23:32:15 UTC (rev 186648)
</span><span class="lines">@@ -1,3 +1,42 @@
</span><ins>+2015-07-09  Anders Carlsson  &lt;andersca@apple.com&gt;
+
+        SafariViewController loads partial webpage or entirely blank webpage
+        https://bugs.webkit.org/show_bug.cgi?id=146815
+        rdar://problem/21734757
+
+        Reviewed by Dan Bernstein.
+
+        * Platform/spi/ios/UIKitSPI.h:
+        Add _hostProcessIdentifier getter.
+
+        * UIProcess/ApplicationStateTracker.h:
+        Change ApplicationStateTracker to hold on to a WKContentView instead of a UIView.
+
+        * UIProcess/ApplicationStateTracker.mm:
+        (WebKit::applicationType):
+        Add a helper function that returns the type of application the web view is hosted by (regular
+        application, view service or extension).
+
+        (WebKit::ApplicationStateTracker::ApplicationStateTracker):
+        Register notifications and update m_isInBackground. For regular applications, just listen for UIApplication notifications.
+        For view services, walk the view controller hierarchy and find the service view controller and listen for notifications and use
+        BKS to find the background state of the host application. For extensions, just use BKSApplicationStateMonitor directly.
+
+        (WebKit::isViewService): Deleted.
+
+        * UIProcess/ios/WKContentView.mm:
+        (-[WKContentView willMoveToWindow:]):
+        If we're moving out of a window, null out the state tracker.
+
+        (-[WKContentView didMoveToWindow]):
+        If we moved to a window, create a new state tracker.
+
+        (-[WKContentView isBackground]):
+        Handle a null application state.
+
+        (-[WKContentView _commonInitializationWithProcessPool:configuration:]): Deleted.
+        Don't create the application state here.
+
</ins><span class="cx"> 2015-07-09  Dan Bernstein  &lt;mitz@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         [Cocoa] Report the first paint after committing a load as a rendering progress event
</span></span></pre></div>
<a id="trunkSourceWebKit2PlatformspiiosUIKitSPIh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/Platform/spi/ios/UIKitSPI.h (186647 => 186648)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/Platform/spi/ios/UIKitSPI.h        2015-07-09 23:29:33 UTC (rev 186647)
+++ trunk/Source/WebKit2/Platform/spi/ios/UIKitSPI.h        2015-07-09 23:32:15 UTC (rev 186648)
</span><span class="lines">@@ -57,6 +57,7 @@
</span><span class="cx"> #import &lt;UIKit/UITextInteractionAssistant_Private.h&gt;
</span><span class="cx"> #import &lt;UIKit/UIViewControllerTransitioning_Private.h&gt;
</span><span class="cx"> #import &lt;UIKit/UIViewController_Private.h&gt;
</span><ins>+#import &lt;UIKit/UIViewController_ViewService.h&gt;
</ins><span class="cx"> #import &lt;UIKit/UIView_Private.h&gt;
</span><span class="cx"> #import &lt;UIKit/UIWKSelectionAssistant.h&gt;
</span><span class="cx"> #import &lt;UIKit/UIWKTextInteractionAssistant.h&gt;
</span><span class="lines">@@ -319,6 +320,10 @@
</span><span class="cx"> + (UIViewController *)viewControllerForView:(UIView *)view;
</span><span class="cx"> @end
</span><span class="cx"> 
</span><ins>+@interface UIViewController (ViewService)
+- (pid_t)_hostProcessIdentifier;
+@end
+
</ins><span class="cx"> @protocol UIViewControllerContextTransitioningEx &lt;UIViewControllerContextTransitioning&gt;
</span><span class="cx"> - (void)__runAlongsideAnimations;
</span><span class="cx"> - (void)_interactivityDidChange:(BOOL)isInteractive;
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessApplicationStateTrackerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/ApplicationStateTracker.h (186647 => 186648)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/ApplicationStateTracker.h        2015-07-09 23:29:33 UTC (rev 186647)
+++ trunk/Source/WebKit2/UIProcess/ApplicationStateTracker.h        2015-07-09 23:32:15 UTC (rev 186648)
</span><span class="lines">@@ -33,13 +33,13 @@
</span><span class="cx"> #import &lt;wtf/WeakPtr.h&gt;
</span><span class="cx"> 
</span><span class="cx"> OBJC_CLASS BKSApplicationStateMonitor;
</span><del>-OBJC_CLASS UIView;
</del><ins>+OBJC_CLASS WKContentView;
</ins><span class="cx"> 
</span><span class="cx"> namespace WebKit {
</span><span class="cx"> 
</span><span class="cx"> class ApplicationStateTracker {
</span><span class="cx"> public:
</span><del>-    ApplicationStateTracker(UIView *, SEL didEnterBackgroundSelector, SEL willEnterForegroundSelector);
</del><ins>+    ApplicationStateTracker(WKContentView *, SEL didEnterBackgroundSelector, SEL willEnterForegroundSelector);
</ins><span class="cx">     ~ApplicationStateTracker();
</span><span class="cx"> 
</span><span class="cx">     bool isInBackground() const { return m_isInBackground; }
</span><span class="lines">@@ -48,7 +48,7 @@
</span><span class="cx">     void applicationDidEnterBackground();
</span><span class="cx">     void applicationWillEnterForeground();
</span><span class="cx"> 
</span><del>-    WeakObjCPtr&lt;UIView&gt; m_view;
</del><ins>+    WeakObjCPtr&lt;WKContentView&gt; m_view;
</ins><span class="cx">     SEL m_didEnterBackgroundSelector;
</span><span class="cx">     SEL m_willEnterForegroundSelector;
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessApplicationStateTrackermm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/ApplicationStateTracker.mm (186647 => 186648)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/ApplicationStateTracker.mm        2015-07-09 23:29:33 UTC (rev 186647)
+++ trunk/Source/WebKit2/UIProcess/ApplicationStateTracker.mm        2015-07-09 23:32:15 UTC (rev 186648)
</span><span class="lines">@@ -30,19 +30,19 @@
</span><span class="cx"> 
</span><span class="cx"> #import &quot;AssertionServicesSPI.h&quot;
</span><span class="cx"> #import &quot;UIKitSPI.h&quot;
</span><del>-#import &lt;UIKit/UIApplication.h&gt;
</del><ins>+#import &quot;WKContentView.h&quot;
</ins><span class="cx"> #import &lt;WebCore/SecuritySPI.h&gt;
</span><span class="cx"> #import &lt;wtf/NeverDestroyed.h&gt;
</span><span class="cx"> #import &lt;wtf/ObjcRuntimeExtras.h&gt;
</span><span class="cx"> 
</span><ins>+@interface UIWindow (WKDetails)
+- (BOOL)_isHostedInAnotherProcess;
+@end
+
</ins><span class="cx"> namespace WebKit {
</span><span class="cx"> 
</span><span class="cx"> static bool hasEntitlement(NSString *entitlement)
</span><span class="cx"> {
</span><del>-#if PLATFORM(IOS_SIMULATOR)
-    // The simulator doesn't support entitlements.
-    return true;
-#else
</del><span class="cx">     auto task = adoptCF(SecTaskCreateFromSelf(CFAllocatorGetDefault()));
</span><span class="cx">     if (!task)
</span><span class="cx">         return false;
</span><span class="lines">@@ -55,18 +55,25 @@
</span><span class="cx">         return false;
</span><span class="cx"> 
</span><span class="cx">     return CFBooleanGetValue(static_cast&lt;CFBooleanRef&gt;(value.get()));
</span><del>-#endif
</del><span class="cx"> }
</span><span class="cx"> 
</span><del>-static bool isViewService()
</del><ins>+enum class ApplicationType {
+    Application,
+    ViewService,
+    Extension,
+};
+
+static ApplicationType applicationType(UIWindow *window)
</ins><span class="cx"> {
</span><ins>+    ASSERT(window);
+
</ins><span class="cx">     if (_UIApplicationIsExtension())
</span><del>-        return true;
</del><ins>+        return ApplicationType::Extension;
</ins><span class="cx"> 
</span><del>-    if (hasEntitlement(@&quot;com.apple.UIKit.vends-view-services&quot;))
-        return true;
</del><ins>+    if (hasEntitlement(@&quot;com.apple.UIKit.vends-view-services&quot;) &amp;&amp; window._isHostedInAnotherProcess)
+        return ApplicationType::ViewService;
</ins><span class="cx"> 
</span><del>-    return false;
</del><ins>+    return ApplicationType::Application;
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> static bool isBackgroundState(BKSApplicationState state)
</span><span class="lines">@@ -81,10 +88,11 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-ApplicationStateTracker::ApplicationStateTracker(UIView *view, SEL didEnterBackgroundSelector, SEL willEnterForegroundSelector)
</del><ins>+ApplicationStateTracker::ApplicationStateTracker(WKContentView *view, SEL didEnterBackgroundSelector, SEL willEnterForegroundSelector)
</ins><span class="cx">     : m_view(view)
</span><span class="cx">     , m_didEnterBackgroundSelector(didEnterBackgroundSelector)
</span><span class="cx">     , m_willEnterForegroundSelector(willEnterForegroundSelector)
</span><ins>+    , m_isInBackground(true)
</ins><span class="cx">     , m_weakPtrFactory(this)
</span><span class="cx">     , m_didEnterBackgroundObserver(nullptr)
</span><span class="cx">     , m_willEnterForegroundObserver(nullptr)
</span><span class="lines">@@ -92,7 +100,59 @@
</span><span class="cx">     ASSERT([m_view.get() respondsToSelector:m_didEnterBackgroundSelector]);
</span><span class="cx">     ASSERT([m_view.get() respondsToSelector:m_willEnterForegroundSelector]);
</span><span class="cx"> 
</span><del>-    if (isViewService()) {
</del><ins>+    UIWindow *window = [m_view.get() window];
+    ASSERT(window);
+
+    switch (applicationType(window)) {
+    case ApplicationType::Application: {
+        UIApplication *application = [UIApplication sharedApplication];
+        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
+
+        m_isInBackground = application.applicationState == UIApplicationStateBackground;
+
+        m_didEnterBackgroundObserver = [notificationCenter addObserverForName:UIApplicationDidEnterBackgroundNotification object:application queue:nil usingBlock:[this](NSNotification *) {
+            applicationDidEnterBackground();
+        }];
+
+        m_willEnterForegroundObserver = [notificationCenter addObserverForName:UIApplicationWillEnterForegroundNotification object:application queue:nil usingBlock:[this](NSNotification *) {
+            applicationWillEnterForeground();
+        }];
+        break;
+    }
+
+    case ApplicationType::ViewService: {
+        UIViewController *serviceViewController = nil;
+
+        for (UIView *view = m_view.get().get(); view; view = view.superview) {
+            UIViewController *viewController = [UIViewController viewControllerForView:view];
+
+            if (viewController._hostProcessIdentifier) {
+                serviceViewController = viewController;
+                break;
+            }
+        }
+
+        ASSERT(serviceViewController);
+
+        pid_t applicationPID = serviceViewController._hostProcessIdentifier;
+        ASSERT(applicationPID);
+
+        auto applicationStateMonitor = adoptNS([[BKSApplicationStateMonitor alloc] init]);
+        m_isInBackground = isBackgroundState([m_applicationStateMonitor mostElevatedApplicationStateForPID:applicationPID]);
+
+        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
+        m_didEnterBackgroundObserver = [notificationCenter addObserverForName:@&quot;_UIViewServiceHostDidEnterBackgroundNotificationName&quot; object:serviceViewController queue:nil usingBlock:[this](NSNotification *) {
+            applicationDidEnterBackground();
+        }];
+
+        m_willEnterForegroundObserver = [notificationCenter addObserverForName:@&quot;_UIViewServiceHostWillEnterForegroundNotificationName&quot; object:serviceViewController queue:nil usingBlock:[this](NSNotification *) {
+            applicationWillEnterForeground();
+        }];
+
+        break;
+    }
+
+    case ApplicationType::Extension: {
</ins><span class="cx">         m_applicationStateMonitor = adoptNS([[BKSApplicationStateMonitor alloc] init]);
</span><span class="cx"> 
</span><span class="cx">         m_isInBackground = isBackgroundState([m_applicationStateMonitor mostElevatedApplicationStateForPID:getpid()]);
</span><span class="lines">@@ -117,21 +177,8 @@
</span><span class="cx">                     applicationStateTracker-&gt;applicationWillEnterForeground();
</span><span class="cx">             });
</span><span class="cx">         }];
</span><del>-    } else {
-        UIApplication *application = [UIApplication sharedApplication];
-        NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
-
-        m_isInBackground = application.applicationState == UIApplicationStateBackground;
-
-        m_didEnterBackgroundObserver = [notificationCenter addObserverForName:UIApplicationDidEnterBackgroundNotification object:application queue:nil usingBlock:[this](NSNotification *) {
-            applicationDidEnterBackground();
-        }];
-
-        m_willEnterForegroundObserver = [notificationCenter addObserverForName:UIApplicationWillEnterForegroundNotification object:application queue:nil usingBlock:[this](NSNotification *) {
-            applicationWillEnterForeground();
-        }];
-
</del><span class="cx">     }
</span><ins>+    }
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> ApplicationStateTracker::~ApplicationStateTracker()
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessiosWKContentViewmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/ios/WKContentView.mm (186647 => 186648)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/ios/WKContentView.mm        2015-07-09 23:29:33 UTC (rev 186647)
+++ trunk/Source/WebKit2/UIProcess/ios/WKContentView.mm        2015-07-09 23:32:15 UTC (rev 186648)
</span><span class="lines">@@ -212,8 +212,6 @@
</span><span class="cx"> 
</span><span class="cx">     self.layer.hitTestsAsOpaque = YES;
</span><span class="cx"> 
</span><del>-    _applicationStateTracker = std::make_unique&lt;ApplicationStateTracker&gt;(self, @selector(_applicationDidEnterBackground), @selector(_applicationWillEnterForeground));
-
</del><span class="cx">     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:[UIApplication sharedApplication]];
</span><span class="cx">     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_applicationDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:[UIApplication sharedApplication]];
</span><span class="cx"> 
</span><span class="lines">@@ -274,6 +272,11 @@
</span><span class="cx">         if (_webView._allowsLinkPreview)
</span><span class="cx">             [self _unregisterPreviewInWindow:window];
</span><span class="cx"> #endif
</span><ins>+
+        if (!newWindow) {
+            ASSERT(_applicationStateTracker);
+            _applicationStateTracker = nullptr;
+        }
</ins><span class="cx">     }
</span><span class="cx"> 
</span><span class="cx">     if (newWindow) {
</span><span class="lines">@@ -287,6 +290,15 @@
</span><span class="cx">     }
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+- (void)didMoveToWindow
+{
+    if (!self.window)
+        return;
+
+    ASSERT(!_applicationStateTracker);
+    _applicationStateTracker = std::make_unique&lt;ApplicationStateTracker&gt;(self, @selector(_applicationDidEnterBackground), @selector(_applicationWillEnterForeground));
+}
+
</ins><span class="cx"> - (WKBrowsingContextController *)browsingContextController
</span><span class="cx"> {
</span><span class="cx">     if (!_browsingContextController)
</span><span class="lines">@@ -307,6 +319,9 @@
</span><span class="cx"> 
</span><span class="cx"> - (BOOL)isBackground
</span><span class="cx"> {
</span><ins>+    if (!_applicationStateTracker)
+        return YES;
+
</ins><span class="cx">     return _applicationStateTracker-&gt;isInBackground();
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre>
</div>
</div>

</body>
</html>