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/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/histogram.h" | |
16 #include "base/prefs/pref_service.h" | |
17 #include "base/prefs/scoped_user_pref_update.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/favicon/fallback_icon_service_factory.h" | |
24 #include "chrome/browser/favicon/large_icon_service_factory.h" | |
25 #include "chrome/browser/history/top_sites_factory.h" | |
26 #include "chrome/browser/profiles/profile.h" | |
27 #include "chrome/browser/thumbnails/thumbnail_list_source.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/tabs/tab_strip_model_utils.h" | |
32 #include "chrome/browser/ui/webui/fallback_icon_source.h" | |
33 #include "chrome/browser/ui/webui/favicon_source.h" | |
34 #include "chrome/browser/ui/webui/large_icon_source.h" | |
35 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | |
36 #include "chrome/browser/ui/webui/ntp/ntp_stats.h" | |
37 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | |
38 #include "chrome/common/pref_names.h" | |
39 #include "chrome/common/url_constants.h" | |
40 #include "components/favicon/core/fallback_icon_service.h" | |
41 #include "components/favicon/core/large_icon_service.h" | |
42 #include "components/history/core/browser/page_usage_data.h" | |
43 #include "components/history/core/browser/top_sites.h" | |
44 #include "components/keyed_service/core/service_access_type.h" | |
45 #include "components/pref_registry/pref_registry_syncable.h" | |
46 #include "content/public/browser/navigation_controller.h" | |
47 #include "content/public/browser/navigation_entry.h" | |
48 #include "content/public/browser/url_data_source.h" | |
49 #include "content/public/browser/user_metrics.h" | |
50 #include "content/public/browser/web_contents.h" | |
51 #include "content/public/browser/web_ui.h" | |
52 #include "url/gurl.h" | |
53 | |
54 using base::UserMetricsAction; | |
55 | |
56 MostVisitedHandler::MostVisitedHandler() | |
57 : scoped_observer_(this), | |
58 got_first_most_visited_request_(false), | |
59 most_visited_viewed_(false), | |
60 user_action_logged_(false), | |
61 weak_ptr_factory_(this) { | |
62 } | |
63 | |
64 MostVisitedHandler::~MostVisitedHandler() { | |
65 if (!user_action_logged_ && most_visited_viewed_) { | |
66 const GURL ntp_url = GURL(chrome::kChromeUINewTabURL); | |
67 int action_id = NTP_FOLLOW_ACTION_OTHER; | |
68 content::NavigationEntry* entry = | |
69 web_ui()->GetWebContents()->GetController().GetLastCommittedEntry(); | |
70 if (entry && (entry->GetURL() != ntp_url)) { | |
71 action_id = | |
72 ui::PageTransitionStripQualifier(entry->GetTransitionType()); | |
73 } | |
74 | |
75 UMA_HISTOGRAM_ENUMERATION("NewTabPage.MostVisitedAction", action_id, | |
76 NUM_NTP_FOLLOW_ACTIONS); | |
77 } | |
78 } | |
79 | |
80 void MostVisitedHandler::RegisterMessages() { | |
81 Profile* profile = Profile::FromWebUI(web_ui()); | |
82 // Set up our sources for thumbnail and favicon data. | |
83 content::URLDataSource::Add(profile, new ThumbnailSource(profile, false)); | |
84 content::URLDataSource::Add(profile, new ThumbnailSource(profile, true)); | |
85 | |
86 // Set up our sources for top-sites data. | |
87 content::URLDataSource::Add(profile, new ThumbnailListSource(profile)); | |
88 | |
89 favicon::FallbackIconService* fallback_icon_service = | |
90 FallbackIconServiceFactory::GetForBrowserContext(profile); | |
91 favicon::LargeIconService* large_icon_service = | |
92 LargeIconServiceFactory::GetForBrowserContext(profile); | |
93 | |
94 // Register chrome://fallback-icon as a data source for fallback icons. | |
95 content::URLDataSource::Add(profile, | |
96 new FallbackIconSource(fallback_icon_service)); | |
97 | |
98 // Register chrome://favicon as a data source for favicons. | |
99 content::URLDataSource::Add( | |
100 profile, new FaviconSource(profile, FaviconSource::FAVICON)); | |
101 | |
102 // Register chrome://large-icon as a data source for large icons. | |
103 content::URLDataSource::Add( | |
104 profile, new LargeIconSource(fallback_icon_service, large_icon_service)); | |
105 | |
106 scoped_refptr<history::TopSites> top_sites = | |
107 TopSitesFactory::GetForProfile(profile); | |
108 if (top_sites) { | |
109 // TopSites updates itself after a delay. This is especially noticable when | |
110 // your profile is empty. Ask TopSites to update itself when we're about to | |
111 // show the new tab page. | |
112 top_sites->SyncWithHistory(); | |
113 | |
114 // Register as TopSitesObserver so that we can update ourselves when the | |
115 // TopSites changes. | |
116 scoped_observer_.Add(top_sites.get()); | |
117 } | |
118 | |
119 // We pre-emptively make a fetch for the most visited pages so we have the | |
120 // results sooner. | |
121 StartQueryForMostVisited(); | |
122 | |
123 web_ui()->RegisterMessageCallback("getMostVisited", | |
124 base::Bind(&MostVisitedHandler::HandleGetMostVisited, | |
125 base::Unretained(this))); | |
126 | |
127 // Register ourselves for any most-visited item blacklisting. | |
128 web_ui()->RegisterMessageCallback("blacklistURLFromMostVisited", | |
129 base::Bind(&MostVisitedHandler::HandleBlacklistUrl, | |
130 base::Unretained(this))); | |
131 web_ui()->RegisterMessageCallback("removeURLsFromMostVisitedBlacklist", | |
132 base::Bind(&MostVisitedHandler::HandleRemoveUrlsFromBlacklist, | |
133 base::Unretained(this))); | |
134 web_ui()->RegisterMessageCallback("clearMostVisitedURLsBlacklist", | |
135 base::Bind(&MostVisitedHandler::HandleClearBlacklist, | |
136 base::Unretained(this))); | |
137 web_ui()->RegisterMessageCallback("mostVisitedAction", | |
138 base::Bind(&MostVisitedHandler::HandleMostVisitedAction, | |
139 base::Unretained(this))); | |
140 web_ui()->RegisterMessageCallback("mostVisitedSelected", | |
141 base::Bind(&MostVisitedHandler::HandleMostVisitedSelected, | |
142 base::Unretained(this))); | |
143 } | |
144 | |
145 void MostVisitedHandler::HandleGetMostVisited(const base::ListValue* args) { | |
146 if (!got_first_most_visited_request_) { | |
147 // If our initial data is already here, return it. | |
148 SendPagesValue(); | |
149 got_first_most_visited_request_ = true; | |
150 } else { | |
151 StartQueryForMostVisited(); | |
152 } | |
153 } | |
154 | |
155 void MostVisitedHandler::SendPagesValue() { | |
156 if (pages_value_) { | |
157 Profile* profile = Profile::FromWebUI(web_ui()); | |
158 const base::DictionaryValue* url_blacklist = | |
159 profile->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist); | |
160 bool has_blacklisted_urls = !url_blacklist->empty(); | |
161 scoped_refptr<history::TopSites> ts = | |
162 TopSitesFactory::GetForProfile(profile); | |
163 if (ts) | |
164 has_blacklisted_urls = ts->HasBlacklistedItems(); | |
165 | |
166 base::FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); | |
167 web_ui()->CallJavascriptFunction("ntp.setMostVisitedPages", | |
168 *pages_value_, | |
169 has_blacklisted_urls_value); | |
170 pages_value_.reset(); | |
171 } | |
172 } | |
173 | |
174 void MostVisitedHandler::StartQueryForMostVisited() { | |
175 scoped_refptr<history::TopSites> ts = | |
176 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); | |
177 if (ts) { | |
178 ts->GetMostVisitedURLs( | |
179 base::Bind(&MostVisitedHandler::OnMostVisitedUrlsAvailable, | |
180 weak_ptr_factory_.GetWeakPtr()), false); | |
181 } | |
182 } | |
183 | |
184 void MostVisitedHandler::HandleBlacklistUrl(const base::ListValue* args) { | |
185 std::string url = base::UTF16ToUTF8(ExtractStringValue(args)); | |
186 BlacklistUrl(GURL(url)); | |
187 } | |
188 | |
189 void MostVisitedHandler::HandleRemoveUrlsFromBlacklist( | |
190 const base::ListValue* args) { | |
191 DCHECK(args->GetSize() != 0); | |
192 | |
193 for (base::ListValue::const_iterator iter = args->begin(); | |
194 iter != args->end(); ++iter) { | |
195 std::string url; | |
196 bool r = (*iter)->GetAsString(&url); | |
197 if (!r) { | |
198 NOTREACHED(); | |
199 return; | |
200 } | |
201 content::RecordAction(UserMetricsAction("MostVisited_UrlRemoved")); | |
202 scoped_refptr<history::TopSites> ts = | |
203 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); | |
204 if (ts) | |
205 ts->RemoveBlacklistedURL(GURL(url)); | |
206 } | |
207 } | |
208 | |
209 void MostVisitedHandler::HandleClearBlacklist(const base::ListValue* args) { | |
210 content::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); | |
211 | |
212 scoped_refptr<history::TopSites> ts = | |
213 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); | |
214 if (ts) | |
215 ts->ClearBlacklistedURLs(); | |
216 } | |
217 | |
218 void MostVisitedHandler::HandleMostVisitedAction(const base::ListValue* args) { | |
219 DCHECK(args); | |
220 | |
221 double action_id; | |
222 if (!args->GetDouble(0, &action_id)) | |
223 NOTREACHED(); | |
224 | |
225 UMA_HISTOGRAM_ENUMERATION("NewTabPage.MostVisitedAction", | |
226 static_cast<int>(action_id), | |
227 NUM_NTP_FOLLOW_ACTIONS); | |
228 most_visited_viewed_ = true; | |
229 user_action_logged_ = true; | |
230 } | |
231 | |
232 void MostVisitedHandler::HandleMostVisitedSelected( | |
233 const base::ListValue* args) { | |
234 most_visited_viewed_ = true; | |
235 } | |
236 | |
237 void MostVisitedHandler::SetPagesValueFromTopSites( | |
238 const history::MostVisitedURLList& data) { | |
239 pages_value_.reset(new base::ListValue); | |
240 | |
241 history::MostVisitedURLList top_sites(data); | |
242 for (size_t i = 0; i < top_sites.size(); i++) { | |
243 const history::MostVisitedURL& url = top_sites[i]; | |
244 | |
245 // The items which are to be written into |page_value| are also described in | |
246 // chrome/browser/resources/ntp4/new_tab.js in @typedef for PageData. Please | |
247 // update it whenever you add or remove any keys here. | |
248 base::DictionaryValue* page_value = new base::DictionaryValue(); | |
249 if (url.url.is_empty()) { | |
250 page_value->SetBoolean("filler", true); | |
251 pages_value_->Append(page_value); | |
252 continue; | |
253 } | |
254 | |
255 NewTabUI::SetUrlTitleAndDirection(page_value, | |
256 url.title, | |
257 url.url); | |
258 pages_value_->Append(page_value); | |
259 } | |
260 } | |
261 | |
262 void MostVisitedHandler::OnMostVisitedUrlsAvailable( | |
263 const history::MostVisitedURLList& data) { | |
264 SetPagesValueFromTopSites(data); | |
265 if (got_first_most_visited_request_) { | |
266 SendPagesValue(); | |
267 } | |
268 } | |
269 | |
270 void MostVisitedHandler::TopSitesLoaded(history::TopSites* top_sites) { | |
271 } | |
272 | |
273 void MostVisitedHandler::TopSitesChanged(history::TopSites* top_sites) { | |
274 // Most visited urls changed, query again. | |
275 StartQueryForMostVisited(); | |
276 } | |
277 | |
278 void MostVisitedHandler::BlacklistUrl(const GURL& url) { | |
279 scoped_refptr<history::TopSites> ts = | |
280 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); | |
281 if (ts) | |
282 ts->AddBlacklistedURL(url); | |
283 content::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); | |
284 } | |
285 | |
286 std::string MostVisitedHandler::GetDictionaryKeyForUrl(const std::string& url) { | |
287 return base::MD5String(url); | |
288 } | |
289 | |
290 // static | |
291 void MostVisitedHandler::RegisterProfilePrefs( | |
292 user_prefs::PrefRegistrySyncable* registry) { | |
293 registry->RegisterDictionaryPref(prefs::kNtpMostVisitedURLsBlacklist); | |
294 } | |
OLD | NEW |