Asynchronous API framework
[platform/core/security/key-manager.git] / src / manager / client-async / client-manager-async-impl.cpp
1 /*
2  *  Copyright (c) 2000 - 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License
15  */
16 /*
17  * @file       client-manager-async-impl.cpp
18  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
19  * @version    1.0
20  */
21
22 #include <client-manager-async-impl.h>
23 #include <ckm/ckm-error.h>
24 #include <message-buffer.h>
25 #include <client-common.h>
26 #include <stdexcept>
27
28 namespace CKM {
29
30 int ManagerAsync::Impl::m_counter = 0;
31
32 ManagerAsync::Impl::Impl()
33 {
34 }
35
36 ManagerAsync::Impl::~Impl()
37 {
38 }
39
40 void ManagerAsync::Impl::saveKey(const ManagerAsync::ObserverPtr& observer,
41                                  const Alias& alias,
42                                  const KeyShPtr& key,
43                                  const Policy& policy)
44 {
45     observerCheck(observer);
46
47     if (!key) {
48         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
49         return;
50     }
51     saveBinaryData(observer, alias, toDBDataType(key->getType()), key->getDER(), policy);
52 }
53
54 void ManagerAsync::Impl::saveBinaryData(const ManagerAsync::ObserverPtr& observer,
55                                         const Alias& alias,
56                                         DBDataType dataType,
57                                         const RawBuffer& rawData,
58                                         const Policy& policy)
59 {
60     if (alias.empty() || rawData.empty()) {
61         observer->ReceivedError(CKM_API_ERROR_INPUT_PARAM);
62         return;
63     }
64
65     try_catch_async([&] {
66         m_counter++;
67
68         MessageBuffer send;
69         Serialization::Serialize(send, static_cast<int>(LogicCommand::SAVE));
70         Serialization::Serialize(send, m_counter);
71         Serialization::Serialize(send, static_cast<int>(dataType));
72         Serialization::Serialize(send, alias);
73         Serialization::Serialize(send, rawData);
74         Serialization::Serialize(send, PolicySerializable(policy));
75
76         thread()->sendMessage(AsyncRequest(observer,
77                                            SERVICE_SOCKET_CKM_STORAGE,
78                                            send.Pop(),
79                                            m_counter));
80
81     }, [&observer](int error){ observer->ReceivedError(error); } );
82 }
83
84 void ManagerAsync::Impl::observerCheck(const ManagerAsync::ObserverPtr& observer)
85 {
86     if(!observer)
87         throw std::invalid_argument("Empty observer");
88 }
89
90 } // namespace CKM