Chromium Code Reviews| OLD | NEW |
|---|---|
| (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" | |
|
Dan Beam
2012/02/28 20:14:24
are all these still necessary?
GeorgeY
2012/02/28 21:17:42
For now yes, as it mirrors the MostVisited behavio
Dan Beam
2012/02/28 22:30:12
The reason I ask is because I searched for l10n in
GeorgeY
2012/02/29 00:15:45
Oh, cool, more for me to remove :) haven't gone th
| |
| 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 page_value->SetBoolean("filler", true); | |
|
Dan Beam
2012/02/28 20:14:24
if they're always fillers, can you remove this log
GeorgeY
2012/02/28 21:17:42
OK, it was removed in the other cl anyway, removed
| |
| 150 pages_value_->Append(page_value); | |
| 151 continue; | |
| 152 } | |
| 153 | |
| 154 NewTabUI::SetURLTitleAndDirection(page_value, | |
| 155 suggested_url.title, | |
| 156 suggested_url.url); | |
| 157 pages_value_->Append(page_value); | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 void SuggestionsHandler::OnSuggestionsURLsAvailable( | |
| 162 const history::MostVisitedURLList& data) { | |
| 163 SetPagesValueFromTopSites(data); | |
| 164 if (got_first_suggestions_request_) | |
| 165 SendPagesValue(); | |
| 166 } | |
| 167 | |
| 168 void SuggestionsHandler::Observe(int type, | |
| 169 const content::NotificationSource& source, | |
| 170 const content::NotificationDetails& details) { | |
| 171 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); | |
| 172 | |
| 173 // Most visited urls changed, query again. | |
|
Dan Beam
2012/02/28 20:14:24
s/most visited/
GeorgeY
2012/02/28 21:17:42
Done.
| |
| 174 StartQueryForSuggestions(); | |
| 175 } | |
| 176 | |
| 177 void SuggestionsHandler::BlacklistURL(const GURL& url) { | |
| 178 // TODO(georgey) blacklist an URL. | |
| 179 } | |
| 180 | |
| 181 std::string SuggestionsHandler::GetDictionaryKeyForURL(const std::string& url) { | |
| 182 return base::MD5String(url); | |
| 183 } | |
| 184 | |
| 185 // static | |
| 186 void SuggestionsHandler::RegisterUserPrefs(PrefService* prefs) { | |
| 187 // TODO(georgey) add user preferences (such as own blacklist) as needed. | |
| 188 } | |
| OLD | NEW |