CloudyTabs icon indicating copy to clipboard operation
CloudyTabs copied to clipboard

Refreshed only when I open Safari

Open timscha opened this issue 7 years ago • 11 comments

Hi! I tried the latest version on macOS 10.13. My problem is that the App refresh the tabs only, everytime I re-open Safari on my Mac. Do you need some more informations?

timscha avatar Apr 27 '18 17:04 timscha

Same problem for me. The tabs in CloudyTabs are stale until I open Safari, then they update. Of course this defeats the purpose of CloudyTabs...

mynameistechno avatar May 10 '18 17:05 mynameistechno

Hi, I'm seeing the same behaviour where only opening safari will update the tabs.

macOS 10.13.5 (High Sierra), CloudyTabs 1.9.1

If you need any logs or anything to help with reproduction, shout out!

jamie--stewart avatar Jun 06 '18 21:06 jamie--stewart

Thanks for the feedback @timscha, @mynameistechno and @jamie--stewart! I've done a bit of digging to try and resolve this but unfortunately haven't been able to come up with a solution as of yet. If anyone has any thoughts I'd love to hear them!

Technical investigation:

The SafariBookmarksSyncAgent (located at /System/Library/CoreServices/SafariSupport.bundle/Contents/MacOS/SafariBookmarksSyncAgent) is the daemon that is responsible for syncing iCloud Tabs in recent macOS version. It receives notifications from Apple's Push Notification Service (through apsd) and is registered with it at startup.

SafariCore (located at /System/Library/PrivateFrameworks/SafariCore.framework/Versions/A/SafariCore) is the executable responsible for establishing a connection to this XPC service and writing the resulting data to the CloudTabs.db database (at /Users/josh/Library/Safari/CloudTabs.db – read by CloudyTabs). It is used by the Safari application and thus will only write these values to the database when Safari is running. This is observable by using the "File Activity" report in Instruments.app and observing SafariCore modifying the database through its WBSSQLiteStatement statements.

I've tried to establish an XPC connection to the SafariBookmarksSyncAgent in the same manner that SafariCore does by using the code in this Gist (which was from an otool reverse-engineering of SafariCore). However the invalidationHandler is always immediately called and thus I can't establish a connection to the service. This seems strange because SafariBookmarksSyncAgent's shouldAcceptNewConnection method seems to just return YES without checking for a callers bundle or anything:

screen shot 2018-05-20 at 12 11 28 pm

This leads me to believe that I am somehow incorrectly trying to connect to the service (probably because, the initWithMachServiceName:options: seems to only look in /Library/LaunchAgents or ~/Library/LaunchAgents). Furthermore, the SafariBookmarksSyncAgent is sandboxed so that might cause some further considerations.

I'll keep investigating but if anyone else has any further insight I'd be extremely grateful to hear it 🙌

josh- avatar Jun 10 '18 05:06 josh-

Update

Turns out all that needed to be done was actually link against SafariSupport.bundle and then the XPC connection works totally fine 😂 (after removing the NSXPCConnectionPrivileged flag)

I've been able to successfully view tabs updating without having Safari open. 👍 The next steps will be to try and integrate this into CloudyTabs – I'll keep you posted with how that's going!

josh- avatar Jun 10 '18 11:06 josh-

Great, thanks! Good spot 👌

jamie--stewart avatar Jun 11 '18 07:06 jamie--stewart

@josh- If you need some testers or other help let me know. :)

timscha avatar Jun 11 '18 07:06 timscha

Any update here? I have the same issue on 2.0.0.

felixradtke avatar Oct 06 '20 23:10 felixradtke

Not ideal, but I've made a pretty lightweight way to automatically open and close Safari on an interval to help with this. I use SwiftBar/xbar/BitBar and a script run on a 15-minute interval (so named bouncesafari.15m.sh) containing the following:

#!/usr/bin/env sh

open -g -j /Applications/Safari.app
sleep 10
osascript -e 'tell app "Safari" to quit'

This doesn't take keyboard focus as -g opens in the background and -j starts hidden, then after 10 seconds Safari is quit. You do see it briefly in the Dock, but I can live with that for now.

Hope it helps someone else!

incanus avatar Oct 20 '21 17:10 incanus

A tweak to the above, which helps if you start using Safari for some reason and it's the active, frontmost app:

#!/usr/bin/env bash

open -g -j /Applications/Safari.app
sleep 10
FRONT=`osascript -e 'tell app "System Events" to get name of processes whose frontmost is true'`
if [ $FRONT != "Safari" ]; then
    osascript -e 'tell app "Safari" to quit'
fi

incanus avatar Oct 26 '21 17:10 incanus

I've homed in on a better solution for my case, which is a Hammerspoon action to open Safari in the background (not stealing focus) based on a key shortcut, then quit after five seconds in order to sync bookmarks.

hs.hotkey.bind({'cmd', 'alt', 'ctrl'}, 's', function()
  hs.task.new('/usr/bin/open', nil, {'-g', '-j', '-a', 'Safari'}):start()
  hs.timer.doAfter(5, function()
    _, _, front = hs.osascript.applescript('tell app "System Events" to get name of processes whose frontmost is true')
    if front ~= 'Safari' then
      hs.appfinder.appFromName('Safari'):kill()
    end
  end)
end)

incanus avatar Nov 16 '21 17:11 incanus

Update

Turns out all that needed to be done was actually link against SafariSupport.bundle and then the XPC connection works totally fine 😂 (after removing the NSXPCConnectionPrivileged flag)

I've been able to successfully view tabs updating without having Safari open. 👍 The next steps will be to try and integrate this into CloudyTabs – I'll keep you posted with how that's going!

Hi @josh-, may I know is this fixed? As of now, I still need to run safari to have the tabs updated in CloudyTabs, would love to hear back from you.

Bicheng-G avatar Mar 13 '23 07:03 Bicheng-G