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 "chrome/browser/history/shortcuts_backend.h" 17 #include "sql/connection.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 typedef std::map<std::string, ShortcutsBackend::Shortcut> GuidToShortcutMap; 43 44 explicit ShortcutsDatabase(const base::FilePath& database_path); 45 46 bool Init(); 47 48 // Adds the ShortcutsProvider::Shortcut to the database. 49 bool AddShortcut(const ShortcutsBackend::Shortcut& shortcut); 50 51 // Updates timing and selection count for the ShortcutsProvider::Shortcut. 52 bool UpdateShortcut(const ShortcutsBackend::Shortcut& shortcut); 53 54 // Deletes the ShortcutsProvider::Shortcuts with the id. 55 bool DeleteShortcutsWithIds(const std::vector<std::string>& shortcut_ids); 56 57 // Deletes the ShortcutsProvider::Shortcuts with the url. 58 bool DeleteShortcutsWithUrl(const std::string& shortcut_url_spec); 59 60 // Deletes all of the ShortcutsProvider::Shortcuts. 61 bool DeleteAllShortcuts(); 62 63 // Loads all of the shortcuts. 64 void LoadShortcuts(GuidToShortcutMap* shortcuts); 65 66 private: 67 friend class base::RefCountedThreadSafe<ShortcutsDatabase>; 68 friend class ShortcutsDatabaseTest; 69 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, AddShortcut); 70 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, UpdateShortcut); 71 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, DeleteShortcutsWithIds); 72 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, DeleteShortcutsWithUrl); 73 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, LoadShortcuts); 74 75 virtual ~ShortcutsDatabase(); 76 77 // Ensures that the table is present. 78 bool EnsureTable(); 79 80 // The sql database. Not valid until Init is called. 81 sql::Connection db_; 82 base::FilePath database_path_; 83 84 static const base::FilePath::CharType kShortcutsDatabaseName[]; 85 86 DISALLOW_COPY_AND_ASSIGN(ShortcutsDatabase); 87 }; 88 89 } // namespace history 90 91 #endif // CHROME_BROWSER_HISTORY_SHORTCUTS_DATABASE_H_ 92