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..0fd94448cd9a8b5e2e0f4c2d674fd5f56e41886b 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" |
| @@ -130,6 +135,18 @@ InstantService::InstantService(Profile* profile) |
| content::URLDataSource::Add(profile_, new MostVisitedIframeSource()); |
| content::URLDataSource::Add( |
| profile_, new suggestions::SuggestionsSource(profile_)); |
| + |
| + if (!base::FieldTrialList::FindFullName("LocalNTPSuggestionsService") |
|
Mathieu
2015/07/28 13:32:42
bring the field trial check to anonymous namespace
fserb
2015/07/28 17:55:30
Done.
|
| + .empty()) { |
| + suggestions::SuggestionsService* suggestions_service = |
|
Mathieu
2015/07/28 13:32:42
Instead of calling the factory every time should w
fserb
2015/07/28 17:55:30
Done.
|
| + suggestions::SuggestionsServiceFactory::GetForProfile(profile_); |
| + if (suggestions_service) { |
| + suggestions_service->FetchSuggestionsData( |
| + suggestions::INITIALIZED_ENABLED_HISTORY, |
| + base::Bind(&InstantService::OnSuggestionsAvailable, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + } |
| + } |
| } |
| InstantService::~InstantService() { |
| @@ -167,6 +184,17 @@ void InstantService::DeleteMostVisitedItem(const GURL& url) { |
| return; |
| top_sites->AddBlacklistedURL(url); |
| + if (!base::FieldTrialList::FindFullName("LocalNTPSuggestionsService") |
| + .empty()) { |
| + suggestions::SuggestionsService* suggestions_service = |
| + suggestions::SuggestionsServiceFactory::GetForProfile(profile_); |
| + if (suggestions_service) { |
| + suggestions_service->BlacklistURL( |
| + url, base::Bind(&InstantService::OnSuggestionsAvailable, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::Closure()); |
| + } |
| + } |
| } |
| void InstantService::UndoMostVisitedDeletion(const GURL& url) { |
| @@ -176,6 +204,17 @@ void InstantService::UndoMostVisitedDeletion(const GURL& url) { |
| return; |
| top_sites->RemoveBlacklistedURL(url); |
| + if (!base::FieldTrialList::FindFullName("LocalNTPSuggestionsService") |
| + .empty()) { |
| + suggestions::SuggestionsService* suggestions_service = |
| + suggestions::SuggestionsServiceFactory::GetForProfile(profile_); |
| + if (suggestions_service) { |
| + suggestions_service->UndoBlacklistURL( |
| + url, base::Bind(&InstantService::OnSuggestionsAvailable, |
| + weak_ptr_factory_.GetWeakPtr()), |
| + base::Closure()); |
| + } |
| + } |
| } |
| void InstantService::UndoAllMostVisitedDeletions() { |
| @@ -185,6 +224,16 @@ void InstantService::UndoAllMostVisitedDeletions() { |
| return; |
| top_sites->ClearBlacklistedURLs(); |
| + if (!base::FieldTrialList::FindFullName("LocalNTPSuggestionsService") |
| + .empty()) { |
| + suggestions::SuggestionsService* suggestions_service = |
| + suggestions::SuggestionsServiceFactory::GetForProfile(profile_); |
| + if (suggestions_service) { |
| + suggestions_service->ClearBlacklist( |
| + base::Bind(&InstantService::OnSuggestionsAvailable, |
| + weak_ptr_factory_.GetWeakPtr())); |
| + } |
| + } |
| } |
| void InstantService::UpdateThemeInfo() { |
| @@ -265,6 +314,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 +348,15 @@ void InstantService::OnMostVisitedItemsReceived( |
| } |
| void InstantService::NotifyAboutMostVisitedItems() { |
| - FOR_EACH_OBSERVER(InstantServiceObserver, observers_, |
| - MostVisitedItemsChanged(most_visited_items_)); |
| + if (!suggestions_items_.empty() && |
| + !base::FieldTrialList::FindFullName("LocalNTPSuggestionsService") |
| + .empty()) { |
| + FOR_EACH_OBSERVER(InstantServiceObserver, observers_, |
| + MostVisitedItemsChanged(suggestions_items_)); |
| + } else { |
| + FOR_EACH_OBSERVER(InstantServiceObserver, observers_, |
| + MostVisitedItemsChanged(most_visited_items_)); |
| + } |
| } |
| #if defined(ENABLE_THEMES) |