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

Side by Side Diff: chrome/browser/ui/webui/ntp/suggested_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: remove extra line Created 8 years, 10 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/suggested_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 SuggestedHandler::SuggestedHandler()
45 : got_first_suggested_request_(false) {
46 }
47
48 SuggestedHandler::~SuggestedHandler() {
49 }
50
51 void SuggestedHandler::RegisterMessages() {
52 Profile* profile = Profile::FromWebUI(web_ui());
53 // Set up our sources for thumbnail and favicon data.
54 ThumbnailSource* thumbnail_src = new ThumbnailSource(profile);
55 profile->GetChromeURLDataManager()->AddDataSource(thumbnail_src);
Dan Beam 2012/02/09 01:43:06 nit: make look like line below
GeorgeY 2012/02/10 00:00:36 Done.
56
Dan Beam 2012/02/09 01:43:06 nit: remove newline
GeorgeY 2012/02/10 00:00:36 Done.
57 profile->GetChromeURLDataManager()->AddDataSource(
58 new FaviconSource(profile, FaviconSource::FAVICON));
59
60 // TODO(georgey) change it to provide our data.
Dan Beam 2012/02/09 01:43:06 nit: Capitalize and describe what "it" means in th
GeorgeY 2012/02/10 00:00:36 Done.
61 history::TopSites* ts = profile->GetTopSites();
Dan Beam 2012/02/09 01:43:06 nit: ts could be thumbnail source, make more descr
GeorgeY 2012/02/10 00:00:36 Done.
62 if (ts) {
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 ts->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>(ts));
72 }
73
74 // We pre-emptively make a fetch for the available pages so we have the
75 // results sooner.
76 StartQueryForSuggested();
77
78 web_ui()->RegisterMessageCallback("getSuggested",
79 base::Bind(&SuggestedHandler::HandleGetSuggested,
80 base::Unretained(this)));
Dan Beam 2012/02/09 01:43:06 nit: no newline
GeorgeY 2012/02/10 00:00:36 Done.
81
82 // Register ourselves for any suggested item blacklisting.
83 web_ui()->RegisterMessageCallback("blacklistURLFromSuggested",
84 base::Bind(&SuggestedHandler::HandleBlacklistURL,
85 base::Unretained(this)));
86 web_ui()->RegisterMessageCallback("removeURLsFromSuggestedBlacklist",
87 base::Bind(&SuggestedHandler::HandleRemoveURLsFromBlacklist,
88 base::Unretained(this)));
89 web_ui()->RegisterMessageCallback("clearSuggestedURLsBlacklist",
90 base::Bind(&SuggestedHandler::HandleClearBlacklist,
91 base::Unretained(this)));
92 }
93
94 void SuggestedHandler::HandleGetSuggested(const ListValue* args) {
95 if (!NewTabUI::IsSuggestedPageEnabled())
Dan Beam 2012/02/09 01:43:06 I don't think these preventative measures are nece
GeorgeY 2012/02/10 00:00:36 Yep, remnants of the bug when this function was al
96 return;
97 if (!got_first_suggested_request_) {
98 // If our initial data is already here, return it.
99 SendPagesValue();
100 got_first_suggested_request_ = true;
101 } else {
102 StartQueryForSuggested();
103 }
104 }
105
106 void SuggestedHandler::SendPagesValue() {
107 if (pages_value_.get()) {
108 // TODO(georgey) add actual blacklist.
109 bool has_blacklisted_urls = false;
110 base::FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls);
111 web_ui()->CallJavascriptFunction("setSuggestedPages",
112 *(pages_value_.get()),
Dan Beam 2012/02/09 01:43:06 nit: can't one just use pages_value_.release()?
GeorgeY 2012/02/10 00:00:36 Nope, you get memory leak.
113 has_blacklisted_urls_value);
114 pages_value_.reset();
115 }
116 }
117
118 void SuggestedHandler::StartQueryForSuggested() {
119 // TODO(georgey) change it to provide our data.
120 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites();
121 if (ts) {
Dan Beam 2012/02/09 01:43:06 when is this NULL?
GeorgeY 2012/02/10 00:00:36 AFAIK, for TestProfileImpl
122 ts->GetMostVisitedURLs(
123 &topsites_consumer_,
124 base::Bind(&SuggestedHandler::OnSuggestedURLsAvailable,
125 base::Unretained(this)));
126 }
127 }
128
129 void SuggestedHandler::HandleBlacklistURL(const ListValue* args) {
130 if (!NewTabUI::IsSuggestedPageEnabled())
131 return;
132 std::string url = UTF16ToUTF8(ExtractStringValue(args));
133 BlacklistURL(GURL(url));
134 }
135
136 void SuggestedHandler::HandleRemoveURLsFromBlacklist(const ListValue* args) {
137 if (!NewTabUI::IsSuggestedPageEnabled())
138 return;
139 DCHECK(args->GetSize() != 0);
Dan Beam 2012/02/09 01:43:06 nit: > 0 (I understand this is a size_t, but say s
GeorgeY 2012/02/10 00:00:36 Done.
140 // TODO(georgey) remove URLs from blacklist.
141 }
142
143 void SuggestedHandler::HandleClearBlacklist(const ListValue* args) {
144 if (!NewTabUI::IsSuggestedPageEnabled())
145 return;
146 // TODO(georgey) clear blacklist.
Dan Beam 2012/02/09 01:43:06 I'd prefer you just wait until the TODO() is done
GeorgeY 2012/02/10 00:00:36 We *do* want to add it before the first internal r
147 }
148
149 void SuggestedHandler::SetPagesValueFromTopSites(
150 const history::MostVisitedURLList& data) {
151 // TODO(georgey) - change to our suggested pages - right now as a test
152 // returns top 20 sites in reverse order.
153 pages_value_.reset(new ListValue);
Dan Beam 2012/02/09 01:43:06 Don't know what the style guide says about optiona
GeorgeY 2012/02/10 00:00:36 Done.
154 for (size_t i = 0; i < data.size(); i++) {
155 const history::MostVisitedURL& url = data[data.size() - i - 1];
Dan Beam 2012/02/09 01:43:06 nit: name this something different so it doesn't l
GeorgeY 2012/02/10 00:00:36 Done.
156 DictionaryValue* page_value = new DictionaryValue();
157 if (url.url.is_empty()) {
158 page_value->SetBoolean("filler", true);
159 pages_value_->Append(page_value);
160 continue;
161 }
162
163 NewTabUI::SetURLTitleAndDirection(page_value,
164 url.title,
165 url.url);
166 pages_value_->Append(page_value);
167 }
168 }
169
170 void SuggestedHandler::OnSuggestedURLsAvailable(
171 const history::MostVisitedURLList& data) {
172 SetPagesValueFromTopSites(data);
173 if (got_first_suggested_request_)
174 SendPagesValue();
175 }
176
177 void SuggestedHandler::Observe(int type,
178 const content::NotificationSource& source,
179 const content::NotificationDetails& details) {
180 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED);
181
182 // Most visited urls changed, query again.
183 StartQueryForSuggested();
184 }
185
186 void SuggestedHandler::BlacklistURL(const GURL& url) {
187 // TODO(georgey) blacklist an URL.
Dan Beam 2012/02/09 01:43:06 same for these two stubs
GeorgeY 2012/02/10 00:00:36 see above
188 }
189
190 std::string SuggestedHandler::GetDictionaryKeyForURL(const std::string& url) {
191 return base::MD5String(url);
192 }
193
194 // static
195 void SuggestedHandler::RegisterUserPrefs(PrefService* prefs) {
196 // TODO(georgey) add user preferences (such as own blacklist) as needed.
197 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698