1/*
2 * Copyright (c) 1998-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#include "DAAgent.h"
25#include "DADialog.h"
26
27#include <xpc/xpc.h>
28#include <DiskArbitration/DiskArbitrationPrivate.h>
29
30static void __DAAgentMessageCallback( xpc_object_t object );
31
32static void __DAAgentConnectionCallback( xpc_object_t object )
33{
34    xpc_type_t type;
35
36    type = xpc_get_type( object );
37
38    if ( type == XPC_TYPE_CONNECTION )
39    {
40        xpc_connection_set_event_handler( object, ^( xpc_object_t object ) { __DAAgentMessageCallback( object ); } );
41
42        xpc_connection_resume( object );
43    }
44}
45
46static void __DAAgentMessageCallback( xpc_object_t object )
47{
48    xpc_type_t type;
49
50    type = xpc_get_type( object );
51
52    if ( type == XPC_TYPE_DICTIONARY )
53    {
54        const void * _disk;
55        size_t       _diskSize;
56
57        _disk = xpc_dictionary_get_data( object, _kDAAgentDiskKey, &_diskSize );
58
59        if ( _disk )
60        {
61            CFDataRef serialization;
62
63            serialization = CFDataCreateWithBytesNoCopy( kCFAllocatorDefault, _disk, _diskSize, kCFAllocatorNull );
64
65            if ( serialization )
66            {
67                DASessionRef session;
68
69                session = DASessionCreate( kCFAllocatorDefault );
70
71                if ( session )
72                {
73                    DADiskRef disk;
74
75                    disk = _DADiskCreateFromSerialization( kCFAllocatorDefault, session, serialization );
76
77                    if ( disk )
78                    {
79                        _DAAgentAction _action;
80
81                        _action = xpc_dictionary_get_uint64( object, _kDAAgentActionKey );
82
83                        switch ( _action )
84                        {
85                            case _kDAAgentActionShowDeviceRemoval:
86                            {
87                                DADialogShowDeviceRemoval( disk );
88
89                                break;
90                            }
91                            case _kDAAgentActionShowDeviceUnreadable:
92                            {
93                                DADialogShowDeviceUnreadable( disk );
94
95                                break;
96                            }
97                            case _kDAAgentActionShowDeviceUnrepairable:
98                            {
99                                DADialogShowDeviceUnrepairable( disk );
100
101                                break;
102                            }
103                        }
104
105                        CFRelease( disk );
106                    }
107
108                    CFRelease( session );
109                }
110
111                CFRelease( serialization );
112            }
113        }
114    }
115}
116
117int main( )
118{
119    xpc_connection_t connection;
120
121    connection = xpc_connection_create_mach_service( _kDAAgentName, NULL, XPC_CONNECTION_MACH_SERVICE_LISTENER );
122
123    if ( connection )
124    {
125        xpc_connection_set_event_handler( connection, ^( xpc_object_t object ) { __DAAgentConnectionCallback( object ); } );
126
127        xpc_connection_resume( connection );
128
129        dispatch_main( );
130    }
131
132    return 0;
133}
134