<!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>[172366] 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/172366">172366</a></dd>
<dt>Author</dt> <dd>timothy_horton@apple.com</dd>
<dt>Date</dt> <dd>2014-08-08 16:27:25 -0700 (Fri, 08 Aug 2014)</dd>
</dl>

<h3>Log Message</h3>
<pre>[mac] Dynamically update serviceability when the set of services changes
https://bugs.webkit.org/show_bug.cgi?id=135738
&lt;rdar://problem/17533459&gt;

Reviewed by Brady Eidson.

* UIProcess/WebContext.cpp:
(WebKit::WebContext::createNewWebProcess):
Adopt the new universal refreshExistingServices.

(WebKit::WebContext::refreshExistingServices): Deleted.
* UIProcess/WebContext.h:
Remove WebContext::refreshExistingServices; there's no need for it.

* UIProcess/mac/ServicesController.h:
* UIProcess/mac/ServicesController.mm:
Fix the build with only public headers by including NSSharingService.h itself.
Place the NSSharingServicePicker (Details) category outside the #ifdef.
Forward-declare and possibly import NSExtension SPI.

(WebKit::ServicesController::ServicesController):
Register a callback to be notified whenever the set of services changes.
When this occurs, call refreshExistingServices. We let refreshExistingServices
coalesce updates because these notifications can come in small batches.

(WebKit::ServicesController::refreshExistingServices):
Dispatch changes in service availability to all processes in all contexts.

