Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(295)

Side by Side Diff: chrome/browser/autocomplete/shortcuts_backend.h

Issue 1176153006: Componentize ShortcutsBackend (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shortcuts_backend_dependency_injection
Patch Set: Rebase Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/browser/autocomplete/shortcuts_backend.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
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_AUTOCOMPLETE_SHORTCUTS_BACKEND_H_
6 #define CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_BACKEND_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/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/autocomplete_match.h"
25 #include "components/omnibox/shortcuts_database.h"
26 #include "components/search_engines/search_terms_data.h"
27 #include "url/gurl.h"
28
29 class TemplateURLService;
30
31 namespace history {
32 class HistoryService;
33 class ShortcutsDatabase;
34 }; // namespace history
35
36 // This class manages the shortcut provider backend - access to database on the
37 // db thread, etc.
38 class ShortcutsBackend : public RefcountedKeyedService,
39 public history::HistoryServiceObserver {
40 public:
41 typedef std::multimap<base::string16, const ShortcutsDatabase::Shortcut>
42 ShortcutMap;
43
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,
51 bool suppress_db);
52
53 // The interface is guaranteed to be called on the thread AddObserver()
54 // was called.
55 class ShortcutsBackendObserver {
56 public:
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() {}
61
62 protected:
63 virtual ~ShortcutsBackendObserver() {}
64 };
65
66 // Asynchronously initializes the ShortcutsBackend, it is safe to call
67 // multiple times - only the first call will be processed.
68 bool Init();
69
70 // All of the public functions *must* be called on UI thread only!
71
72 bool initialized() const { return current_state_ == INITIALIZED; }
73 const ShortcutMap& shortcuts_map() const { return shortcuts_map_; }
74
75 // Deletes the Shortcuts with the url.
76 bool DeleteShortcutsWithURL(const GURL& shortcut_url);
77
78 // Deletes the Shortcuts that begin with the url.
79 bool DeleteShortcutsBeginningWithURL(const GURL& shortcut_url);
80
81 void AddObserver(ShortcutsBackendObserver* obs);
82 void RemoveObserver(ShortcutsBackendObserver* obs);
83
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);
88
89 private:
90 friend class base::RefCountedThreadSafe<ShortcutsBackend>;
91 friend class ShortcutsProviderTest;
92 friend class ShortcutsBackendTest;
93 FRIEND_TEST_ALL_PREFIXES(ShortcutsBackendTest, EntitySuggestionTest);
94
95 enum CurrentState {
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
99 // called.
100 };
101
102 typedef std::map<std::string, ShortcutMap::iterator> GuidMap;
103
104 ~ShortcutsBackend() override;
105
106 static ShortcutsDatabase::Shortcut::MatchCore MatchToMatchCore(
107 const AutocompleteMatch& match,
108 TemplateURLService* template_url_service,
109 SearchTermsData* search_terms_data);
110
111 // RefcountedKeyedService:
112 void ShutdownOnUIThread() override;
113
114 // history::HistoryServiceObserver:
115 void OnURLsDeleted(history::HistoryService* history_service,
116 bool all_history,
117 bool expired,
118 const history::URLRows& deleted_rows,
119 const std::set<GURL>& favicon_urls) override;
120
121 // Internal initialization of the back-end. Posted by Init() to the DB thread.
122 // On completion posts InitCompleted() back to UI thread.
123 void InitInternal();
124
125 // Finishes initialization on UI thread, notifies all observers.
126 void InitCompleted();
127
128 // Adds the Shortcut to the database.
129 bool AddShortcut(const ShortcutsDatabase::Shortcut& shortcut);
130
131 // Updates timing and selection count for the Shortcut.
132 bool UpdateShortcut(const ShortcutsDatabase::Shortcut& shortcut);
133
134 // Deletes the Shortcuts with these IDs.
135 bool DeleteShortcutsWithIDs(
136 const ShortcutsDatabase::ShortcutIDs& shortcut_ids);
137
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);
141
142 // Deletes all of the shortcuts.
143 bool DeleteAllShortcuts();
144
145 TemplateURLService* template_url_service_;
146 scoped_ptr<SearchTermsData> search_terms_data_;
147
148 CurrentState current_state_;
149 base::ObserverList<ShortcutsBackendObserver> observer_list_;
150 scoped_refptr<ShortcutsDatabase> db_;
151
152 // The |temp_shortcuts_map_| and |temp_guid_map_| used for temporary storage
153 // between InitInternal() and InitComplete() to avoid doing a potentially huge
154 // copy.
155 scoped_ptr<ShortcutMap> temp_shortcuts_map_;
156 scoped_ptr<GuidMap> temp_guid_map_;
157
158 ShortcutMap shortcuts_map_;
159 // This is a helper map for quick access to a shortcut by guid.
160 GuidMap guid_map_;
161
162 ScopedObserver<history::HistoryService, history::HistoryServiceObserver>
163 history_service_observer_;
164
165 scoped_refptr<base::SequencedTaskRunner> main_runner_;
166 scoped_refptr<base::SequencedTaskRunner> db_runner_;
167
168 // For some unit-test only.
169 bool no_db_access_;
170
171 DISALLOW_COPY_AND_ASSIGN(ShortcutsBackend);
172 };
173
174 #endif // CHROME_BROWSER_AUTOCOMPLETE_SHORTCUTS_BACKEND_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/autocomplete/shortcuts_backend.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698