| 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/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 #include "chrome/common/url_constants.h" | 32 #include "chrome/common/url_constants.h" |
| 33 #include "content/browser/browser_thread.h" | 33 #include "content/browser/browser_thread.h" |
| 34 #include "content/browser/user_metrics.h" | 34 #include "content/browser/user_metrics.h" |
| 35 #include "content/public/browser/notification_source.h" | 35 #include "content/public/browser/notification_source.h" |
| 36 #include "googleurl/src/gurl.h" | 36 #include "googleurl/src/gurl.h" |
| 37 #include "grit/chromium_strings.h" | 37 #include "grit/chromium_strings.h" |
| 38 #include "grit/generated_resources.h" | 38 #include "grit/generated_resources.h" |
| 39 #include "grit/locale_settings.h" | 39 #include "grit/locale_settings.h" |
| 40 #include "ui/base/l10n/l10n_util.h" | 40 #include "ui/base/l10n/l10n_util.h" |
| 41 | 41 |
| 42 // This struct is used when getting the pre-populated pages in case the user | |
| 43 // hasn't filled up his most visited pages. | |
| 44 struct MostVisitedHandler::MostVisitedPage { | |
| 45 string16 title; | |
| 46 GURL url; | |
| 47 GURL thumbnail_url; | |
| 48 GURL favicon_url; | |
| 49 }; | |
| 50 | |
| 51 MostVisitedHandler::MostVisitedHandler() | 42 MostVisitedHandler::MostVisitedHandler() |
| 52 : got_first_most_visited_request_(false) { | 43 : got_first_most_visited_request_(false) { |
| 53 } | 44 } |
| 54 | 45 |
| 55 MostVisitedHandler::~MostVisitedHandler() { | 46 MostVisitedHandler::~MostVisitedHandler() { |
| 56 } | 47 } |
| 57 | 48 |
| 58 WebUIMessageHandler* MostVisitedHandler::Attach(WebUI* web_ui) { | 49 WebUIMessageHandler* MostVisitedHandler::Attach(WebUI* web_ui) { |
| 59 Profile* profile = Profile::FromWebUI(web_ui); | 50 Profile* profile = Profile::FromWebUI(web_ui); |
| 60 // Set up our sources for thumbnail and favicon data. | 51 // Set up our sources for thumbnail and favicon data. |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 // Register ourselves for any most-visited item blacklisting. | 84 // Register ourselves for any most-visited item blacklisting. |
| 94 web_ui_->RegisterMessageCallback("blacklistURLFromMostVisited", | 85 web_ui_->RegisterMessageCallback("blacklistURLFromMostVisited", |
| 95 base::Bind(&MostVisitedHandler::HandleBlacklistURL, | 86 base::Bind(&MostVisitedHandler::HandleBlacklistURL, |
| 96 base::Unretained(this))); | 87 base::Unretained(this))); |
| 97 web_ui_->RegisterMessageCallback("removeURLsFromMostVisitedBlacklist", | 88 web_ui_->RegisterMessageCallback("removeURLsFromMostVisitedBlacklist", |
| 98 base::Bind(&MostVisitedHandler::HandleRemoveURLsFromBlacklist, | 89 base::Bind(&MostVisitedHandler::HandleRemoveURLsFromBlacklist, |
| 99 base::Unretained(this))); | 90 base::Unretained(this))); |
| 100 web_ui_->RegisterMessageCallback("clearMostVisitedURLsBlacklist", | 91 web_ui_->RegisterMessageCallback("clearMostVisitedURLsBlacklist", |
| 101 base::Bind(&MostVisitedHandler::HandleClearBlacklist, | 92 base::Bind(&MostVisitedHandler::HandleClearBlacklist, |
| 102 base::Unretained(this))); | 93 base::Unretained(this))); |
| 103 | |
| 104 // Register ourself for pinned URL messages. | |
| 105 web_ui_->RegisterMessageCallback("addPinnedURL", | |
| 106 base::Bind(&MostVisitedHandler::HandleAddPinnedURL, | |
| 107 base::Unretained(this))); | |
| 108 web_ui_->RegisterMessageCallback("removePinnedURL", | |
| 109 base::Bind(&MostVisitedHandler::HandleRemovePinnedURL, | |
| 110 base::Unretained(this))); | |
| 111 } | 94 } |
| 112 | 95 |
| 113 void MostVisitedHandler::HandleGetMostVisited(const ListValue* args) { | 96 void MostVisitedHandler::HandleGetMostVisited(const ListValue* args) { |
| 114 if (!got_first_most_visited_request_) { | 97 if (!got_first_most_visited_request_) { |
| 115 // If our initial data is already here, return it. | 98 // If our initial data is already here, return it. |
| 116 SendPagesValue(); | 99 SendPagesValue(); |
| 117 got_first_most_visited_request_ = true; | 100 got_first_most_visited_request_ = true; |
| 118 } else { | 101 } else { |
| 119 StartQueryForMostVisited(); | 102 StartQueryForMostVisited(); |
| 120 } | 103 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 } | 154 } |
| 172 | 155 |
| 173 void MostVisitedHandler::HandleClearBlacklist(const ListValue* args) { | 156 void MostVisitedHandler::HandleClearBlacklist(const ListValue* args) { |
| 174 UserMetrics::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); | 157 UserMetrics::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); |
| 175 | 158 |
| 176 history::TopSites* ts = Profile::FromWebUI(web_ui_)->GetTopSites(); | 159 history::TopSites* ts = Profile::FromWebUI(web_ui_)->GetTopSites(); |
| 177 if (ts) | 160 if (ts) |
| 178 ts->ClearBlacklistedURLs(); | 161 ts->ClearBlacklistedURLs(); |
| 179 } | 162 } |
| 180 | 163 |
| 181 void MostVisitedHandler::HandleAddPinnedURL(const ListValue* args) { | |
| 182 DCHECK_EQ(5U, args->GetSize()) << "Wrong number of params to addPinnedURL"; | |
| 183 MostVisitedPage mvp; | |
| 184 std::string tmp_string; | |
| 185 string16 tmp_string16; | |
| 186 int index; | |
| 187 | |
| 188 bool r = args->GetString(0, &tmp_string); | |
| 189 DCHECK(r) << "Missing URL in addPinnedURL from the NTP Most Visited."; | |
| 190 mvp.url = GURL(tmp_string); | |
| 191 | |
| 192 r = args->GetString(1, &tmp_string16); | |
| 193 DCHECK(r) << "Missing title in addPinnedURL from the NTP Most Visited."; | |
| 194 mvp.title = tmp_string16; | |
| 195 | |
| 196 r = args->GetString(2, &tmp_string); | |
| 197 DCHECK(r) << "Failed to read the favicon URL in addPinnedURL from the NTP " | |
| 198 << "Most Visited."; | |
| 199 if (!tmp_string.empty()) | |
| 200 mvp.favicon_url = GURL(tmp_string); | |
| 201 | |
| 202 r = args->GetString(3, &tmp_string); | |
| 203 DCHECK(r) << "Failed to read the thumbnail URL in addPinnedURL from the NTP " | |
| 204 << "Most Visited."; | |
| 205 if (!tmp_string.empty()) | |
| 206 mvp.thumbnail_url = GURL(tmp_string); | |
| 207 | |
| 208 r = args->GetString(4, &tmp_string); | |
| 209 DCHECK(r) << "Missing index in addPinnedURL from the NTP Most Visited."; | |
| 210 base::StringToInt(tmp_string, &index); | |
| 211 | |
| 212 AddPinnedURL(mvp, index); | |
| 213 } | |
| 214 | |
| 215 void MostVisitedHandler::AddPinnedURL(const MostVisitedPage& page, int index) { | |
| 216 history::TopSites* ts = Profile::FromWebUI(web_ui_)->GetTopSites(); | |
| 217 if (ts) | |
| 218 ts->AddPinnedURL(page.url, index); | |
| 219 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlPinned")); | |
| 220 } | |
| 221 | |
| 222 void MostVisitedHandler::HandleRemovePinnedURL(const ListValue* args) { | |
| 223 std::string url = UTF16ToUTF8(ExtractStringValue(args)); | |
| 224 RemovePinnedURL(GURL(url)); | |
| 225 } | |
| 226 | |
| 227 void MostVisitedHandler::RemovePinnedURL(const GURL& url) { | |
| 228 history::TopSites* ts = Profile::FromWebUI(web_ui_)->GetTopSites(); | |
| 229 if (ts) | |
| 230 ts->RemovePinnedURL(url); | |
| 231 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlUnpinned")); | |
| 232 } | |
| 233 | |
| 234 bool MostVisitedHandler::GetPinnedURLAtIndex(int index, | |
| 235 MostVisitedPage* page) { | |
| 236 // This iterates over all the pinned URLs. It might seem like it is worth | |
| 237 // having a map from the index to the item but the number of items is limited | |
| 238 // to the number of items the most visited section is showing on the NTP so | |
| 239 // this will be fast enough for now. | |
| 240 PrefService* prefs = Profile::FromWebUI(web_ui_)->GetPrefs(); | |
| 241 const DictionaryValue* pinned_urls = | |
| 242 prefs->GetDictionary(prefs::kNTPMostVisitedPinnedURLs); | |
| 243 for (DictionaryValue::key_iterator it = pinned_urls->begin_keys(); | |
| 244 it != pinned_urls->end_keys(); ++it) { | |
| 245 Value* value; | |
| 246 if (pinned_urls->GetWithoutPathExpansion(*it, &value)) { | |
| 247 if (!value->IsType(DictionaryValue::TYPE_DICTIONARY)) { | |
| 248 // Moved on to TopSites and now going back. | |
| 249 DictionaryPrefUpdate update(prefs, prefs::kNTPMostVisitedPinnedURLs); | |
| 250 update.Get()->Clear(); | |
| 251 return false; | |
| 252 } | |
| 253 | |
| 254 int dict_index; | |
| 255 const DictionaryValue* dict = static_cast<DictionaryValue*>(value); | |
| 256 if (dict->GetInteger("index", &dict_index) && dict_index == index) { | |
| 257 // The favicon and thumbnail URLs may be empty. | |
| 258 std::string tmp_string; | |
| 259 if (dict->GetString("faviconUrl", &tmp_string)) | |
| 260 page->favicon_url = GURL(tmp_string); | |
| 261 if (dict->GetString("thumbnailUrl", &tmp_string)) | |
| 262 page->thumbnail_url = GURL(tmp_string); | |
| 263 | |
| 264 if (dict->GetString("url", &tmp_string)) | |
| 265 page->url = GURL(tmp_string); | |
| 266 else | |
| 267 return false; | |
| 268 | |
| 269 return dict->GetString("title", &page->title); | |
| 270 } | |
| 271 } else { | |
| 272 NOTREACHED() << "DictionaryValue iterators are filthy liars."; | |
| 273 } | |
| 274 } | |
| 275 | |
| 276 return false; | |
| 277 } | |
| 278 | 164 |
| 279 void MostVisitedHandler::SetPagesValueFromTopSites( | 165 void MostVisitedHandler::SetPagesValueFromTopSites( |
| 280 const history::MostVisitedURLList& data) { | 166 const history::MostVisitedURLList& data) { |
| 281 pages_value_.reset(new ListValue); | 167 pages_value_.reset(new ListValue); |
| 282 for (size_t i = 0; i < data.size(); i++) { | 168 for (size_t i = 0; i < data.size(); i++) { |
| 283 const history::MostVisitedURL& url = data[i]; | 169 const history::MostVisitedURL& url = data[i]; |
| 284 DictionaryValue* page_value = new DictionaryValue(); | 170 DictionaryValue* page_value = new DictionaryValue(); |
| 285 if (url.url.is_empty()) { | 171 if (url.url.is_empty()) { |
| 286 page_value->SetBoolean("filler", true); | 172 page_value->SetBoolean("filler", true); |
| 287 pages_value_->Append(page_value); | 173 pages_value_->Append(page_value); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 299 page_value->SetString("thumbnailUrl", | 185 page_value->SetString("thumbnailUrl", |
| 300 "chrome://theme/IDR_NEWTAB_CHROME_WELCOME_PAGE_THUMBNAIL"); | 186 "chrome://theme/IDR_NEWTAB_CHROME_WELCOME_PAGE_THUMBNAIL"); |
| 301 page_value->SetString("faviconDominantColor", "rgb(0, 147, 60)"); | 187 page_value->SetString("faviconDominantColor", "rgb(0, 147, 60)"); |
| 302 } else if (url.url.spec() == | 188 } else if (url.url.spec() == |
| 303 l10n_util::GetStringUTF8(IDS_WEBSTORE_URL)) { | 189 l10n_util::GetStringUTF8(IDS_WEBSTORE_URL)) { |
| 304 page_value->SetString("thumbnailUrl", | 190 page_value->SetString("thumbnailUrl", |
| 305 "chrome://theme/IDR_NEWTAB_WEBSTORE_THUMBNAIL"); | 191 "chrome://theme/IDR_NEWTAB_WEBSTORE_THUMBNAIL"); |
| 306 page_value->SetString("faviconDominantColor", "rgb(63, 132, 197)"); | 192 page_value->SetString("faviconDominantColor", "rgb(63, 132, 197)"); |
| 307 } | 193 } |
| 308 | 194 |
| 309 history::TopSites* ts = Profile::FromWebUI(web_ui_)->GetTopSites(); | |
| 310 if (ts && ts->IsURLPinned(url.url)) | |
| 311 page_value->SetBoolean("pinned", true); | |
| 312 pages_value_->Append(page_value); | 195 pages_value_->Append(page_value); |
| 313 } | 196 } |
| 314 } | 197 } |
| 315 | 198 |
| 316 void MostVisitedHandler::OnMostVisitedURLsAvailable( | 199 void MostVisitedHandler::OnMostVisitedURLsAvailable( |
| 317 const history::MostVisitedURLList& data) { | 200 const history::MostVisitedURLList& data) { |
| 318 SetPagesValueFromTopSites(data); | 201 SetPagesValueFromTopSites(data); |
| 319 if (got_first_most_visited_request_) { | 202 if (got_first_most_visited_request_) { |
| 320 SendPagesValue(); | 203 SendPagesValue(); |
| 321 } | 204 } |
| (...skipping 16 matching lines...) Expand all Loading... |
| 338 } | 221 } |
| 339 | 222 |
| 340 std::string MostVisitedHandler::GetDictionaryKeyForURL(const std::string& url) { | 223 std::string MostVisitedHandler::GetDictionaryKeyForURL(const std::string& url) { |
| 341 return base::MD5String(url); | 224 return base::MD5String(url); |
| 342 } | 225 } |
| 343 | 226 |
| 344 // static | 227 // static |
| 345 void MostVisitedHandler::RegisterUserPrefs(PrefService* prefs) { | 228 void MostVisitedHandler::RegisterUserPrefs(PrefService* prefs) { |
| 346 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedURLsBlacklist, | 229 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedURLsBlacklist, |
| 347 PrefService::UNSYNCABLE_PREF); | 230 PrefService::UNSYNCABLE_PREF); |
| 231 // TODO(estade): remove this. |
| 348 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedPinnedURLs, | 232 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedPinnedURLs, |
| 349 PrefService::UNSYNCABLE_PREF); | 233 PrefService::UNSYNCABLE_PREF); |
| 350 } | 234 } |
| OLD | NEW |