Home | History | Annotate | Download | only in android
      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 "base/callback.h"
      9 #include "base/memory/ref_counted.h"
     10 #include "chrome/browser/common/cancelable_request.h"
     11 #include "chrome/browser/history/android/android_history_types.h"
     12 #include "sql/statement.h"
     13 
     14 class Profile;
     15 
     16 // This class provides the methods to communicate with history backend service
     17 // for the Android content provider.
     18 // The methods of this class must run on the UI thread to cooperate with the
     19 // BookmarkModel task posted in the DB thread.
     20 class AndroidHistoryProviderService : public CancelableRequestProvider {
     21  public:
     22   explicit AndroidHistoryProviderService(Profile* profile);
     23   virtual ~AndroidHistoryProviderService();
     24 
     25   // The callback definitions ------------------------------------------------
     26   typedef base::Callback<void(
     27                     Handle,                // handle
     28                     bool,                  // true if the query succeeded.
     29                     history::AndroidStatement*)>  // the result of query
     30                     QueryCallback;
     31   typedef CancelableRequest<QueryCallback> QueryRequest;
     32 
     33   typedef base::Callback<void(
     34                     Handle,                // handle
     35                     bool,                  // true if the update succeeded.
     36                     int)>                  // the number of row updated.
     37                     UpdateCallback;
     38   typedef CancelableRequest<UpdateCallback> UpdateRequest;
     39 
     40   typedef base::Callback<void(
     41                     Handle,                // handle
     42                     bool,                  // true if the insert succeeded.
     43                     int64)>                // the id of inserted row.
     44                     InsertCallback;
     45   typedef CancelableRequest<InsertCallback> InsertRequest;
     46 
     47   typedef base::Callback<void(
     48                     Handle,                // handle
     49                     bool,                  // true if the deletion succeeded.
     50                     int)>                  // the number of row deleted.
     51                     DeleteCallback;
     52   typedef CancelableRequest<DeleteCallback> DeleteRequest;
     53 
     54   typedef base::Callback<void(
     55                     Handle,                // handle
     56                     int)>                  // the new position.
     57                     MoveStatementCallback;
     58   typedef CancelableRequest<MoveStatementCallback> MoveStatementRequest;
     59 
     60   // History and Bookmarks ----------------------------------------------------
     61   //
     62   // Runs the given query on history backend, and invokes the |callback| to
     63   // return the result.
     64   //
     65   // |projections| is the vector of the result columns.
     66   // |selection| is the SQL WHERE clause without 'WHERE'.
     67   // |selection_args| is the arguments for WHERE clause.
     68   // |sort_order| is the SQL ORDER clause.
     69   Handle QueryHistoryAndBookmarks(
     70       const std::vector<history::HistoryAndBookmarkRow::ColumnID>& projections,
     71       const std::string& selection,
     72       const std::vector<string16>& selection_args,
     73       const std::string& sort_order,
     74       CancelableRequestConsumerBase* consumer,
     75       const QueryCallback& callback);
     76 
     77   // Runs the given update and the number of the row updated is returned to the
     78   // |callback| on success.
     79   //
     80   // |row| is the value to update.
     81   // |selection| is the SQL WHERE clause without 'WHERE'.
     82   // |selection_args| is the arguments for the WHERE clause.
     83   Handle UpdateHistoryAndBookmarks(const history::HistoryAndBookmarkRow& row,
     84                                    const std::string& selection,
     85                                    const std::vector<string16>& selection_args,
     86                                    CancelableRequestConsumerBase* consumer,
     87                                    const UpdateCallback& callback);
     88 
     89   // Deletes the specified rows and invokes the |callback| to return the number
     90   // of row deleted on success.
     91   //
     92   // |selection| is the SQL WHERE clause without 'WHERE'.
     93   // |selection_args| is the arguments for the WHERE clause.
     94   //
     95   // if |selection| is empty all history and bookmarks are deleted.
     96   Handle DeleteHistoryAndBookmarks(const std::string& selection,
     97                                    const std::vector<string16>& selection_args,
     98                                    CancelableRequestConsumerBase* consumer,
     99                                    const DeleteCallback& callback);
    100 
    101   // Inserts the given values into history backend, and invokes the |callback|
    102   // to return the result.
    103   Handle InsertHistoryAndBookmark(const history::HistoryAndBookmarkRow& values,
    104                                   CancelableRequestConsumerBase* consumer,
    105                                   const InsertCallback& callback);
    106 
    107   // Deletes the matched history and invokes |callback| to return the number of
    108   // the row deleted from the |callback|.
    109   Handle DeleteHistory(const std::string& selection,
    110                        const std::vector<string16>& selection_args,
    111                        CancelableRequestConsumerBase* consumer,
    112                        const DeleteCallback& callback);
    113 
    114   // Statement ----------------------------------------------------------------
    115   // Moves the statement's current row from |current_pos| to |destination| in DB
    116   // thread. The new position is returned to the callback. The result supplied
    117   // the callback is constrained by the number of rows might.
    118   Handle MoveStatement(history::AndroidStatement* statement,
    119                        int current_pos,
    120                        int destination,
    121                        CancelableRequestConsumerBase* consumer,
    122                        const MoveStatementCallback& callback);
    123 
    124   // Closes the statement in db thread. The AndroidHistoryProviderService takes
    125   // the ownership of |statement|.
    126   void CloseStatement(history::AndroidStatement* statement);
    127 
    128   // Search term --------------------------------------------------------------
    129   // Inserts the given values and returns the SearchTermID of the inserted row
    130   // from the |callback| on success.
    131   Handle InsertSearchTerm(const history::SearchRow& row,
    132                           CancelableRequestConsumerBase* consumer,
    133                           const InsertCallback& callback);
    134 
    135   // Runs the given update and returns the number of the update rows from 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   Handle UpdateSearchTerms(const history::SearchRow& row,
    142                            const std::string& selection,
    143                            const std::vector<string16>& selection_args,
    144                            CancelableRequestConsumerBase* consumer,
    145                            const UpdateCallback& callback);
    146 
    147   // Deletes the matched rows and the number of deleted rows is returned from
    148   // the |callback| on success.
    149   // |selection| is the SQL WHERE clause without 'WHERE'.
    150   // |selection_args| is the arguments for WHERE clause.
    151   //
    152   // if |selection| is empty all search be deleted.
    153   Handle DeleteSearchTerms(const std::string& selection,
    154                            const std::vector<string16>& selection_args,
    155                            CancelableRequestConsumerBase* consumer,
    156                            const DeleteCallback& callback);
    157 
    158   // Returns the result of the given query from the |callback|.
    159   // |projections| specifies the result columns, can not be empty, otherwise
    160   // NULL is returned.
    161   // |selection| is the SQL WHERE clause without 'WHERE'.
    162   // |selection_args| is the arguments for WHERE clause.
    163   // |sort_order| the SQL ORDER clause.
    164   Handle QuerySearchTerms(
    165       const std::vector<history::SearchRow::ColumnID>& projections,
    166       const std::string& selection,
    167       const std::vector<string16>& selection_args,
    168       const std::string& sort_order,
    169       CancelableRequestConsumerBase* consumer,
    170       const QueryCallback& callback);
    171 
    172  private:
    173   Profile* profile_;
    174 
    175   DISALLOW_COPY_AND_ASSIGN(AndroidHistoryProviderService);
    176 };
    177 
    178 #endif  // CHROME_BROWSER_HISTORY_ANDROID_ANDROID_HISTORY_PROVIDER_SERVICE_H_
    179