Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1295)

Unified Diff: chrome/browser/search/instant_service.cc

Issue 1260113002: Adds an experiment that enabled SuggestionsService suggestions (MostLikely) on LocalNTP (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..5c6c3b9edb0babf26c94d65950039543ba8a1f2f 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"
kmadhusu 2015/07/31 21:05:45 This is already included in instant_service.h. You
fserb 2015/08/05 14:56:26 Done.
+#include "components/suggestions/suggestions_service.h"
kmadhusu 2015/07/31 21:05:44 This is already included in instant_service.h. You
fserb 2015/08/05 14:56:26 Done.
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_types.h"
@@ -49,10 +54,22 @@
#include "chrome/browser/themes/theme_service_factory.h"
#endif // defined(ENABLE_THEMES)
+namespace {
+const char kLocalNTPSuggestionService[] = "LocalNTPSuggestionsService";
kmadhusu 2015/07/31 21:05:44 style nit: Add a blank line before this.
fserb 2015/08/05 14:56:26 Done.
+const char kLocalNTPSuggestionServiceEnabled[] = "Enabled";
+
+bool IsLocalNTPSuggestionServiceEnabled() {
+ return base::StartsWith(
+ base::FieldTrialList::FindFullName(kLocalNTPSuggestionService),
+ kLocalNTPSuggestionServiceEnabled, base::CompareCase::INSENSITIVE_ASCII);
kmadhusu 2015/07/31 21:05:44 Include base/strings/string_util.h for CompareCase
fserb 2015/08/05 14:56:26 Done.
+}
kmadhusu 2015/07/31 21:05:44 style nit: Add a blank line after this.
fserb 2015/08/05 14:56:26 Done.
+} // namespace
+
InstantService::InstantService(Profile* profile)
: profile_(profile),
template_url_service_(TemplateURLServiceFactory::GetForProfile(profile_)),
omnibox_start_margin_(chrome::kDisableStartMargin),
+ suggestions_service_(NULL),
weak_ptr_factory_(this) {
// 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.
@@ -130,6 +147,18 @@ InstantService::InstantService(Profile* profile)
content::URLDataSource::Add(profile_, new MostVisitedIframeSource());
content::URLDataSource::Add(
profile_, new suggestions::SuggestionsSource(profile_));
+
+ if (IsLocalNTPSuggestionServiceEnabled()) {
+ suggestions_service_ =
+ suggestions::SuggestionsServiceFactory::GetForProfile(profile_);
+ }
+
+ if (suggestions_service_) {
+ suggestions_service_->FetchSuggestionsData(
+ suggestions::INITIALIZED_ENABLED_HISTORY,
+ base::Bind(&InstantService::OnSuggestionsAvailable,
+ weak_ptr_factory_.GetWeakPtr()));
+ }
}
InstantService::~InstantService() {
@@ -163,28 +192,42 @@ void InstantService::RemoveObserver(InstantServiceObserver* observer) {
void InstantService::DeleteMostVisitedItem(const GURL& url) {
scoped_refptr<history::TopSites> top_sites =
TopSitesFactory::GetForProfile(profile_);
- if (!top_sites)
- return;
+ if (top_sites)
+ top_sites->AddBlacklistedURL(url);
- top_sites->AddBlacklistedURL(url);
+ if (suggestions_service_) {
+ suggestions_service_->BlacklistURL(
+ url, base::Bind(&InstantService::OnSuggestionsAvailable,
+ weak_ptr_factory_.GetWeakPtr()),
+ base::Closure());
+ }
}
void InstantService::UndoMostVisitedDeletion(const GURL& url) {
scoped_refptr<history::TopSites> top_sites =
TopSitesFactory::GetForProfile(profile_);
- if (!top_sites)
- return;
+ if (top_sites)
+ top_sites->RemoveBlacklistedURL(url);
- top_sites->RemoveBlacklistedURL(url);
+ if (suggestions_service_) {
+ suggestions_service_->UndoBlacklistURL(
+ url, base::Bind(&InstantService::OnSuggestionsAvailable,
+ weak_ptr_factory_.GetWeakPtr()),
+ base::Closure());
+ }
}
void InstantService::UndoAllMostVisitedDeletions() {
scoped_refptr<history::TopSites> top_sites =
TopSitesFactory::GetForProfile(profile_);
- if (!top_sites)
- return;
+ if (top_sites)
+ top_sites->ClearBlacklistedURLs();
- top_sites->ClearBlacklistedURLs();
+ if (suggestions_service_) {
+ suggestions_service_->ClearBlacklist(
+ base::Bind(&InstantService::OnSuggestionsAvailable,
+ weak_ptr_factory_.GetWeakPtr()));
+ }
}
void InstantService::UpdateThemeInfo() {
@@ -265,6 +308,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());
kmadhusu 2015/07/31 21:05:45 nit: Its better to check if suggestion.has_thumbna
fserb 2015/08/05 14:56:25 Done.
+ 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 +342,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)

Powered by Google App Engine
This is Rietveld 408576698