* UIProcess/mac/WebContextMenuProxyMac.mm:
(WebKit::WebContextMenuProxyMac::setupServicesMenu):
Adjust the referenced rdar:// and call ServicesController::refreshExistingServices
instead of the now-removed WebContext:: variant. We can't remove this
yet because our services state can still be stale because NSServiceSharingPicker
can still sometimes lie about the current service state immediately after a change occurs;
once that is fixed, we should get rid of this as well as the refresh in Web Process creation.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceWebKit2ChangeLog">trunk/Source/WebKit2/ChangeLog</a></li>
<li><a href="#trunkSourceWebKit2UIProcessWebContextcpp">trunk/Source/WebKit2/UIProcess/WebContext.cpp</a></li>
<li><a href="#trunkSourceWebKit2UIProcessWebContexth">trunk/Source/WebKit2/UIProcess/WebContext.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessmacServicesControllerh">trunk/Source/WebKit2/UIProcess/mac/ServicesController.h</a></li>
<li><a href="#trunkSourceWebKit2UIProcessmacServicesControllermm">trunk/Source/WebKit2/UIProcess/mac/ServicesController.mm</a></li>
<li><a href="#trunkSourceWebKit2UIProcessmacWebContextMenuProxyMacmm">trunk/Source/WebKit2/UIProcess/mac/WebContextMenuProxyMac.mm</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceWebKit2ChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/ChangeLog (172365 => 172366)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/ChangeLog        2014-08-08 23:24:02 UTC (rev 172365)
+++ trunk/Source/WebKit2/ChangeLog        2014-08-08 23:27:25 UTC (rev 172366)
</span><span class="lines">@@ -1,3 +1,41 @@
</span><ins>+2014-08-08  Tim Horton  &lt;timothy_horton@apple.com&gt;
+
+        [mac] Dynamically update serviceability when the set of services changes
+        https://bugs.webkit.org/show_bug.cgi?id=135738
+        &lt;rdar://problem/17533459&gt;
+
+        Reviewed by Brady Eidson.
+
+        * UIProcess/WebContext.cpp:
+        (WebKit::WebContext::createNewWebProcess):
+        Adopt the new universal refreshExistingServices.
+
+        (WebKit::WebContext::refreshExistingServices): Deleted.
+        * UIProcess/WebContext.h:
+        Remove WebContext::refreshExistingServices; there's no need for it.
+
+        * UIProcess/mac/ServicesController.h:
+        * UIProcess/mac/ServicesController.mm:
+        Fix the build with only public headers by including NSSharingService.h itself.
+        Place the NSSharingServicePicker (Details) category outside the #ifdef.
+        Forward-declare and possibly import NSExtension SPI.
+
+        (WebKit::ServicesController::ServicesController):
+        Register a callback to be notified whenever the set of services changes.
+        When this occurs, call refreshExistingServices. We let refreshExistingServices
+        coalesce updates because these notifications can come in small batches.
+
+        (WebKit::ServicesController::refreshExistingServices):
+        Dispatch changes in service availability to all processes in all contexts.
+
+        * UIProcess/mac/WebContextMenuProxyMac.mm:
+        (WebKit::WebContextMenuProxyMac::setupServicesMenu):
+        Adjust the referenced rdar:// and call ServicesController::refreshExistingServices
+        instead of the now-removed WebContext:: variant. We can't remove this
+        yet because our services state can still be stale because NSServiceSharingPicker
+        can still sometimes lie about the current service state immediately after a change occurs;
+        once that is fixed, we should get rid of this as well as the refresh in Web Process creation.
+
</ins><span class="cx"> 2014-08-08  Timothy Horton  &lt;timothy_horton@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Clients that request the selection services menu after WebKit2 will get one with different metrics than otherwise
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessWebContextcpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/WebContext.cpp (172365 => 172366)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/WebContext.cpp        2014-08-08 23:24:02 UTC (rev 172365)
+++ trunk/Source/WebKit2/UIProcess/WebContext.cpp        2014-08-08 23:27:25 UTC (rev 172366)
</span><span class="lines">@@ -682,7 +682,7 @@
</span><span class="cx">     parameters.hasImageServices = ServicesController::shared().hasImageServices();
</span><span class="cx">     parameters.hasSelectionServices = ServicesController::shared().hasSelectionServices();
</span><span class="cx">     parameters.hasRichContentServices = ServicesController::shared().hasRichContentServices();
</span><del>-    ServicesController::shared().refreshExistingServices(this);
</del><ins>+    ServicesController::shared().refreshExistingServices();
</ins><span class="cx"> #endif
</span><span class="cx"> 
</span><span class="cx">     // Add any platform specific parameters
</span><span class="lines">@@ -1361,14 +1361,6 @@
</span><span class="cx">     request-&gt;completedRequest(requestID, statisticsData);
</span><span class="cx"> }
</span><span class="cx"> 
</span><del>-#if ENABLE(SERVICE_CONTROLS)
-void WebContext::refreshExistingServices()
-{
-    ServicesController::shared().refreshExistingServices(this);
-}
-#endif
-
-    
</del><span class="cx"> void WebContext::garbageCollectJavaScriptObjects()
</span><span class="cx"> {
</span><span class="cx">     sendToAllProcesses(Messages::WebProcess::GarbageCollectJavaScriptObjects());
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessWebContexth"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/WebContext.h (172365 => 172366)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/WebContext.h        2014-08-08 23:24:02 UTC (rev 172365)
+++ trunk/Source/WebKit2/UIProcess/WebContext.h        2014-08-08 23:27:25 UTC (rev 172366)
</span><span class="lines">@@ -352,10 +352,6 @@
</span><span class="cx"> 
</span><span class="cx">     void setMemoryCacheDisabled(bool);
</span><span class="cx"> 
</span><del>-#if ENABLE(SERVICE_CONTROLS)
-    void refreshExistingServices();
-#endif
-
</del><span class="cx"> private:
</span><span class="cx">     void platformInitialize();
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessmacServicesControllerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/mac/ServicesController.h (172365 => 172366)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/mac/ServicesController.h        2014-08-08 23:24:02 UTC (rev 172365)
+++ trunk/Source/WebKit2/UIProcess/mac/ServicesController.h        2014-08-08 23:27:25 UTC (rev 172366)
</span><span class="lines">@@ -28,10 +28,9 @@
</span><span class="cx"> 
</span><span class="cx"> #if ENABLE(SERVICE_CONTROLS)
</span><span class="cx"> 
</span><del>-#include &lt;wtf/HashSet.h&gt;
</del><ins>+#include &lt;wtf/Forward.h&gt;
</ins><span class="cx"> #include &lt;wtf/Noncopyable.h&gt;
</span><del>-#include &lt;wtf/RefPtr.h&gt;
-#include &lt;wtf/RunLoop.h&gt;
</del><ins>+#include &lt;wtf/RetainPtr.h&gt;
</ins><span class="cx"> 
</span><span class="cx"> namespace WebKit {
</span><span class="cx"> 
</span><span class="lines">@@ -47,21 +46,20 @@
</span><span class="cx">     bool hasSelectionServices() const { return m_hasSelectionServices; }
</span><span class="cx">     bool hasRichContentServices() const { return m_hasRichContentServices; }
</span><span class="cx"> 
</span><del>-    void refreshExistingServices(WebContext*);
</del><ins>+    void refreshExistingServices(bool refreshImmediately = true);
</ins><span class="cx"> 
</span><span class="cx"> private:
</span><span class="cx">     ServicesController();
</span><span class="cx"> 
</span><del>-    void refreshExistingServices();
-
</del><span class="cx">     dispatch_queue_t m_refreshQueue;
</span><del>-    bool m_isRefreshing;
</del><ins>+    std::atomic_bool m_hasPendingRefresh;
</ins><span class="cx"> 
</span><span class="cx">     bool m_hasImageServices;
</span><span class="cx">     bool m_hasSelectionServices;
</span><span class="cx">     bool m_hasRichContentServices;
</span><span class="cx"> 
</span><del>-    HashSet&lt;RefPtr&lt;WebContext&gt;&gt; m_contextsToNotify;
</del><ins>+    RetainPtr&lt;id&gt; m_extensionWatcher;
+    RetainPtr&lt;id&gt; m_uiExtensionWatcher;
</ins><span class="cx"> };
</span><span class="cx"> 
</span><span class="cx"> } // namespace WebKit
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessmacServicesControllermm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/mac/ServicesController.mm (172365 => 172366)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/mac/ServicesController.mm        2014-08-08 23:24:02 UTC (rev 172365)
+++ trunk/Source/WebKit2/UIProcess/mac/ServicesController.mm        2014-08-08 23:27:25 UTC (rev 172366)
</span><span class="lines">@@ -30,6 +30,7 @@
</span><span class="cx"> 
</span><span class="cx"> #import &quot;WebContext.h&quot;
</span><span class="cx"> #import &quot;WebProcessMessages.h&quot;
</span><ins>+#import &lt;AppKit/NSSharingService.h&gt;
</ins><span class="cx"> #import &lt;wtf/NeverDestroyed.h&gt;
</span><span class="cx"> 
</span><span class="cx"> #if __has_include(&lt;AppKit/NSSharingService_Private.h&gt;)
</span><span class="lines">@@ -41,13 +42,24 @@
</span><span class="cx">     NSSharingServicePickerStyleTextSelection = 2,
</span><span class="cx">     NSSharingServicePickerStyleDataDetector = 3
</span><span class="cx"> } NSSharingServicePickerStyle;
</span><ins>+#endif
</ins><span class="cx"> 
</span><span class="cx"> @interface NSSharingServicePicker (Details)
</span><span class="cx"> @property NSSharingServicePickerStyle style;
</span><span class="cx"> - (NSMenu *)menu;
</span><span class="cx"> @end
</span><ins>+
+#if __has_include(&lt;Foundation/NSExtension.h&gt;)
+#import &lt;Foundation/NSExtension.h&gt;
+#else
+@interface NSExtension
+@end
</ins><span class="cx"> #endif
</span><span class="cx"> 
</span><ins>+@interface NSExtension (Details)
++ (id)beginMatchingExtensionsWithAttributes:(NSDictionary *)attributes completion:(void (^)(NSArray *matchingExtensions, NSError *error))handler;
+@end
+
</ins><span class="cx"> namespace WebKit {
</span><span class="cx"> 
</span><span class="cx"> ServicesController&amp; ServicesController::shared()
</span><span class="lines">@@ -58,30 +70,33 @@
</span><span class="cx"> 
</span><span class="cx"> ServicesController::ServicesController()
</span><span class="cx">     : m_refreshQueue(dispatch_queue_create(&quot;com.apple.WebKit.ServicesController&quot;, DISPATCH_QUEUE_SERIAL))
</span><del>-    , m_isRefreshing(false)
</del><ins>+    , m_hasPendingRefresh(false)
</ins><span class="cx">     , m_hasImageServices(false)
</span><span class="cx">     , m_hasSelectionServices(false)
</span><span class="cx">     , m_hasRichContentServices(false)
</span><span class="cx"> {
</span><span class="cx">     refreshExistingServices();
</span><del>-}
</del><span class="cx"> 
</span><del>-void ServicesController::refreshExistingServices(WebContext* context)
-{
-    ASSERT_ARG(context, context);
-    ASSERT([NSThread isMainThread]);
</del><ins>+    auto refreshCallback = [](NSArray *, NSError *) {
+        // We coalese refreshes from the notification callbacks because they can come in small batches.
+        ServicesController::shared().refreshExistingServices(false);
+    };
</ins><span class="cx"> 
</span><del>-    m_contextsToNotify.add(context);
-    refreshExistingServices();
</del><ins>+    auto extensionAttributes = @{ @&quot;NSExtensionPointName&quot; : @&quot;com.apple.services&quot; };
+    m_extensionWatcher = [NSExtension beginMatchingExtensionsWithAttributes:extensionAttributes completion:refreshCallback];
+    auto uiExtensionAttributes = @{ @&quot;NSExtensionPointName&quot; : @&quot;com.apple.ui-services&quot; };
+    m_uiExtensionWatcher = [NSExtension beginMatchingExtensionsWithAttributes:uiExtensionAttributes completion:refreshCallback];
</ins><span class="cx"> }
</span><span class="cx"> 
</span><del>-void ServicesController::refreshExistingServices()
</del><ins>+void ServicesController::refreshExistingServices(bool refreshImmediately)
</ins><span class="cx"> {
</span><del>-    if (m_isRefreshing)
</del><ins>+    if (m_hasPendingRefresh)
</ins><span class="cx">         return;
</span><span class="cx"> 
</span><del>-    m_isRefreshing = true;
-    dispatch_async(m_refreshQueue, ^{
</del><ins>+    m_hasPendingRefresh = true;
+
+    auto refreshTime = dispatch_time(DISPATCH_TIME_NOW, refreshImmediately ? 0 : (int64_t)(1 * NSEC_PER_SEC));
+    dispatch_after(refreshTime, m_refreshQueue, ^{
</ins><span class="cx">         static NeverDestroyed&lt;NSImage *&gt; image([[NSImage alloc] init]);
</span><span class="cx">         RetainPtr&lt;NSSharingServicePicker&gt;  picker = adoptNS([[NSSharingServicePicker alloc] initWithItems:@[ image ]]);
</span><span class="cx">         [picker setStyle:NSSharingServicePickerStyleRollover];
</span><span class="lines">@@ -110,19 +125,18 @@
</span><span class="cx">         bool hasRichContentServices = picker.get().menu;
</span><span class="cx">         
</span><span class="cx">         dispatch_async(dispatch_get_main_queue(), ^{
</span><del>-            bool notifyContexts = (hasImageServices != m_hasImageServices) || (hasSelectionServices != m_hasSelectionServices) || (hasRichContentServices != m_hasRichContentServices);
</del><ins>+            bool availableServicesChanged = (hasImageServices != m_hasImageServices) || (hasSelectionServices != m_hasSelectionServices) || (hasRichContentServices != m_hasRichContentServices);
+
</ins><span class="cx">             m_hasSelectionServices = hasSelectionServices;
</span><span class="cx">             m_hasImageServices = hasImageServices;
</span><span class="cx">             m_hasRichContentServices = hasRichContentServices;
</span><span class="cx"> 
</span><del>-            if (notifyContexts) {
-                for (const RefPtr&lt;WebContext&gt;&amp; context : m_contextsToNotify)
</del><ins>+            if (availableServicesChanged) {
+                for (auto&amp; context : WebContext::allContexts())
</ins><span class="cx">                     context-&gt;sendToAllProcesses(Messages::WebProcess::SetEnabledServices(m_hasImageServices, m_hasSelectionServices, m_hasRichContentServices));
</span><span class="cx">             }
</span><span class="cx"> 
</span><del>-            m_contextsToNotify.clear();
-
-            m_isRefreshing = false;
</del><ins>+            m_hasPendingRefresh = false;
</ins><span class="cx">         });
</span><span class="cx">     });
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkSourceWebKit2UIProcessmacWebContextMenuProxyMacmm"></a>
<div class="modfile"><h4>Modified: trunk/Source/WebKit2/UIProcess/mac/WebContextMenuProxyMac.mm (172365 => 172366)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/WebKit2/UIProcess/mac/WebContextMenuProxyMac.mm        2014-08-08 23:24:02 UTC (rev 172365)
+++ trunk/Source/WebKit2/UIProcess/mac/WebContextMenuProxyMac.mm        2014-08-08 23:27:25 UTC (rev 172366)
</span><span class="lines">@@ -31,6 +31,7 @@
</span><span class="cx"> #import &quot;DataReference.h&quot;
</span><span class="cx"> #import &quot;MenuUtilities.h&quot;
</span><span class="cx"> #import &quot;PageClientImpl.h&quot;
</span><ins>+#import &quot;ServicesController.h&quot;
</ins><span class="cx"> #import &quot;ShareableBitmap.h&quot;
</span><span class="cx"> #import &quot;StringUtilities.h&quot;
</span><span class="cx"> #import &quot;WebContext.h&quot;
</span><span class="lines">@@ -412,11 +413,10 @@
</span><span class="cx">         }
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    // If there is no services menu, then the existing services on the system have changed.
-    // Ask the UIProcess to refresh that list of services.
-    // If &lt;rdar://problem/16776831&gt; is resolved then we can more accurately keep the list up to date without this call.
</del><ins>+    // If there is no services menu, then the existing services on the system have changed, so refresh that list of services.
+    // If &lt;rdar://problem/17954709&gt; is resolved then we can more accurately keep the list up to date without this call.
</ins><span class="cx">     if (!m_servicesMenu)
</span><del>-        m_page-&gt;process().context().refreshExistingServices();
</del><ins>+        ServicesController::shared().refreshExistingServices();
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> void WebContextMenuProxyMac::clearServicesMenu()
</span></span></pre>
</div>
</div>

</body>
</html>