Chromium Code Reviews| Index: chrome/browser/search/instant_service.cc |
| diff --git a/chrome/browser/search/instant_service.cc b/chrome/browser/search/instant_service.cc |
| index e6db319cb57adf2df49f10adc6d2c80b44557534..56318dfc1cc8a426401011d98ce3a45b5180af39 100644 |
| --- a/chrome/browser/search/instant_service.cc |
| +++ b/chrome/browser/search/instant_service.cc |
| @@ -4,14 +4,22 @@ |
| #include "chrome/browser/search/instant_service.h" |
| +#include <vector> |
| + |
| #include "base/strings/string_number_conversions.h" |
| #include "chrome/browser/history/history_notifications.h" |
| +#include "chrome/browser/history/top_sites.h" |
| #include "chrome/browser/profiles/profile.h" |
| #include "chrome/browser/search/instant_io_context.h" |
| #include "chrome/browser/search/instant_service_factory.h" |
| #include "chrome/browser/search/local_ntp_source.h" |
| #include "chrome/browser/search/most_visited_iframe_source.h" |
| #include "chrome/browser/search/suggestion_iframe_source.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_finder.h" |
| +#include "chrome/browser/ui/browser_instant_controller.h" |
| +#include "chrome/browser/ui/host_desktop.h" |
| +#include "chrome/browser/ui/search/instant_controller.h" |
| #include "chrome/browser/ui/webui/favicon_source.h" |
| #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
| #include "chrome/browser/ui/webui/theme_source.h" |
| @@ -28,7 +36,8 @@ using content::BrowserThread; |
| InstantService::InstantService(Profile* profile) |
| : profile_(profile), |
| - most_visited_item_cache_(kMaxInstantMostVisitedItemCacheSize) { |
| + most_visited_item_cache_(kMaxInstantMostVisitedItemCacheSize), |
| + weak_ptr_factory_(this) { |
| // Stub for unit tests. |
| if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) |
| return; |
| @@ -37,6 +46,12 @@ InstantService::InstantService(Profile* profile) |
| content::NOTIFICATION_RENDERER_PROCESS_TERMINATED, |
| content::NotificationService::AllSources()); |
| + history::TopSites* top_sites = profile_->GetTopSites(); |
| + if (top_sites) { |
| + registrar_.Add(this, |
| + chrome::NOTIFICATION_TOP_SITES_CHANGED, |
| + content::Source<history::TopSites>(top_sites)); |
| + } |
| instant_io_context_ = new InstantIOContext(); |
| if (profile_ && profile_->GetResourceContext()) { |
| @@ -138,18 +153,36 @@ void InstantService::AddMostVisitedItems( |
| } |
| } |
| +void InstantService::DeleteMostVisitedItem(const GURL& item_url) { |
| + history::TopSites* top_sites = profile_->GetTopSites(); |
| + if (!top_sites) |
| + return; |
| + |
| + top_sites->AddBlacklistedURL(item_url); |
| +} |
| + |
| +void InstantService::UndoMostVisitedDeletion( |
| + const GURL& most_visited_item_url) { |
|
Jered
2013/05/21 20:18:21
"item_url" or "url" would be clear to me.
kmadhusu
2013/05/23 18:26:22
most_visited_item_url => url. Fixed.
|
| + history::TopSites* top_sites = profile_->GetTopSites(); |
| + if (!top_sites) |
| + return; |
| + |
| + top_sites->RemoveBlacklistedURL(most_visited_item_url); |
| +} |
| + |
| +void InstantService::UndoAllMostVisitedDeletions() { |
| + history::TopSites* top_sites = profile_->GetTopSites(); |
| + if (!top_sites) |
| + return; |
| + |
| + top_sites->ClearBlacklistedURLs(); |
| +} |
| + |
| void InstantService::GetCurrentMostVisitedItems( |
| std::vector<InstantMostVisitedItemIDPair>* items) const { |
| most_visited_item_cache_.GetCurrentItems(items); |
| } |
| -bool InstantService::GetMostVisitedItemForID( |
| - InstantRestrictedID most_visited_item_id, |
| - InstantMostVisitedItem* item) const { |
| - return most_visited_item_cache_.GetItemWithRestrictedID( |
| - most_visited_item_id, item); |
| -} |
| - |
| void InstantService::Shutdown() { |
| process_ids_.clear(); |
| @@ -179,7 +212,55 @@ void InstantService::Observe(int type, |
| } |
| break; |
| } |
| + case chrome::NOTIFICATION_TOP_SITES_CHANGED: { |
| + history::TopSites* top_sites = profile_->GetTopSites(); |
| + if (top_sites) { |
| + top_sites->GetMostVisitedURLs( |
| + base::Bind(&InstantService::OnMostVisitedItemsReceived, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + } |
| + break; |
| + } |
| default: |
| NOTREACHED() << "Unexpected notification type in InstantService."; |
| } |
| } |
| + |
| +bool InstantService::GetMostVisitedItemForID( |
| + InstantRestrictedID most_visited_item_id, |
| + InstantMostVisitedItem* item) const { |
| + return most_visited_item_cache_.GetItemWithRestrictedID( |
| + most_visited_item_id, item); |
| +} |
| + |
| +void InstantService::OnMostVisitedItemsReceived( |
| + const history::MostVisitedURLList& data) { |
| + if (data.empty()) |
| + return; |
| + |
| + std::vector<InstantMostVisitedItem> most_visited_items; |
| + for (size_t i = 0; i < data.size(); i++) { |
| + const history::MostVisitedURL& url = data[i]; |
| + InstantMostVisitedItem item; |
| + item.url = url.url; |
| + item.title = url.title; |
| + most_visited_items.push_back(item); |
| + } |
| + AddMostVisitedItems(most_visited_items); |
| + SendMostVisitedItems(); |
| +} |
| + |
| +void InstantService::SendMostVisitedItems() { |
| + Browser* browser = chrome::FindBrowserWithProfile(profile_, |
|
Jered
2013/05/21 20:18:21
Is it correct to send the update only to one brows
kmadhusu
2013/05/23 18:26:22
Good catch. Fixed to send updates to all the brows
|
| + chrome::GetActiveDesktop()); |
| + if (!browser || !browser->instant_controller()) |
| + return; |
| + |
| + InstantController* controller = browser->instant_controller()->instant(); |
| + if (!controller) |
| + return; |
| + |
| + std::vector<InstantMostVisitedItemIDPair> items_with_ids; |
|
Jered
2013/05/21 20:18:21
Instead, expose UpdateMostVisitedItems() and call
kmadhusu
2013/05/23 18:26:22
Done.
|
| + GetCurrentMostVisitedItems(&items_with_ids); |
| + controller->SendMostVisitedItems(items_with_ids); |
| +} |