1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/history/shortcuts_backend.h"
#include <map>
#include <string>
#include <vector>
#include "base/i18n/case_conversion.h"
#include "base/string_util.h"
#include "base/task.h"
#include "chrome/browser/autocomplete/autocomplete.h"
#include "chrome/browser/autocomplete/autocomplete_match.h"
#include "chrome/browser/history/history.h"
#include "chrome/browser/history/history_notifications.h"
#include "chrome/browser/history/shortcuts_database.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/chrome_notification_types.h"
#include "chrome/common/guid.h"
#include "content/browser/browser_thread.h"
#include "content/common/notification_details.h"
#include "content/common/notification_source.h"
namespace history {
ShortcutsBackend::ShortcutsBackend(const FilePath& db_folder_path,
Profile *profile)
: current_state_(NOT_INITIALIZED),
observer_list_(new ObserverListThreadSafe<ShortcutsBackendObserver>()),
db_(db_folder_path) {
// |profile| can be NULL in tests.
if (profile) {
notification_registrar_.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL,
Source<Profile>(profile));
notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URLS_DELETED,
Source<Profile>(profile));
}
}
ShortcutsBackend::~ShortcutsBackend() {}
bool ShortcutsBackend::Init() {
if (base::subtle::NoBarrier_CompareAndSwap(¤t_state_,
NOT_INITIALIZED,
INITIALIZING) == NOT_INITIALIZED) {
return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
NewRunnableMethod(this, &ShortcutsBackend::InitInternal));
} else {
return false;
}
}
bool ShortcutsBackend::AddShortcut(shortcuts_provider::Shortcut shortcut) {
// It is safe to add a shortcut while the backend is being initialized since
// the addition will occur on the same thread as the initialization.
DCHECK(current_state_ != NOT_INITIALIZED);
return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
NewRunnableMethod(this, &ShortcutsBackend::AddOrUpdateShortcutInternal,
shortcut, true));
}
bool ShortcutsBackend::UpdateShortcut(shortcuts_provider::Shortcut shortcut) {
DCHECK(initialized());
return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
NewRunnableMethod(this, &ShortcutsBackend::AddOrUpdateShortcutInternal,
shortcut, false));
}
bool ShortcutsBackend::DeleteShortcutsWithIds(
const std::vector<std::string>& shortcut_ids) {
DCHECK(initialized());
return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
NewRunnableMethod(this, &ShortcutsBackend::DeleteShortcutsWithIdsInternal,
shortcut_ids));
}
bool ShortcutsBackend::DeleteShortcutsWithUrl(const GURL& shortcut_url) {
DCHECK(initialized());
return BrowserThread::PostTask(BrowserThread::DB, FROM_HERE,
NewRunnableMethod(this, &ShortcutsBackend::DeleteShortcutsWithUrlInternal,
shortcut_url.spec()));
}
bool ShortcutsBackend::GetShortcuts(
shortcuts_provider::ShortcutMap* shortcuts) {
DCHECK(initialized());
DCHECK(shortcuts);
if (!initialized())
return false;
shortcuts->clear();
base::AutoLock lock(data_access_lock_);
shortcuts->insert(shortcuts_map_.begin(), shortcuts_map_.end());
return true;
}
void ShortcutsBackend::InitInternal() {
db_.Init();
shortcuts_provider::GuidToShortcutMap shortcuts;
db_.LoadShortcuts(&shortcuts);
for (shortcuts_provider::GuidToShortcutMap::iterator it = shortcuts.begin();
it != shortcuts.end(); ++it) {
guid_map_[it->first] = shortcuts_map_.insert(
std::make_pair(base::i18n::ToLower(it->second.text), it->second));
}
base::subtle::NoBarrier_CompareAndSwap(¤t_state_, INITIALIZING,
INITIALIZED);
observer_list_->Notify(&ShortcutsBackendObserver::OnShortcutsLoaded);
}
void ShortcutsBackend::AddOrUpdateShortcutInternal(
shortcuts_provider::Shortcut shortcut, bool add) {
{
// Update local copy.
base::AutoLock lock(data_access_lock_);
shortcuts_provider::GuidToShortcutsIteratorMap::iterator it =
guid_map_.find(shortcut.id);
if (it != guid_map_.end())
shortcuts_map_.erase(it->second);
guid_map_[shortcut.id] = shortcuts_map_.insert(
std::make_pair(base::i18n::ToLower(shortcut.text), shortcut));
}
if (add)
db_.AddShortcut(shortcut);
else
db_.UpdateShortcut(shortcut);
observer_list_->Notify(&ShortcutsBackendObserver::OnShortcutAddedOrUpdated,
shortcut);
}
void ShortcutsBackend::DeleteShortcutsWithIdsInternal(
std::vector<std::string> shortcut_ids) {
{
// Update local copy.
base::AutoLock lock(data_access_lock_);
for (size_t i = 0; i < shortcut_ids.size(); ++i) {
shortcuts_provider::GuidToShortcutsIteratorMap::iterator it =
guid_map_.find(shortcut_ids[i]);
if (it != guid_map_.end()) {
shortcuts_map_.erase(it->second);
guid_map_.erase(it);
}
}
}
db_.DeleteShortcutsWithIds(shortcut_ids);
observer_list_->Notify(&ShortcutsBackendObserver::OnShortcutsRemoved,
shortcut_ids);
}
void ShortcutsBackend::DeleteShortcutsWithUrlInternal(
std::string shortcut_url) {
std::vector<std::string> shortcut_ids;
{
// Update local copy.
base::AutoLock lock(data_access_lock_);
for (shortcuts_provider::GuidToShortcutsIteratorMap::iterator
it = guid_map_.begin();
it != guid_map_.end();) {
if (it->second->second.url.spec() == shortcut_url) {
shortcut_ids.push_back(it->first);
shortcuts_map_.erase(it->second);
guid_map_.erase(it++);
} else {
++it;
}
}
}
db_.DeleteShortcutsWithUrl(shortcut_url);
observer_list_->Notify(&ShortcutsBackendObserver::OnShortcutsRemoved,
shortcut_ids);
}
// NotificationObserver:
void ShortcutsBackend::Observe(int type,
const NotificationSource& source,
const NotificationDetails& details) {
if (type == chrome::NOTIFICATION_HISTORY_URLS_DELETED) {
const std::set<GURL>& urls =
Details<const history::URLsDeletedDetails>(details)->urls;
std::vector<std::string> shortcut_ids;
base::AutoLock lock(data_access_lock_);
for (shortcuts_provider::GuidToShortcutsIteratorMap::iterator
it = guid_map_.begin();
it != guid_map_.end(); ++it) {
if (urls.find(it->second->second.url) != urls.end())
shortcut_ids.push_back(it->first);
}
DeleteShortcutsWithIds(shortcut_ids);
return;
}
DCHECK(type == chrome::NOTIFICATION_OMNIBOX_OPENED_URL);
AutocompleteLog* log = Details<AutocompleteLog>(details).ptr();
string16 text_lowercase(base::i18n::ToLower(log->text));
int number_of_hits = 1;
std::string id;
const AutocompleteMatch& match(log->result.match_at(log->selected_index));
base::AutoLock lock(data_access_lock_);
for (shortcuts_provider::ShortcutMap::iterator it =
shortcuts_map_.lower_bound(text_lowercase);
it != shortcuts_map_.end() &&
StartsWith(it->first, text_lowercase, true); ++it) {
if (match.destination_url == it->second.url) {
number_of_hits = it->second.number_of_hits + 1;
id = it->second.id;
// guid_map_ will be updated further down on re-insertion.
shortcuts_map_.erase(it);
break;
}
}
shortcuts_provider::Shortcut shortcut(log->text, match.destination_url,
match.contents, match.contents_class, match.description,
match.description_class);
shortcut.number_of_hits = number_of_hits;
if (number_of_hits == 1)
shortcut.id = guid::GenerateGUID();
else
shortcut.id = id;
guid_map_[shortcut.id] = shortcuts_map_.insert(
std::make_pair(text_lowercase, shortcut));
if (number_of_hits == 1)
AddShortcut(shortcut);
else
UpdateShortcut(shortcut);
}
} // namespace history
|