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.
5 #ifndef CHROME_BROWSER_HISTORY_SHORTCUTS_DATABASE_H_
6 #define CHROME_BROWSER_HISTORY_SHORTCUTS_DATABASE_H_
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"
22 // This class manages the shortcut provider table within the SQLite database
23 // passed to the constructor. It expects the following schema:
25 // Note: The database stores time in seconds, UTC.
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
37 // last_access_time Time the entry was accessed last, stored in seconds,
39 // number_of_hits Number of times that the entry has been selected.
40 class ShortcutsDatabase : public base::RefCountedThreadSafe<ShortcutsDatabase> {
42 // The following struct encapsulates one previously selected omnibox shortcut.
44 // The fields of an AutocompleteMatch that we preserve in a shortcut.
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,
54 const base::string16& keyword);
57 base::string16 fill_into_edit;
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;
68 base::string16 keyword;
71 Shortcut(const std::string& id,
72 const base::string16& text,
73 const MatchCore& match_core,
74 const base::Time& last_access_time,
76 // Required for STL, we don't use this directly.
80 std::string id; // Unique guid for the shortcut.
81 base::string16 text; // The user's original input string.
83 base::Time last_access_time; // Last time shortcut was selected.
84 int number_of_hits; // How many times shortcut was selected.
87 typedef std::vector<std::string> ShortcutIDs;
88 typedef std::map<std::string, Shortcut> GuidToShortcutMap;
90 explicit ShortcutsDatabase(const base::FilePath& database_path);
94 // Adds the ShortcutsProvider::Shortcut to the database.
95 bool AddShortcut(const Shortcut& shortcut);
97 // Updates timing and selection count for the ShortcutsProvider::Shortcut.
98 bool UpdateShortcut(const Shortcut& shortcut);
100 // Deletes the ShortcutsProvider::Shortcuts with these IDs.
101 bool DeleteShortcutsWithIDs(const ShortcutIDs& shortcut_ids);
103 // Deletes the ShortcutsProvider::Shortcuts with the url.
104 bool DeleteShortcutsWithURL(const std::string& shortcut_url_spec);
106 // Deletes all of the ShortcutsProvider::Shortcuts.
107 bool DeleteAllShortcuts();
109 // Loads all of the shortcuts.
110 void LoadShortcuts(GuidToShortcutMap* shortcuts);
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);
121 virtual ~ShortcutsDatabase();
123 // Ensures that the table is present.
126 // The sql database. Not valid until Init is called.
128 base::FilePath database_path_;
130 sql::MetaTable meta_table_;
132 static const base::FilePath::CharType kShortcutsDatabaseName[];
134 DISALLOW_COPY_AND_ASSIGN(ShortcutsDatabase);
137 } // namespace history
139 #endif // CHROME_BROWSER_HISTORY_SHORTCUTS_DATABASE_H_