Home | History | Annotate | Download | only in search_engines
      1 // Copyright 2014 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 COMPONENTS_SEARCH_ENGINES_KEYWORD_TABLE_H_
      6 #define COMPONENTS_SEARCH_ENGINES_KEYWORD_TABLE_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/compiler_specific.h"
     12 #include "base/gtest_prod_util.h"
     13 #include "base/strings/string16.h"
     14 #include "components/search_engines/template_url_id.h"
     15 #include "components/webdata/common/web_database_table.h"
     16 
     17 struct TemplateURLData;
     18 class WebDatabase;
     19 
     20 namespace sql {
     21 class Statement;
     22 }  // namespace sql
     23 
     24 // This class manages the |keywords| MetaTable within the SQLite database
     25 // passed to the constructor. It expects the following schema:
     26 //
     27 // Note: The database stores time in seconds, UTC.
     28 //
     29 // keywords                 Most of the columns mirror that of a field in
     30 //                          TemplateURLData.  See that struct for more details.
     31 //   id
     32 //   short_name
     33 //   keyword
     34 //   favicon_url
     35 //   url
     36 //   show_in_default_list
     37 //   safe_for_autoreplace
     38 //   originating_url
     39 //   date_created           This column was added after we allowed keywords.
     40 //                          Keywords created before we started tracking
     41 //                          creation date have a value of 0 for this.
     42 //   usage_count
     43 //   input_encodings        Semicolon separated list of supported input
     44 //                          encodings, may be empty.
     45 //   suggest_url
     46 //   prepopulate_id         See TemplateURLData::prepopulate_id.
     47 //   created_by_policy      See TemplateURLData::created_by_policy.  This was
     48 //                          added in version 26.
     49 //   instant_url            See TemplateURLData::instant_url.  This was added in
     50 //                          version 29.
     51 //   last_modified          See TemplateURLData::last_modified.  This was added
     52 //                          in version 38.
     53 //   sync_guid              See TemplateURLData::sync_guid. This was added in
     54 //                          version 39.
     55 //   alternate_urls         See TemplateURLData::alternate_urls. This was added
     56 //                          in version 47.
     57 //   search_terms_replacement_key
     58 //                          See TemplateURLData::search_terms_replacement_key.
     59 //                          This was added in version 49.
     60 //   image_url              See TemplateURLData::image_url. This was added in
     61 //                          version 52.
     62 //   search_url_post_params See TemplateURLData::search_url_post_params. This
     63 //                          was added in version 52.
     64 //   suggest_url_post_params See TemplateURLData::suggestions_url_post_params.
     65 //                          This was added in version 52.
     66 //   instant_url_post_params See TemplateURLData::instant_url_post_params. This
     67 //                          was added in version 52.
     68 //   image_url_post_params  See TemplateURLData::image_url_post_params. This
     69 //                          was added in version 52.
     70 //   new_tab_url            See TemplateURLData::new_tab_url. This was added in
     71 //                          version 53.
     72 //
     73 // This class also manages some fields in the |meta| table:
     74 //
     75 // Default Search Provider ID        The id of the default search provider.
     76 // Builtin Keyword Version           The version of builtin keywords data.
     77 //
     78 class KeywordTable : public WebDatabaseTable {
     79  public:
     80   enum OperationType {
     81     ADD,
     82     REMOVE,
     83     UPDATE,
     84   };
     85 
     86   typedef std::pair<OperationType, TemplateURLData> Operation;
     87   typedef std::vector<Operation> Operations;
     88   typedef std::vector<TemplateURLData> Keywords;
     89 
     90   // Constants exposed for the benefit of test code:
     91 
     92   static const char kDefaultSearchProviderKey[];
     93 
     94   KeywordTable();
     95   virtual ~KeywordTable();
     96 
     97   // Retrieves the KeywordTable* owned by |database|.
     98   static KeywordTable* FromWebDatabase(WebDatabase* db);
     99 
    100   virtual WebDatabaseTable::TypeKey GetTypeKey() const OVERRIDE;
    101   virtual bool CreateTablesIfNecessary() OVERRIDE;
    102   virtual bool IsSyncable() OVERRIDE;
    103   virtual bool MigrateToVersion(int version,
    104                                 bool* update_compatible_version) OVERRIDE;
    105 
    106   // Performs an arbitrary number of Add/Remove/Update operations as a single
    107   // transaction.  This is provided for efficiency reasons: if the caller needs
    108   // to perform a large number of operations, doing them in a single transaction
    109   // instead of one-per-transaction can be dramatically more efficient.
    110   bool PerformOperations(const Operations& operations);
    111 
    112   // Loads the keywords into the specified vector. It's up to the caller to
    113   // delete the returned objects.
    114   // Returns true on success.
    115   bool GetKeywords(Keywords* keywords);
    116 
    117   // ID (TemplateURLData->id) of the default search provider.
    118   bool SetDefaultSearchProviderID(int64 id);
    119   int64 GetDefaultSearchProviderID();
    120 
    121   // Version of the built-in keywords.
    122   bool SetBuiltinKeywordVersion(int version);
    123   int GetBuiltinKeywordVersion();
    124 
    125   // Returns a comma-separated list of the keyword columns for the current
    126   // version of the table.
    127   static std::string GetKeywordColumns();
    128 
    129   // Table migration functions.
    130   bool MigrateToVersion21AutoGenerateKeywordColumn();
    131   bool MigrateToVersion25AddLogoIDColumn();
    132   bool MigrateToVersion26AddCreatedByPolicyColumn();
    133   bool MigrateToVersion28SupportsInstantColumn();
    134   bool MigrateToVersion29InstantURLToSupportsInstant();
    135   bool MigrateToVersion38AddLastModifiedColumn();
    136   bool MigrateToVersion39AddSyncGUIDColumn();
    137   bool MigrateToVersion44AddDefaultSearchProviderBackup();
    138   bool MigrateToVersion45RemoveLogoIDAndAutogenerateColumns();
    139   bool MigrateToVersion47AddAlternateURLsColumn();
    140   bool MigrateToVersion48RemoveKeywordsBackup();
    141   bool MigrateToVersion49AddSearchTermsReplacementKeyColumn();
    142   bool MigrateToVersion52AddImageSearchAndPOSTSupport();
    143   bool MigrateToVersion53AddNewTabURLColumn();
    144 
    145  private:
    146   friend class KeywordTableTest;
    147   FRIEND_TEST_ALL_PREFIXES(WebDatabaseMigrationTest, MigrateVersion44ToCurrent);
    148 
    149   // NOTE: Since the table columns have changed in different versions, many
    150   // functions below take a |table_version| argument which dictates which
    151   // version number's column set to use.
    152 
    153   // Fills |data| with the data in |s|.  Returns false if we couldn't fill
    154   // |data| for some reason, e.g. |s| tried to set one of the fields to an
    155   // illegal value.
    156   static bool GetKeywordDataFromStatement(const sql::Statement& s,
    157                                           TemplateURLData* data);
    158 
    159   // Adds a new keyword, updating the id field on success.
    160   // Returns true if successful.
    161   bool AddKeyword(const TemplateURLData& data);
    162 
    163   // Removes the specified keyword.
    164   // Returns true if successful.
    165   bool RemoveKeyword(TemplateURLID id);
    166 
    167   // Updates the database values for the specified url.
    168   // Returns true on success.
    169   bool UpdateKeyword(const TemplateURLData& data);
    170 
    171   // Gets a string representation for keyword with id specified.
    172   // Used to store its result in |meta| table or to compare with another
    173   // keyword. Returns true on success, false otherwise.
    174   bool GetKeywordAsString(TemplateURLID id,
    175                           const std::string& table_name,
    176                           std::string* result);
    177 
    178   // Migrates table |name| (which should be either "keywords" or
    179   // "keywords_backup") from version 44 to version 45.
    180   bool MigrateKeywordsTableForVersion45(const std::string& name);
    181 
    182   DISALLOW_COPY_AND_ASSIGN(KeywordTable);
    183 };
    184 
    185 #endif  // COMPONENTS_SEARCH_ENGINES_KEYWORD_TABLE_H_
    186