Safari App Extension and XPC

Is it possible for the main app to comunicate via XPC with the Safari App Extension?


I've been trying to get this to work, but have failed to setup NSXPCConnection between the two. From experience using XPC services, I know that setting this up can be very finicky. So before wasting many hours trying to guess what's the problem, I'd love to know if it's even possible.


Thanks!

Replies

What are you trying to accomplish with the communication between your app extension and your native app?

I want my extension to be able to fetch some data (for filling in web forms) to the main app.


I've already built this around plain unix sockets, but it would be nice to know if it would be possible to use XPC.

Hello, Did you find out if it's possible ? I'm also interested to know. Thanks

Also interested in the answer to this question. Anyone from Apple know the answer? It seems like it might be possible to lookup a service by its machServiceName (instead of serviceName), however, I tried this and sandbox violations occur. It may just be misconfigured in the App Sandbox?


From what I have seen in the Apple documentation, app to app XPC is not possible due to application sandboxing, however, I've seen a couple of internet threads that imply it can be achieved.

I just tried this very thing with a swift version of the basic "XPC Service" xcode template. I successfully received a reply from an XPC service from the main container app, but when I try to run the same code from the app extension I get the following error:

The connection to service named com.xpc-service was invalidated.


Just based on that, it does not appear to possible, but would love some confirmation from apple as well!

Have the same problem. Any ideas?

We can open a reliable XPC channel betwixt our container application and our extension; there are two catches though, one of which might prove a showstopper for most usages.


(1) the container application needs to listen as an XPC server for connections from other tasks


