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"
21 // This class manages the shortcut provider table within the SQLite database
22 // passed to the constructor. It expects the following schema:
24 // Note: The database stores time in seconds, UTC.
27 // id Unique id of the entry (needed for the sync).
28 // search_text Text that shortcuts was searched with.
29 // url The url of the shortcut.
30 // contents Contents of the original omni-box entry.
31 // contents_matches Comma separated matches of the |search_text| in
32 // |contents|, for example "0,0,5,3,9,0".
33 // description Description of the original omni-box entry.
34 // description_matches Comma separated matches of the |search_text| in
36 // last_access_time Time the entry was accessed last, stored in seconds,
38 // number_of_hits Number of times that the entry has been selected.
39 class ShortcutsDatabase : public base::RefCountedThreadSafe<ShortcutsDatabase> {
41 // The following struct encapsulates one previously selected omnibox shortcut.
43 // The fields of an AutocompleteMatch that we preserve in a shortcut.
45 MatchCore(const base::string16& fill_into_edit,
46 const GURL& destination_url,
47 const base::string16& contents,
48 const std::string& contents_class,
49 const base::string16& description,
50 const std::string& description_class,
53 const base::string16& keyword);
56 base::string16 fill_into_edit;
58 base::string16 contents;
59 // For both contents_class and description_class, we strip MATCH
60 // classifications; the ShortcutsProvider will re-mark MATCH regions based
61 // on the user's current typing.
62 std::string contents_class;
63 base::string16 description;
64 std::string description_class;
67 base::string16 keyword;
70 Shortcut(const std::string& id,
71 const base::string16& text,
72 const MatchCore& match_core,
73 const base::Time& last_access_time,
75 // Required for STL, we don't use this directly.
79 std::string id; // Unique guid for the shortcut.
80 base::string16 text; // The user's original input string.
82 base::Time last_access_time; // Last time shortcut was selected.
83 int number_of_hits; // How many times shortcut was selected.
86 typedef std::vector<std::string> ShortcutIDs;
87 typedef std::map<std::string, Shortcut> GuidToShortcutMap;
89 explicit ShortcutsDatabase(const base::FilePath& database_path);
93 // Adds the ShortcutsProvider::Shortcut to the database.
94 bool AddShortcut(const Shortcut& shortcut);
96 // Updates timing and selection count for the ShortcutsProvider::Shortcut.
97 bool UpdateShortcut(const Shortcut& shortcut);
99 // Deletes the ShortcutsProvider::Shortcuts with these IDs.
100 bool DeleteShortcutsWithIDs(const ShortcutIDs& shortcut_ids);
102 // Deletes the ShortcutsProvider::Shortcuts with the url.
103 bool DeleteShortcutsWithURL(const std::string& shortcut_url_spec);
105 // Deletes all of the ShortcutsProvider::Shortcuts.
106 bool DeleteAllShortcuts();
108 // Loads all of the shortcuts.
109 void LoadShortcuts(GuidToShortcutMap* shortcuts);
112 friend class base::RefCountedThreadSafe<ShortcutsDatabase>;
113 friend class ShortcutsDatabaseTest;
114 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, AddShortcut);
115 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, UpdateShortcut);
116 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, DeleteShortcutsWithIds);
117 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, DeleteShortcutsWithURL);
118 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest, LoadShortcuts);
120 virtual ~ShortcutsDatabase();
122 // Ensures that the table is present.
125 // The sql database. Not valid until Init is called.
127 base::FilePath database_path_;
129 static const base::FilePath::CharType kShortcutsDatabaseName[];
131 DISALLOW_COPY_AND_ASSIGN(ShortcutsDatabase);
134 } // namespace history
136 #endif // CHROME_BROWSER_HISTORY_SHORTCUTS_DATABASE_H_