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