15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_HISTORY_ANDROID_ANDROID_PROVIDER_BACKEND_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_HISTORY_ANDROID_ANDROID_PROVIDER_BACKEND_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include <list>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/containers/hash_tables.h"
122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/gtest_prod_util.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/scoped_vector.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/android/android_cache_database.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/android/sql_handler.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/history_backend.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/history_notifications.h"
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/history/core/android/android_history_types.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sql/statement.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sql/transaction.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace history {
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AndroidProviderBackend;
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AndroidURLsSQLHandler;
2946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class HistoryClient;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class HistoryDatabase;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ThumbnailDatabase;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class provides the query/insert/update/remove methods to implement
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// android.provider.Browser.BookmarkColumns and
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// android.provider.Browser.SearchColumns API.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// When used it:
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a. The android_urls table is created in history database if it doesn't
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    exists.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// b. The android_cache database is created.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// c. The bookmark_cache table is created.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Android_urls and android_cache database is only updated before the related
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// methods are accessed. A data change will not triger the update.
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The android_cache database is deleted when shutdown.
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AndroidProviderBackend {
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AndroidProviderBackend(const base::FilePath& cache_db_name,
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         HistoryDatabase* history_db,
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         ThumbnailDatabase* thumbnail_db,
5246d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                         HistoryClient* history_client_,
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         HistoryBackend::Delegate* delegate);
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~AndroidProviderBackend();
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Bookmarks ----------------------------------------------------------------
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the given query and returns the result on success, NULL on error or
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the |projections| is empty.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |projections| is the vector of the result columns.
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for WHERE clause.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |sort_order| is the SQL ORDER clause.
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AndroidStatement* QueryHistoryAndBookmarks(
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::vector<HistoryAndBookmarkRow::ColumnID>& projections,
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& selection,
695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<base::string16>& selection_args,
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& sort_order);
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the given update and returns the number of the updated rows in
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |update_count| and return true on success, false on error.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |row| is the value to update.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for the WHERE clause.
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool UpdateHistoryAndBookmarks(
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const HistoryAndBookmarkRow& row,
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& selection,
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<base::string16>& selection_args,
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      int* update_count);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Inserts the given values and returns the URLID of the inserted row.
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AndroidURLID InsertHistoryAndBookmark(const HistoryAndBookmarkRow& values);
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes the specified rows and returns the number of the deleted rows in
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |deleted_count|.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for the WHERE clause.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if |selection| is empty all history and bookmarks are deleted.
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool DeleteHistoryAndBookmarks(
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& selection,
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<base::string16>& selection_args,
965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      int* deleted_count);
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes the matched history, returns true on success, false on error.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The number of deleted row is returned in |deleted_count|.
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The url row is kept and the visit count is reset if the matched url
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is bookmarked.
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool DeleteHistory(const std::string& selection,
1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     const std::vector<base::string16>& selection_args,
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     int* deleted_count);
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SearchTerms --------------------------------------------------------------
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the result of the given query.
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |projections| specifies the result columns, can not be empty, otherwise
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NULL is returned.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for WHERE clause.
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |sort_order| the SQL ORDER clause.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AndroidStatement* QuerySearchTerms(
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::vector<SearchRow::ColumnID>& projections,
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& selection,
1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<base::string16>& selection_args,
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& sort_order);
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the given update and returns the number of updated rows in
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |update_count| and return true, false returned if there is any error.
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |row| is the value need to update.
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for WHERE clause.
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateSearchTerms(const SearchRow& row,
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         const std::string& selection,
1285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         const std::vector<base::string16>& selection_args,
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         int* update_count);
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Inserts the given valus and return the SearchTermID of inserted row.
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SearchTermID InsertSearchTerm(const SearchRow& values);
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes the matched rows and the number of deleted rows is returned in
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |deleted_count|.
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for WHERE clause.
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if |selection| is empty all search be deleted.
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool DeleteSearchTerms(const std::string& selection,
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         const std::vector<base::string16>& selection_args,
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         int * deleted_count);
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class AndroidProviderBackendTest;
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(AndroidProviderBackendTest, UpdateTables);
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(AndroidProviderBackendTest, UpdateSearchTermTable);
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  typedef std::list<base::Closure> HistoryNotifications;
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The scoped transaction for AndroidProviderBackend.
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The new transactions are started automatically in both history and
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // thumbnail database and could be a nesting transaction, if so, rolling back
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of this transaction will cause the exsting and subsequent nesting
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // transactions failed.
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Commit() is used to commit the transaction, otherwise the transaction will
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be rolled back when the object is out of scope. This transaction could
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // failed even the commit() is called if it is in a transaction that has been
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // rolled back or the subsequent transaction in the same outermost
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // transaction would be rolled back latter.
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ScopedTransaction {
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ScopedTransaction(HistoryDatabase* history_db,
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      ThumbnailDatabase* thumbnail_db);
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ~ScopedTransaction();
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Commit the transaction.
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void Commit();
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   private:
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HistoryDatabase* history_db_;
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ThumbnailDatabase* thumbnail_db_;
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Whether the transaction was committed.
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool committed_;
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The count of the nested transaction in history database.
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const int history_transaction_nesting_;
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The count of the nested transaction in thumbnail database.
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const int thumbnail_transaction_nesting_;
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(ScopedTransaction);
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the given update and returns the number of updated rows in
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |update_count| and return true on success, false on error.
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The notifications are returned in |notifications| and the ownership of them
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is transfered to caller.
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |row| is the value to update.
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for the WHERE clause.
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateHistoryAndBookmarks(const HistoryAndBookmarkRow& row,
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const std::string& selection,
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       const std::vector<base::string16>& selection_args,
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       int* update_count,
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       HistoryNotifications* notifications);
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Inserts the given values and returns the URLID of the inserted row.
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The notifications are returned in |notifications| and the ownership of them
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is transfered to caller.
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The EnsureInitializedAndUpdated() will not be invoked if the
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |ensure_initialized_and_updated| is false.
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AndroidURLID InsertHistoryAndBookmark(const HistoryAndBookmarkRow& values,
208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                        bool ensure_initialized_and_updated,
209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                        HistoryNotifications* notifications);
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes the specified rows and returns the number of the deleted rows in
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |deleted_count|.
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for the WHERE clause.
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The notifications are returned in |notifications| and the ownership of them
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is transfered to the caller.
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if |selection| is empty all history and bookmarks are deleted.
2195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool DeleteHistoryAndBookmarks(
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& selection,
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<base::string16>& selection_args,
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      int* deleted_count,
2235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      HistoryNotifications* notifications);
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes the matched history, returns true on success, false on error.
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The number of deleted row is returned in |deleted_count|.
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The notifications are returned in |notifications| and the ownership of them
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is transfered to caller.
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The url row is kept and the visit is reset if the matched url is
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // bookmarked.
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool DeleteHistory(const std::string& selection,
2325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     const std::vector<base::string16>& selection_args,
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     int* deleted_count,
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     HistoryNotifications* notifications);
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes and updates tables if necessary.
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool EnsureInitializedAndUpdated();
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes AndroidProviderBackend.
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Init();
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update android_urls and bookmark_cache table if it is necessary.
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateTables();
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the android_urls and bookmark_cache for visited urls.
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateVisitedURLs();
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the android_urls for removed urls.
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateRemovedURLs();
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the bookmark_cache table with bookmarks.
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateBookmarks();
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the bookmark_cache table for favicon.
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateFavicon();
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the search_term table
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateSearchTermTable();
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Append the specified result columns in |projections| to the given
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |result_column|.
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // To support the lazy binding, the index of favicon column will be
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returned if it exists, otherwise returns -1.
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int AppendBookmarkResultColumn(
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::vector<HistoryAndBookmarkRow::ColumnID>& projections,
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::string* result_column);
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Append the specified search result columns in |projections| to the given
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |result_column|.
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendSearchResultColumn(
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::vector<SearchRow::ColumnID>& projections,
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::string* result_column);
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the given query on history_bookmark virtual table and returns true if
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // succeeds, the selected URLID and url are returned in |rows|.
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetSelectedURLs(const std::string& selection,
2775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       const std::vector<base::string16>& selection_args,
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       TableIDRows* rows);
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the given query on search_terms table and returns true on success,
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The selected search term are returned in |rows|.
2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  typedef std::vector<base::string16> SearchTerms;
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetSelectedSearchTerms(const std::string& selection,
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              const std::vector<base::string16>& selection_args,
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              SearchTerms* rows);
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Simulates update url by deleting the previous URL and creating a new one.
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return true on success.
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SimulateUpdateURL(const HistoryAndBookmarkRow& row,
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         const TableIDRows& ids,
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         HistoryNotifications* notifications);
2925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Query bookmark without sync the tables. It should be used after syncing
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // tables.
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AndroidStatement* QueryHistoryAndBookmarksInternal(
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::vector<HistoryAndBookmarkRow::ColumnID>& projections,
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& selection,
2985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<base::string16>& selection_args,
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& sort_order);
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Delete the given urls' history, returns true on success, or false on error.
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |delete_bookmarks| is set, the bookmarks are deleted as well.
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The notifications are returned in |notifications| and the ownership of them
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is transfered to caller.
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool DeleteHistoryInternal(const TableIDRows& urls,
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             bool delete_bookmarks,
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             HistoryNotifications* notifications);
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
309a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Broadcasts |notifications|.  Broadcasting takes ownership of the
310a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // notifications, so on return |notifications| will be empty.
311a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void BroadcastNotifications(HistoryNotifications* notifications);
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add the search term from the given |values|. It will add the values.url()
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in the urls table if it doesn't exist, insert visit in the visits table,
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // also add keyword in keyword_search_term.
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool AddSearchTerm(const SearchRow& values);
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SQLHandlers for different tables.
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SQLHandler> urls_handler_;
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SQLHandler> visit_handler_;
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SQLHandler> android_urls_handler_;
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SQLHandler> favicon_handler_;
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SQLHandler> bookmark_model_handler_;
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The vector of all handlers
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<SQLHandler*> sql_handlers_;
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Android cache database filename.
3292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const base::FilePath android_cache_db_filename_;
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The history db's connection.
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  sql::Connection* db_;
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryDatabase* history_db_;
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ThumbnailDatabase* thumbnail_db_;
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
33846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  HistoryClient* history_client_;
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether AndroidProviderBackend has been initialized.
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool initialized_;
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryBackend::Delegate* delegate_;
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(AndroidProviderBackend);
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace history
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_HISTORY_ANDROID_ANDROID_PROVIDER_BACKEND_H_
351