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/most_visited_handler.h" | 5 #include "chrome/browser/ui/webui/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" |
11 #include "base/md5.h" | 11 #include "base/md5.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
14 #include "base/string16.h" | 14 #include "base/string16.h" |
15 #include "base/string_number_conversions.h" | 15 #include "base/string_number_conversions.h" |
16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
17 #include "base/utf_string_conversions.h" | 17 #include "base/utf_string_conversions.h" |
18 #include "base/values.h" | 18 #include "base/values.h" |
19 #include "chrome/browser/history/page_usage_data.h" | 19 #include "chrome/browser/history/page_usage_data.h" |
20 #include "chrome/browser/history/top_sites.h" | 20 #include "chrome/browser/history/top_sites.h" |
21 #include "chrome/browser/metrics/user_metrics.h" | 21 #include "chrome/browser/metrics/user_metrics.h" |
22 #include "chrome/browser/prefs/pref_service.h" | 22 #include "chrome/browser/prefs/pref_service.h" |
| 23 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
23 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
24 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 25 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
25 #include "chrome/browser/ui/webui/favicon_source.h" | 26 #include "chrome/browser/ui/webui/favicon_source.h" |
26 #include "chrome/browser/ui/webui/new_tab_ui.h" | 27 #include "chrome/browser/ui/webui/new_tab_ui.h" |
27 #include "chrome/browser/ui/webui/thumbnail_source.h" | 28 #include "chrome/browser/ui/webui/thumbnail_source.h" |
28 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
29 #include "content/browser/browser_thread.h" | 30 #include "content/browser/browser_thread.h" |
30 #include "content/common/notification_source.h" | 31 #include "content/common/notification_source.h" |
31 #include "content/common/notification_type.h" | 32 #include "content/common/notification_type.h" |
32 #include "googleurl/src/gurl.h" | 33 #include "googleurl/src/gurl.h" |
(...skipping 15 matching lines...) Expand all Loading... |
48 // This struct is used when getting the pre-populated pages in case the user | 49 // This struct is used when getting the pre-populated pages in case the user |
49 // hasn't filled up his most visited pages. | 50 // hasn't filled up his most visited pages. |
50 struct MostVisitedHandler::MostVisitedPage { | 51 struct MostVisitedHandler::MostVisitedPage { |
51 string16 title; | 52 string16 title; |
52 GURL url; | 53 GURL url; |
53 GURL thumbnail_url; | 54 GURL thumbnail_url; |
54 GURL favicon_url; | 55 GURL favicon_url; |
55 }; | 56 }; |
56 | 57 |
57 MostVisitedHandler::MostVisitedHandler() | 58 MostVisitedHandler::MostVisitedHandler() |
58 : url_blacklist_(NULL), | 59 : got_first_most_visited_request_(false) { |
59 pinned_urls_(NULL), | |
60 got_first_most_visited_request_(false) { | |
61 } | 60 } |
62 | 61 |
63 MostVisitedHandler::~MostVisitedHandler() { | 62 MostVisitedHandler::~MostVisitedHandler() { |
64 } | 63 } |
65 | 64 |
66 WebUIMessageHandler* MostVisitedHandler::Attach(WebUI* web_ui) { | 65 WebUIMessageHandler* MostVisitedHandler::Attach(WebUI* web_ui) { |
67 Profile* profile = web_ui->GetProfile(); | 66 Profile* profile = web_ui->GetProfile(); |
68 url_blacklist_ = profile->GetPrefs()->GetMutableDictionary( | |
69 prefs::kNTPMostVisitedURLsBlacklist); | |
70 pinned_urls_ = profile->GetPrefs()->GetMutableDictionary( | |
71 prefs::kNTPMostVisitedPinnedURLs); | |
72 // Set up our sources for thumbnail and favicon data. | 67 // Set up our sources for thumbnail and favicon data. |
73 ThumbnailSource* thumbnail_src = new ThumbnailSource(profile); | 68 ThumbnailSource* thumbnail_src = new ThumbnailSource(profile); |
74 profile->GetChromeURLDataManager()->AddDataSource(thumbnail_src); | 69 profile->GetChromeURLDataManager()->AddDataSource(thumbnail_src); |
75 | 70 |
76 FaviconSource* favicon_src = new FaviconSource(profile); | 71 FaviconSource* favicon_src = new FaviconSource(profile); |
77 profile->GetChromeURLDataManager()->AddDataSource(favicon_src); | 72 profile->GetChromeURLDataManager()->AddDataSource(favicon_src); |
78 | 73 |
79 // Get notifications when history is cleared. | 74 // Get notifications when history is cleared. |
80 registrar_.Add(this, NotificationType::HISTORY_URLS_DELETED, | 75 registrar_.Add(this, NotificationType::HISTORY_URLS_DELETED, |
81 Source<Profile>(profile)); | 76 Source<Profile>(profile)); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
114 // If our intial data is already here, return it. | 109 // If our intial data is already here, return it. |
115 SendPagesValue(); | 110 SendPagesValue(); |
116 got_first_most_visited_request_ = true; | 111 got_first_most_visited_request_ = true; |
117 } else { | 112 } else { |
118 StartQueryForMostVisited(); | 113 StartQueryForMostVisited(); |
119 } | 114 } |
120 } | 115 } |
121 | 116 |
122 void MostVisitedHandler::SendPagesValue() { | 117 void MostVisitedHandler::SendPagesValue() { |
123 if (pages_value_.get()) { | 118 if (pages_value_.get()) { |
124 bool has_blacklisted_urls = !url_blacklist_->empty(); | 119 Profile* profile = web_ui_->GetProfile(); |
125 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 120 const DictionaryValue* url_blacklist = |
| 121 profile->GetPrefs()->GetDictionary(prefs::kNTPMostVisitedURLsBlacklist); |
| 122 bool has_blacklisted_urls = !url_blacklist->empty(); |
| 123 history::TopSites* ts = profile->GetTopSites(); |
126 if (ts) | 124 if (ts) |
127 has_blacklisted_urls = ts->HasBlacklistedItems(); | 125 has_blacklisted_urls = ts->HasBlacklistedItems(); |
128 FundamentalValue first_run(IsFirstRun()); | 126 FundamentalValue first_run(IsFirstRun()); |
129 FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); | 127 FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); |
130 web_ui_->CallJavascriptFunction("mostVisitedPages", | 128 web_ui_->CallJavascriptFunction("mostVisitedPages", |
131 *(pages_value_.get()), | 129 *(pages_value_.get()), |
132 first_run, | 130 first_run, |
133 has_blacklisted_urls_value); | 131 has_blacklisted_urls_value); |
134 pages_value_.reset(); | 132 pages_value_.reset(); |
135 } | 133 } |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 if (ts) | 226 if (ts) |
229 ts->RemovePinnedURL(url); | 227 ts->RemovePinnedURL(url); |
230 } | 228 } |
231 | 229 |
232 bool MostVisitedHandler::GetPinnedURLAtIndex(int index, | 230 bool MostVisitedHandler::GetPinnedURLAtIndex(int index, |
233 MostVisitedPage* page) { | 231 MostVisitedPage* page) { |
234 // This iterates over all the pinned URLs. It might seem like it is worth | 232 // This iterates over all the pinned URLs. It might seem like it is worth |
235 // having a map from the index to the item but the number of items is limited | 233 // having a map from the index to the item but the number of items is limited |
236 // to the number of items the most visited section is showing on the NTP so | 234 // to the number of items the most visited section is showing on the NTP so |
237 // this will be fast enough for now. | 235 // this will be fast enough for now. |
238 for (DictionaryValue::key_iterator it = pinned_urls_->begin_keys(); | 236 PrefService* prefs = web_ui_->GetProfile()->GetPrefs(); |
239 it != pinned_urls_->end_keys(); ++it) { | 237 const DictionaryValue* pinned_urls = |
| 238 prefs->GetDictionary(prefs::kNTPMostVisitedPinnedURLs); |
| 239 for (DictionaryValue::key_iterator it = pinned_urls->begin_keys(); |
| 240 it != pinned_urls->end_keys(); ++it) { |
240 Value* value; | 241 Value* value; |
241 if (pinned_urls_->GetWithoutPathExpansion(*it, &value)) { | 242 if (pinned_urls->GetWithoutPathExpansion(*it, &value)) { |
242 if (!value->IsType(DictionaryValue::TYPE_DICTIONARY)) { | 243 if (!value->IsType(DictionaryValue::TYPE_DICTIONARY)) { |
243 // Moved on to TopSites and now going back. | 244 // Moved on to TopSites and now going back. |
244 pinned_urls_->Clear(); | 245 DictionaryPrefUpdate update(prefs, prefs::kNTPMostVisitedPinnedURLs); |
| 246 update.Get()->Clear(); |
245 return false; | 247 return false; |
246 } | 248 } |
247 | 249 |
248 int dict_index; | 250 int dict_index; |
249 DictionaryValue* dict = static_cast<DictionaryValue*>(value); | 251 const DictionaryValue* dict = static_cast<DictionaryValue*>(value); |
250 if (dict->GetInteger("index", &dict_index) && dict_index == index) { | 252 if (dict->GetInteger("index", &dict_index) && dict_index == index) { |
251 // The favicon and thumbnail URLs may be empty. | 253 // The favicon and thumbnail URLs may be empty. |
252 std::string tmp_string; | 254 std::string tmp_string; |
253 if (dict->GetString("faviconUrl", &tmp_string)) | 255 if (dict->GetString("faviconUrl", &tmp_string)) |
254 page->favicon_url = GURL(tmp_string); | 256 page->favicon_url = GURL(tmp_string); |
255 if (dict->GetString("thumbnailUrl", &tmp_string)) | 257 if (dict->GetString("thumbnailUrl", &tmp_string)) |
256 page->thumbnail_url = GURL(tmp_string); | 258 page->thumbnail_url = GURL(tmp_string); |
257 | 259 |
258 if (dict->GetString("url", &tmp_string)) | 260 if (dict->GetString("url", &tmp_string)) |
259 page->url = GURL(tmp_string); | 261 page->url = GURL(tmp_string); |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
378 | 380 |
379 // static | 381 // static |
380 std::vector<GURL> MostVisitedHandler::GetPrePopulatedUrls() { | 382 std::vector<GURL> MostVisitedHandler::GetPrePopulatedUrls() { |
381 const std::vector<MostVisitedPage> pages = | 383 const std::vector<MostVisitedPage> pages = |
382 MostVisitedHandler::GetPrePopulatedPages(); | 384 MostVisitedHandler::GetPrePopulatedPages(); |
383 std::vector<GURL> page_urls; | 385 std::vector<GURL> page_urls; |
384 for (size_t i = 0; i < pages.size(); ++i) | 386 for (size_t i = 0; i < pages.size(); ++i) |
385 page_urls.push_back(pages[i].url); | 387 page_urls.push_back(pages[i].url); |
386 return page_urls; | 388 return page_urls; |
387 } | 389 } |
OLD | NEW |