OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/history/history_extension_api.h" | 5 #include "chrome/browser/history/history_extension_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
12 #include "base/string_number_conversions.h" | 12 #include "base/string_number_conversions.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "chrome/browser/extensions/extension_event_router.h" | 14 #include "chrome/browser/extensions/extension_event_router.h" |
15 #include "chrome/browser/history/history.h" | 15 #include "chrome/browser/history/history.h" |
16 #include "chrome/browser/history/history_types.h" | 16 #include "chrome/browser/history/history_types.h" |
| 17 #include "chrome/browser/history/time_filter.h" |
17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/common/chrome_notification_types.h" | 19 #include "chrome/common/chrome_notification_types.h" |
19 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
20 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 const char kAllHistoryKey[] = "allHistory"; | 25 const char kAllHistoryKey[] = "allHistory"; |
| 26 const char kDayOfTheWeekKey[] = "dayOfTheWeek"; |
25 const char kEndTimeKey[] = "endTime"; | 27 const char kEndTimeKey[] = "endTime"; |
26 const char kFaviconUrlKey[] = "favIconUrl"; | 28 const char kFaviconUrlKey[] = "favIconUrl"; |
27 const char kIdKey[] = "id"; | 29 const char kIdKey[] = "id"; |
28 const char kLastVisitdKey[] = "lastVisitTime"; | 30 const char kLastVisitdKey[] = "lastVisitTime"; |
29 const char kMaxResultsKey[] = "maxResults"; | 31 const char kMaxResultsKey[] = "maxResults"; |
30 const char kNewKey[] = "new"; | 32 const char kNewKey[] = "new"; |
31 const char kReferringVisitId[] = "referringVisitId"; | 33 const char kReferringVisitId[] = "referringVisitId"; |
32 const char kRemovedKey[] = "removed"; | 34 const char kRemovedKey[] = "removed"; |
33 const char kStartTimeKey[] = "startTime"; | 35 const char kStartTimeKey[] = "startTime"; |
34 const char kTextKey[] = "text"; | 36 const char kTextKey[] = "text"; |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 MessageLoop::current()->PostTask( | 223 MessageLoop::current()->PostTask( |
222 FROM_HERE, | 224 FROM_HERE, |
223 base::Bind(&HistoryFunctionWithCallback::SendResponseToCallback, this)); | 225 base::Bind(&HistoryFunctionWithCallback::SendResponseToCallback, this)); |
224 } | 226 } |
225 | 227 |
226 void HistoryFunctionWithCallback::SendResponseToCallback() { | 228 void HistoryFunctionWithCallback::SendResponseToCallback() { |
227 SendResponse(true); | 229 SendResponse(true); |
228 Release(); // Balanced in RunImpl(). | 230 Release(); // Balanced in RunImpl(). |
229 } | 231 } |
230 | 232 |
| 233 bool GetMostVisitedHistoryFunction::RunAsyncImpl() { |
| 234 DictionaryValue* json; |
| 235 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &json)); |
| 236 |
| 237 history::TimeFilter time_filter; |
| 238 int max_results = 100; |
| 239 if (json->HasKey(kStartTimeKey)) { // Optional. |
| 240 Value* value; |
| 241 base::Time start_time; |
| 242 EXTENSION_FUNCTION_VALIDATE(json->Get(kStartTimeKey, &value)); |
| 243 EXTENSION_FUNCTION_VALIDATE(GetTimeFromValue(value, &start_time)); |
| 244 if (json->HasKey(kEndTimeKey)) { |
| 245 base::Time end_time; |
| 246 EXTENSION_FUNCTION_VALIDATE(json->Get(kEndTimeKey, &value)); |
| 247 EXTENSION_FUNCTION_VALIDATE(GetTimeFromValue(value, &end_time)); |
| 248 time_filter.SetTimeInRangeFilter(start_time, end_time); |
| 249 } |
| 250 } |
| 251 if (json->HasKey(kDayOfTheWeekKey)) { // Optional. |
| 252 int day; |
| 253 EXTENSION_FUNCTION_VALIDATE(json->GetInteger(kDayOfTheWeekKey, &day)); |
| 254 time_filter.SetDayOfTheWeekFilter(day, base::Time::Now()); |
| 255 } |
| 256 if (json->HasKey(kMaxResultsKey)) { // Optional. |
| 257 EXTENSION_FUNCTION_VALIDATE(json->GetInteger(kMaxResultsKey, &max_results)); |
| 258 } |
| 259 HistoryService* hs = profile()->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 260 hs->QueryMostVisitedURLsDuringTime( |
| 261 max_results, time_filter, &cancelable_consumer_, |
| 262 base::Bind(&GetMostVisitedHistoryFunction::QueryComplete, |
| 263 base::Unretained(this))); |
| 264 return true; |
| 265 } |
| 266 |
| 267 void GetMostVisitedHistoryFunction::QueryComplete( |
| 268 CancelableRequestProvider::Handle handle, |
| 269 history::MostVisitedURLList data) { |
| 270 ListValue* list = new ListValue(); |
| 271 for (size_t i = 0; i < data.size(); i++) { |
| 272 const history::MostVisitedURL& queried_url = data[i]; |
| 273 if (queried_url.url.is_empty() && queried_url.title.empty()) |
| 274 continue; |
| 275 DictionaryValue* page_value = new DictionaryValue(); |
| 276 // TODO(georgey) add a real id. |
| 277 page_value->SetInteger("id", 0); |
| 278 page_value->SetString("url", queried_url.url.spec()); |
| 279 page_value->SetString("title", queried_url.title); |
| 280 list->Append(page_value); |
| 281 } |
| 282 result_.reset(list); |
| 283 SendAsyncResponse(); |
| 284 } |
| 285 |
231 bool GetVisitsHistoryFunction::RunAsyncImpl() { | 286 bool GetVisitsHistoryFunction::RunAsyncImpl() { |
232 DictionaryValue* json; | 287 DictionaryValue* json; |
233 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &json)); | 288 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &json)); |
234 | 289 |
235 Value* value; | 290 Value* value; |
236 EXTENSION_FUNCTION_VALIDATE(json->Get(kUrlKey, &value)); | 291 EXTENSION_FUNCTION_VALIDATE(json->Get(kUrlKey, &value)); |
237 | 292 |
238 GURL url; | 293 GURL url; |
239 if (!GetUrlFromValue(value, &url)) | 294 if (!GetUrlFromValue(value, &url)) |
240 return false; | 295 return false; |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
393 &cancelable_consumer_, | 448 &cancelable_consumer_, |
394 base::Bind(&DeleteAllHistoryFunction::DeleteComplete, | 449 base::Bind(&DeleteAllHistoryFunction::DeleteComplete, |
395 base::Unretained(this))); | 450 base::Unretained(this))); |
396 | 451 |
397 return true; | 452 return true; |
398 } | 453 } |
399 | 454 |
400 void DeleteAllHistoryFunction::DeleteComplete() { | 455 void DeleteAllHistoryFunction::DeleteComplete() { |
401 SendAsyncResponse(); | 456 SendAsyncResponse(); |
402 } | 457 } |
OLD | NEW |