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_BACKEND_H_
6 #define CHROME_BROWSER_HISTORY_SHORTCUTS_BACKEND_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/memory/scoped_ptr.h"
16 #include "base/observer_list.h"
17 #include "base/strings/string16.h"
18 #include "base/synchronization/lock.h"
19 #include "base/time/time.h"
20 #include "chrome/browser/autocomplete/autocomplete_match.h"
21 #include "components/browser_context_keyed_service/refcounted_browser_context_keyed_service.h"
22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h"
30 class ShortcutsDatabase;
32 // This class manages the shortcut provider backend - access to database on the
34 class ShortcutsBackend : public RefcountedBrowserContextKeyedService,
35 public content::NotificationObserver {
37 // The following struct encapsulates one previously selected omnibox shortcut.
39 // The pieces of an AutocompleteMatch that we preserve in a shortcut.
41 explicit MatchCore(const AutocompleteMatch& match);
42 MatchCore(const string16& fill_into_edit,
43 const GURL& destination_url,
44 const string16& contents,
45 const ACMatchClassifications& contents_class,
46 const string16& description,
47 const ACMatchClassifications& description_class,
48 content::PageTransition transition,
49 AutocompleteMatch::Type type,
50 const string16& keyword);
53 AutocompleteMatch ToMatch() const;
55 string16 fill_into_edit;
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 ACMatchClassifications contents_class;
63 ACMatchClassifications description_class;
64 content::PageTransition transition;
65 AutocompleteMatch::Type type;
69 Shortcut(const std::string& id,
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 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::multimap<string16, const Shortcut> ShortcutMap;
87 // |profile| is necessary for profile notifications only and can be NULL in
88 // unit-tests. For unit testing, set |suppress_db| to true to prevent creation
89 // of the database, in which case all operations are performed in memory only.
90 ShortcutsBackend(Profile* profile, bool suppress_db);
92 // The interface is guaranteed to be called on the thread AddObserver()
94 class ShortcutsBackendObserver {
96 // Called after the database is loaded and Init() completed.
97 virtual void OnShortcutsLoaded() = 0;
98 // Called when shortcuts changed (added/updated/removed) in the database.
99 virtual void OnShortcutsChanged() {}
102 virtual ~ShortcutsBackendObserver() {}
105 // Asynchronously initializes the ShortcutsBackend, it is safe to call
106 // multiple times - only the first call will be processed.
109 // All of the public functions *must* be called on UI thread only!
111 bool initialized() const { return current_state_ == INITIALIZED; }
112 const ShortcutMap& shortcuts_map() const { return shortcuts_map_; }
114 // Deletes the Shortcuts with the url.
115 bool DeleteShortcutsWithUrl(const GURL& shortcut_url);
117 void AddObserver(ShortcutsBackendObserver* obs);
118 void RemoveObserver(ShortcutsBackendObserver* obs);
120 // Called when a successful omnibox navigation occurs. Adds a corresponding
122 void OnOmniboxNavigation(const string16& text,
123 const AutocompleteMatch& match);
126 friend class base::RefCountedThreadSafe<ShortcutsBackend>;
127 friend class ShortcutsProviderTest;
128 FRIEND_TEST_ALL_PREFIXES(ShortcutsBackendTest, AddAndUpdateShortcut);
129 FRIEND_TEST_ALL_PREFIXES(ShortcutsBackendTest, DeleteShortcuts);
132 NOT_INITIALIZED, // Backend created but not initialized.
133 INITIALIZING, // Init() called, but not completed yet.
134 INITIALIZED, // Initialization completed, all accessors can be safely
138 typedef std::map<std::string, ShortcutMap::iterator> GuidMap;
140 virtual ~ShortcutsBackend();
142 // RefcountedBrowserContextKeyedService:
143 virtual void ShutdownOnUIThread() OVERRIDE;
145 // content::NotificationObserver:
146 virtual void Observe(int type,
147 const content::NotificationSource& source,
148 const content::NotificationDetails& details) OVERRIDE;
150 // Internal initialization of the back-end. Posted by Init() to the DB thread.
151 // On completion posts InitCompleted() back to UI thread.
154 // Finishes initialization on UI thread, notifies all observers.
155 void InitCompleted();
157 // Adds the Shortcut to the database.
158 bool AddShortcut(const Shortcut& shortcut);
160 // Updates timing and selection count for the Shortcut.
161 bool UpdateShortcut(const Shortcut& shortcut);
163 // Deletes the Shortcuts with the id.
164 bool DeleteShortcutsWithIds(const std::vector<std::string>& shortcut_ids);
166 // Deletes all shortcuts whose URLs begin with |url|. If |exact_match| is
167 // true, only shortcuts from exactly |url| are deleted.
168 bool DeleteShortcutsWithUrl(const GURL& url, bool exact_match);
170 // Deletes all of the shortcuts.
171 bool DeleteAllShortcuts();
173 CurrentState current_state_;
174 ObserverList<ShortcutsBackendObserver> observer_list_;
175 scoped_refptr<ShortcutsDatabase> db_;
177 // The |temp_shortcuts_map_| and |temp_guid_map_| used for temporary storage
178 // between InitInternal() and InitComplete() to avoid doing a potentially huge
180 scoped_ptr<ShortcutMap> temp_shortcuts_map_;
181 scoped_ptr<GuidMap> temp_guid_map_;
183 ShortcutMap shortcuts_map_;
184 // This is a helper map for quick access to a shortcut by guid.
187 content::NotificationRegistrar notification_registrar_;
189 // For some unit-test only.
192 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackend);
195 } // namespace history
197 #endif // CHROME_BROWSER_HISTORY_SHORTCUTS_BACKEND_H_