Far as I know (based on some excellent Quinn's advice), this, alas, is possible only if the container application happens to be a login item; otherwise the XPC system does not allow for that. This might be a big problem, and I absolutely can't see why Apple did limit the XPC usage in this way 😟


(2) the extension, being sandboxed, cannot directly open an XPC connexion to the container application; nevertheless, it can contain its own XPC service

- to which a connexion is open automatically;

- and which can open its own connexion to the container.


It's alas a bit at the Rube-Goldbergish side, but so far it seems to work for us all right.

Hi OCS1,


Now, I have a problem .

I want to use safari app extension to dlopen a share library(.dylib).This library have ipc sysv sem ..But, extension is a sanbox..how can i do?

I have try with xpc,Impossible。


Could you help me, how it contain its own xpc service.I have no idea. Is there a doc?


Thanks very much ~

I am not quite sure what was “impossible with xpc” — so you already have tried and it did not work?


Anyway, to add a contained XPC service to an extension is pretty straightforward — just use the standard Xcode “New / Target” menu item, select “XPC Service” from the panel, and finish that stuff. At the moment, I can't recall whether Xcode allows you to select directly the extension for “Embed in Application”; if it does, just select it, and you are done.


If it does not, choose “None”, and then in your Extension “Build Phases” panel

- add the new target into its Target Dependencies;

- add a new Copy Files phase, to “Contents/XPCServices”, add the product of the new target.


That should be all. I believe it's documented somewhere in the Apple XPC Services documentation; probably not directly for a Safari App Extension, but the principle is the same for any kind of a client executable.

thank you very much .

that is very helpful for me .

hi ,

now i have a trouble with xpc.

I need to load a dynamic library use xpc..this library need to display UI.Then console catch error :


*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:


'NSWindow drag regions should only be invalidated on the Main Thread!'

*** First throw call stack:

(

0 CoreFoundation 0x00007fff49eceecd __exceptionPreprocess + 256

1 libobjc.A.dylib 0x00007fff75f96720 objc_exception_throw + 48

2 CoreFoundation 0x00007fff49ee895d -[NSException raise] + 9

3 AppKit 0x00007fff473e9c8e -[NSWindow(NSWindow_Theme) _postWindowNeedsToResetDragMarginsUnlessPostingDisabled] + 324

4 AppKit 0x00007fff473e707c -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 1488

5 AppKit 0x00007fff473e6aa6 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 45

6 AppKit 0x00007fff473e4fb7 -[NSWindowTemplate nibInstantiate] + 495

7 AppKit 0x00007fff4736004e -[N<…>


how can i run it ?

This sounds like you aren't creating the NSWIndow on the main thread.


Make sure that any code you have to create the NSWindow is done inside a:


dispatch_async(dispatch_get_main_queue(), ^{

//Your code here.

})

I have been try it ..code for example:


dispatch_async(dispatch_get_main_queue(), ^{

testView *tv = [[testView alloc] initWithWindowNibName:@"testView"];

[tv showSelfWindow];

NSLog(@"show Self Windows.");

});


-(void)showSelfWindow

{

[NSApp activateIgnoringOtherApps:YES];

[[self window] makeKeyAndOrderFront:self];

[[self window] setLevel:NSFloatingWindowLevel];

[NSApp beginModalSessionForWindow:[self window]];

}

Then,this xpc process is crashed. such as:


Process: xpcHelp [24703]

Path: /Users/USER/Library/Developer/Xcode/DerivedData/FeiTianMgr-dksrvbbveekoewdqoukdcwspttaz/Build/Products/Debug/FeiTianMgr.app/Contents/PlugIns/BrowserExt.appex/Contents/XPCServices/xpcHelp.xpc/Contents/MacOS/xpcHelp

Identifier: com.ftsafe.xpcHelp

Version: 1.0 (1)

Code Type: X86-64 (Native)

Parent Process: ??? [24682]

Responsible: BrowserExt [24688]

User ID: 501



Date/Time: 2019-02-18 11:50:05.637 +0800

OS Version: Mac OS X 10.14.3 (18D32a)

Report Version: 12

Bridge OS Version: 3.3 (16P53129a)

Anonymous UUID: 7E1DE390-4665-9E9B-5FE8-F973A45D07C5



Sleep/Wake UUID: 9424F2D7-4B0C-41F7-9917-14B1C774054C



Time Awake Since Boot: 230000 seconds

Time Since Wake: 69000 seconds



System Integrity Protection: enabled



Crashed Thread: 4 Dispatch queue: com.apple.main-thread



Exception Type: EXC_CRASH (SIGABRT)

Exception Codes: 0x0000000000000000, 0x0000000000000000

Exception Note: EXC_CORPSE_NOTIFY



External Modification Warnings:

Debugger attached to process.



Application Specific Information:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSWindow drag regions should only be invalidated on the Main Thread!'

abort() called

terminating with uncaught exception of type NSException



Application Specific Backtrace 1:

0 CoreFoundation 0x00007fff49eceecd __exceptionPreprocess + 256

1 libobjc.A.dylib 0x00007fff75f96720 objc_exception_throw + 48

2 CoreFoundation 0x00007fff49ee895d -[NSException raise] + 9

3 AppKit 0x00007fff473e9c8e -[NSWindow(NSWindow_Theme) _postWindowNeedsToResetDragMarginsUnlessPostingDisabled] + 324

4 AppKit 0x00007fff473e707c -[NSWindow _initContent:styleMask:backing:defer:contentView:] + 1488

5 AppKit 0x00007fff473e6aa6 -[NSWindow initWithContentRect:styleMask:backing:defer:] + 45

6 AppKit 0x00007fff473e4fb7 -[NSWindowTemplate nibInstantiate] + 495

7 AppKit 0x00007fff4736004e -[NSIBObjectData instantiateObject:] + 267

8 AppKit 0x00007fff4735f3bc -[NSIBObjectData nibInstantiateWithOwner:options:topLevelObjects:] + 579

9 AppKit 0x00007fff4735d5e9 loadNib + 401

10 AppKit 0x00007fff4735cab1 +[NSBundle(NSNibLoading) _loadNibFile:nameTable:options:withZone:ownerBundle:] + 696

11 AppKit 0x00007fff474e90d2 +[NSBundle(NSNibLoadingInternal) _loadNibFile:externalNameTable:options:withZone:] + 156

12 AppKit 0x00007fff474e8e8f -[NSWindowController loadWindow] + 322

13 AppKit 0x00007fff4744b618 -[NSWindowController window] + 84

14 xpcHelp 0x00000001099e71da -[testView showSelfWindow] + 58

15 xpcHelp 0x0000000109a3a747 __48-[xpcHelp sendToXpcWith:withUserInfo:withReply:]_block_invoke + 87

16 libdispatch.dylib 0x00007fff77015d53 _dispatch_call_block_and_release + 12

17 libdispatch.dylib 0x00007fff77016dcf _dispatch_client_callout + 8

18 libdispatch.dylib 0x00007fff7701d124 _dispatch_lane_serial_drain + 618

19 libdispatch.dylib 0x00007fff7701dc0c _dispatch_lane_invoke + 436

20 libdispatch.dylib 0x00007fff77024d26 _dispatch_root_queue_drain + 325

21 libdispatch.dylib 0x00007fff770254bd _dispatch_worker_thread2 + 90

22 libsystem_pthread.dylib 0x00007fff772576dd _pthread_wqthread + 619

23 libsystem_pthread.dylib 0x00007fff77257405 start_wqthread + 13



Thread 0:

0 libsystem_kernel.dylib 0x00007fff771a207a __semwait_signal_nocancel + 10

1 libsystem_c.dylib 0x00007fff770b7962 nanosleep$NOCANCEL + 188

2 libsystem_c.dylib 0x00007fff770ddf2a sleep$NOCANCEL + 41

3 libdispatch.dylib 0x00007fff77026838 _dispatch_queue_cleanup2 + 112

4 libsystem_pthread.dylib 0x00007fff772586c2 _pthread_tsd_cleanup + 550

5 libsystem_pthread.dylib 0x00007fff7725b679 _pthread_exit + 70

6 libsystem_pthread.dylib 0x00007fff77258473 pthread_exit + 30

7 libdispatch.dylib 0x00007fff77022291 dispatch_main + 110

8 libxpc.dylib 0x00007fff7729ceda _xpc_objc_main + 583

9 libxpc.dylib 0x00007fff7729c9bd xpc_main + 433

10 com.apple.Foundation 0x00007fff4c189ce4 -[NSXPCListener resume] + 162

11 com.ftsafe.xpcHelp 0x00000001099e73de main + 142 (main.m:47)

12 libdyld.dylib 0x00007fff77064ed9 start + 1



Thread 1:: Dispatch queue: com.apple.NSXPCConnection.user.24688

0 libsystem_kernel.dylib 0x00007fff771a1eae __semwait_signal + 10

1 libsystem_c.dylib 0x00007fff7712c830 nanosleep + 199

2 libsystem_c.dylib 0x00007fff7712c692 sleep + 41

3 com.ftsafe.xpcHelp 0x0000000109a3a603 -[xpcHelp sendToXpcWith:withUserInfo:withReply:] + 371 (xpcHelp.mm:25)

4 com.apple.Foundation 0x00007fff4c1acd93 __NSXPCCONNECTION_IS_CALLING_OUT_TO_EXPORTED_OBJECT_S3__ + 12

5 com.apple.Foundation 0x00007fff4c18bb38 -[NSXPCConnection _decodeAndInvokeMessageWithEvent:flags:] + 2609

6 com.apple.Foundation 0x00007fff4c18ae9c message_handler + 227

7 libxpc.dylib 0x00007fff77298a28 _xpc_connection_call_event_handler + 56

8 libxpc.dylib 0x00007fff772969be _xpc_connection_mach_event + 933

9 libdispatch.dylib 0x00007fff77016e6f _dispatch_client_callout4 + 9

10 libdispatch.dylib 0x00007fff7702bb0f _dispatch_mach_msg_invoke + 449

11 libdispatch.dylib 0x00007fff7701cfc9 _dispatch_lane_serial_drain + 271

12 libdispatch.dylib 0x00007fff7702c639 _dispatch_mach_invoke + 485

13 libdispatch.dylib 0x00007fff7701cfc9 _dispatch_lane_serial_drain + 271

14 libdispatch.dylib 0x00007fff7701dc0c _dispatch_lane_invoke + 436

15 libdispatch.dylib 0x00007fff77026090 _dispatch_workloop_worker_thread + 603

16 libsystem_pthread.dylib 0x00007fff7725760b _pthread_wqthread + 409

17 libsystem_pthread.dylib 0x00007fff77257405 start_wqthread + 13



Thread 2:: Dispatch queue: com.apple.root.default-qos.overcommit

0 libsystem_kernel.dylib 0x00007fff771a20a2 __sigsuspend_nocancel + 10

1 libdispatch.dylib 0x00007fff770268b5 _dispatch_sigsuspend + 21

2 libdispatch.dylib 0x00007fff770268a0 _dispatch_sig_thread + 53



Thread 3:

0 libsystem_pthread.dylib 0x00007fff772573f8 start_wqthread + 0

1 ??? 0xffffffffffffff00 0 + 18446744073709551360



Thread 4 Crashed:: Dispatch queue: com.apple.main-thread

0 libsystem_kernel.dylib 0x00007fff771a423e __pthread_kill + 10

1 libsystem_pthread.dylib 0x00007fff7725ac1c pthread_kill + 285

2 libsystem_c.dylib 0x00007fff7710d1c9 abort + 127

3 libc++abi.dylib 0x00007fff7478d231 abort_message + 231

4 libc++abi.dylib 0x00007fff7478d3cd default_terminate_handler() + 265

5 libobjc.A.dylib 0x00007fff75f98c87 _objc_terminate() + 97

6 libc++abi.dylib 0x00007fff74798dfe std::__terminate(void (*)()) + 8

7 libc++abi.dylib 0x00007fff74798e73 std::terminate() + 51

8 libdispatch.dylib 0x00007fff77016de3 _dispatch_client_callout + 28

9 libdispatch.dylib 0x00007fff7701d124 _dispatch_lane_serial_drain + 618

10 libdispatch.dylib 0x00007fff7701dc0c _dispatch_lane_invoke + 436

11 libdispatch.dylib 0x00007fff77024d26 _dispatch_root_queue_drain + 325

12 libdispatch.dylib 0x00007fff770254bd _dispatch_worker_thread2 + 90

13 libsystem_pthread.dylib 0x00007fff772576dd _pthread_wqthread + 619

14 libsystem_pthread.dylib 0x00007fff77257405 start_wqthread + 13