a3cbfdb5b4820deb5f2df8b62a595b0569d8e71e
[platform/framework/web/crosswalk.git] / src / chrome / browser / history / shortcuts_database.h
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 "sql/connection.h"
17 #include "url/gurl.h"
18
19 namespace history {
20
21 // This class manages the shortcut provider table within the SQLite database
22 // passed to the constructor. It expects the following schema:
23 //
24 // Note: The database stores time in seconds, UTC.
25 //
26 // omni_box_shortcuts
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
35 //                       |description|.
36 //   last_access_time    Time the entry was accessed last, stored in seconds,
37 //                       UTC.
38 //   number_of_hits      Number of times that the entry has been selected.
39 class ShortcutsDatabase : public base::RefCountedThreadSafe<ShortcutsDatabase> {
40  public:
41   // The following struct encapsulates one previously selected omnibox shortcut.
42   struct Shortcut {
43     // The fields of an AutocompleteMatch that we preserve in a shortcut.
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.
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;
88
89   explicit ShortcutsDatabase(const base::FilePath& database_path);
90
91   bool Init();
92
93   // Adds the ShortcutsProvider::Shortcut to the database.
94   bool AddShortcut(const Shortcut& shortcut);
95
96   // Updates timing and selection count for the ShortcutsProvider::Shortcut.
97   bool UpdateShortcut(const Shortcut& shortcut);
98
99   // Deletes the ShortcutsProvider::Shortcuts with these IDs.
100   bool DeleteShortcutsWithIDs(const ShortcutIDs& shortcut_ids);
101
102   // Deletes the ShortcutsProvider::Shortcuts with the url.
103   bool DeleteShortcutsWithURL(const std::string& shortcut_url_spec);
104
105   // Deletes all of the ShortcutsProvider::Shortcuts.
106   bool DeleteAllShortcuts();
107
108   // Loads all of the shortcuts.
109   void LoadShortcuts(GuidToShortcutMap* shortcuts);
110
111  private:
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);
119
120   virtual ~ShortcutsDatabase();
121
122   // Ensures that the table is present.
123   bool EnsureTable();
124
125   // The sql database. Not valid until Init is called.
126   sql::Connection db_;
127   base::FilePath database_path_;
128
129   static const base::FilePath::CharType kShortcutsDatabaseName[];
130
131   DISALLOW_COPY_AND_ASSIGN(ShortcutsDatabase);
132 };
133
134 }  // namespace history
135
136 #endif  // CHROME_BROWSER_HISTORY_SHORTCUTS_DATABASE_H_