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<base::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(
     84       const history::HistoryAndBookmarkRow& row,
     85       const std::string& selection,
     86       const std::vector<base::string16>& selection_args,
     87       CancelableRequestConsumerBase* consumer,
     88       const UpdateCallback& callback);
     89 
     90   // Deletes the specified rows and invokes the |callback| to return the number
     91   // of row deleted on success.
     92   //
     93   // |selection| is the SQL WHERE clause without 'WHERE'.
     94   // |selection_args| is the arguments for the WHERE clause.
     95   //
     96   // if |selection| is empty all history and bookmarks are deleted.
     97   Handle DeleteHistoryAndBookmarks(
     98       const std::string& selection,
     99       const std::vector<base::string16>& selection_args,
    100       CancelableRequestConsumerBase* consumer,
    101       const DeleteCallback& callback);
    102 
    103   // Inserts the given values into history backend, and invokes the |callback|
    104   // to return the result.
    105   Handle InsertHistoryAndBookmark(const history::HistoryAndBookmarkRow& values,
    106                                   CancelableRequestConsumerBase* consumer,
    107                                   const InsertCallback& callback);
    108 
    109   // Deletes the matched history and invokes |callback| to return the number of
    110   // the row deleted from the |callback|.
    111   Handle DeleteHistory(const std::string& selection,
    112                        const std::vector<base::string16>& selection_args,
    113                        CancelableRequestConsumerBase* consumer,
    114                        const DeleteCallback& callback);
    115 
    116   // Statement ----------------------------------------------------------------
    117   // Moves the statement's current row from |current_pos| to |destination| in DB
    118   // thread. The new position is returned to the callback. The result supplied
    119   // the callback is constrained by the number of rows might.
    120   Handle MoveStatement(history::AndroidStatement* statement,
    121                        int current_pos,
    122                        int destination,
    123                        CancelableRequestConsumerBase* consumer,
    124                        const MoveStatementCallback& callback);
    125 
    126   // Closes the statement in db thread. The AndroidHistoryProviderService takes
    127   // the ownership of |statement|.
    128   void CloseStatement(history::AndroidStatement* statement);
    129 
    130   // Search term --------------------------------------------------------------
    131   // Inserts the given values and returns the SearchTermID of the inserted row
    132   // from the |callback| on success.
    133   Handle InsertSearchTerm(const history::SearchRow& row,
    134                           CancelableRequestConsumerBase* consumer,
    135                           const InsertCallback& callback);
    136 
    137   // Runs the given update and returns the number of the update rows from the
    138   // |callback| on success.
    139   //
    140   // |row| is the value need to update.
    141   // |selection| is the SQL WHERE clause without 'WHERE'.
    142   // |selection_args| is the arguments for WHERE clause.
    143   Handle UpdateSearchTerms(const history::SearchRow& row,
    144                            const std::string& selection,
    145                            const std::vector<base::string16>& selection_args,
    146                            CancelableRequestConsumerBase* consumer,
    147                            const UpdateCallback& callback);
    148 
    149   // Deletes the matched rows and the number of deleted rows is returned from
    150   // the |callback| on success.
    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 be deleted.
    155   Handle DeleteSearchTerms(const std::string& selection,
    156                            const std::vector<base::string16>& selection_args,
    157                            CancelableRequestConsumerBase* consumer,
    158                            const DeleteCallback& callback);
    159 
    160   // Returns the result of the given query from the |callback|.
    161   // |projections| specifies the result columns, can not be empty, otherwise
    162   // NULL is returned.
    163   // |selection| is the SQL WHERE clause without 'WHERE'.
    164   // |selection_args| is the arguments for WHERE clause.
    165   // |sort_order| the SQL ORDER clause.
    166   Handle QuerySearchTerms(
    167       const std::vector<history::SearchRow::ColumnID>& projections,
    168       const std::string& selection,
    169       const std::vector<base::string16>& selection_args,
    170       const std::string& sort_order,
    171       CancelableRequestConsumerBase* consumer,
    172       const QueryCallback& callback);
    173 
    174  private:
    175   Profile* profile_;
    176 
    177   DISALLOW_COPY_AND_ASSIGN(AndroidHistoryProviderService);
    178 };
    179 
    180 #endif  // CHROME_BROWSER_HISTORY_ANDROID_ANDROID_HISTORY_PROVIDER_SERVICE_H_
    181