| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/extensions/api/history/history_api.h" | 5 #include "chrome/browser/extensions/api/history/history_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/command_line.h" |
| 10 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
| 11 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 14 #include "base/prefs/pref_service.h" | 15 #include "base/prefs/pref_service.h" |
| 15 #include "base/strings/string_number_conversions.h" | 16 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 18 #include "base/values.h" | 19 #include "base/values.h" |
| 19 #include "chrome/browser/chrome_notification_types.h" | 20 #include "chrome/browser/chrome_notification_types.h" |
| 21 #include "chrome/browser/extensions/activity_log/activity_log.h" |
| 20 #include "chrome/browser/extensions/event_router.h" | 22 #include "chrome/browser/extensions/event_router.h" |
| 21 #include "chrome/browser/extensions/extension_system.h" | 23 #include "chrome/browser/extensions/extension_system.h" |
| 22 #include "chrome/browser/history/history_service.h" | 24 #include "chrome/browser/history/history_service.h" |
| 23 #include "chrome/browser/history/history_service_factory.h" | 25 #include "chrome/browser/history/history_service_factory.h" |
| 24 #include "chrome/browser/history/history_types.h" | 26 #include "chrome/browser/history/history_types.h" |
| 25 #include "chrome/browser/history/visit_filter.h" | 27 #include "chrome/browser/history/visit_filter.h" |
| 26 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
| 27 #include "chrome/common/cancelable_task_tracker.h" | 29 #include "chrome/common/cancelable_task_tracker.h" |
| 30 #include "chrome/common/chrome_switches.h" |
| 28 #include "chrome/common/extensions/api/experimental_history.h" | 31 #include "chrome/common/extensions/api/experimental_history.h" |
| 29 #include "chrome/common/extensions/api/history.h" | 32 #include "chrome/common/extensions/api/history.h" |
| 30 #include "chrome/common/pref_names.h" | 33 #include "chrome/common/pref_names.h" |
| 31 #include "content/public/browser/notification_details.h" | 34 #include "content/public/browser/notification_details.h" |
| 32 #include "content/public/browser/notification_source.h" | 35 #include "content/public/browser/notification_source.h" |
| 33 | 36 |
| 34 namespace extensions { | 37 namespace extensions { |
| 35 | 38 |
| 36 using api::experimental_history::MostVisitedItem; | 39 using api::experimental_history::MostVisitedItem; |
| 37 using api::history::HistoryItem; | 40 using api::history::HistoryItem; |
| 38 using api::history::VisitItem; | 41 using api::history::VisitItem; |
| 42 using extensions::ActivityLog; |
| 39 | 43 |
| 40 typedef std::vector<linked_ptr<api::history::HistoryItem> > | 44 typedef std::vector<linked_ptr<api::history::HistoryItem> > |
| 41 HistoryItemList; | 45 HistoryItemList; |
| 42 typedef std::vector<linked_ptr<api::history::VisitItem> > | 46 typedef std::vector<linked_ptr<api::history::VisitItem> > |
| 43 VisitItemList; | 47 VisitItemList; |
| 44 | 48 |
| 45 namespace AddUrl = api::history::AddUrl; | 49 namespace AddUrl = api::history::AddUrl; |
| 46 namespace DeleteUrl = api::history::DeleteUrl; | 50 namespace DeleteUrl = api::history::DeleteUrl; |
| 47 namespace DeleteRange = api::history::DeleteRange; | 51 namespace DeleteRange = api::history::DeleteRange; |
| 48 namespace GetMostVisited = api::experimental_history::GetMostVisited; | 52 namespace GetMostVisited = api::experimental_history::GetMostVisited; |
| (...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 } | 220 } |
| 217 | 221 |
| 218 static base::LazyInstance<ProfileKeyedAPIFactory<HistoryAPI> > | 222 static base::LazyInstance<ProfileKeyedAPIFactory<HistoryAPI> > |
| 219 g_factory = LAZY_INSTANCE_INITIALIZER; | 223 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 220 | 224 |
| 221 // static | 225 // static |
| 222 ProfileKeyedAPIFactory<HistoryAPI>* HistoryAPI::GetFactoryInstance() { | 226 ProfileKeyedAPIFactory<HistoryAPI>* HistoryAPI::GetFactoryInstance() { |
| 223 return &g_factory.Get(); | 227 return &g_factory.Get(); |
| 224 } | 228 } |
| 225 | 229 |
| 230 template<> |
| 231 void ProfileKeyedAPIFactory<HistoryAPI>::DeclareFactoryDependencies() { |
| 232 DependsOn(ActivityLogFactory::GetInstance()); |
| 233 } |
| 234 |
| 226 void HistoryAPI::OnListenerAdded(const EventListenerInfo& details) { | 235 void HistoryAPI::OnListenerAdded(const EventListenerInfo& details) { |
| 227 history_event_router_.reset(new HistoryEventRouter(profile_)); | 236 history_event_router_.reset(new HistoryEventRouter(profile_)); |
| 228 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); | 237 ExtensionSystem::Get(profile_)->event_router()->UnregisterObserver(this); |
| 229 } | 238 } |
| 230 | 239 |
| 231 void HistoryFunction::Run() { | 240 void HistoryFunction::Run() { |
| 232 if (!RunImpl()) { | 241 if (!RunImpl()) { |
| 233 SendResponse(false); | 242 SendResponse(false); |
| 234 } | 243 } |
| 235 } | 244 } |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 437 | 446 |
| 438 GURL url; | 447 GURL url; |
| 439 if (!ValidateUrl(params->details.url, &url)) | 448 if (!ValidateUrl(params->details.url, &url)) |
| 440 return false; | 449 return false; |
| 441 | 450 |
| 442 HistoryService* hs = | 451 HistoryService* hs = |
| 443 HistoryServiceFactory::GetForProfile(profile(), | 452 HistoryServiceFactory::GetForProfile(profile(), |
| 444 Profile::EXPLICIT_ACCESS); | 453 Profile::EXPLICIT_ACCESS); |
| 445 hs->DeleteURL(url); | 454 hs->DeleteURL(url); |
| 446 | 455 |
| 456 // Also clean out from the activity log. If the activity log testing flag is |
| 457 // set then don't clean so testers can see what potentially malicious |
| 458 // extensions have been trying to clean from their logs. |
| 459 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 460 switches::kEnableExtensionActivityLogTesting)) { |
| 461 ActivityLog* activity_log = ActivityLog::GetInstance(profile_); |
| 462 DCHECK(activity_log); |
| 463 activity_log->RemoveURL(url); |
| 464 } |
| 465 |
| 447 SendResponse(true); | 466 SendResponse(true); |
| 448 return true; | 467 return true; |
| 449 } | 468 } |
| 450 | 469 |
| 451 bool HistoryDeleteRangeFunction::RunAsyncImpl() { | 470 bool HistoryDeleteRangeFunction::RunAsyncImpl() { |
| 452 scoped_ptr<DeleteRange::Params> params(DeleteRange::Params::Create(*args_)); | 471 scoped_ptr<DeleteRange::Params> params(DeleteRange::Params::Create(*args_)); |
| 453 EXTENSION_FUNCTION_VALIDATE(params.get()); | 472 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 454 | 473 |
| 455 if (!VerifyDeleteAllowed()) | 474 if (!VerifyDeleteAllowed()) |
| 456 return false; | 475 return false; |
| 457 | 476 |
| 458 base::Time start_time = GetTime(params->range.start_time); | 477 base::Time start_time = GetTime(params->range.start_time); |
| 459 base::Time end_time = GetTime(params->range.end_time); | 478 base::Time end_time = GetTime(params->range.end_time); |
| 460 | 479 |
| 461 std::set<GURL> restrict_urls; | 480 std::set<GURL> restrict_urls; |
| 462 HistoryService* hs = | 481 HistoryService* hs = |
| 463 HistoryServiceFactory::GetForProfile(profile(), | 482 HistoryServiceFactory::GetForProfile(profile(), |
| 464 Profile::EXPLICIT_ACCESS); | 483 Profile::EXPLICIT_ACCESS); |
| 465 hs->ExpireHistoryBetween( | 484 hs->ExpireHistoryBetween( |
| 466 restrict_urls, | 485 restrict_urls, |
| 467 start_time, | 486 start_time, |
| 468 end_time, | 487 end_time, |
| 469 base::Bind(&HistoryDeleteRangeFunction::DeleteComplete, | 488 base::Bind(&HistoryDeleteRangeFunction::DeleteComplete, |
| 470 base::Unretained(this)), | 489 base::Unretained(this)), |
| 471 &task_tracker_); | 490 &task_tracker_); |
| 472 | 491 |
| 492 // Also clean from the activity log unless in testing mode. |
| 493 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 494 switches::kEnableExtensionActivityLogTesting)) { |
| 495 ActivityLog* activity_log = ActivityLog::GetInstance(profile_); |
| 496 DCHECK(activity_log); |
| 497 activity_log->RemoveURLs(restrict_urls); |
| 498 } |
| 499 |
| 473 return true; | 500 return true; |
| 474 } | 501 } |
| 475 | 502 |
| 476 void HistoryDeleteRangeFunction::DeleteComplete() { | 503 void HistoryDeleteRangeFunction::DeleteComplete() { |
| 477 SendAsyncResponse(); | 504 SendAsyncResponse(); |
| 478 } | 505 } |
| 479 | 506 |
| 480 bool HistoryDeleteAllFunction::RunAsyncImpl() { | 507 bool HistoryDeleteAllFunction::RunAsyncImpl() { |
| 481 if (!VerifyDeleteAllowed()) | 508 if (!VerifyDeleteAllowed()) |
| 482 return false; | 509 return false; |
| 483 | 510 |
| 484 std::set<GURL> restrict_urls; | 511 std::set<GURL> restrict_urls; |
| 485 HistoryService* hs = | 512 HistoryService* hs = |
| 486 HistoryServiceFactory::GetForProfile(profile(), | 513 HistoryServiceFactory::GetForProfile(profile(), |
| 487 Profile::EXPLICIT_ACCESS); | 514 Profile::EXPLICIT_ACCESS); |
| 488 hs->ExpireHistoryBetween( | 515 hs->ExpireHistoryBetween( |
| 489 restrict_urls, | 516 restrict_urls, |
| 490 base::Time(), // Unbounded beginning... | 517 base::Time(), // Unbounded beginning... |
| 491 base::Time(), // ...and the end. | 518 base::Time(), // ...and the end. |
| 492 base::Bind(&HistoryDeleteAllFunction::DeleteComplete, | 519 base::Bind(&HistoryDeleteAllFunction::DeleteComplete, |
| 493 base::Unretained(this)), | 520 base::Unretained(this)), |
| 494 &task_tracker_); | 521 &task_tracker_); |
| 495 | 522 |
| 523 // Also clean from the activity log unless in testing mode. |
| 524 if (!CommandLine::ForCurrentProcess()->HasSwitch( |
| 525 switches::kEnableExtensionActivityLogTesting)) { |
| 526 ActivityLog* activity_log = ActivityLog::GetInstance(profile_); |
| 527 DCHECK(activity_log); |
| 528 activity_log->RemoveURLs(restrict_urls); |
| 529 } |
| 530 |
| 496 return true; | 531 return true; |
| 497 } | 532 } |
| 498 | 533 |
| 499 void HistoryDeleteAllFunction::DeleteComplete() { | 534 void HistoryDeleteAllFunction::DeleteComplete() { |
| 500 SendAsyncResponse(); | 535 SendAsyncResponse(); |
| 501 } | 536 } |
| 502 | 537 |
| 503 } // namespace extensions | 538 } // namespace extensions |
| OLD | NEW |