OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/callback.h" | 9 #include "base/callback.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 }; | 47 }; |
48 | 48 |
49 MostVisitedHandler::MostVisitedHandler() | 49 MostVisitedHandler::MostVisitedHandler() |
50 : got_first_most_visited_request_(false) { | 50 : got_first_most_visited_request_(false) { |
51 } | 51 } |
52 | 52 |
53 MostVisitedHandler::~MostVisitedHandler() { | 53 MostVisitedHandler::~MostVisitedHandler() { |
54 } | 54 } |
55 | 55 |
56 WebUIMessageHandler* MostVisitedHandler::Attach(WebUI* web_ui) { | 56 WebUIMessageHandler* MostVisitedHandler::Attach(WebUI* web_ui) { |
57 Profile* profile = web_ui->GetProfile(); | 57 Profile* profile = Profile::FromWebUI(web_ui); |
58 // Set up our sources for thumbnail and favicon data. | 58 // Set up our sources for thumbnail and favicon data. |
59 ThumbnailSource* thumbnail_src = new ThumbnailSource(profile); | 59 ThumbnailSource* thumbnail_src = new ThumbnailSource(profile); |
60 profile->GetChromeURLDataManager()->AddDataSource(thumbnail_src); | 60 profile->GetChromeURLDataManager()->AddDataSource(thumbnail_src); |
61 | 61 |
62 profile->GetChromeURLDataManager()->AddDataSource( | 62 profile->GetChromeURLDataManager()->AddDataSource( |
63 new FaviconSource(profile, FaviconSource::FAVICON)); | 63 new FaviconSource(profile, FaviconSource::FAVICON)); |
64 | 64 |
65 WebUIMessageHandler* result = WebUIMessageHandler::Attach(web_ui); | 65 WebUIMessageHandler* result = WebUIMessageHandler::Attach(web_ui); |
66 | 66 |
67 history::TopSites* ts = profile->GetTopSites(); | 67 history::TopSites* ts = profile->GetTopSites(); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 // If our initial data is already here, return it. | 107 // If our initial data is already here, return it. |
108 SendPagesValue(); | 108 SendPagesValue(); |
109 got_first_most_visited_request_ = true; | 109 got_first_most_visited_request_ = true; |
110 } else { | 110 } else { |
111 StartQueryForMostVisited(); | 111 StartQueryForMostVisited(); |
112 } | 112 } |
113 } | 113 } |
114 | 114 |
115 void MostVisitedHandler::SendPagesValue() { | 115 void MostVisitedHandler::SendPagesValue() { |
116 if (pages_value_.get()) { | 116 if (pages_value_.get()) { |
117 Profile* profile = web_ui_->GetProfile(); | 117 Profile* profile = Profile::FromWebUI(web_ui_); |
118 const DictionaryValue* url_blacklist = | 118 const DictionaryValue* url_blacklist = |
119 profile->GetPrefs()->GetDictionary(prefs::kNTPMostVisitedURLsBlacklist); | 119 profile->GetPrefs()->GetDictionary(prefs::kNTPMostVisitedURLsBlacklist); |
120 bool has_blacklisted_urls = !url_blacklist->empty(); | 120 bool has_blacklisted_urls = !url_blacklist->empty(); |
121 history::TopSites* ts = profile->GetTopSites(); | 121 history::TopSites* ts = profile->GetTopSites(); |
122 if (ts) | 122 if (ts) |
123 has_blacklisted_urls = ts->HasBlacklistedItems(); | 123 has_blacklisted_urls = ts->HasBlacklistedItems(); |
124 FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); | 124 FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); |
125 web_ui_->CallJavascriptFunction("setMostVisitedPages", | 125 web_ui_->CallJavascriptFunction("setMostVisitedPages", |
126 *(pages_value_.get()), | 126 *(pages_value_.get()), |
127 has_blacklisted_urls_value); | 127 has_blacklisted_urls_value); |
128 pages_value_.reset(); | 128 pages_value_.reset(); |
129 } | 129 } |
130 } | 130 } |
131 | 131 |
132 void MostVisitedHandler::StartQueryForMostVisited() { | 132 void MostVisitedHandler::StartQueryForMostVisited() { |
133 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 133 history::TopSites* ts = Profile::FromWebUI(web_ui_)->GetTopSites(); |
134 if (ts) { | 134 if (ts) { |
135 ts->GetMostVisitedURLs( | 135 ts->GetMostVisitedURLs( |
136 &topsites_consumer_, | 136 &topsites_consumer_, |
137 NewCallback(this, &MostVisitedHandler::OnMostVisitedURLsAvailable)); | 137 NewCallback(this, &MostVisitedHandler::OnMostVisitedURLsAvailable)); |
138 } | 138 } |
139 } | 139 } |
140 | 140 |
141 void MostVisitedHandler::HandleBlacklistURL(const ListValue* args) { | 141 void MostVisitedHandler::HandleBlacklistURL(const ListValue* args) { |
142 std::string url = UTF16ToUTF8(ExtractStringValue(args)); | 142 std::string url = UTF16ToUTF8(ExtractStringValue(args)); |
143 BlacklistURL(GURL(url)); | 143 BlacklistURL(GURL(url)); |
144 } | 144 } |
145 | 145 |
146 void MostVisitedHandler::HandleRemoveURLsFromBlacklist(const ListValue* args) { | 146 void MostVisitedHandler::HandleRemoveURLsFromBlacklist(const ListValue* args) { |
147 DCHECK(args->GetSize() != 0); | 147 DCHECK(args->GetSize() != 0); |
148 | 148 |
149 for (ListValue::const_iterator iter = args->begin(); | 149 for (ListValue::const_iterator iter = args->begin(); |
150 iter != args->end(); ++iter) { | 150 iter != args->end(); ++iter) { |
151 std::string url; | 151 std::string url; |
152 bool r = (*iter)->GetAsString(&url); | 152 bool r = (*iter)->GetAsString(&url); |
153 if (!r) { | 153 if (!r) { |
154 NOTREACHED(); | 154 NOTREACHED(); |
155 return; | 155 return; |
156 } | 156 } |
157 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlRemoved")); | 157 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlRemoved")); |
158 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 158 history::TopSites* ts = Profile::FromWebUI(web_ui_)->GetTopSites(); |
159 if (ts) | 159 if (ts) |
160 ts->RemoveBlacklistedURL(GURL(url)); | 160 ts->RemoveBlacklistedURL(GURL(url)); |
161 } | 161 } |
162 } | 162 } |
163 | 163 |
164 void MostVisitedHandler::HandleClearBlacklist(const ListValue* args) { | 164 void MostVisitedHandler::HandleClearBlacklist(const ListValue* args) { |
165 UserMetrics::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); | 165 UserMetrics::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); |
166 | 166 |
167 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 167 history::TopSites* ts = Profile::FromWebUI(web_ui_)->GetTopSites(); |
168 if (ts) | 168 if (ts) |
169 ts->ClearBlacklistedURLs(); | 169 ts->ClearBlacklistedURLs(); |
170 } | 170 } |
171 | 171 |
172 void MostVisitedHandler::HandleAddPinnedURL(const ListValue* args) { | 172 void MostVisitedHandler::HandleAddPinnedURL(const ListValue* args) { |
173 DCHECK_EQ(5U, args->GetSize()) << "Wrong number of params to addPinnedURL"; | 173 DCHECK_EQ(5U, args->GetSize()) << "Wrong number of params to addPinnedURL"; |
174 MostVisitedPage mvp; | 174 MostVisitedPage mvp; |
175 std::string tmp_string; | 175 std::string tmp_string; |
176 string16 tmp_string16; | 176 string16 tmp_string16; |
177 int index; | 177 int index; |
(...skipping 19 matching lines...) Expand all Loading... |
197 mvp.thumbnail_url = GURL(tmp_string); | 197 mvp.thumbnail_url = GURL(tmp_string); |
198 | 198 |
199 r = args->GetString(4, &tmp_string); | 199 r = args->GetString(4, &tmp_string); |
200 DCHECK(r) << "Missing index in addPinnedURL from the NTP Most Visited."; | 200 DCHECK(r) << "Missing index in addPinnedURL from the NTP Most Visited."; |
201 base::StringToInt(tmp_string, &index); | 201 base::StringToInt(tmp_string, &index); |
202 | 202 |
203 AddPinnedURL(mvp, index); | 203 AddPinnedURL(mvp, index); |
204 } | 204 } |
205 | 205 |
206 void MostVisitedHandler::AddPinnedURL(const MostVisitedPage& page, int index) { | 206 void MostVisitedHandler::AddPinnedURL(const MostVisitedPage& page, int index) { |
207 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 207 history::TopSites* ts = Profile::FromWebUI(web_ui_)->GetTopSites(); |
208 if (ts) | 208 if (ts) |
209 ts->AddPinnedURL(page.url, index); | 209 ts->AddPinnedURL(page.url, index); |
210 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlPinned")); | 210 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlPinned")); |
211 } | 211 } |
212 | 212 |
213 void MostVisitedHandler::HandleRemovePinnedURL(const ListValue* args) { | 213 void MostVisitedHandler::HandleRemovePinnedURL(const ListValue* args) { |
214 std::string url = UTF16ToUTF8(ExtractStringValue(args)); | 214 std::string url = UTF16ToUTF8(ExtractStringValue(args)); |
215 RemovePinnedURL(GURL(url)); | 215 RemovePinnedURL(GURL(url)); |
216 } | 216 } |
217 | 217 |
218 void MostVisitedHandler::RemovePinnedURL(const GURL& url) { | 218 void MostVisitedHandler::RemovePinnedURL(const GURL& url) { |
219 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 219 history::TopSites* ts = Profile::FromWebUI(web_ui_)->GetTopSites(); |
220 if (ts) | 220 if (ts) |
221 ts->RemovePinnedURL(url); | 221 ts->RemovePinnedURL(url); |
222 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlUnpinned")); | 222 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlUnpinned")); |
223 } | 223 } |
224 | 224 |
225 bool MostVisitedHandler::GetPinnedURLAtIndex(int index, | 225 bool MostVisitedHandler::GetPinnedURLAtIndex(int index, |
226 MostVisitedPage* page) { | 226 MostVisitedPage* page) { |
227 // This iterates over all the pinned URLs. It might seem like it is worth | 227 // This iterates over all the pinned URLs. It might seem like it is worth |
228 // having a map from the index to the item but the number of items is limited | 228 // having a map from the index to the item but the number of items is limited |
229 // to the number of items the most visited section is showing on the NTP so | 229 // to the number of items the most visited section is showing on the NTP so |
230 // this will be fast enough for now. | 230 // this will be fast enough for now. |
231 PrefService* prefs = web_ui_->GetProfile()->GetPrefs(); | 231 PrefService* prefs = Profile::FromWebUI(web_ui_)->GetPrefs(); |
232 const DictionaryValue* pinned_urls = | 232 const DictionaryValue* pinned_urls = |
233 prefs->GetDictionary(prefs::kNTPMostVisitedPinnedURLs); | 233 prefs->GetDictionary(prefs::kNTPMostVisitedPinnedURLs); |
234 for (DictionaryValue::key_iterator it = pinned_urls->begin_keys(); | 234 for (DictionaryValue::key_iterator it = pinned_urls->begin_keys(); |
235 it != pinned_urls->end_keys(); ++it) { | 235 it != pinned_urls->end_keys(); ++it) { |
236 Value* value; | 236 Value* value; |
237 if (pinned_urls->GetWithoutPathExpansion(*it, &value)) { | 237 if (pinned_urls->GetWithoutPathExpansion(*it, &value)) { |
238 if (!value->IsType(DictionaryValue::TYPE_DICTIONARY)) { | 238 if (!value->IsType(DictionaryValue::TYPE_DICTIONARY)) { |
239 // Moved on to TopSites and now going back. | 239 // Moved on to TopSites and now going back. |
240 DictionaryPrefUpdate update(prefs, prefs::kNTPMostVisitedPinnedURLs); | 240 DictionaryPrefUpdate update(prefs, prefs::kNTPMostVisitedPinnedURLs); |
241 update.Get()->Clear(); | 241 update.Get()->Clear(); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 page_value->SetString("thumbnailUrl", | 290 page_value->SetString("thumbnailUrl", |
291 "chrome://theme/IDR_NEWTAB_CHROME_WELCOME_PAGE_THUMBNAIL"); | 291 "chrome://theme/IDR_NEWTAB_CHROME_WELCOME_PAGE_THUMBNAIL"); |
292 page_value->SetString("faviconDominantColor", "rgb(0, 147, 60)"); | 292 page_value->SetString("faviconDominantColor", "rgb(0, 147, 60)"); |
293 } else if (url.url.spec() == | 293 } else if (url.url.spec() == |
294 l10n_util::GetStringUTF8(IDS_WEBSTORE_URL)) { | 294 l10n_util::GetStringUTF8(IDS_WEBSTORE_URL)) { |
295 page_value->SetString("thumbnailUrl", | 295 page_value->SetString("thumbnailUrl", |
296 "chrome://theme/IDR_NEWTAB_WEBSTORE_THUMBNAIL"); | 296 "chrome://theme/IDR_NEWTAB_WEBSTORE_THUMBNAIL"); |
297 page_value->SetString("faviconDominantColor", "rgb(63, 132, 197)"); | 297 page_value->SetString("faviconDominantColor", "rgb(63, 132, 197)"); |
298 } | 298 } |
299 | 299 |
300 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 300 history::TopSites* ts = Profile::FromWebUI(web_ui_)->GetTopSites(); |
301 if (ts && ts->IsURLPinned(url.url)) | 301 if (ts && ts->IsURLPinned(url.url)) |
302 page_value->SetBoolean("pinned", true); | 302 page_value->SetBoolean("pinned", true); |
303 pages_value_->Append(page_value); | 303 pages_value_->Append(page_value); |
304 } | 304 } |
305 } | 305 } |
306 | 306 |
307 void MostVisitedHandler::OnMostVisitedURLsAvailable( | 307 void MostVisitedHandler::OnMostVisitedURLsAvailable( |
308 const history::MostVisitedURLList& data) { | 308 const history::MostVisitedURLList& data) { |
309 SetPagesValueFromTopSites(data); | 309 SetPagesValueFromTopSites(data); |
310 if (got_first_most_visited_request_) { | 310 if (got_first_most_visited_request_) { |
311 SendPagesValue(); | 311 SendPagesValue(); |
312 } | 312 } |
313 } | 313 } |
314 | 314 |
315 void MostVisitedHandler::Observe(int type, | 315 void MostVisitedHandler::Observe(int type, |
316 const NotificationSource& source, | 316 const NotificationSource& source, |
317 const NotificationDetails& details) { | 317 const NotificationDetails& details) { |
318 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); | 318 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); |
319 | 319 |
320 // Most visited urls changed, query again. | 320 // Most visited urls changed, query again. |
321 StartQueryForMostVisited(); | 321 StartQueryForMostVisited(); |
322 } | 322 } |
323 | 323 |
324 void MostVisitedHandler::BlacklistURL(const GURL& url) { | 324 void MostVisitedHandler::BlacklistURL(const GURL& url) { |
325 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 325 history::TopSites* ts = Profile::FromWebUI(web_ui_)->GetTopSites(); |
326 if (ts) | 326 if (ts) |
327 ts->AddBlacklistedURL(url); | 327 ts->AddBlacklistedURL(url); |
328 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); | 328 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); |
329 } | 329 } |
330 | 330 |
331 std::string MostVisitedHandler::GetDictionaryKeyForURL(const std::string& url) { | 331 std::string MostVisitedHandler::GetDictionaryKeyForURL(const std::string& url) { |
332 return base::MD5String(url); | 332 return base::MD5String(url); |
333 } | 333 } |
334 | 334 |
335 // static | 335 // static |
336 void MostVisitedHandler::RegisterUserPrefs(PrefService* prefs) { | 336 void MostVisitedHandler::RegisterUserPrefs(PrefService* prefs) { |
337 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedURLsBlacklist, | 337 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedURLsBlacklist, |
338 PrefService::UNSYNCABLE_PREF); | 338 PrefService::UNSYNCABLE_PREF); |
339 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedPinnedURLs, | 339 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedPinnedURLs, |
340 PrefService::UNSYNCABLE_PREF); | 340 PrefService::UNSYNCABLE_PREF); |
341 } | 341 } |
OLD | NEW |