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

Side by Side Diff: chrome/browser/ui/webui/ntp/suggestions_page_handler.cc

Issue 9358031: Added new adaptive "Suggest" tab on the New Tab Page, behing the flag, for the experiments. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed Dan's comments Created 8 years, 9 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/webui/ntp/suggestions_page_handler.h"
6
7 #include <set>
8
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/command_line.h"
12 #include "base/md5.h"
13 #include "base/memory/scoped_vector.h"
14 #include "base/memory/singleton.h"
15 #include "base/string16.h"
16 #include "base/string_number_conversions.h"
17 #include "base/threading/thread.h"
18 #include "base/utf_string_conversions.h"
19 #include "base/values.h"
20 #include "chrome/browser/history/page_usage_data.h"
21 #include "chrome/browser/history/top_sites.h"
22 #include "chrome/browser/prefs/pref_service.h"
23 #include "chrome/browser/prefs/scoped_user_pref_update.h"
24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/ui/webui/chrome_url_data_manager.h"
26 #include "chrome/browser/ui/webui/favicon_source.h"
27 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
28 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
29 #include "chrome/common/chrome_notification_types.h"
30 #include "chrome/common/pref_names.h"
31 #include "chrome/common/url_constants.h"
32 #include "content/public/browser/browser_thread.h"
33 #include "content/public/browser/notification_source.h"
34 #include "content/public/browser/user_metrics.h"
35 #include "content/public/browser/web_ui.h"
36 #include "googleurl/src/gurl.h"
37 #include "grit/chromium_strings.h"
38 #include "grit/generated_resources.h"
39 #include "grit/locale_settings.h"
40 #include "ui/base/l10n/l10n_util.h"
41
42 using content::UserMetricsAction;
43
44 SuggestionsHandler::SuggestionsHandler()
45 : got_first_suggestions_request_(false) {
46 }
47
48 SuggestionsHandler::~SuggestionsHandler() {
49 }
50
51 void SuggestionsHandler::RegisterMessages() {
52 Profile* profile = Profile::FromWebUI(web_ui());
53 // Set up our sources for thumbnail and favicon data.
54 profile->GetChromeURLDataManager()->AddDataSource(
55 new ThumbnailSource(profile));
56 profile->GetChromeURLDataManager()->AddDataSource(
57 new FaviconSource(profile, FaviconSource::FAVICON));
58
59 // TODO(georgey) change the source of the web-sites to provide our data.
60 // Initial commit uses top sites as a data source.
61 history::TopSites* top_sites = profile->GetTopSites();
62 if (top_sites) {
63 // TopSites updates itself after a delay. This is especially noticable when
64 // your profile is empty. Ask TopSites to update itself when we're about to
65 // show the new tab page.
66 top_sites->SyncWithHistory();
67
68 // Register for notification when TopSites changes so that we can update
69 // ourself.
70 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED,
71 content::Source<history::TopSites>(top_sites));
72 }
73
74 // We pre-emptively make a fetch for the available pages so we have the
75 // results sooner.
76 StartQueryForSuggestions();
77
78 web_ui()->RegisterMessageCallback("getSuggestions",
79 base::Bind(&SuggestionsHandler::HandleGetSuggestions,
80 base::Unretained(this)));
81 // Register ourselves for any suggestions item blacklisting.
82 web_ui()->RegisterMessageCallback("blacklistURLFromSuggestions",
83 base::Bind(&SuggestionsHandler::HandleBlacklistURL,
84 base::Unretained(this)));
85 web_ui()->RegisterMessageCallback("removeURLsFromSuggestionsBlacklist",
86 base::Bind(&SuggestionsHandler::HandleRemoveURLsFromBlacklist,
87 base::Unretained(this)));
88 web_ui()->RegisterMessageCallback("clearSuggestionsURLsBlacklist",
89 base::Bind(&SuggestionsHandler::HandleClearBlacklist,
90 base::Unretained(this)));
91 }
92
93 void SuggestionsHandler::HandleGetSuggestions(const ListValue* args) {
94 if (!got_first_suggestions_request_) {
95 // If our initial data is already here, return it.
96 SendPagesValue();
97 got_first_suggestions_request_ = true;
98 } else {
99 StartQueryForSuggestions();
100 }
101 }
102
103 void SuggestionsHandler::SendPagesValue() {
104 if (pages_value_.get()) {
105 // TODO(georgey) add actual blacklist.
106 bool has_blacklisted_urls = false;
107 base::FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls);
108 web_ui()->CallJavascriptFunction("ntp4.setSuggestionsPages",
109 *(pages_value_.get()),
110 has_blacklisted_urls_value);
111 pages_value_.reset();
112 }
113 }
114
115 void SuggestionsHandler::StartQueryForSuggestions() {
116 // TODO(georgey) change it to provide our data.
117 history::TopSites* top_sites = Profile::FromWebUI(web_ui())->GetTopSites();
118 if (top_sites) {
119 top_sites->GetMostVisitedURLs(
120 &topsites_consumer_,
121 base::Bind(&SuggestionsHandler::OnSuggestionsURLsAvailable,
122 base::Unretained(this)));
123 }
124 }
125
126 void SuggestionsHandler::HandleBlacklistURL(const ListValue* args) {
127 std::string url = UTF16ToUTF8(ExtractStringValue(args));
128 BlacklistURL(GURL(url));
129 }
130
131 void SuggestionsHandler::HandleRemoveURLsFromBlacklist(const ListValue* args) {
132 DCHECK_GT(args->GetSize(), 0U);
133 // TODO(georgey) remove URLs from blacklist.
134 }
135
136 void SuggestionsHandler::HandleClearBlacklist(const ListValue* args) {
137 // TODO(georgey) clear blacklist.
138 }
139
140 void SuggestionsHandler::SetPagesValueFromTopSites(
141 const history::MostVisitedURLList& data) {
142 // TODO(georgey) - change to our suggestions pages - right now as a test
143 // returns top 20 sites in reverse order.
144 pages_value_.reset(new ListValue());
145 for (size_t i = 0; i < data.size(); i++) {
146 const history::MostVisitedURL& suggested_url = data[data.size() - i - 1];
147 DictionaryValue* page_value = new DictionaryValue();
148 if (suggested_url.url.is_empty()) {
149 continue;
150 }
151
152 NewTabUI::SetURLTitleAndDirection(page_value,
153 suggested_url.title,
154 suggested_url.url);
155 pages_value_->Append(page_value);
156 }
157 }
158
159 void SuggestionsHandler::OnSuggestionsURLsAvailable(
160 const history::MostVisitedURLList& data) {
161 SetPagesValueFromTopSites(data);
162 if (got_first_suggestions_request_)
163 SendPagesValue();
164 }
165
166 void SuggestionsHandler::Observe(int type,
167 const content::NotificationSource& source,
168 const content::NotificationDetails& details) {
169 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED);
170
171 // Suggestions urls changed, query again.
172 StartQueryForSuggestions();
173 }
174
175 void SuggestionsHandler::BlacklistURL(const GURL& url) {
176 // TODO(georgey) blacklist an URL.
177 }
178
179 std::string SuggestionsHandler::GetDictionaryKeyForURL(const std::string& url) {
180 return base::MD5String(url);
181 }
182
183 // static
184 void SuggestionsHandler::RegisterUserPrefs(PrefService* prefs) {
185 // TODO(georgey) add user preferences (such as own blacklist) as needed.
186 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698