most_visited_handler.cc revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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/most_visited_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/metrics/field_trial.h"
16#include "base/metrics/histogram.h"
17#include "base/prefs/pref_service.h"
18#include "base/strings/string16.h"
19#include "base/strings/string_number_conversions.h"
20#include "base/strings/utf_string_conversions.h"
21#include "base/threading/thread.h"
22#include "base/values.h"
23#include "chrome/browser/chrome_notification_types.h"
24#include "chrome/browser/history/page_usage_data.h"
25#include "chrome/browser/history/top_sites.h"
26#include "chrome/browser/prefs/scoped_user_pref_update.h"
27#include "chrome/browser/profiles/profile.h"
28#include "chrome/browser/ui/browser.h"
29#include "chrome/browser/ui/browser_finder.h"
30#include "chrome/browser/ui/tabs/tab_strip_model.h"
31#include "chrome/browser/ui/webui/favicon_source.h"
32#include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
33#include "chrome/browser/ui/webui/ntp/ntp_stats.h"
34#include "chrome/browser/ui/webui/ntp/thumbnail_source.h"
35#include "chrome/common/pref_names.h"
36#include "chrome/common/url_constants.h"
37#include "components/user_prefs/pref_registry_syncable.h"
38#include "content/public/browser/navigation_controller.h"
39#include "content/public/browser/navigation_entry.h"
40#include "content/public/browser/notification_source.h"
41#include "content/public/browser/url_data_source.h"
42#include "content/public/browser/user_metrics.h"
43#include "content/public/browser/web_contents.h"
44#include "content/public/browser/web_ui.h"
45#include "grit/chromium_strings.h"
46#include "grit/generated_resources.h"
47#include "grit/locale_settings.h"
48#include "ui/base/l10n/l10n_util.h"
49#include "url/gurl.h"
50
51using content::UserMetricsAction;
52
53namespace {
54
55// Constants for the most visited tile placement field trial.
56const char kMostVisitedFieldTrialName[] = "MostVisitedTilePlacement";
57const char kTabsGroupName[] = "DontShowOpenTabs";
58
59// Minimum number of suggestions that |pages_value_| must hold for the Most
60// Visited Field Trial to remove a URL if already open in the browser.
61const size_t kMinUrlSuggestions = 8;
62
63// Creates a set containing the canonical URLs of the currently open tabs.
64void GetOpenUrls(const TabStripModel& tabs,
65                 const history::TopSites& ts,
66                 std::set<std::string>* urls) {
67  for (int i = 0; i < tabs.count(); ++i) {
68    content::WebContents* web_contents = tabs.GetWebContentsAt(i);
69    if (web_contents)
70      urls->insert(ts.GetCanonicalURLString(web_contents->GetURL()));
71  }
72}
73
74}  // namespace
75
76MostVisitedHandler::MostVisitedHandler()
77    : weak_ptr_factory_(this),
78      got_first_most_visited_request_(false),
79      most_visited_viewed_(false),
80      user_action_logged_(false) {
81}
82
83MostVisitedHandler::~MostVisitedHandler() {
84  if (!user_action_logged_ && most_visited_viewed_) {
85    const GURL ntp_url = GURL(chrome::kChromeUINewTabURL);
86    int action_id = NTP_FOLLOW_ACTION_OTHER;
87    content::NavigationEntry* entry =
88        web_ui()->GetWebContents()->GetController().GetActiveEntry();
89    if (entry && (entry->GetURL() != ntp_url)) {
90      action_id =
91          content::PageTransitionStripQualifier(entry->GetTransitionType());
92    }
93
94    UMA_HISTOGRAM_ENUMERATION("NewTabPage.MostVisitedAction", action_id,
95                              NUM_NTP_FOLLOW_ACTIONS);
96  }
97}
98
99void MostVisitedHandler::RegisterMessages() {
100  Profile* profile = Profile::FromWebUI(web_ui());
101  // Set up our sources for thumbnail and favicon data.
102  ThumbnailSource* thumbnail_src = new ThumbnailSource(profile);
103  content::URLDataSource::Add(profile, thumbnail_src);
104
105#if defined(OS_ANDROID)
106  // Register chrome://touch-icon as a data source for touch icons or favicons.
107  content::URLDataSource::Add(profile,
108                              new FaviconSource(profile, FaviconSource::ANY));
109#endif
110  // Register chrome://favicon as a data source for favicons.
111  content::URLDataSource::Add(
112      profile, new FaviconSource(profile, FaviconSource::FAVICON));
113
114  history::TopSites* ts = profile->GetTopSites();
115  if (ts) {
116    // TopSites updates itself after a delay. This is especially noticable when
117    // your profile is empty. Ask TopSites to update itself when we're about to
118    // show the new tab page.
119    ts->SyncWithHistory();
120
121    // Register for notification when TopSites changes so that we can update
122    // ourself.
123    registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED,
124                   content::Source<history::TopSites>(ts));
125  }
126
127  // We pre-emptively make a fetch for the most visited pages so we have the
128  // results sooner.
129  StartQueryForMostVisited();
130
131  web_ui()->RegisterMessageCallback("getMostVisited",
132      base::Bind(&MostVisitedHandler::HandleGetMostVisited,
133                 base::Unretained(this)));
134
135  // Register ourselves for any most-visited item blacklisting.
136  web_ui()->RegisterMessageCallback("blacklistURLFromMostVisited",
137      base::Bind(&MostVisitedHandler::HandleBlacklistUrl,
138                 base::Unretained(this)));
139  web_ui()->RegisterMessageCallback("removeURLsFromMostVisitedBlacklist",
140      base::Bind(&MostVisitedHandler::HandleRemoveUrlsFromBlacklist,
141                 base::Unretained(this)));
142  web_ui()->RegisterMessageCallback("clearMostVisitedURLsBlacklist",
143      base::Bind(&MostVisitedHandler::HandleClearBlacklist,
144                 base::Unretained(this)));
145  web_ui()->RegisterMessageCallback("mostVisitedAction",
146      base::Bind(&MostVisitedHandler::HandleMostVisitedAction,
147                 base::Unretained(this)));
148  web_ui()->RegisterMessageCallback("mostVisitedSelected",
149      base::Bind(&MostVisitedHandler::HandleMostVisitedSelected,
150                 base::Unretained(this)));
151}
152
153void MostVisitedHandler::HandleGetMostVisited(const ListValue* args) {
154  if (!got_first_most_visited_request_) {
155    // If our initial data is already here, return it.
156    SendPagesValue();
157    got_first_most_visited_request_ = true;
158  } else {
159    StartQueryForMostVisited();
160  }
161}
162
163void MostVisitedHandler::SendPagesValue() {
164  if (pages_value_) {
165    Profile* profile = Profile::FromWebUI(web_ui());
166    const DictionaryValue* url_blacklist =
167        profile->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist);
168    bool has_blacklisted_urls = !url_blacklist->empty();
169    history::TopSites* ts = profile->GetTopSites();
170    if (ts) {
171      has_blacklisted_urls = ts->HasBlacklistedItems();
172
173      // The following experiment removes recommended URLs if a matching URL is
174      // already open in the Browser. Note: this targets only the
175      // top-level of sites i.e. if www.foo.com/bar is open in browser, and
176      // www.foo.com is a recommended URL, www.foo.com will still appear on the
177      // next NTP open.
178      if (base::FieldTrialList::FindFullName(kMostVisitedFieldTrialName) ==
179          kTabsGroupName) {
180        RemovePageValuesMatchingOpenTabs();
181      }
182    }
183
184    base::FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls);
185    web_ui()->CallJavascriptFunction("ntp.setMostVisitedPages",
186                                     *pages_value_,
187                                     has_blacklisted_urls_value);
188    pages_value_.reset();
189  }
190}
191
192void MostVisitedHandler::StartQueryForMostVisited() {
193  history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites();
194  if (ts) {
195    ts->GetMostVisitedURLs(
196        base::Bind(&MostVisitedHandler::OnMostVisitedUrlsAvailable,
197                   weak_ptr_factory_.GetWeakPtr()));
198  }
199}
200
201void MostVisitedHandler::HandleBlacklistUrl(const ListValue* args) {
202  std::string url = UTF16ToUTF8(ExtractStringValue(args));
203  BlacklistUrl(GURL(url));
204}
205
206void MostVisitedHandler::HandleRemoveUrlsFromBlacklist(const ListValue* args) {
207  DCHECK(args->GetSize() != 0);
208
209  for (ListValue::const_iterator iter = args->begin();
210       iter != args->end(); ++iter) {
211    std::string url;
212    bool r = (*iter)->GetAsString(&url);
213    if (!r) {
214      NOTREACHED();
215      return;
216    }
217    content::RecordAction(UserMetricsAction("MostVisited_UrlRemoved"));
218    history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites();
219    if (ts)
220      ts->RemoveBlacklistedURL(GURL(url));
221  }
222}
223
224void MostVisitedHandler::HandleClearBlacklist(const ListValue* args) {
225  content::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared"));
226
227  history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites();
228  if (ts)
229    ts->ClearBlacklistedURLs();
230}
231
232void MostVisitedHandler::HandleMostVisitedAction(const base::ListValue* args) {
233  DCHECK(args);
234
235  double action_id;
236  if (!args->GetDouble(0, &action_id))
237    NOTREACHED();
238
239  UMA_HISTOGRAM_ENUMERATION("NewTabPage.MostVisitedAction",
240                            static_cast<int>(action_id),
241                            NUM_NTP_FOLLOW_ACTIONS);
242  most_visited_viewed_ = true;
243  user_action_logged_ = true;
244}
245
246void MostVisitedHandler::HandleMostVisitedSelected(
247    const base::ListValue* args) {
248  most_visited_viewed_ = true;
249}
250
251void MostVisitedHandler::SetPagesValueFromTopSites(
252    const history::MostVisitedURLList& data) {
253  pages_value_.reset(new ListValue);
254
255  history::MostVisitedURLList top_sites(data);
256  history::TopSites::MaybeShuffle(&top_sites);
257
258  for (size_t i = 0; i < top_sites.size(); i++) {
259    const history::MostVisitedURL& url = top_sites[i];
260    DictionaryValue* page_value = new DictionaryValue();
261    if (url.url.is_empty()) {
262      page_value->SetBoolean("filler", true);
263      pages_value_->Append(page_value);
264      continue;
265    }
266
267    NewTabUI::SetUrlTitleAndDirection(page_value,
268                                      url.title,
269                                      url.url);
270    pages_value_->Append(page_value);
271  }
272}
273
274void MostVisitedHandler::OnMostVisitedUrlsAvailable(
275    const history::MostVisitedURLList& data) {
276  SetPagesValueFromTopSites(data);
277  if (got_first_most_visited_request_) {
278    SendPagesValue();
279  }
280}
281
282void MostVisitedHandler::Observe(int type,
283                                 const content::NotificationSource& source,
284                                 const content::NotificationDetails& details) {
285  DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED);
286
287  // Most visited urls changed, query again.
288  StartQueryForMostVisited();
289}
290
291void MostVisitedHandler::BlacklistUrl(const GURL& url) {
292  history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites();
293  if (ts)
294    ts->AddBlacklistedURL(url);
295  content::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted"));
296}
297
298std::string MostVisitedHandler::GetDictionaryKeyForUrl(const std::string& url) {
299  return base::MD5String(url);
300}
301
302void MostVisitedHandler::RemovePageValuesMatchingOpenTabs() {
303#if !defined(OS_ANDROID)
304  TabStripModel* tab_strip_model = chrome::FindBrowserWithWebContents(
305      web_ui()->GetWebContents())->tab_strip_model();
306  history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites();
307  if (!tab_strip_model || !ts) {
308    NOTREACHED();
309    return;
310  }
311
312  // Iterate through most visited suggestions and remove pages already open in
313  // current browser, making sure to not drop below 8 suggestions.
314  std::set<std::string> open_urls;
315  GetOpenUrls(*tab_strip_model, *ts, &open_urls);
316  size_t i = 0;
317  while (i < pages_value_->GetSize() &&
318         pages_value_->GetSize() > kMinUrlSuggestions) {
319    base::DictionaryValue* page_value;
320    std::string url;
321    if (pages_value_->GetDictionary(i, &page_value) &&
322        page_value->GetString("url", &url) &&
323        open_urls.count(url) != 0) {
324      pages_value_->Remove(*page_value, &i);
325    } else {
326      ++i;
327    }
328  }
329#endif
330}
331
332// static
333void MostVisitedHandler::RegisterProfilePrefs(
334    user_prefs::PrefRegistrySyncable* registry) {
335  registry->RegisterDictionaryPref(
336      prefs::kNtpMostVisitedURLsBlacklist,
337      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
338}
339