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_BACKEND_H_
6 #define CHROME_BROWSER_AUTOCOMPLETE_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/scoped_observer.h"
18 #include "base/strings/string16.h"
19 #include "base/synchronization/lock.h"
20 #include "base/time/time.h"
21 #include "chrome/browser/autocomplete/shortcuts_database.h"
22 #include "components/history/core/browser/history_service_observer.h"
23 #include "components/keyed_service/core/refcounted_keyed_service.h"
24 #include "components/omnibox/autocomplete_match.h"
25 #include "content/public/browser/notification_observer.h"
26 #include "content/public/browser/notification_registrar.h"
32 class ShortcutsDatabase
;
33 }; // namespace history
35 // This class manages the shortcut provider backend - access to database on the
37 class ShortcutsBackend
: public RefcountedKeyedService
,
38 public content::NotificationObserver
,
39 public history::HistoryServiceObserver
{
41 typedef std::multimap
<base::string16
, const ShortcutsDatabase::Shortcut
>
44 // |profile| is necessary for profile notifications only and can be NULL in
45 // unit-tests. For unit testing, set |suppress_db| to true to prevent creation
46 // of the database, in which case all operations are performed in memory only.
47 ShortcutsBackend(Profile
* profile
, bool suppress_db
);
49 // The interface is guaranteed to be called on the thread AddObserver()
51 class ShortcutsBackendObserver
{
53 // Called after the database is loaded and Init() completed.
54 virtual void OnShortcutsLoaded() = 0;
55 // Called when shortcuts changed (added/updated/removed) in the database.
56 virtual void OnShortcutsChanged() {}
59 virtual ~ShortcutsBackendObserver() {}
62 // Asynchronously initializes the ShortcutsBackend, it is safe to call
63 // multiple times - only the first call will be processed.
66 // All of the public functions *must* be called on UI thread only!
68 bool initialized() const { return current_state_
== INITIALIZED
; }
69 const ShortcutMap
& shortcuts_map() const { return shortcuts_map_
; }
71 // Deletes the Shortcuts with the url.
72 bool DeleteShortcutsWithURL(const GURL
& shortcut_url
);
74 void AddObserver(ShortcutsBackendObserver
* obs
);
75 void RemoveObserver(ShortcutsBackendObserver
* obs
);
77 // Looks for an existing shortcut to match.destination_url that starts with
78 // |text|. Updates that shortcut if found, otherwise adds a new shortcut.
79 void AddOrUpdateShortcut(const base::string16
& text
,
80 const AutocompleteMatch
& match
);
83 friend class base::RefCountedThreadSafe
<ShortcutsBackend
>;
84 friend class ShortcutsProviderTest
;
85 friend class ShortcutsBackendTest
;
86 FRIEND_TEST_ALL_PREFIXES(ShortcutsBackendTest
, EntitySuggestionTest
);
89 NOT_INITIALIZED
, // Backend created but not initialized.
90 INITIALIZING
, // Init() called, but not completed yet.
91 INITIALIZED
, // Initialization completed, all accessors can be safely
95 typedef std::map
<std::string
, ShortcutMap::iterator
> GuidMap
;
97 ~ShortcutsBackend() override
;
99 static ShortcutsDatabase::Shortcut::MatchCore
MatchToMatchCore(
100 const AutocompleteMatch
& match
,
103 // RefcountedKeyedService:
104 void ShutdownOnUIThread() override
;
106 // content::NotificationObserver:
107 void Observe(int type
,
108 const content::NotificationSource
& source
,
109 const content::NotificationDetails
& details
) override
;
111 // history::HistoryServiceObserver:
112 void OnURLsDeleted(history::HistoryService
* history_service
,
115 const history::URLRows
& deleted_rows
,
116 const std::set
<GURL
>& favicon_urls
) override
;
118 // Internal initialization of the back-end. Posted by Init() to the DB thread.
119 // On completion posts InitCompleted() back to UI thread.
122 // Finishes initialization on UI thread, notifies all observers.
123 void InitCompleted();
125 // Adds the Shortcut to the database.
126 bool AddShortcut(const ShortcutsDatabase::Shortcut
& shortcut
);
128 // Updates timing and selection count for the Shortcut.
129 bool UpdateShortcut(const ShortcutsDatabase::Shortcut
& shortcut
);
131 // Deletes the Shortcuts with these IDs.
132 bool DeleteShortcutsWithIDs(
133 const ShortcutsDatabase::ShortcutIDs
& shortcut_ids
);
135 // Deletes all shortcuts whose URLs begin with |url|. If |exact_match| is
136 // true, only shortcuts from exactly |url| are deleted.
137 bool DeleteShortcutsWithURL(const GURL
& url
, bool exact_match
);
139 // Deletes all of the shortcuts.
140 bool DeleteAllShortcuts();
143 CurrentState current_state_
;
144 ObserverList
<ShortcutsBackendObserver
> observer_list_
;
145 scoped_refptr
<ShortcutsDatabase
> db_
;
147 // The |temp_shortcuts_map_| and |temp_guid_map_| used for temporary storage
148 // between InitInternal() and InitComplete() to avoid doing a potentially huge
150 scoped_ptr
<ShortcutMap
> temp_shortcuts_map_
;
151 scoped_ptr
<GuidMap
> temp_guid_map_
;
153 ShortcutMap shortcuts_map_
;
154 // This is a helper map for quick access to a shortcut by guid.
157 content::NotificationRegistrar notification_registrar_
;
158 ScopedObserver
<history::HistoryService
, history::HistoryServiceObserver
>
159 history_service_observer_
;
161 // For some unit-test only.
164 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackend
);
167 #endif // CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_BACKEND_H_