Add client cache class for manage data robustly
[platform/core/security/pubkey-pinning.git] / src / common / src / tpkp_client_cache.cpp
1 /*
2  * Copyright (c) 2016 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        tpkp_client_cache.cpp
18  * @author      Kyungwook Tak (k.tak@samsung.com)
19  * @version     1.0
20  * @brief       Https Public Key Pinning client cache implementation.
21  */
22 #include "tpkp_client_cache.h"
23
24 #include <string>
25 #include <map>
26 #include <sys/syscall.h>
27 #include <unistd.h>
28
29 #include "tpkp_logger.h"
30
31 namespace {
32
33 pid_t _getThreadId()
34 {
35         return syscall(SYS_gettid);
36 }
37
38 }
39
40 namespace TPKP {
41
42 ClientCache::ClientCache() {}
43
44 ClientCache::~ClientCache() {}
45
46 void ClientCache::setUrl(const std::string &url)
47 {
48         auto tid = _getThreadId();
49         {
50                 std::lock_guard<std::mutex> lock(m_url_mutex);
51                 m_urls[tid] = url;
52         }
53
54         SLOGD("set url[%s] of thread id[%u]", url.c_str(), tid);
55 }
56
57 std::string ClientCache::getUrl(void)
58 {
59         std::string url;
60
61         auto tid = _getThreadId();
62         {
63                 std::lock_guard<std::mutex> lock(m_url_mutex);
64                 url = m_urls[tid];
65         }
66
67         SLOGD("get url[%s] from thread id[%u]", url.c_str(), tid);
68
69         return url;
70 }
71
72 void ClientCache::eraseUrl(void)
73 {
74         auto tid = _getThreadId();
75         {
76                 std::lock_guard<std::mutex> lock(m_url_mutex);
77                 m_urls.erase(tid);
78         }
79
80         SLOGD("erase url of mapped by thread id[%u]", tid);
81 }
82
83 void ClientCache::eraseUrlAll(void)
84 {
85         m_urls.clear();
86
87         SLOGD("erase all urls saved of client");
88 }
89
90 }