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 5f668effb17431b5f644491abaeffea26079ed2a..c8ad4e877eff148cffba01601ea8522175aba327 100644 |
| --- a/chrome/browser/search/instant_service.cc |
| +++ b/chrome/browser/search/instant_service.cc |
| @@ -4,6 +4,8 @@ |
| #include "chrome/browser/search/instant_service.h" |
| +#include "base/metrics/field_trial.h" |
| +#include "base/strings/utf_string_conversions.h" |
| #include "chrome/browser/chrome_notification_types.h" |
| #include "chrome/browser/favicon/fallback_icon_service_factory.h" |
| #include "chrome/browser/favicon/large_icon_service_factory.h" |
| @@ -13,6 +15,7 @@ |
| #include "chrome/browser/search/instant_service_observer.h" |
| #include "chrome/browser/search/most_visited_iframe_source.h" |
| #include "chrome/browser/search/search.h" |
| +#include "chrome/browser/search/suggestions/suggestions_service_factory.h" |
| #include "chrome/browser/search/suggestions/suggestions_source.h" |
| #include "chrome/browser/search/thumbnail_source.h" |
| #include "chrome/browser/search_engines/template_url_service_factory.h" |
| @@ -29,6 +32,8 @@ |
| #include "components/history/core/browser/top_sites.h" |
| #include "components/keyed_service/core/service_access_type.h" |
| #include "components/search_engines/template_url_service.h" |
| +#include "components/suggestions/proto/suggestions.pb.h" |
| +#include "components/suggestions/suggestions_service.h" |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/notification_service.h" |
| #include "content/public/browser/notification_types.h" |
| @@ -53,7 +58,8 @@ InstantService::InstantService(Profile* profile) |
| : profile_(profile), |
| template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)), |
| omnibox_start_margin_(chrome::kDisableStartMargin), |
| - weak_ptr_factory_(this) { |
| + weak_ptr_factory_(this), |
| + suggestions_service(NULL) { |
| // The initialization below depends on a typical set of browser threads. Skip |
| // it if we are running in a unit test without the full suite. |
| if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) |
| @@ -130,6 +136,22 @@ InstantService::InstantService(Profile* profile) |
| content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); |
| content::URLDataSource::Add( |
| profile_, new suggestions::SuggestionsSource(profile_)); |
| + |
| + if (base::StartsWith( |
| + base::FieldTrialList::FindFullName("LocalNTPSuggestionsService"), |
| + "Enabled", base::CompareCase::INSENSITIVE_ASCII)) { |
| + suggestions_service = |
| + suggestions::SuggestionsServiceFactory::GetForProfile(profile_); |
| + } |
| + |
| + if (suggestions_service) { |
| + suggestions::SuggestionsService* if (suggestions_service) { |
|
kmadhusu
2015/07/29 17:42:31
Can you please explain this code? Why do you need
fserb
2015/07/29 19:02:59
I messed up the last comment fix. :) thanks. done.
|
| + suggestions_service->FetchSuggestionsData( |
| + suggestions::INITIALIZED_ENABLED_HISTORY, |
| + base::Bind(&InstantService::OnSuggestionsAvailable, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + } |
| + } |
| } |
| InstantService::~InstantService() { |
| @@ -167,6 +189,12 @@ void InstantService::DeleteMostVisitedItem(const GURL& url) { |
| return; |
| top_sites->AddBlacklistedURL(url); |
| + if (suggestions_service) { |
| + suggestions_service->BlacklistURL( |
| + url, base::Bind(&InstantService::OnSuggestionsAvailable, |
|
kmadhusu
2015/07/29 17:42:31
Instead of passing a callback as an argument, have
fserb
2015/07/29 19:03:00
SuggestionsService has a callback model, it doesn'
kmadhusu
2015/07/30 21:57:02
(My 2 cents) Hmm. I am not entirely convinced by t
|
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::Closure()); |
| + } |
| } |
| void InstantService::UndoMostVisitedDeletion(const GURL& url) { |
| @@ -176,6 +204,12 @@ void InstantService::UndoMostVisitedDeletion(const GURL& url) { |
| return; |
| top_sites->RemoveBlacklistedURL(url); |
| + if (suggestions_service) { |
| + suggestions_service->UndoBlacklistURL( |
| + url, base::Bind(&InstantService::OnSuggestionsAvailable, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::Closure()); |
| + } |
| } |
| void InstantService::UndoAllMostVisitedDeletions() { |
| @@ -185,6 +219,11 @@ void InstantService::UndoAllMostVisitedDeletions() { |
| return; |
| top_sites->ClearBlacklistedURLs(); |
| + if (suggestions_service) { |
| + suggestions_service->ClearBlacklist( |
| + base::Bind(&InstantService::OnSuggestionsAvailable, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + } |
| } |
| void InstantService::UpdateThemeInfo() { |
| @@ -265,6 +304,23 @@ void InstantService::OnRendererProcessTerminated(int process_id) { |
| } |
| } |
| +void InstantService::OnSuggestionsAvailable( |
| + const suggestions::SuggestionsProfile& profile) { |
| + std::vector<InstantMostVisitedItem> new_suggestions_items; |
| + for (int i = 0; i < profile.suggestions_size(); ++i) { |
| + const suggestions::ChromeSuggestion& suggestion = profile.suggestions(i); |
| + |
| + InstantMostVisitedItem item; |
| + item.url = GURL(suggestion.url()); |
| + item.title = base::UTF8ToUTF16(suggestion.title()); |
| + item.thumbnail = GURL(suggestion.thumbnail()); |
| + item.favicon = GURL(suggestion.favicon_url()); |
| + new_suggestions_items.push_back(item); |
| + } |
| + suggestions_items_ = new_suggestions_items; |
| + NotifyAboutMostVisitedItems(); |
| +} |
| + |
| void InstantService::OnMostVisitedItemsReceived( |
| const history::MostVisitedURLList& data) { |
| history::MostVisitedURLList reordered_data(data); |
| @@ -282,8 +338,13 @@ void InstantService::OnMostVisitedItemsReceived( |
| } |
| void InstantService::NotifyAboutMostVisitedItems() { |
| - FOR_EACH_OBSERVER(InstantServiceObserver, observers_, |
| - MostVisitedItemsChanged(most_visited_items_)); |
| + if (suggestions_service && !suggestions_items_.empty()) { |
| + FOR_EACH_OBSERVER(InstantServiceObserver, observers_, |
| + MostVisitedItemsChanged(suggestions_items_)); |
| + } else { |
| + FOR_EACH_OBSERVER(InstantServiceObserver, observers_, |
| + MostVisitedItemsChanged(most_visited_items_)); |
| + } |
| } |
| #if defined(ENABLE_THEMES) |