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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
|
// Copyright (c) 2009 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/extensions/extension_history_api.h"
#include "base/json/json_writer.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "base/task.h"
#include "base/values.h"
#include "chrome/browser/extensions/extension_history_api_constants.h"
#include "chrome/browser/extensions/extension_message_service.h"
#include "chrome/browser/history/history.h"
#include "chrome/browser/history/history_types.h"
#include "chrome/browser/profile.h"
#include "chrome/common/notification_type.h"
#include "chrome/common/notification_service.h"
namespace keys = extension_history_api_constants;
namespace {
double MilliSecondsFromTime(const base::Time& time) {
return 1000 * time.ToDoubleT();
}
void GetHistoryItemDictionary(const history::URLRow& row,
DictionaryValue* value) {
value->SetString(keys::kIdKey, Int64ToString(row.id()));
value->SetString(keys::kUrlKey, row.url().spec());
value->SetString(keys::kTitleKey, row.title());
value->SetReal(keys::kLastVisitdKey, MilliSecondsFromTime(row.last_visit()));
value->SetInteger(keys::kTypedCountKey, row.typed_count());
value->SetInteger(keys::kVisitCountKey, row.visit_count());
}
void AddHistoryNode(const history::URLRow& row, ListValue* list) {
DictionaryValue* dict = new DictionaryValue();
GetHistoryItemDictionary(row, dict);
list->Append(dict);
}
void GetVisitInfoDictionary(const history::VisitRow& row,
DictionaryValue* value) {
value->SetString(keys::kIdKey, Int64ToString(row.url_id));
value->SetString(keys::kVisitId, Int64ToString(row.visit_id));
value->SetReal(keys::kVisitTime, MilliSecondsFromTime(row.visit_time));
value->SetString(keys::kReferringVisitId,
Int64ToString(row.referring_visit));
value->SetInteger(keys::kTransition,
row.transition && PageTransition::CORE_MASK);
}
void AddVisitNode(const history::VisitRow& row, ListValue* list) {
DictionaryValue* dict = new DictionaryValue();
GetVisitInfoDictionary(row, dict);
list->Append(dict);
}
} // namespace
ExtensionHistoryEventRouter* ExtensionHistoryEventRouter::GetInstance() {
return Singleton<ExtensionHistoryEventRouter>::get();
}
void ExtensionHistoryEventRouter::ObserveProfile(Profile* profile) {
NotificationSource source = Source<Profile>(profile);
if (profiles_.find(source.map_key()) == profiles_.end())
profiles_[source.map_key()] = profile;
if (registrar_.IsEmpty()) {
registrar_.Add(this,
NotificationType::HISTORY_URL_VISITED,
NotificationService::AllSources());
registrar_.Add(this,
NotificationType::HISTORY_URLS_DELETED,
NotificationService::AllSources());
}
}
void ExtensionHistoryEventRouter::Observe(NotificationType type,
const NotificationSource& source,
const NotificationDetails& details) {
ProfileMap::iterator it = profiles_.find(source.map_key());
if (it != profiles_.end()) {
Profile* profile = it->second;
switch (type.value) {
case NotificationType::HISTORY_URL_VISITED:
HistoryUrlVisited(
profile,
Details<const history::URLVisitedDetails>(details).ptr());
break;
case NotificationType::HISTORY_URLS_DELETED:
HistoryUrlsRemoved(
profile,
Details<const history::URLsDeletedDetails>(details).ptr());
break;
default:
NOTREACHED();
}
}
}
void ExtensionHistoryEventRouter::HistoryUrlVisited(
Profile* profile,
const history::URLVisitedDetails* details) {
ListValue args;
DictionaryValue* dict = new DictionaryValue();
GetHistoryItemDictionary(details->row, dict);
args.Append(dict);
std::string json_args;
base::JSONWriter::Write(&args, false, &json_args);
DispatchEvent(profile, keys::kOnVisited, json_args);
}
void ExtensionHistoryEventRouter::HistoryUrlsRemoved(
Profile* profile,
const history::URLsDeletedDetails* details) {
ListValue args;
DictionaryValue* dict = new DictionaryValue();
dict->SetBoolean(keys::kAllHistoryKey, details->all_history);
ListValue* urls = new ListValue();
for (std::set<GURL>::const_iterator iterator = details->urls.begin();
iterator != details->urls.end();
++iterator) {
urls->Append(new StringValue(iterator->spec()));
}
dict->Set(keys::kUrlsKey, urls);
args.Append(dict);
std::string json_args;
base::JSONWriter::Write(&args, false, &json_args);
DispatchEvent(profile, keys::kOnVisitRemoved, json_args);
}
void ExtensionHistoryEventRouter::DispatchEvent(Profile* profile,
const char* event_name,
const std::string& json_args) {
if (profile && profile->GetExtensionMessageService()) {
profile->GetExtensionMessageService()->
DispatchEventToRenderers(event_name, json_args);
}
}
void HistoryFunction::Run() {
if (!RunImpl()) {
SendResponse(false);
}
}
bool HistoryFunction::GetUrlFromValue(Value* value, GURL* url) {
std::string url_string;
if (!value->GetAsString(&url_string)) {
bad_message_ = true;
return false;
}
GURL temp_url(url_string);
if (!temp_url.is_valid()) {
error_ = keys::kInvalidUrlError;
return false;
}
url->Swap(&temp_url);
return true;
}
bool HistoryFunction::GetTimeFromValue(Value* value, base::Time* time) {
double ms_from_epoch = 0;
if (!value->GetAsReal(&ms_from_epoch)) {
return false;
}
// The history service has seconds resolution, while javascript Date() has
// milliseconds resolution.
double seconds_from_epoch = ms_from_epoch / 1000;
*time = base::Time::FromDoubleT(seconds_from_epoch);
return true;
}
bool HistoryFunctionWithCallback::RunImpl() {
AddRef(); // Balanced in SendAysncRepose() and below.
bool retval = RunAsyncImpl();
if (false == retval)
Release();
return retval;
}
void HistoryFunctionWithCallback::SendAsyncResponse() {
MessageLoop::current()->PostTask(
FROM_HERE,
NewRunnableMethod(
this,
&HistoryFunctionWithCallback::SendResponseToCallback));
}
void HistoryFunctionWithCallback::SendResponseToCallback() {
SendResponse(true);
Release(); // Balanced in RunImpl().
}
bool GetVisitsHistoryFunction::RunAsyncImpl() {
EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
const DictionaryValue* json = args_as_dictionary();
Value* value;
EXTENSION_FUNCTION_VALIDATE(json->Get(keys::kUrlKey, &value));
GURL url;
if (!GetUrlFromValue(value, &url))
return false;
HistoryService* hs = profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
hs->QueryURL(url,
true, // Retrieve full history of a URL.
&cancelable_consumer_,
NewCallback(this, &GetVisitsHistoryFunction::QueryComplete));
return true;
}
void GetVisitsHistoryFunction::QueryComplete(
HistoryService::Handle request_service,
bool success,
const history::URLRow* url_row,
history::VisitVector* visits) {
ListValue* list = new ListValue();
if (visits && !visits->empty()) {
for (history::VisitVector::iterator iterator = visits->begin();
iterator != visits->end();
++iterator) {
AddVisitNode(*iterator, list);
}
}
result_.reset(list);
SendAsyncResponse();
}
bool SearchHistoryFunction::RunAsyncImpl() {
EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
const DictionaryValue* json = args_as_dictionary();
// Initialize the HistoryQuery
std::wstring search_text;
EXTENSION_FUNCTION_VALIDATE(json->GetString(keys::kSearchKey, &search_text));
history::QueryOptions options;
options.SetRecentDayRange(1);
options.max_count = 100;
if (json->HasKey(keys::kStartTimeKey)) { // Optional.
Value* value;
EXTENSION_FUNCTION_VALIDATE(json->Get(keys::kStartTimeKey, &value));
EXTENSION_FUNCTION_VALIDATE(GetTimeFromValue(value, &options.begin_time));
}
if (json->HasKey(keys::kEndTimeKey)) { // Optional.
Value* value;
EXTENSION_FUNCTION_VALIDATE(json->Get(keys::kEndTimeKey, &value));
EXTENSION_FUNCTION_VALIDATE(GetTimeFromValue(value, &options.end_time));
}
if (json->HasKey(keys::kMaxResultsKey)) { // Optional.
EXTENSION_FUNCTION_VALIDATE(json->GetInteger(keys::kMaxResultsKey,
&options.max_count));
}
HistoryService* hs = profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
hs->QueryHistory(search_text, options, &cancelable_consumer_,
NewCallback(this, &SearchHistoryFunction::SearchComplete));
return true;
}
void SearchHistoryFunction::SearchComplete(
HistoryService::Handle request_handle,
history::QueryResults* results) {
ListValue* list = new ListValue();
if (results && !results->empty()) {
for (history::QueryResults::URLResultVector::const_iterator iterator =
results->begin();
iterator != results->end();
++iterator) {
AddHistoryNode(**iterator, list);
}
}
result_.reset(list);
SendAsyncResponse();
}
bool AddUrlHistoryFunction::RunImpl() {
EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
const DictionaryValue* json = args_as_dictionary();
Value* value;
EXTENSION_FUNCTION_VALIDATE(json->Get(keys::kUrlKey, &value));
GURL url;
if (!GetUrlFromValue(value, &url))
return false;
HistoryService* hs = profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
hs->AddPage(url);
SendResponse(true);
return true;
}
bool DeleteUrlHistoryFunction::RunImpl() {
EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
const DictionaryValue* json = args_as_dictionary();
Value* value;
EXTENSION_FUNCTION_VALIDATE(json->Get(keys::kUrlKey, &value));
GURL url;
if (!GetUrlFromValue(value, &url))
return false;
HistoryService* hs = profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
hs->DeleteURL(url);
SendResponse(true);
return true;
}
bool DeleteRangeHistoryFunction::RunAsyncImpl() {
EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_DICTIONARY));
const DictionaryValue* json = args_as_dictionary();
Value* value = NULL;
EXTENSION_FUNCTION_VALIDATE(json->Get(keys::kStartTimeKey, &value));
base::Time begin_time;
EXTENSION_FUNCTION_VALIDATE(GetTimeFromValue(value, &begin_time));
EXTENSION_FUNCTION_VALIDATE(json->Get(keys::kEndTimeKey, &value));
base::Time end_time;
EXTENSION_FUNCTION_VALIDATE(GetTimeFromValue(value, &end_time));
HistoryService* hs = profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
hs->ExpireHistoryBetween(
begin_time,
end_time,
&cancelable_consumer_,
NewCallback(this, &DeleteRangeHistoryFunction::DeleteComplete));
return true;
}
void DeleteRangeHistoryFunction::DeleteComplete() {
SendAsyncResponse();
}
bool DeleteAllHistoryFunction::RunAsyncImpl() {
HistoryService* hs = profile()->GetHistoryService(Profile::EXPLICIT_ACCESS);
hs->ExpireHistoryBetween(
base::Time::FromDoubleT(0), // From the beginning of the epoch.
base::Time::Now(), // To the current time.
&cancelable_consumer_,
NewCallback(this, &DeleteAllHistoryFunction::DeleteComplete));
return true;
}
void DeleteAllHistoryFunction::DeleteComplete() {
SendAsyncResponse();
}
|