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