Fixed privilege for web-history.admin
[profile/tv/apps/web/browser.git] / services / HistoryService / src / HistoryService.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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 <string>
18 #include <BrowserAssert.h>
19 #include <boost/date_time/posix_time/posix_time.hpp>
20 #include <boost/date_time/date.hpp>
21 #include <boost/date_time/date_defs.hpp>
22 #include <boost/date_time/gregorian/gregorian.hpp>
23
24 #include "ServiceManager.h"
25 #include "HistoryService.h"
26 #include "HistoryItem.h"
27 #include "AbstractWebEngine.h"
28 #include "EflTools.h"
29
30 namespace tizen_browser
31 {
32 namespace services
33 {
34
35 EXPORT_SERVICE(HistoryService, DOMAIN_HISTORY_SERVICE)
36
37 HistoryService::HistoryService() : m_testDbMod(false)
38 {
39     BROWSER_LOGD("HistoryService");
40 }
41
42 HistoryService::~HistoryService()
43 {
44 }
45
46 std::shared_ptr<tizen_browser::services::StorageService> HistoryService::getStorageManager()
47 {
48     if (!m_storageManager) {
49         m_storageManager = std::dynamic_pointer_cast <
50                            tizen_browser::services::StorageService,
51                            tizen_browser::core::AbstractService > (
52                                tizen_browser::core::ServiceManager::getInstance().getService(
53                                    DOMAIN_STORAGE_SERVICE));
54     }
55
56     M_ASSERT(m_storageManager);
57     m_storageManager->init(m_testDbMod);
58
59     return m_storageManager;
60 }
61
62 void HistoryService::setStorageServiceTestMode(bool testmode) {
63         m_testDbMod = testmode;
64 }
65
66 int HistoryService::getHistoryItemsCount(){
67     return history_list.size();
68 }
69
70 static int __get_duplicated_ids_p(int **ids, int *count, const int limit, const int offset,
71                 const bp_history_offset order_column_offset, const int ordering,
72                 const bp_history_offset check_column_offset,
73                 const char *keyword, const int is_like)
74 {
75     bp_history_rows_cond_fmt conds;
76     memset(&conds, 0x00, sizeof(bp_history_rows_cond_fmt));
77
78     conds.limit = limit;
79     conds.offset = offset;
80     conds.ordering = ordering;
81     conds.order_offset = order_column_offset;
82     conds.period_offset = BP_HISTORY_O_DATE_CREATED;
83     conds.period_type = BP_HISTORY_DATE_ALL;
84
85     return bp_history_adaptor_get_cond_ids_p
86         (ids, count,
87          &conds,
88          check_column_offset,
89          keyword,
90          is_like);
91 }
92
93 void HistoryService::addHistoryItem(std::shared_ptr<HistoryItem> his){
94
95     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
96     his->setFavIcon(his->getFavIcon());
97     std::shared_ptr<tizen_browser::tools::BrowserImage> favicon = his->getFavIcon();
98
99     int id = -1;
100     int ret = bp_history_adaptor_create(&id);
101     if (ret<0){
102         BROWSER_LOGE("Error! Could not create new bookmark!");
103     }
104
105     int *ids=NULL;
106     int count=-1;
107     int **id1=&ids;
108     int *count1=&count;
109
110     bp_history_rows_cond_fmt conds;
111     conds.limit = 20;  //no of rows to get negative means no limitation
112
113     conds.offset = -1;   //the first row's index
114     conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
115
116     conds.ordering = 1; //way of ordering 0 asc 1 desc
117
118     conds.period_offset = BP_HISTORY_O_DATE_CREATED;
119     conds.period_type = BP_HISTORY_DATE_TODAY;
120
121     ret = bp_history_adaptor_get_cond_ids_p(id1 ,count1, &conds, 0, NULL, 0);
122     if (ret<0){
123         BROWSER_LOGE("Error! Could not get ids!");
124     }
125
126     bp_history_adaptor_set_url(id, (his->getUrl()).c_str());
127     bp_history_adaptor_set_title(id, (his->getTitle()).c_str());
128     bp_history_adaptor_set_date_visited(id,-1);
129
130     std::unique_ptr<tizen_browser::tools::Blob> favicon_blob = tizen_browser::tools::EflTools::getBlobPNG(favicon);
131     unsigned char * fav = std::move((unsigned char*)favicon_blob->getData());
132     bp_history_adaptor_set_icon(id, favicon->width, favicon->height, fav, favicon_blob->getLength());
133     history_list.push_back(his);
134
135     BROWSER_LOGD("[%s:%d] ", __PRETTY_FUNCTION__, __LINE__);
136 }
137
138
139 void HistoryService::insertOrRefresh(std::shared_ptr<HistoryItem> hi) {
140         getStorageManager()->insertOrRefresh(hi);
141 }
142
143 /**
144  * @throws HistoryException on error
145  */
146 void HistoryService::clearAllHistory()
147 {
148     bp_history_adaptor_reset();
149     history_list.clear();
150 }
151
152 int HistoryService::getHistoryId(const std::string & url)
153 {
154     bp_history_rows_cond_fmt conds;
155     conds.limit = -1;
156     conds.offset = 0;
157     conds.order_offset = BP_HISTORY_O_DATE_CREATED;
158     conds.ordering = 0;
159     conds.period_offset = BP_HISTORY_O_DATE_CREATED;
160     conds.period_type = BP_HISTORY_DATE_ALL;
161     int *ids = 0;
162     int ids_count = 0;
163     int ret = bp_history_adaptor_get_cond_ids_p(&ids ,&ids_count, &conds, BP_HISTORY_O_URL, url.c_str(), 0);
164     if (ids_count!=0){
165         int i = *ids;
166         free(ids);
167         return i;
168     }
169     return 0;
170 }
171
172 /**
173  * @throws HistoryException on error
174  */
175 void HistoryService::clearURLHistory(const std::string & url)
176 {
177     int id = getHistoryId(url);
178     if (id != 0)
179         bp_history_adaptor_delete(id);
180     if (0 == (getHistoryItemsCount() - 1)) {
181         historyEmpty(true);
182         history_list.clear();
183     }
184 }
185
186
187 HistoryItemVector& HistoryService::getHistoryItems(int historyDepthInDays, int maxItems)
188 {
189     history_list.clear();
190
191     int *ids=NULL;
192     int count=-1;
193     int **id1=&ids;
194     int *count1=&count;
195
196     bp_history_rows_cond_fmt conds;
197     conds.limit = 20;  //no of rows to get negative means no limitation
198
199     conds.offset = -1;   //the first row's index
200     conds.order_offset =BP_HISTORY_O_DATE_CREATED; // property to sort
201
202     conds.ordering = 1; //way of ordering 0 asc 1 desc
203
204     conds.period_offset = BP_HISTORY_O_DATE_CREATED;
205
206     conds.period_type = BP_HISTORY_DATE_TODAY;
207
208     int ret = bp_history_adaptor_get_cond_ids_p(id1 ,count1, &conds, 0, NULL, 0);
209     if (ret<0){
210         BROWSER_LOGD("Error! Could not get ids!");
211     }
212
213     bp_history_offset offset = (BP_HISTORY_O_URL | BP_HISTORY_O_TITLE | BP_HISTORY_O_FAVICON | BP_HISTORY_O_DATE_CREATED);
214
215     for(int i = 0; i< (*count1); i++){
216         bp_history_info_fmt history_info;
217         bp_history_adaptor_get_info(ids[i],offset,&history_info);
218
219         int date;
220         bp_history_adaptor_get_date_created(ids[i], &date);
221
222         struct tm *item_time_info;
223         time_t item_time = (time_t)date;
224         item_time_info = localtime(&item_time);
225
226         int m_year = item_time_info->tm_year;
227         int m_month = item_time_info->tm_mon + 1;
228         int m_day = item_time_info->tm_yday;
229         int m_month_day = item_time_info->tm_mday;
230         int m_date = date;
231         int min = item_time_info->tm_min;
232         int hour= item_time_info->tm_hour;
233         int sec = item_time_info->tm_sec;
234         m_year = 2000 + m_year % 100;
235
236         std::shared_ptr<HistoryItem> history = std::make_shared<HistoryItem>(std::string(history_info.url));
237         boost::gregorian::date d(m_year,m_month,m_month_day);
238         boost::posix_time::ptime t(d,boost::posix_time::time_duration(hour,min,sec));
239         history->setLastVisit(t);
240         history->setUrl(std::string(history_info.url ? history_info.url : ""));
241         history->setTitle(std::string(history_info.title ? history_info.title : ""));
242         history_list.push_back(history);
243     }
244     ids = NULL;
245     free(ids);
246
247     return history_list;
248 }
249
250 int HistoryService::getHistoryVisitCounter(const std::string & url)
251 {
252     return getStorageManager()->getHistoryVisitCounter(url);
253 }
254
255 }
256 }