e8ba3414810bb69deb5d575dfa5779a1146468ba
[platform/core/api/webapi-plugins.git] / src / callhistory / callhistory_instance.cc
1 /*
2  * Copyright (c) 2015 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 #include "callhistory/callhistory_instance.h"
18
19 #include "common/logger.h"
20 #include "common/picojson.h"
21 #include "common/platform_exception.h"
22 #include "common/tools.h"
23
24 namespace extension {
25 namespace callhistory {
26
27 namespace {
28 // The privileges that required in CallHistory API
29 const std::string kPrivilegeCallHistoryRead = "http://tizen.org/privilege/callhistory.read";
30 const std::string kPrivilegeCallHistoryWrite = "http://tizen.org/privilege/callhistory.write";
31 }
32
33 using namespace common;
34
35 CallHistoryInstance::CallHistoryInstance() : history_(*this) {
36   ScopeLogger();
37   using std::placeholders::_1;
38   using std::placeholders::_2;
39
40 #define REGISTER_METHOD(M) \
41     RegisterSyncHandler(#M, std::bind(&CallHistoryInstance::M, this, _1, _2))
42
43   REGISTER_METHOD(CallHistoryRemove);
44   REGISTER_METHOD(CallHistoryAddChangeListener);
45   REGISTER_METHOD(CallHistoryRemoveChangeListener);
46   REGISTER_METHOD(CallHistorySetMissedDirection);
47
48   REGISTER_METHOD(CallHistoryFind);
49   REGISTER_METHOD(CallHistoryRemoveBatch);
50   REGISTER_METHOD(CallHistoryRemoveAll);
51
52 #undef REGISTER_METHOD
53 }
54
55 CallHistoryInstance::~CallHistoryInstance() {
56   ScopeLogger();
57 }
58
59 void CallHistoryInstance::CallHistoryFind(const picojson::value& args, picojson::object& out) {
60   ScopeLogger();
61   CHECK_PRIVILEGE_ACCESS(kPrivilegeCallHistoryRead, &out);
62   history_.find(args.get<picojson::object>());
63   ReportSuccess(out);
64 }
65
66 void CallHistoryInstance::CallHistoryRemove(const picojson::value& args, picojson::object& out) {
67   ScopeLogger();
68   CHECK_PRIVILEGE_ACCESS(kPrivilegeCallHistoryWrite, &out);
69   PlatformResult result = history_.remove(args.get<picojson::object>());
70   if (result.IsSuccess()) {
71     ReportSuccess(out);
72   } else {
73     LogAndReportError(result, &out);
74   }
75 }
76
77 void CallHistoryInstance::CallHistoryRemoveBatch(const picojson::value& args, picojson::object& out) {
78   ScopeLogger();
79   CHECK_PRIVILEGE_ACCESS(kPrivilegeCallHistoryWrite, &out);
80   PlatformResult result = history_.removeBatch(args.get<picojson::object>());
81   if (result.IsSuccess()) {
82     ReportSuccess(out);
83   } else {
84     LogAndReportError(result, &out);
85   }
86 }
87
88 void CallHistoryInstance::CallHistoryRemoveAll(const picojson::value& args, picojson::object& out) {
89   ScopeLogger();
90   CHECK_PRIVILEGE_ACCESS(kPrivilegeCallHistoryWrite, &out);
91   history_.removeAll(args.get<picojson::object>());
92   ReportSuccess(out);
93 }
94
95 void CallHistoryInstance::CallHistoryAddChangeListener(const picojson::value& args, picojson::object& out) {
96   ScopeLogger();
97   CHECK_PRIVILEGE_ACCESS(kPrivilegeCallHistoryRead, &out);
98   PlatformResult result = history_.startCallHistoryChangeListener();
99   if (result.IsSuccess()) {
100     ReportSuccess(out);
101   } else {
102     LogAndReportError(result, &out);
103   }
104 }
105
106 void CallHistoryInstance::CallHistoryRemoveChangeListener(const picojson::value& args, picojson::object& out) {
107   ScopeLogger();
108   CHECK_PRIVILEGE_ACCESS(kPrivilegeCallHistoryRead, &out);
109   PlatformResult result = history_.stopCallHistoryChangeListener();
110   if (result.IsSuccess()) {
111     ReportSuccess(out);
112   } else {
113     LogAndReportError(result, &out);
114   }
115 }
116
117 void CallHistoryInstance::CallHistorySetMissedDirection(const picojson::value& args, picojson::object& out) {
118   ScopeLogger();
119
120   if (!args.contains("uid")) {
121     LoggerD("args doesn't contain attribute 'uid'");
122     ReportError(out);
123     return;
124   }
125
126   int uid = std::atoi(args.get("uid").get<std::string>().c_str());
127
128   PlatformResult result = history_.setMissedDirection(uid);
129   if (result.IsSuccess()) {
130     ReportSuccess(out);
131   } else {
132     ReportError(out);
133   }
134 }
135
136 void CallHistoryInstance::CallHistoryChange(picojson::object& data) {
137   ScopeLogger();
138   picojson::value event = picojson::value(data);
139   picojson::object& obj = event.get<picojson::object>();
140   obj["listenerId"] = picojson::value("CallHistoryChangeCallback");
141
142   Instance::PostMessage(this, event.serialize().c_str());
143 }
144
145 }  // namespace callhistory
146 }  // namespace extension