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