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