Chromium Code Reviews| Index: chrome/browser/ui/webui/ntp/suggested_page_handler.cc |
| diff --git a/chrome/browser/ui/webui/ntp/suggested_page_handler.cc b/chrome/browser/ui/webui/ntp/suggested_page_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d3f18d630d92064c725a3403ad5ff61822e18873 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/ntp/suggested_page_handler.cc |
| @@ -0,0 +1,197 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/webui/ntp/suggested_page_handler.h" |
| + |
| +#include <set> |
| + |
| +#include "base/bind.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/command_line.h" |
| +#include "base/md5.h" |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/memory/singleton.h" |
| +#include "base/string16.h" |
| +#include "base/string_number_conversions.h" |
| +#include "base/threading/thread.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/history/page_usage_data.h" |
| +#include "chrome/browser/history/top_sites.h" |
| +#include "chrome/browser/prefs/pref_service.h" |
| +#include "chrome/browser/prefs/scoped_user_pref_update.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| +#include "chrome/browser/ui/webui/favicon_source.h" |
| +#include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
| +#include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
| +#include "chrome/common/chrome_notification_types.h" |
| +#include "chrome/common/pref_names.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/notification_source.h" |
| +#include "content/public/browser/user_metrics.h" |
| +#include "content/public/browser/web_ui.h" |
| +#include "googleurl/src/gurl.h" |
| +#include "grit/chromium_strings.h" |
| +#include "grit/generated_resources.h" |
| +#include "grit/locale_settings.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +using content::UserMetricsAction; |
| + |
| +SuggestedHandler::SuggestedHandler() |
| + : got_first_suggested_request_(false) { |
| +} |
| + |
| +SuggestedHandler::~SuggestedHandler() { |
| +} |
| + |
| +void SuggestedHandler::RegisterMessages() { |
| + Profile* profile = Profile::FromWebUI(web_ui()); |
| + // Set up our sources for thumbnail and favicon data. |
| + ThumbnailSource* thumbnail_src = new ThumbnailSource(profile); |
| + 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.
|
| + |
|
Dan Beam
2012/02/09 01:43:06
nit: remove newline
GeorgeY
2012/02/10 00:00:36
Done.
|
| + profile->GetChromeURLDataManager()->AddDataSource( |
| + new FaviconSource(profile, FaviconSource::FAVICON)); |
| + |
| + // 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.
|
| + 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.
|
| + if (ts) { |
| + // TopSites updates itself after a delay. This is especially noticable when |
| + // your profile is empty. Ask TopSites to update itself when we're about to |
| + // show the new tab page. |
| + ts->SyncWithHistory(); |
| + |
| + // Register for notification when TopSites changes so that we can update |
| + // ourself. |
| + registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, |
| + content::Source<history::TopSites>(ts)); |
| + } |
| + |
| + // We pre-emptively make a fetch for the available pages so we have the |
| + // results sooner. |
| + StartQueryForSuggested(); |
| + |
| + web_ui()->RegisterMessageCallback("getSuggested", |
| + base::Bind(&SuggestedHandler::HandleGetSuggested, |
| + base::Unretained(this))); |
|
Dan Beam
2012/02/09 01:43:06
nit: no newline
GeorgeY
2012/02/10 00:00:36
Done.
|
| + |
| + // Register ourselves for any suggested item blacklisting. |
| + web_ui()->RegisterMessageCallback("blacklistURLFromSuggested", |
| + base::Bind(&SuggestedHandler::HandleBlacklistURL, |
| + base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback("removeURLsFromSuggestedBlacklist", |
| + base::Bind(&SuggestedHandler::HandleRemoveURLsFromBlacklist, |
| + base::Unretained(this))); |
| + web_ui()->RegisterMessageCallback("clearSuggestedURLsBlacklist", |
| + base::Bind(&SuggestedHandler::HandleClearBlacklist, |
| + base::Unretained(this))); |
| +} |
| + |
| +void SuggestedHandler::HandleGetSuggested(const ListValue* args) { |
| + 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
|
| + return; |
| + if (!got_first_suggested_request_) { |
| + // If our initial data is already here, return it. |
| + SendPagesValue(); |
| + got_first_suggested_request_ = true; |
| + } else { |
| + StartQueryForSuggested(); |
| + } |
| +} |
| + |
| +void SuggestedHandler::SendPagesValue() { |
| + if (pages_value_.get()) { |
| + // TODO(georgey) add actual blacklist. |
| + bool has_blacklisted_urls = false; |
| + base::FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); |
| + web_ui()->CallJavascriptFunction("setSuggestedPages", |
| + *(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.
|
| + has_blacklisted_urls_value); |
| + pages_value_.reset(); |
| + } |
| +} |
| + |
| +void SuggestedHandler::StartQueryForSuggested() { |
| + // TODO(georgey) change it to provide our data. |
| + history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); |
| + if (ts) { |
|
Dan Beam
2012/02/09 01:43:06
when is this NULL?
GeorgeY
2012/02/10 00:00:36
AFAIK, for TestProfileImpl
|
| + ts->GetMostVisitedURLs( |
| + &topsites_consumer_, |
| + base::Bind(&SuggestedHandler::OnSuggestedURLsAvailable, |
| + base::Unretained(this))); |
| + } |
| +} |
| + |
| +void SuggestedHandler::HandleBlacklistURL(const ListValue* args) { |
| + if (!NewTabUI::IsSuggestedPageEnabled()) |
| + return; |
| + std::string url = UTF16ToUTF8(ExtractStringValue(args)); |
| + BlacklistURL(GURL(url)); |
| +} |
| + |
| +void SuggestedHandler::HandleRemoveURLsFromBlacklist(const ListValue* args) { |
| + if (!NewTabUI::IsSuggestedPageEnabled()) |
| + return; |
| + 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.
|
| + // TODO(georgey) remove URLs from blacklist. |
| +} |
| + |
| +void SuggestedHandler::HandleClearBlacklist(const ListValue* args) { |
| + if (!NewTabUI::IsSuggestedPageEnabled()) |
| + return; |
| + // 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
|
| +} |
| + |
| +void SuggestedHandler::SetPagesValueFromTopSites( |
| + const history::MostVisitedURLList& data) { |
| + // TODO(georgey) - change to our suggested pages - right now as a test |
| + // returns top 20 sites in reverse order. |
| + 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.
|
| + for (size_t i = 0; i < data.size(); i++) { |
| + 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.
|
| + DictionaryValue* page_value = new DictionaryValue(); |
| + if (url.url.is_empty()) { |
| + page_value->SetBoolean("filler", true); |
| + pages_value_->Append(page_value); |
| + continue; |
| + } |
| + |
| + NewTabUI::SetURLTitleAndDirection(page_value, |
| + url.title, |
| + url.url); |
| + pages_value_->Append(page_value); |
| + } |
| +} |
| + |
| +void SuggestedHandler::OnSuggestedURLsAvailable( |
| + const history::MostVisitedURLList& data) { |
| + SetPagesValueFromTopSites(data); |
| + if (got_first_suggested_request_) |
| + SendPagesValue(); |
| +} |
| + |
| +void SuggestedHandler::Observe(int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) { |
| + DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); |
| + |
| + // Most visited urls changed, query again. |
| + StartQueryForSuggested(); |
| +} |
| + |
| +void SuggestedHandler::BlacklistURL(const GURL& url) { |
| + // 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
|
| +} |
| + |
| +std::string SuggestedHandler::GetDictionaryKeyForURL(const std::string& url) { |
| + return base::MD5String(url); |
| +} |
| + |
| +// static |
| +void SuggestedHandler::RegisterUserPrefs(PrefService* prefs) { |
| + // TODO(georgey) add user preferences (such as own blacklist) as needed. |
| +} |