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_SHORTCUTS_DATABASE_H_ 6 #define CHROME_BROWSER_HISTORY_SHORTCUTS_DATABASE_H_ 7 8 #include <map> 9 #include <string> 10 #include <vector> 11 12 #include "base/files/file_path.h" 13 #include "base/gtest_prod_util.h" 14 #include "base/memory/ref_counted.h" 15 #include "base/strings/string16.h" 16 #include "sql/connection.h" 17 #include "sql/meta_table.h" 18 #include "url/gurl.h" 19 20 namespace history { 21 22 // This class manages the shortcut provider table within the SQLite database 23 // passed to the constructor. It expects the following schema: 24 // 25 // Note: The database stores time in seconds, UTC. 26 // 27 // omni_box_shortcuts 28 // id Unique id of the entry (needed for the sync). 29 // search_text Text that shortcuts was searched with. 30 // url The url of the shortcut. 31 // contents Contents of the original omni-box entry. 32 // contents_matches Comma separated matches of the |search_text| in 33 // |contents|, for example "0,0,5,3,9,0". 34 // description Description of the original omni-box entry. 35 // description_matches Comma separated matches of the |search_text| in 36 // |description|. 37 // last_access_time Time the entry was accessed last, stored in seconds, 38 // UTC. 39 // number_of_hits Number of times that the entry has been selected. 40 class ShortcutsDatabase : public base::RefCountedThreadSafe<ShortcutsDatabase> { 41 public: 42 // The following struct encapsulates one previously selected omnibox shortcut. 43 struct Shortcut { 44 // The fields of an AutocompleteMatch that we preserve in a shortcut. 45 struct MatchCore { 46 MatchCore(const base::string16& fill_into_edit, 47 const GURL& destination_url, 48 const base::string16& contents, 49 const std::string& contents_class, 50 const base::string16& description, 51 const std::string& description_class, 52 int transition, 53 int type, 54 const base::string16& keyword); 55 ~MatchCore(); 56 57 base::string16 fill_into_edit; 58 GURL destination_url; 59 base::string16 contents; 60 // For both contents_class and description_class, we strip MATCH 61 // classifications; the ShortcutsProvider will re-mark MATCH regions based 62 // on the user's current typing. 63 std::string contents_class; 64 base::string16 description; 65 std::string description_class; 66 int transition; 67 int type; 68 base::string16 keyword; 69 }; 70 71 Shortcut(const std::string& id, 72 const base::string16& text, 73 const MatchCore& match_core, 74 const base::Time& last_access_time, 75 int number_of_hits); 76 // Required for STL, we don't use this directly. 77 Shortcut(); 78 ~Shortcut(); 79 80 std::string id; // Unique guid for the shortcut. 81 base::string16 text; // The user's original input string. 82 MatchCore match_core; 83 base::Time last_access_time; // Last time shortcut was selected. 84 int number_of_hits; // How many times shortcut was selected. 85 }; 86 87 typedef std::vector<std::string> ShortcutIDs; 88 typedef std::map<std::string, Shortcut> GuidToShortcutMap; 89 90 explicit ShortcutsDatabase(const base::FilePath& database_path); 91 92 bool Init(); 93 94 // Adds the ShortcutsProvider::Shortcut to the database. 95 bool AddShortcut(const Shortcut& shortcut); 96 97 // Updates timing and selection count for the ShortcutsProvider::Shortcut. 98 bool UpdateShortcut(const Shortcut& shortcut); 99 100 // Deletes the ShortcutsProvider::Shortcuts with these IDs. 101 bool DeleteShortcutsWithIDs(const ShortcutIDs& shortcut_ids); 102 103 // Deletes the ShortcutsProvider::Shortcuts with the url. 104 bool DeleteShortcutsWithURL(const std::string& shortcut_url_spec); 105 106 // Deletes all of the ShortcutsProvider::Shortcuts. 107 bool DeleteAllShortcuts(); 108 109 // Loads all of the shortcuts. 110 void LoadShortcuts(GuidToShortcutMap* shortcuts); 111 112 private: 113 friend class base::RefCountedThreadSafe<ShortcutsDatabase>; 114 friend class ShortcutsDatabaseTest; 115 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, AddShortcut); 116 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, UpdateShortcut); 117 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, DeleteShortcutsWithIds); 118 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, DeleteShortcutsWithURL); 119 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, LoadShortcuts); 120 121 virtual ~ShortcutsDatabase(); 122 123 // Ensures that the table is present. 124 bool EnsureTable(); 125 126 // The sql database. Not valid until Init is called. 127 sql::Connection db_; 128 base::FilePath database_path_; 129 130 sql::MetaTable meta_table_; 131 132 static const base::FilePath::CharType kShortcutsDatabaseName[]; 133 134 DISALLOW_COPY_AND_ASSIGN(ShortcutsDatabase); 135 }; 136 137 } // namespace history 138 139 #endif // CHROME_BROWSER_HISTORY_SHORTCUTS_DATABASE_H_ 140