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)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/containers/hash_tables.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/gtest_prod_util.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/scoped_vector.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/android/android_cache_database.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/history/android/android_history_types.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"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sql/statement.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "sql/transaction.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace history {
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AndroidProviderBackend;
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AndroidURLsSQLHandler;
2846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)class HistoryClient;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class HistoryDatabase;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ThumbnailDatabase;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class provides the query/insert/update/remove methods to implement
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// android.provider.Browser.BookmarkColumns and
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// android.provider.Browser.SearchColumns API.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// When used it:
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// a. The android_urls table is created in history database if it doesn't
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//    exists.
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// b. The android_cache database is created.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// c. The bookmark_cache table is created.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Android_urls and android_cache database is only updated before the related
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// methods are accessed. A data change will not triger the update.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The android_cache database is deleted when shutdown.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AndroidProviderBackend {
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  AndroidProviderBackend(const base::FilePath& cache_db_name,
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         HistoryDatabase* history_db,
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         ThumbnailDatabase* thumbnail_db,
5146d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)                         HistoryClient* history_client_,
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         HistoryBackend::Delegate* delegate);
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~AndroidProviderBackend();
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Bookmarks ----------------------------------------------------------------
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the given query and returns the result on success, NULL on error or
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the |projections| is empty.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |projections| is the vector of the result columns.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for WHERE clause.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |sort_order| is the SQL ORDER clause.
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AndroidStatement* QueryHistoryAndBookmarks(
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::vector<HistoryAndBookmarkRow::ColumnID>& projections,
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& selection,
685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<base::string16>& selection_args,
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& sort_order);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the given update and returns the number of the updated rows in
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |update_count| and return true on success, false on error.
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |row| is the value to update.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for the WHERE clause.
775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool UpdateHistoryAndBookmarks(
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const HistoryAndBookmarkRow& row,
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& selection,
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<base::string16>& selection_args,
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      int* update_count);
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Inserts the given values and returns the URLID of the inserted row.
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AndroidURLID InsertHistoryAndBookmark(const HistoryAndBookmarkRow& values);
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes the specified rows and returns the number of the deleted rows in
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |deleted_count|.
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for the WHERE clause.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if |selection| is empty all history and bookmarks are deleted.
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool DeleteHistoryAndBookmarks(
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& selection,
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<base::string16>& selection_args,
955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      int* deleted_count);
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes the matched history, returns true on success, false on error.
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The number of deleted row is returned in |deleted_count|.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The url row is kept and the visit count is reset if the matched url
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is bookmarked.
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool DeleteHistory(const std::string& selection,
1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     const std::vector<base::string16>& selection_args,
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     int* deleted_count);
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SearchTerms --------------------------------------------------------------
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the result of the given query.
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |projections| specifies the result columns, can not be empty, otherwise
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // NULL is returned.
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for WHERE clause.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |sort_order| the SQL ORDER clause.
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AndroidStatement* QuerySearchTerms(
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::vector<SearchRow::ColumnID>& projections,
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& selection,
1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<base::string16>& selection_args,
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& sort_order);
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the given update and returns the number of updated rows in
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |update_count| and return true, false returned if there is any error.
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |row| is the value need to update.
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for WHERE clause.
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateSearchTerms(const SearchRow& row,
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         const std::string& selection,
1275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         const std::vector<base::string16>& selection_args,
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         int* update_count);
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Inserts the given valus and return the SearchTermID of inserted row.
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SearchTermID InsertSearchTerm(const SearchRow& values);
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes the matched rows and the number of deleted rows is returned in
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |deleted_count|.
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for WHERE clause.
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if |selection| is empty all search be deleted.
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool DeleteSearchTerms(const std::string& selection,
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                         const std::vector<base::string16>& selection_args,
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         int * deleted_count);
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class AndroidProviderBackendTest;
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(AndroidProviderBackendTest, UpdateTables);
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(AndroidProviderBackendTest, UpdateSearchTermTable);
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  class HistoryNotifications {
150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)   public:
151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    HistoryNotifications();
152a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ~HistoryNotifications();
153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
154a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    void PushBack(int type, scoped_ptr<HistoryDetails> detail);
155a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    int PopBackType();
156a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    scoped_ptr<HistoryDetails> PopBackDetails();
157a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
158a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    bool empty() const { return types_.empty(); }
159a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
160a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)   private:
161a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    std::vector<int> types_;
162a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ScopedVector<HistoryDetails> details_;
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
164a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(HistoryNotifications);
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The scoped transaction for AndroidProviderBackend.
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The new transactions are started automatically in both history and
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // thumbnail database and could be a nesting transaction, if so, rolling back
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of this transaction will cause the exsting and subsequent nesting
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // transactions failed.
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Commit() is used to commit the transaction, otherwise the transaction will
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be rolled back when the object is out of scope. This transaction could
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // failed even the commit() is called if it is in a transaction that has been
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // rolled back or the subsequent transaction in the same outermost
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // transaction would be rolled back latter.
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class ScopedTransaction {
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   public:
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ScopedTransaction(HistoryDatabase* history_db,
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      ThumbnailDatabase* thumbnail_db);
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ~ScopedTransaction();
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Commit the transaction.
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    void Commit();
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)   private:
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    HistoryDatabase* history_db_;
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ThumbnailDatabase* thumbnail_db_;
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Whether the transaction was committed.
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    bool committed_;
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The count of the nested transaction in history database.
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const int history_transaction_nesting_;
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // The count of the nested transaction in thumbnail database.
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const int thumbnail_transaction_nesting_;
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DISALLOW_COPY_AND_ASSIGN(ScopedTransaction);
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the given update and returns the number of updated rows in
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |update_count| and return true on success, false on error.
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The notifications are returned in |notifications| and the ownership of them
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is transfered to caller.
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |row| is the value to update.
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for the WHERE clause.
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateHistoryAndBookmarks(const HistoryAndBookmarkRow& row,
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       const std::string& selection,
2135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       const std::vector<base::string16>& selection_args,
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       int* update_count,
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       HistoryNotifications* notifications);
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Inserts the given values and returns the URLID of the inserted row.
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The notifications are returned in |notifications| and the ownership of them
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is transfered to caller.
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The EnsureInitializedAndUpdated() will not be invoked if the
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |ensure_initialized_and_updated| is false.
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AndroidURLID InsertHistoryAndBookmark(const HistoryAndBookmarkRow& values,
223a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                        bool ensure_initialized_and_updated,
224a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                        HistoryNotifications* notifications);
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes the specified rows and returns the number of the deleted rows in
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |deleted_count|.
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection| is the SQL WHERE clause without 'WHERE'.
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selection_args| is the arguments for the WHERE clause.
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The notifications are returned in |notifications| and the ownership of them
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is transfered to the caller.
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if |selection| is empty all history and bookmarks are deleted.
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool DeleteHistoryAndBookmarks(
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::string& selection,
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<base::string16>& selection_args,
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      int* deleted_count,
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      HistoryNotifications* notifications);
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Deletes the matched history, returns true on success, false on error.
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The number of deleted row is returned in |deleted_count|.
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The notifications are returned in |notifications| and the ownership of them
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is transfered to caller.
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The url row is kept and the visit is reset if the matched url is
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // bookmarked.
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool DeleteHistory(const std::string& selection,
2475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                     const std::vector<base::string16>& selection_args,
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     int* deleted_count,
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     HistoryNotifications* notifications);
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes and updates tables if necessary.
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool EnsureInitializedAndUpdated();
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initializes AndroidProviderBackend.
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Init();
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update android_urls and bookmark_cache table if it is necessary.
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateTables();
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the android_urls and bookmark_cache for visited urls.
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateVisitedURLs();
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the android_urls for removed urls.
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateRemovedURLs();
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the bookmark_cache table with bookmarks.
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateBookmarks();
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the bookmark_cache table for favicon.
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateFavicon();
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Update the search_term table
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateSearchTermTable();
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Append the specified result columns in |projections| to the given
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |result_column|.
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // To support the lazy binding, the index of favicon column will be
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // returned if it exists, otherwise returns -1.
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int AppendBookmarkResultColumn(
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::vector<HistoryAndBookmarkRow::ColumnID>& projections,
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::string* result_column);
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Append the specified search result columns in |projections| to the given
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |result_column|.
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void AppendSearchResultColumn(
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::vector<SearchRow::ColumnID>& projections,
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::string* result_column);
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the given query on history_bookmark virtual table and returns true if
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // succeeds, the selected URLID and url are returned in |rows|.
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetSelectedURLs(const std::string& selection,
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                       const std::vector<base::string16>& selection_args,
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       TableIDRows* rows);
2945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the given query on search_terms table and returns true on success,
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The selected search term are returned in |rows|.
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  typedef std::vector<base::string16> SearchTerms;
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool GetSelectedSearchTerms(const std::string& selection,
2995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              const std::vector<base::string16>& selection_args,
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                              SearchTerms* rows);
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Simulates update url by deleting the previous URL and creating a new one.
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Return true on success.
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool SimulateUpdateURL(const HistoryAndBookmarkRow& row,
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         const TableIDRows& ids,
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                         HistoryNotifications* notifications);
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Query bookmark without sync the tables. It should be used after syncing
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // tables.
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AndroidStatement* QueryHistoryAndBookmarksInternal(
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::vector<HistoryAndBookmarkRow::ColumnID>& projections,
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& selection,
3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const std::vector<base::string16>& selection_args,
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& sort_order);
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Delete the given urls' history, returns true on success, or false on error.
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If |delete_bookmarks| is set, the bookmarks are deleted as well.
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The notifications are returned in |notifications| and the ownership of them
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is transfered to caller.
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool DeleteHistoryInternal(const TableIDRows& urls,
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             bool delete_bookmarks,
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             HistoryNotifications* notifications);
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
324a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Broadcasts |notifications|.  Broadcasting takes ownership of the
325a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // notifications, so on return |notifications| will be empty.
326a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void BroadcastNotifications(HistoryNotifications* notifications);
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add the search term from the given |values|. It will add the values.url()
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in the urls table if it doesn't exist, insert visit in the visits table,
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // also add keyword in keyword_search_term.
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool AddSearchTerm(const SearchRow& values);
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SQLHandlers for different tables.
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SQLHandler> urls_handler_;
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SQLHandler> visit_handler_;
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SQLHandler> android_urls_handler_;
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SQLHandler> favicon_handler_;
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<SQLHandler> bookmark_model_handler_;
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The vector of all handlers
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::vector<SQLHandler*> sql_handlers_;
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Android cache database filename.
3442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  const base::FilePath android_cache_db_filename_;
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The history db's connection.
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  sql::Connection* db_;
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryDatabase* history_db_;
3505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ThumbnailDatabase* thumbnail_db_;
3525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
35346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)  HistoryClient* history_client_;
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether AndroidProviderBackend has been initialized.
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool initialized_;
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryBackend::Delegate* delegate_;
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(AndroidProviderBackend);
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace history
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_HISTORY_ANDROID_ANDROID_PROVIDER_BACKEND_H_
366