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 COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_BACKEND_H_
6 #define COMPONENTS_OMNIBOX_BROWSER_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/sequenced_task_runner.h"
19 #include "base/strings/string16.h"
20 #include "base/synchronization/lock.h"
21 #include "base/time/time.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/browser/autocomplete_match.h"
25 #include "components/omnibox/browser/shortcuts_database.h"
26 #include "components/search_engines/search_terms_data.h"
29 class TemplateURLService
;
33 class ShortcutsDatabase
;
34 }; // namespace history
36 // This class manages the shortcut provider backend - access to database on the
38 class ShortcutsBackend
: public RefcountedKeyedService
,
39 public history::HistoryServiceObserver
{
41 typedef std::multimap
<base::string16
, const ShortcutsDatabase::Shortcut
>
44 // For unit testing, set |suppress_db| to true to prevent creation
45 // of the database, in which case all operations are performed in memory only.
46 ShortcutsBackend(TemplateURLService
* template_url_service
,
47 scoped_ptr
<SearchTermsData
> search_terms_data
,
48 history::HistoryService
* history_service
,
49 scoped_refptr
<base::SequencedTaskRunner
> db_runner
,
50 base::FilePath database_path
,
53 // The interface is guaranteed to be called on the thread AddObserver()
55 class ShortcutsBackendObserver
{
57 // Called after the database is loaded and Init() completed.
58 virtual void OnShortcutsLoaded() = 0;
59 // Called when shortcuts changed (added/updated/removed) in the database.
60 virtual void OnShortcutsChanged() {}
63 virtual ~ShortcutsBackendObserver() {}
66 // Asynchronously initializes the ShortcutsBackend, it is safe to call
67 // multiple times - only the first call will be processed.
70 // All of the public functions *must* be called on UI thread only!
72 bool initialized() const { return current_state_
== INITIALIZED
; }
73 const ShortcutMap
& shortcuts_map() const { return shortcuts_map_
; }
75 // Deletes the Shortcuts with the url.
76 bool DeleteShortcutsWithURL(const GURL
& shortcut_url
);
78 // Deletes the Shortcuts that begin with the url.
79 bool DeleteShortcutsBeginningWithURL(const GURL
& shortcut_url
);
81 void AddObserver(ShortcutsBackendObserver
* obs
);
82 void RemoveObserver(ShortcutsBackendObserver
* obs
);
84 // Looks for an existing shortcut to match.destination_url that starts with
85 // |text|. Updates that shortcut if found, otherwise adds a new shortcut.
86 void AddOrUpdateShortcut(const base::string16
& text
,
87 const AutocompleteMatch
& match
);
90 friend class base::RefCountedThreadSafe
<ShortcutsBackend
>;
91 friend class ShortcutsProviderTest
;
92 friend class ShortcutsBackendTest
;
93 FRIEND_TEST_ALL_PREFIXES(ShortcutsBackendTest
, EntitySuggestionTest
);
96 NOT_INITIALIZED
, // Backend created but not initialized.
97 INITIALIZING
, // Init() called, but not completed yet.
98 INITIALIZED
, // Initialization completed, all accessors can be safely
102 typedef std::map
<std::string
, ShortcutMap::iterator
> GuidMap
;
104 ~ShortcutsBackend() override
;
106 static ShortcutsDatabase::Shortcut::MatchCore
MatchToMatchCore(
107 const AutocompleteMatch
& match
,
108 TemplateURLService
* template_url_service
,
109 SearchTermsData
* search_terms_data
);
111 // RefcountedKeyedService:
112 void ShutdownOnUIThread() override
;
114 // history::HistoryServiceObserver:
115 void OnURLsDeleted(history::HistoryService
* history_service
,
118 const history::URLRows
& deleted_rows
,
119 const std::set
<GURL
>& favicon_urls
) override
;
121 // Internal initialization of the back-end. Posted by Init() to the DB thread.
122 // On completion posts InitCompleted() back to UI thread.
125 // Finishes initialization on UI thread, notifies all observers.
126 void InitCompleted();
128 // Adds the Shortcut to the database.
129 bool AddShortcut(const ShortcutsDatabase::Shortcut
& shortcut
);
131 // Updates timing and selection count for the Shortcut.
132 bool UpdateShortcut(const ShortcutsDatabase::Shortcut
& shortcut
);
134 // Deletes the Shortcuts with these IDs.
135 bool DeleteShortcutsWithIDs(
136 const ShortcutsDatabase::ShortcutIDs
& shortcut_ids
);
138 // Deletes all shortcuts whose URLs begin with |url|. If |exact_match| is
139 // true, only shortcuts from exactly |url| are deleted.
140 bool DeleteShortcutsWithURL(const GURL
& url
, bool exact_match
);
142 // Deletes all of the shortcuts.
143 bool DeleteAllShortcuts();
145 TemplateURLService
* template_url_service_
;
146 scoped_ptr
<SearchTermsData
> search_terms_data_
;
148 CurrentState current_state_
;
149 base::ObserverList
<ShortcutsBackendObserver
> observer_list_
;
150 scoped_refptr
<ShortcutsDatabase
> db_
;
152 // The |temp_shortcuts_map_| and |temp_guid_map_| used for temporary storage
153 // between InitInternal() and InitComplete() to avoid doing a potentially huge
155 scoped_ptr
<ShortcutMap
> temp_shortcuts_map_
;
156 scoped_ptr
<GuidMap
> temp_guid_map_
;
158 ShortcutMap shortcuts_map_
;
159 // This is a helper map for quick access to a shortcut by guid.
162 ScopedObserver
<history::HistoryService
, history::HistoryServiceObserver
>
163 history_service_observer_
;
165 scoped_refptr
<base::SequencedTaskRunner
> main_runner_
;
166 scoped_refptr
<base::SequencedTaskRunner
> db_runner_
;
168 // For some unit-test only.
171 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackend
);
174 #endif // COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_BACKEND_H_