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_AUTOCOMPLETE_SHORTCUTS_DATABASE_H_
6 #define CHROME_BROWSER_AUTOCOMPLETE_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"
20 // This class manages the shortcut provider table within the SQLite database
21 // passed to the constructor. It expects the following schema:
23 // Note: The database stores time in seconds, UTC.
26 // id Unique id of the entry (needed for the sync).
27 // search_text Text that shortcuts was searched with.
28 // url The url of the shortcut.
29 // contents Contents of the original omni-box entry.
30 // contents_matches Comma separated matches of the |search_text| in
31 // |contents|, for example "0,0,5,3,9,0".
32 // description Description of the original omni-box entry.
33 // description_matches Comma separated matches of the |search_text| in
35 // last_access_time Time the entry was accessed last, stored in seconds,
37 // number_of_hits Number of times that the entry has been selected.
38 class ShortcutsDatabase
: public base::RefCountedThreadSafe
<ShortcutsDatabase
> {
40 // The following struct encapsulates one previously selected omnibox shortcut.
42 // The fields of an AutocompleteMatch that we preserve in a shortcut.
44 MatchCore(const base::string16
& fill_into_edit
,
45 const GURL
& destination_url
,
46 const base::string16
& contents
,
47 const std::string
& contents_class
,
48 const base::string16
& description
,
49 const std::string
& description_class
,
52 const base::string16
& keyword
);
55 base::string16 fill_into_edit
;
57 base::string16 contents
;
58 // For both contents_class and description_class, we strip MATCH
59 // classifications; the ShortcutsProvider will re-mark MATCH regions based
60 // on the user's current typing.
61 std::string contents_class
;
62 base::string16 description
;
63 std::string description_class
;
66 base::string16 keyword
;
69 Shortcut(const std::string
& id
,
70 const base::string16
& text
,
71 const MatchCore
& match_core
,
72 const base::Time
& last_access_time
,
74 // Required for STL, we don't use this directly.
78 std::string id
; // Unique guid for the shortcut.
79 base::string16 text
; // The user's original input string.
81 base::Time last_access_time
; // Last time shortcut was selected.
82 int number_of_hits
; // How many times shortcut was selected.
85 typedef std::vector
<std::string
> ShortcutIDs
;
86 typedef std::map
<std::string
, Shortcut
> GuidToShortcutMap
;
88 explicit ShortcutsDatabase(const base::FilePath
& database_path
);
92 // Adds the ShortcutsProvider::Shortcut to the database.
93 bool AddShortcut(const Shortcut
& shortcut
);
95 // Updates timing and selection count for the ShortcutsProvider::Shortcut.
96 bool UpdateShortcut(const Shortcut
& shortcut
);
98 // Deletes the ShortcutsProvider::Shortcuts with these IDs.
99 bool DeleteShortcutsWithIDs(const ShortcutIDs
& shortcut_ids
);
101 // Deletes the ShortcutsProvider::Shortcuts with the url.
102 bool DeleteShortcutsWithURL(const std::string
& shortcut_url_spec
);
104 // Deletes all of the ShortcutsProvider::Shortcuts.
105 bool DeleteAllShortcuts();
107 // Loads all of the shortcuts.
108 void LoadShortcuts(GuidToShortcutMap
* shortcuts
);
111 friend class base::RefCountedThreadSafe
<ShortcutsDatabase
>;
112 friend class ShortcutsDatabaseTest
;
113 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest
, AddShortcut
);
114 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest
, UpdateShortcut
);
115 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest
, DeleteShortcutsWithIds
);
116 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest
, DeleteShortcutsWithURL
);
117 FRIEND_TEST_ALL_PREFIXES(ShortcutsDatabaseTest
, LoadShortcuts
);
119 virtual ~ShortcutsDatabase();
121 // Ensures that the table is present.
124 // The sql database. Not valid until Init is called.
126 base::FilePath database_path_
;
128 sql::MetaTable meta_table_
;
130 DISALLOW_COPY_AND_ASSIGN(ShortcutsDatabase
);
133 #endif // CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_DATABASE_H_