1/*
2 * Copyright (C) 2013 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#include "config.h"
27#include "SecItemShimProxy.h"
28
29#if ENABLE(SEC_ITEM_SHIM)
30
31#include "SecItemRequestData.h"
32#include "SecItemResponseData.h"
33#include "SecItemShimMessages.h"
34#include "SecItemShimProxyMessages.h"
35#include <Security/SecBase.h>
36#include <Security/SecItem.h>
37
38namespace WebKit {
39
40SecItemShimProxy& SecItemShimProxy::shared()
41{
42    static SecItemShimProxy* proxy;
43    static dispatch_once_t once;
44    dispatch_once(&once, ^{
45        proxy = adoptRef(new SecItemShimProxy).leakRef();
46    });
47    return *proxy;
48}
49
50SecItemShimProxy::SecItemShimProxy()
51    : m_queue(WorkQueue::create("com.apple.WebKit.SecItemShimProxy"))
52{
53}
54
55void SecItemShimProxy::initializeConnection(IPC::Connection* connection)
56{
57    connection->addWorkQueueMessageReceiver(Messages::SecItemShimProxy::messageReceiverName(), m_queue.get(), this);
58}
59
60void SecItemShimProxy::secItemRequest(IPC::Connection* connection, uint64_t requestID, const SecItemRequestData& request)
61{
62    SecItemResponseData response;
63
64    switch (request.type()) {
65    case SecItemRequestData::Invalid:
66        LOG_ERROR("SecItemShimProxy::secItemRequest received an invalid data request. Please file a bug if you know how you caused this.");
67        response = SecItemResponseData(errSecParam, nullptr);
68        break;
69
70    case SecItemRequestData::CopyMatching: {
71        CFTypeRef resultObject = 0;
72        OSStatus resultCode = SecItemCopyMatching(request.query(), &resultObject);
73        response = SecItemResponseData(resultCode, adoptCF(resultObject).get());
74        break;
75    }
76
77    case SecItemRequestData::Add: {
78        CFTypeRef resultObject = 0;
79        OSStatus resultCode = SecItemAdd(request.query(), &resultObject);
80        response = SecItemResponseData(resultCode, adoptCF(resultObject).get());
81        break;
82    }
83
84    case SecItemRequestData::Update: {
85        OSStatus resultCode = SecItemUpdate(request.query(), request.attributesToMatch());
86        response = SecItemResponseData(resultCode, 0);
87        break;
88    }
89
90    case SecItemRequestData::Delete: {
91        OSStatus resultCode = SecItemDelete(request.query());
92        response = SecItemResponseData(resultCode, 0);
93        break;
94    }
95    }
96
97    connection->send(Messages::SecItemShim::SecItemResponse(requestID, response), 0);
98}
99
100} // namespace WebKit
101
102#endif // ENABLE(SEC_ITEM_SHIM)
103