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 #ifndef CHROME_BROWSER_HISTORY_ANDROID_ANDROID_HISTORY_PROVIDER_SERVICE_H_
6 #define CHROME_BROWSER_HISTORY_ANDROID_ANDROID_HISTORY_PROVIDER_SERVICE_H_
7 
8 #include <stdint.h>
9 
10 #include <string>
11 #include <vector>
12 
13 #include "base/callback.h"
14 #include "base/macros.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/task/cancelable_task_tracker.h"
17 #include "components/favicon_base/favicon_callback.h"
18 #include "components/history/core/browser/android/android_history_types.h"
19 
20 class Profile;
21 
22 // This class provides the methods to communicate with history backend service
23 // for the Android content provider.
24 // The methods of this class must run on the UI thread to cooperate with the
25 // BookmarkModel task posted in the DB thread.
26 class AndroidHistoryProviderService {
27  public:
28   explicit AndroidHistoryProviderService(Profile* profile);
29   virtual ~AndroidHistoryProviderService();
30 
31   // The callback definitions ------------------------------------------------
32 
33   // Callback invoked when a method creating an |AndroidStatement| object is
34   // complete. The pointer is NULL if the creation failed.
35   typedef base::OnceCallback<void(history::AndroidStatement*)> QueryCallback;
36 
37   // Callback invoked when a method updating rows in the database complete.
38   // The parameter is the number of rows updated or 0 if the update failed.
39   typedef base::OnceCallback<void(int)> UpdateCallback;
40 
41   // Callback invoked when a method inserting rows in the database complete.
42   // The value is the new row id or 0 if the insertion failed.
43   typedef base::OnceCallback<void(int64_t)> InsertCallback;
44 
45   // Callback invoked when a method deleting rows in the database complete.
46   // The value is the number of rows deleted or 0 if the deletion failed.
47   typedef base::OnceCallback<void(int)> DeleteCallback;
48 
49   // Callback invoked when a method moving an |AndroidStatement| is complete.
50   // The value passed to the callback is the new position, or in case of
51   // failure, the old position.
52   typedef base::OnceCallback<void(int)> MoveStatementCallback;
53 
54   // History and Bookmarks ----------------------------------------------------
55   //
56   // Runs the given query on history backend, and invokes the |callback| to
57   // return the result.
58   //
59   // |projections| is the vector of the result columns.
60   // |selection| is the SQL WHERE clause without 'WHERE'.
61   // |selection_args| is the arguments for WHERE clause.
62   // |sort_order| is the SQL ORDER clause.
63   base::CancelableTaskTracker::TaskId QueryHistoryAndBookmarks(
64       const std::vector<history::HistoryAndBookmarkRow::ColumnID>& projections,
65       const std::string& selection,
66       const std::vector<base::string16>& selection_args,
67       const std::string& sort_order,
68       QueryCallback callback,
69       base::CancelableTaskTracker* tracker);
70 
71   // Runs the given update and the number of the row updated is returned to the
72   // |callback| on success.
73   //
74   // |row| is the value to update.
75   // |selection| is the SQL WHERE clause without 'WHERE'.
76   // |selection_args| is the arguments for the WHERE clause.
77   base::CancelableTaskTracker::TaskId UpdateHistoryAndBookmarks(
78       const history::HistoryAndBookmarkRow& row,
79       const std::string& selection,
80       const std::vector<base::string16>& selection_args,
81       UpdateCallback callback,
82       base::CancelableTaskTracker* tracker);
83 
84   // Deletes the specified rows and invokes the |callback| to return the number
85   // of row deleted on success.
86   //
87   // |selection| is the SQL WHERE clause without 'WHERE'.
88   // |selection_args| is the arguments for the WHERE clause.
89   //
90   // If |selection| is empty all history and bookmarks are deleted.
91   base::CancelableTaskTracker::TaskId DeleteHistoryAndBookmarks(
92       const std::string& selection,
93       const std::vector<base::string16>& selection_args,
94       DeleteCallback callback,
95       base::CancelableTaskTracker* tracker);
96 
97   // Inserts the given values into history backend, and invokes the |callback|
98   // to return the result.
99   base::CancelableTaskTracker::TaskId InsertHistoryAndBookmark(
100       const history::HistoryAndBookmarkRow& values,
101       InsertCallback callback,
102       base::CancelableTaskTracker* tracker);
103 
104   // Deletes the matched history and invokes |callback| to return the number of
105   // rows deleted.
106   base::CancelableTaskTracker::TaskId DeleteHistory(
107       const std::string& selection,
108       const std::vector<base::string16>& selection_args,
109       DeleteCallback callback,
110       base::CancelableTaskTracker* tracker);
111 
112   // Statement ----------------------------------------------------------------
113   // Moves the statement's current row from |current_pos| to |destination| in DB
114   // thread. The new position is returned to the callback. The result supplied
115   // the callback is constrained by the number of rows might.
116   base::CancelableTaskTracker::TaskId MoveStatement(
117       history::AndroidStatement* statement,
118       int current_pos,
119       int destination,
120       MoveStatementCallback callback,
121       base::CancelableTaskTracker* tracker);
122 
123   // Closes the statement in db thread. The AndroidHistoryProviderService takes
124   // the ownership of |statement|.
125   void CloseStatement(history::AndroidStatement* statement);
126 
127   // Search term --------------------------------------------------------------
128   // Inserts the given values and returns the SearchTermID of the inserted row
129   // to the |callback| on success.
130   base::CancelableTaskTracker::TaskId InsertSearchTerm(
131       const history::SearchRow& row,
132       InsertCallback callback,
133       base::CancelableTaskTracker* tracker);
134 
135   // Runs the given update and returns the number of the update rows to the
136   // |callback| on success.
137   //
138   // |row| is the value need to update.
139   // |selection| is the SQL WHERE clause without 'WHERE'.
140   // |selection_args| is the arguments for WHERE clause.
141   base::CancelableTaskTracker::TaskId UpdateSearchTerms(
142       const history::SearchRow& row,
143       const std::string& selection,
144       const std::vector<base::string16>& selection_args,
145       UpdateCallback callback,
146       base::CancelableTaskTracker* tracker);
147 
148   // Deletes the matched rows and the number of deleted rows is returned to
149   // the |callback| on success.
150   //
151   // |selection| is the SQL WHERE clause without 'WHERE'.
152   // |selection_args| is the arguments for WHERE clause.
153   //
154   // If |selection| is empty all search terms will be deleted.
155   base::CancelableTaskTracker::TaskId DeleteSearchTerms(
156       const std::string& selection,
157       const std::vector<base::string16>& selection_args,
158       DeleteCallback callback,
159       base::CancelableTaskTracker* tracker);
160 
161   // Runs the query and invokes the |callback| to return the result.
162   //
163   // |projections| specifies the result columns, can not be empty, otherwise
164   // NULL is returned.
165   // |selection| is the SQL WHERE clause without 'WHERE'.
166   // |selection_args| is the arguments for WHERE clause.
167   // |sort_order| the SQL ORDER clause.
168   base::CancelableTaskTracker::TaskId QuerySearchTerms(
169       const std::vector<history::SearchRow::ColumnID>& projections,
170       const std::string& selection,
171       const std::vector<base::string16>& selection_args,
172       const std::string& sort_order,
173       QueryCallback callback,
174       base::CancelableTaskTracker* tracker);
175 
176   // Returns the largest Favicon for |favicon_id| and invokes
177   // the |callback| to return the result.
178   base::CancelableTaskTracker::TaskId GetLargestRawFaviconForID(
179       favicon_base::FaviconID favicon_id,
180       favicon_base::FaviconRawBitmapCallback callback,
181       base::CancelableTaskTracker* tracker);
182 
183  private:
184   Profile* profile_;
185 
186   DISALLOW_COPY_AND_ASSIGN(AndroidHistoryProviderService);
187 };
188 
189 #endif  // CHROME_BROWSER_HISTORY_ANDROID_ANDROID_HISTORY_PROVIDER_SERVICE_H_
190