Home | History | Annotate | Download | only in webdata
      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_WEBDATA_KEYWORD_TABLE_H_
      6 #define CHROME_BROWSER_WEBDATA_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 "chrome/browser/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 //
     71 //
     72 // This class also manages some fields in the |meta| table:
     73 //
     74 // Default Search Provider ID        The id of the default search provider.
     75 // Builtin Keyword Version           The version of builtin keywords data.
     76 //
     77 class KeywordTable : public WebDatabaseTable {
     78  public:
     79   typedef std::vector<TemplateURLData> Keywords;
     80 
     81   // Constants exposed for the benefit of test code:
     82 
     83   static const char kDefaultSearchProviderKey[];
     84 
     85   KeywordTable();
     86   virtual ~KeywordTable();
     87 
     88   // Retrieves the KeywordTable* owned by |database|.
     89   static KeywordTable* FromWebDatabase(WebDatabase* db);
     90 
     91   virtual WebDatabaseTable::TypeKey GetTypeKey() const OVERRIDE;
     92   virtual bool Init(sql::Connection* db, sql::MetaTable* meta_table) OVERRIDE;
     93   virtual bool IsSyncable() OVERRIDE;
     94   virtual bool MigrateToVersion(int version,
     95                                 bool* update_compatible_version) OVERRIDE;
     96 
     97   // Adds a new keyword, updating the id field on success.
     98   // Returns true if successful.
     99   bool AddKeyword(const TemplateURLData& data);
    100 
    101   // Removes the specified keyword.
    102   // Returns true if successful.
    103   bool RemoveKeyword(TemplateURLID id);
    104 
    105   // Loads the keywords into the specified vector. It's up to the caller to
    106   // delete the returned objects.
    107   // Returns true on success.
    108   bool GetKeywords(Keywords* keywords);
    109 
    110   // Updates the database values for the specified url.
    111   // Returns true on success.
    112   bool UpdateKeyword(const TemplateURLData& data);
    113 
    114   // ID (TemplateURLData->id) of the default search provider.
    115   bool SetDefaultSearchProviderID(int64 id);
    116   int64 GetDefaultSearchProviderID();
    117 
    118   // Version of the built-in keywords.
    119   bool SetBuiltinKeywordVersion(int version);
    120   int GetBuiltinKeywordVersion();
    121 
    122   // Returns a comma-separated list of the keyword columns for the current
    123   // version of the table.
    124   static std::string GetKeywordColumns();
    125 
    126   // Table migration functions.
    127   bool MigrateToVersion21AutoGenerateKeywordColumn();
    128   bool MigrateToVersion25AddLogoIDColumn();
    129   bool MigrateToVersion26AddCreatedByPolicyColumn();
    130   bool MigrateToVersion28SupportsInstantColumn();
    131   bool MigrateToVersion29InstantURLToSupportsInstant();
    132   bool MigrateToVersion38AddLastModifiedColumn();
    133   bool MigrateToVersion39AddSyncGUIDColumn();
    134   bool MigrateToVersion44AddDefaultSearchProviderBackup();
    135   bool MigrateToVersion45RemoveLogoIDAndAutogenerateColumns();
    136   bool MigrateToVersion47AddAlternateURLsColumn();
    137   bool MigrateToVersion48RemoveKeywordsBackup();
    138   bool MigrateToVersion49AddSearchTermsReplacementKeyColumn();
    139   bool MigrateToVersion52AddImageSearchAndPOSTSupport();
    140 
    141  private:
    142   FRIEND_TEST_ALL_PREFIXES(KeywordTableTest, GetTableContents);
    143   FRIEND_TEST_ALL_PREFIXES(KeywordTableTest, GetTableContentsOrdering);
    144   FRIEND_TEST_ALL_PREFIXES(KeywordTableTest, SanitizeURLs);
    145   FRIEND_TEST_ALL_PREFIXES(WebDatabaseMigrationTest, MigrateVersion44ToCurrent);
    146 
    147   // NOTE: Since the table columns have changed in different versions, many
    148   // functions below take a |table_version| argument which dictates which
    149   // version number's column set to use.
    150 
    151   // Fills |data| with the data in |s|.  Returns false if we couldn't fill
    152   // |data| for some reason, e.g. |s| tried to set one of the fields to an
    153   // illegal value.
    154   static bool GetKeywordDataFromStatement(const sql::Statement& s,
    155                                           TemplateURLData* data);
    156 
    157   // Returns contents of selected table as a string in |contents| parameter.
    158   // Returns true on success, false otherwise.
    159   bool GetTableContents(const char* table_name,
    160                         int table_version,
    161                         std::string* contents);
    162 
    163   // Gets a string representation for keyword with id specified.
    164   // Used to store its result in |meta| table or to compare with another
    165   // keyword. Returns true on success, false otherwise.
    166   bool GetKeywordAsString(TemplateURLID id,
    167                           const std::string& table_name,
    168                           std::string* result);
    169 
    170   // Migrates table |name| (which should be either "keywords" or
    171   // "keywords_backup") from version 44 to version 45.
    172   bool MigrateKeywordsTableForVersion45(const std::string& name);
    173 
    174   DISALLOW_COPY_AND_ASSIGN(KeywordTable);
    175 };
    176 
    177 #endif  // CHROME_BROWSER_WEBDATA_KEYWORD_TABLE_H_
    178