initial import
[vuplus_webkit] / Source / WebKit2 / WebProcess / mac / SecItemShimMethods.mm
1 /*
2  * Copyright (C) 2011 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "config.h"
27 #import "SecItemShimMethods.h"
28
29 #if !defined(BUILDING_ON_SNOW_LEOPARD)
30
31 #import "CoreIPCClientRunLoop.h"
32 #import "SecItemRequestData.h"
33 #import "SecItemResponseData.h"
34 #import "WebProcess.h"
35 #import "WebProcessProxyMessages.h"
36 #import "WebProcessShim.h"
37 #import <Security/SecItem.h>
38 #import <dlfcn.h>
39
40 namespace WebKit {
41
42 // FIXME (https://bugs.webkit.org/show_bug.cgi?id=60975) - Once CoreIPC supports sync messaging from a secondary thread,
43 // we can remove SecItemAPIContext and these 4 main-thread methods, and we can have the shim methods call out directly 
44 // from whatever thread they're on.
45
46 struct SecItemAPIContext {
47     CFDictionaryRef query;
48     CFDictionaryRef attributesToUpdate;
49     CFTypeRef resultObject;
50     OSStatus resultCode;
51 };
52
53 static void webSecItemCopyMatchingMainThread(void* voidContext)
54 {
55     SecItemAPIContext* context = (SecItemAPIContext*)voidContext;
56     
57     SecItemRequestData requestData(context->query);
58     SecItemResponseData response;
59     if (!WebProcess::shared().connection()->sendSync(Messages::WebProcessProxy::SecItemCopyMatching(requestData), Messages::WebProcessProxy::SecItemCopyMatching::Reply(response), 0)) {
60         context->resultCode = errSecInteractionNotAllowed;
61         ASSERT_NOT_REACHED();
62         return;
63     }
64     
65     context->resultObject = response.resultObject().leakRef();
66     context->resultCode = response.resultCode();
67 }
68
69 static OSStatus webSecItemCopyMatching(CFDictionaryRef query, CFTypeRef* result)
70 {
71     SecItemAPIContext context;
72     context.query = query;
73     
74     callOnCoreIPCClientRunLoopAndWait(webSecItemCopyMatchingMainThread, &context);
75     
76     if (result)
77         *result = context.resultObject;
78     return context.resultCode;
79 }
80
81 static void webSecItemAddOnMainThread(void* voidContext)
82 {
83     SecItemAPIContext* context = (SecItemAPIContext*)voidContext;
84     
85     SecItemRequestData requestData(context->query);
86     SecItemResponseData response;
87     if (!WebProcess::shared().connection()->sendSync(Messages::WebProcessProxy::SecItemAdd(requestData), Messages::WebProcessProxy::SecItemAdd::Reply(response), 0)) {
88         context->resultCode = errSecInteractionNotAllowed;
89         ASSERT_NOT_REACHED();
90         return;
91     }
92     
93     context->resultObject = response.resultObject().leakRef();
94     context->resultCode = response.resultCode();
95 }
96
97 static OSStatus webSecItemAdd(CFDictionaryRef query, CFTypeRef* result)
98 {
99     SecItemAPIContext context;
100     context.query = query;
101     
102     callOnCoreIPCClientRunLoopAndWait(webSecItemAddOnMainThread, &context);
103     
104     if (result)
105         *result = context.resultObject;
106     return context.resultCode;
107 }
108
109 static void webSecItemUpdateOnMainThread(void* voidContext)
110 {
111     SecItemAPIContext* context = (SecItemAPIContext*)voidContext;
112     
113     SecItemRequestData requestData(context->query, context->attributesToUpdate);
114     SecItemResponseData response;
115     if (!WebProcess::shared().connection()->sendSync(Messages::WebProcessProxy::SecItemUpdate(requestData), Messages::WebProcessProxy::SecItemUpdate::Reply(response), 0)) {
116         context->resultCode = errSecInteractionNotAllowed;
117         ASSERT_NOT_REACHED();
118         return;
119     }
120     
121     context->resultCode = response.resultCode();
122 }
123
124 static OSStatus webSecItemUpdate(CFDictionaryRef query, CFDictionaryRef attributesToUpdate)
125 {
126     SecItemAPIContext context;
127     context.query = query;
128     context.attributesToUpdate = attributesToUpdate;
129     
130     callOnCoreIPCClientRunLoopAndWait(webSecItemUpdateOnMainThread, &context);
131     
132     return context.resultCode;
133 }
134
135 static void webSecItemDeleteOnMainThread(void* voidContext)
136 {
137     SecItemAPIContext* context = (SecItemAPIContext*)voidContext;
138     
139     SecItemRequestData requestData(context->query);
140     SecItemResponseData response;
141     if (!WebProcess::shared().connection()->sendSync(Messages::WebProcessProxy::SecItemDelete(requestData), Messages::WebProcessProxy::SecItemDelete::Reply(response), 0)) {
142         context->resultCode = errSecInteractionNotAllowed;
143         ASSERT_NOT_REACHED();
144         return;
145     }
146     
147     context->resultCode = response.resultCode();
148 }
149
150 static OSStatus webSecItemDelete(CFDictionaryRef query)
151 {
152     SecItemAPIContext context;
153     context.query = query;
154     
155     callOnCoreIPCClientRunLoopAndWait(webSecItemDeleteOnMainThread, &context);
156
157     return context.resultCode;
158 }
159
160 void initializeSecItemShim()
161 {
162     const WebProcessSecItemShimCallbacks callbacks = {
163         webSecItemCopyMatching,
164         webSecItemAdd,
165         webSecItemUpdate,
166         webSecItemDelete
167     };
168     
169     WebProcessSecItemShimInitializeFunc func = reinterpret_cast<WebProcessSecItemShimInitializeFunc>(dlsym(RTLD_DEFAULT, "WebKitWebProcessSecItemShimInitialize"));
170     func(callbacks);
171 }
172
173 } // namespace WebKit
174
175 #endif // !BUILDING_ON_SNOW_LEOPARD