95ee009cae70886e394aff3294eadfcaaa40a32e
[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
21 #include "ServiceManager.h"
22 #include "HistoryService.h"
23 #include "AbstractWebEngine.h"
24
25 namespace tizen_browser
26 {
27 namespace services
28 {
29
30 EXPORT_SERVICE(HistoryService, DOMAIN_HISTORY_SERVICE)
31
32 HistoryService::HistoryService() : m_testDbMod(false)
33 {
34     BROWSER_LOGD("HistoryService");
35 }
36
37 HistoryService::~HistoryService()
38 {
39 }
40
41 std::shared_ptr<tizen_browser::services::StorageService> HistoryService::getStorageManager()
42 {
43     if (!m_storageManager) {
44         m_storageManager = std::dynamic_pointer_cast <
45                            tizen_browser::services::StorageService,
46                            tizen_browser::core::AbstractService > (
47                                tizen_browser::core::ServiceManager::getInstance().getService(
48                                    DOMAIN_STORAGE_SERVICE));
49     }
50
51     M_ASSERT(m_storageManager);
52     m_storageManager->init(m_testDbMod);
53
54     return m_storageManager;
55 }
56
57 void HistoryService::setStorageServiceTestMode(bool testmode) {
58         m_testDbMod = testmode;
59 }
60
61 /**
62  * @throws HistoryException on error
63  */
64 void HistoryService::addHistoryItem(std::shared_ptr<HistoryItem> hi)
65 {
66     getStorageManager()->addHistoryItem(hi);
67 }
68
69 /**
70  * If hi->getUrl() exists on a table HISTORY update visit_counter and visit_date, unless insert hi to database.
71  *
72  * @throws HistoryException on error
73  */
74 void HistoryService::insertOrRefresh(std::shared_ptr<HistoryItem> hi) {
75         getStorageManager()->insertOrRefresh(hi);
76 }
77
78 /**
79  * @throws HistoryException on error
80  */
81 void HistoryService::clearAllHistory()
82 {
83     getStorageManager()->deleteHistory();
84 }
85
86 /**
87  * @throws HistoryException on error
88  */
89 void HistoryService::clearURLHistory(const std::string & url)
90 {
91     getStorageManager()->deleteHistory(url);
92     if(0 == getHistoryItemsCount()){
93         historyEmpty(true);
94     }
95 }
96
97 /**
98  * @throws HistoryException on error
99  */
100 std::shared_ptr<HistoryItem> HistoryService::getHistoryItem(const std::string & url)
101 {
102     return getStorageManager()->getHistoryItem(url);
103 }
104
105 /**
106  * @throws HistoryException on error
107  */
108 HistoryItemVector& HistoryService::getHistoryItems(int historyDepthInDays, int maxItems)
109 {
110     return getStorageManager()->getHistoryItems(historyDepthInDays, maxItems);
111 }
112
113 /**
114  * @throws HistoryException on error
115  */
116 int HistoryService::getHistoryItemsCount()
117 {
118     return getStorageManager()->getHistoryItemsCount();
119 }
120
121 /**
122  * @throws HistoryException on error
123  */
124 int HistoryService::getHistoryVisitCounter(const std::string & url)
125 {
126     return getStorageManager()->getHistoryVisitCounter(url);
127 }
128
129 }
130 }