Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / history / android / android_history_provider_service.cc
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/history/android/android_history_provider_service.h"
6
7 #include "chrome/browser/history/history_backend.h"
8 #include "chrome/browser/history/history_service.h"
9 #include "chrome/browser/history/history_service_factory.h"
10 #include "chrome/browser/profiles/profile.h"
11
12 using history::HistoryBackend;
13
14 AndroidHistoryProviderService::AndroidHistoryProviderService(Profile* profile)
15     : profile_(profile) {
16 }
17
18 AndroidHistoryProviderService::~AndroidHistoryProviderService() {
19 }
20
21 base::CancelableTaskTracker::TaskId
22 AndroidHistoryProviderService::QueryHistoryAndBookmarks(
23     const std::vector<history::HistoryAndBookmarkRow::ColumnID>& projections,
24     const std::string& selection,
25     const std::vector<base::string16>& selection_args,
26     const std::string& sort_order,
27     const QueryCallback& callback,
28     base::CancelableTaskTracker* tracker) {
29   HistoryService* hs =
30       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
31   if (hs) {
32     DCHECK(hs->thread_) << "History service being called after cleanup";
33     DCHECK(hs->thread_checker_.CalledOnValidThread());
34     return tracker->PostTaskAndReplyWithResult(
35         hs->thread_->message_loop_proxy().get(),
36         FROM_HERE,
37         base::Bind(&HistoryBackend::QueryHistoryAndBookmarks,
38                    hs->history_backend_.get(),
39                    projections,
40                    selection,
41                    selection_args,
42                    sort_order),
43         callback);
44   } else {
45     callback.Run(NULL);
46     return base::CancelableTaskTracker::kBadTaskId;
47   }
48 }
49
50 base::CancelableTaskTracker::TaskId
51 AndroidHistoryProviderService::UpdateHistoryAndBookmarks(
52     const history::HistoryAndBookmarkRow& row,
53     const std::string& selection,
54     const std::vector<base::string16>& selection_args,
55     const UpdateCallback& callback,
56     base::CancelableTaskTracker* tracker) {
57   HistoryService* hs =
58       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
59   if (hs) {
60     DCHECK(hs->thread_) << "History service being called after cleanup";
61     DCHECK(hs->thread_checker_.CalledOnValidThread());
62     return tracker->PostTaskAndReplyWithResult(
63         hs->thread_->message_loop_proxy().get(),
64         FROM_HERE,
65         base::Bind(&HistoryBackend::UpdateHistoryAndBookmarks,
66                    hs->history_backend_.get(),
67                    row,
68                    selection,
69                    selection_args),
70         callback);
71   } else {
72     callback.Run(0);
73     return base::CancelableTaskTracker::kBadTaskId;
74   }
75 }
76
77 base::CancelableTaskTracker::TaskId
78 AndroidHistoryProviderService::DeleteHistoryAndBookmarks(
79     const std::string& selection,
80     const std::vector<base::string16>& selection_args,
81     const DeleteCallback& callback,
82     base::CancelableTaskTracker* tracker) {
83   HistoryService* hs =
84       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
85   if (hs) {
86     DCHECK(hs->thread_) << "History service being called after cleanup";
87     DCHECK(hs->thread_checker_.CalledOnValidThread());
88     return tracker->PostTaskAndReplyWithResult(
89         hs->thread_->message_loop_proxy().get(),
90         FROM_HERE,
91         base::Bind(&HistoryBackend::DeleteHistoryAndBookmarks,
92                    hs->history_backend_.get(),
93                    selection,
94                    selection_args),
95         callback);
96   } else {
97     callback.Run(0);
98     return base::CancelableTaskTracker::kBadTaskId;
99   }
100 }
101
102 base::CancelableTaskTracker::TaskId
103 AndroidHistoryProviderService::InsertHistoryAndBookmark(
104     const history::HistoryAndBookmarkRow& values,
105     const InsertCallback& callback,
106     base::CancelableTaskTracker* tracker) {
107   HistoryService* hs =
108       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
109   if (hs) {
110     DCHECK(hs->thread_) << "History service being called after cleanup";
111     DCHECK(hs->thread_checker_.CalledOnValidThread());
112     return tracker->PostTaskAndReplyWithResult(
113         hs->thread_->message_loop_proxy().get(),
114         FROM_HERE,
115         base::Bind(&HistoryBackend::InsertHistoryAndBookmark,
116                    hs->history_backend_.get(),
117                    values),
118         callback);
119   } else {
120     callback.Run(0);
121     return base::CancelableTaskTracker::kBadTaskId;
122   }
123 }
124
125 base::CancelableTaskTracker::TaskId
126 AndroidHistoryProviderService::DeleteHistory(
127     const std::string& selection,
128     const std::vector<base::string16>& selection_args,
129     const DeleteCallback& callback,
130     base::CancelableTaskTracker* tracker) {
131   HistoryService* hs =
132       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
133   if (hs) {
134     DCHECK(hs->thread_) << "History service being called after cleanup";
135     DCHECK(hs->thread_checker_.CalledOnValidThread());
136     return tracker->PostTaskAndReplyWithResult(
137         hs->thread_->message_loop_proxy().get(),
138         FROM_HERE,
139         base::Bind(&HistoryBackend::DeleteHistory,
140                    hs->history_backend_.get(),
141                    selection,
142                    selection_args),
143         callback);
144   } else {
145     callback.Run(0);
146     return base::CancelableTaskTracker::kBadTaskId;
147   }
148 }
149
150 base::CancelableTaskTracker::TaskId
151 AndroidHistoryProviderService::MoveStatement(
152     history::AndroidStatement* statement,
153     int current_pos,
154     int destination,
155     const MoveStatementCallback& callback,
156     base::CancelableTaskTracker* tracker) {
157   HistoryService* hs =
158       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
159   if (hs) {
160     DCHECK(hs->thread_) << "History service being called after cleanup";
161     DCHECK(hs->thread_checker_.CalledOnValidThread());
162     return tracker->PostTaskAndReplyWithResult(
163         hs->thread_->message_loop_proxy().get(),
164         FROM_HERE,
165         base::Bind(&HistoryBackend::MoveStatement,
166                    hs->history_backend_.get(),
167                    statement,
168                    current_pos,
169                    destination),
170         callback);
171   } else {
172     callback.Run(current_pos);
173     return base::CancelableTaskTracker::kBadTaskId;
174   }
175 }
176
177 void AndroidHistoryProviderService::CloseStatement(
178     history::AndroidStatement* statement) {
179   HistoryService* hs =
180       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
181   if (hs) {
182     hs->ScheduleAndForget(HistoryService::PRIORITY_NORMAL,
183             &HistoryBackend::CloseStatement, statement);
184   } else {
185     delete statement;
186   }
187 }
188
189 base::CancelableTaskTracker::TaskId
190 AndroidHistoryProviderService::InsertSearchTerm(
191     const history::SearchRow& row,
192     const InsertCallback& callback,
193     base::CancelableTaskTracker* tracker) {
194   HistoryService* hs =
195       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
196   if (hs) {
197     DCHECK(hs->thread_) << "History service being called after cleanup";
198     DCHECK(hs->thread_checker_.CalledOnValidThread());
199     return tracker->PostTaskAndReplyWithResult(
200         hs->thread_->message_loop_proxy().get(),
201         FROM_HERE,
202         base::Bind(
203             &HistoryBackend::InsertSearchTerm, hs->history_backend_.get(), row),
204         callback);
205   } else {
206     callback.Run(0);
207     return base::CancelableTaskTracker::kBadTaskId;
208   }
209 }
210
211 base::CancelableTaskTracker::TaskId
212 AndroidHistoryProviderService::UpdateSearchTerms(
213     const history::SearchRow& row,
214     const std::string& selection,
215     const std::vector<base::string16>& selection_args,
216     const UpdateCallback& callback,
217     base::CancelableTaskTracker* tracker) {
218   HistoryService* hs =
219       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
220   if (hs) {
221     DCHECK(hs->thread_) << "History service being called after cleanup";
222     DCHECK(hs->thread_checker_.CalledOnValidThread());
223     return tracker->PostTaskAndReplyWithResult(
224         hs->thread_->message_loop_proxy().get(),
225         FROM_HERE,
226         base::Bind(&HistoryBackend::UpdateSearchTerms,
227                    hs->history_backend_.get(),
228                    row,
229                    selection,
230                    selection_args),
231         callback);
232   } else {
233     callback.Run(0);
234     return base::CancelableTaskTracker::kBadTaskId;
235   }
236 }
237
238 base::CancelableTaskTracker::TaskId
239 AndroidHistoryProviderService::DeleteSearchTerms(
240     const std::string& selection,
241     const std::vector<base::string16>& selection_args,
242     const DeleteCallback& callback,
243     base::CancelableTaskTracker* tracker) {
244   HistoryService* hs =
245       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
246   if (hs) {
247     DCHECK(hs->thread_) << "History service being called after cleanup";
248     DCHECK(hs->thread_checker_.CalledOnValidThread());
249     return tracker->PostTaskAndReplyWithResult(
250         hs->thread_->message_loop_proxy().get(),
251         FROM_HERE,
252         base::Bind(&HistoryBackend::DeleteSearchTerms,
253                    hs->history_backend_.get(),
254                    selection,
255                    selection_args),
256         callback);
257   } else {
258     callback.Run(0);
259     return base::CancelableTaskTracker::kBadTaskId;
260   }
261 }
262
263 base::CancelableTaskTracker::TaskId
264 AndroidHistoryProviderService::QuerySearchTerms(
265     const std::vector<history::SearchRow::ColumnID>& projections,
266     const std::string& selection,
267     const std::vector<base::string16>& selection_args,
268     const std::string& sort_order,
269     const QueryCallback& callback,
270     base::CancelableTaskTracker* tracker) {
271   HistoryService* hs =
272       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
273   if (hs) {
274     DCHECK(hs->thread_) << "History service being called after cleanup";
275     DCHECK(hs->thread_checker_.CalledOnValidThread());
276     return tracker->PostTaskAndReplyWithResult(
277         hs->thread_->message_loop_proxy().get(),
278         FROM_HERE,
279         base::Bind(&HistoryBackend::QuerySearchTerms,
280                    hs->history_backend_.get(),
281                    projections,
282                    selection,
283                    selection_args,
284                    sort_order),
285         callback);
286   } else {
287     callback.Run(NULL);
288     return base::CancelableTaskTracker::kBadTaskId;
289   }
290 }