OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/webui/ntp/most_visited_handler.h" | 5 #include "chrome/browser/ui/webui/ntp/most_visited_handler.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/md5.h" | 12 #include "base/md5.h" |
13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
16 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
17 #include "base/prefs/scoped_user_pref_update.h" | 17 #include "base/prefs/scoped_user_pref_update.h" |
18 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
21 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
22 #include "base/values.h" | 22 #include "base/values.h" |
23 #include "chrome/browser/history/top_sites.h" | 23 #include "chrome/browser/history/top_sites.h" |
| 24 #include "chrome/browser/history/top_sites_factory.h" |
24 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
25 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 26 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
26 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
27 #include "chrome/browser/ui/browser_finder.h" | 28 #include "chrome/browser/ui/browser_finder.h" |
28 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 29 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
29 #include "chrome/browser/ui/tabs/tab_strip_model_utils.h" | 30 #include "chrome/browser/ui/tabs/tab_strip_model_utils.h" |
30 #include "chrome/browser/ui/webui/favicon_source.h" | 31 #include "chrome/browser/ui/webui/favicon_source.h" |
31 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 32 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
32 #include "chrome/browser/ui/webui/ntp/ntp_stats.h" | 33 #include "chrome/browser/ui/webui/ntp/ntp_stats.h" |
33 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | 34 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 content::URLDataSource::Add(profile, new ThumbnailSource(profile, false)); | 76 content::URLDataSource::Add(profile, new ThumbnailSource(profile, false)); |
76 content::URLDataSource::Add(profile, new ThumbnailSource(profile, true)); | 77 content::URLDataSource::Add(profile, new ThumbnailSource(profile, true)); |
77 | 78 |
78 // Set up our sources for top-sites data. | 79 // Set up our sources for top-sites data. |
79 content::URLDataSource::Add(profile, new ThumbnailListSource(profile)); | 80 content::URLDataSource::Add(profile, new ThumbnailListSource(profile)); |
80 | 81 |
81 // Register chrome://favicon as a data source for favicons. | 82 // Register chrome://favicon as a data source for favicons. |
82 content::URLDataSource::Add( | 83 content::URLDataSource::Add( |
83 profile, new FaviconSource(profile, FaviconSource::FAVICON)); | 84 profile, new FaviconSource(profile, FaviconSource::FAVICON)); |
84 | 85 |
85 history::TopSites* top_sites = profile->GetTopSites(); | 86 scoped_refptr<history::TopSites> top_sites = |
| 87 TopSitesFactory::GetForProfile(profile); |
86 if (top_sites) { | 88 if (top_sites) { |
87 // TopSites updates itself after a delay. This is especially noticable when | 89 // TopSites updates itself after a delay. This is especially noticable when |
88 // your profile is empty. Ask TopSites to update itself when we're about to | 90 // your profile is empty. Ask TopSites to update itself when we're about to |
89 // show the new tab page. | 91 // show the new tab page. |
90 top_sites->SyncWithHistory(); | 92 top_sites->SyncWithHistory(); |
91 | 93 |
92 // Register as TopSitesObserver so that we can update ourselves when the | 94 // Register as TopSitesObserver so that we can update ourselves when the |
93 // TopSites changes. | 95 // TopSites changes. |
94 scoped_observer_.Add(top_sites); | 96 scoped_observer_.Add(top_sites.get()); |
95 } | 97 } |
96 | 98 |
97 // We pre-emptively make a fetch for the most visited pages so we have the | 99 // We pre-emptively make a fetch for the most visited pages so we have the |
98 // results sooner. | 100 // results sooner. |
99 StartQueryForMostVisited(); | 101 StartQueryForMostVisited(); |
100 | 102 |
101 web_ui()->RegisterMessageCallback("getMostVisited", | 103 web_ui()->RegisterMessageCallback("getMostVisited", |
102 base::Bind(&MostVisitedHandler::HandleGetMostVisited, | 104 base::Bind(&MostVisitedHandler::HandleGetMostVisited, |
103 base::Unretained(this))); | 105 base::Unretained(this))); |
104 | 106 |
(...skipping 24 matching lines...) Expand all Loading... |
129 StartQueryForMostVisited(); | 131 StartQueryForMostVisited(); |
130 } | 132 } |
131 } | 133 } |
132 | 134 |
133 void MostVisitedHandler::SendPagesValue() { | 135 void MostVisitedHandler::SendPagesValue() { |
134 if (pages_value_) { | 136 if (pages_value_) { |
135 Profile* profile = Profile::FromWebUI(web_ui()); | 137 Profile* profile = Profile::FromWebUI(web_ui()); |
136 const base::DictionaryValue* url_blacklist = | 138 const base::DictionaryValue* url_blacklist = |
137 profile->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist); | 139 profile->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist); |
138 bool has_blacklisted_urls = !url_blacklist->empty(); | 140 bool has_blacklisted_urls = !url_blacklist->empty(); |
139 history::TopSites* ts = profile->GetTopSites(); | 141 scoped_refptr<history::TopSites> ts = |
| 142 TopSitesFactory::GetForProfile(profile); |
140 if (ts) | 143 if (ts) |
141 has_blacklisted_urls = ts->HasBlacklistedItems(); | 144 has_blacklisted_urls = ts->HasBlacklistedItems(); |
142 | 145 |
143 base::FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); | 146 base::FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); |
144 web_ui()->CallJavascriptFunction("ntp.setMostVisitedPages", | 147 web_ui()->CallJavascriptFunction("ntp.setMostVisitedPages", |
145 *pages_value_, | 148 *pages_value_, |
146 has_blacklisted_urls_value); | 149 has_blacklisted_urls_value); |
147 pages_value_.reset(); | 150 pages_value_.reset(); |
148 } | 151 } |
149 } | 152 } |
150 | 153 |
151 void MostVisitedHandler::StartQueryForMostVisited() { | 154 void MostVisitedHandler::StartQueryForMostVisited() { |
152 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 155 scoped_refptr<history::TopSites> ts = |
| 156 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
153 if (ts) { | 157 if (ts) { |
154 ts->GetMostVisitedURLs( | 158 ts->GetMostVisitedURLs( |
155 base::Bind(&MostVisitedHandler::OnMostVisitedUrlsAvailable, | 159 base::Bind(&MostVisitedHandler::OnMostVisitedUrlsAvailable, |
156 weak_ptr_factory_.GetWeakPtr()), false); | 160 weak_ptr_factory_.GetWeakPtr()), false); |
157 } | 161 } |
158 } | 162 } |
159 | 163 |
160 void MostVisitedHandler::HandleBlacklistUrl(const base::ListValue* args) { | 164 void MostVisitedHandler::HandleBlacklistUrl(const base::ListValue* args) { |
161 std::string url = base::UTF16ToUTF8(ExtractStringValue(args)); | 165 std::string url = base::UTF16ToUTF8(ExtractStringValue(args)); |
162 BlacklistUrl(GURL(url)); | 166 BlacklistUrl(GURL(url)); |
163 } | 167 } |
164 | 168 |
165 void MostVisitedHandler::HandleRemoveUrlsFromBlacklist( | 169 void MostVisitedHandler::HandleRemoveUrlsFromBlacklist( |
166 const base::ListValue* args) { | 170 const base::ListValue* args) { |
167 DCHECK(args->GetSize() != 0); | 171 DCHECK(args->GetSize() != 0); |
168 | 172 |
169 for (base::ListValue::const_iterator iter = args->begin(); | 173 for (base::ListValue::const_iterator iter = args->begin(); |
170 iter != args->end(); ++iter) { | 174 iter != args->end(); ++iter) { |
171 std::string url; | 175 std::string url; |
172 bool r = (*iter)->GetAsString(&url); | 176 bool r = (*iter)->GetAsString(&url); |
173 if (!r) { | 177 if (!r) { |
174 NOTREACHED(); | 178 NOTREACHED(); |
175 return; | 179 return; |
176 } | 180 } |
177 content::RecordAction(UserMetricsAction("MostVisited_UrlRemoved")); | 181 content::RecordAction(UserMetricsAction("MostVisited_UrlRemoved")); |
178 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 182 scoped_refptr<history::TopSites> ts = |
| 183 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
179 if (ts) | 184 if (ts) |
180 ts->RemoveBlacklistedURL(GURL(url)); | 185 ts->RemoveBlacklistedURL(GURL(url)); |
181 } | 186 } |
182 } | 187 } |
183 | 188 |
184 void MostVisitedHandler::HandleClearBlacklist(const base::ListValue* args) { | 189 void MostVisitedHandler::HandleClearBlacklist(const base::ListValue* args) { |
185 content::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); | 190 content::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); |
186 | 191 |
187 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 192 scoped_refptr<history::TopSites> ts = |
| 193 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
188 if (ts) | 194 if (ts) |
189 ts->ClearBlacklistedURLs(); | 195 ts->ClearBlacklistedURLs(); |
190 } | 196 } |
191 | 197 |
192 void MostVisitedHandler::HandleMostVisitedAction(const base::ListValue* args) { | 198 void MostVisitedHandler::HandleMostVisitedAction(const base::ListValue* args) { |
193 DCHECK(args); | 199 DCHECK(args); |
194 | 200 |
195 double action_id; | 201 double action_id; |
196 if (!args->GetDouble(0, &action_id)) | 202 if (!args->GetDouble(0, &action_id)) |
197 NOTREACHED(); | 203 NOTREACHED(); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 | 249 |
244 void MostVisitedHandler::TopSitesLoaded(history::TopSites* top_sites) { | 250 void MostVisitedHandler::TopSitesLoaded(history::TopSites* top_sites) { |
245 } | 251 } |
246 | 252 |
247 void MostVisitedHandler::TopSitesChanged(history::TopSites* top_sites) { | 253 void MostVisitedHandler::TopSitesChanged(history::TopSites* top_sites) { |
248 // Most visited urls changed, query again. | 254 // Most visited urls changed, query again. |
249 StartQueryForMostVisited(); | 255 StartQueryForMostVisited(); |
250 } | 256 } |
251 | 257 |
252 void MostVisitedHandler::BlacklistUrl(const GURL& url) { | 258 void MostVisitedHandler::BlacklistUrl(const GURL& url) { |
253 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 259 scoped_refptr<history::TopSites> ts = |
| 260 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
254 if (ts) | 261 if (ts) |
255 ts->AddBlacklistedURL(url); | 262 ts->AddBlacklistedURL(url); |
256 content::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); | 263 content::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); |
257 } | 264 } |
258 | 265 |
259 std::string MostVisitedHandler::GetDictionaryKeyForUrl(const std::string& url) { | 266 std::string MostVisitedHandler::GetDictionaryKeyForUrl(const std::string& url) { |
260 return base::MD5String(url); | 267 return base::MD5String(url); |
261 } | 268 } |
262 | 269 |
263 // static | 270 // static |
264 void MostVisitedHandler::RegisterProfilePrefs( | 271 void MostVisitedHandler::RegisterProfilePrefs( |
265 user_prefs::PrefRegistrySyncable* registry) { | 272 user_prefs::PrefRegistrySyncable* registry) { |
266 registry->RegisterDictionaryPref( | 273 registry->RegisterDictionaryPref( |
267 prefs::kNtpMostVisitedURLsBlacklist, | 274 prefs::kNtpMostVisitedURLsBlacklist, |
268 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 275 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
269 } | 276 } |
OLD | NEW |