| 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/string16.h" | 16 #include "base/string16.h" | 
| 16 #include "base/string_number_conversions.h" | 17 #include "base/string_number_conversions.h" | 
| 17 #include "base/threading/thread.h" | 18 #include "base/threading/thread.h" | 
| 18 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" | 
| 19 #include "base/values.h" | 20 #include "base/values.h" | 
| 20 #include "chrome/browser/history/page_usage_data.h" | 21 #include "chrome/browser/history/page_usage_data.h" | 
| 21 #include "chrome/browser/history/top_sites.h" | 22 #include "chrome/browser/history/top_sites.h" | 
| 22 #include "chrome/browser/prefs/pref_service.h" | 23 #include "chrome/browser/prefs/pref_service.h" | 
| 23 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 24 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 
| 24 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" | 
| 25 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 26 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 
| 26 #include "chrome/browser/ui/webui/favicon_source.h" | 27 #include "chrome/browser/ui/webui/favicon_source.h" | 
| 27 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 28 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 
|  | 29 #include "chrome/browser/ui/webui/ntp/ntp_stats.h" | 
| 28 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | 30 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | 
| 29 #include "chrome/common/chrome_notification_types.h" | 31 #include "chrome/common/chrome_notification_types.h" | 
| 30 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" | 
| 31 #include "chrome/common/url_constants.h" | 33 #include "chrome/common/url_constants.h" | 
| 32 #include "content/public/browser/browser_thread.h" | 34 #include "content/public/browser/browser_thread.h" | 
|  | 35 #include "content/public/browser/navigation_controller.h" | 
|  | 36 #include "content/public/browser/navigation_entry.h" | 
| 33 #include "content/public/browser/notification_source.h" | 37 #include "content/public/browser/notification_source.h" | 
| 34 #include "content/public/browser/user_metrics.h" | 38 #include "content/public/browser/user_metrics.h" | 
|  | 39 #include "content/public/browser/web_contents.h" | 
| 35 #include "content/public/browser/web_ui.h" | 40 #include "content/public/browser/web_ui.h" | 
| 36 #include "googleurl/src/gurl.h" | 41 #include "googleurl/src/gurl.h" | 
| 37 #include "grit/chromium_strings.h" | 42 #include "grit/chromium_strings.h" | 
| 38 #include "grit/generated_resources.h" | 43 #include "grit/generated_resources.h" | 
| 39 #include "grit/locale_settings.h" | 44 #include "grit/locale_settings.h" | 
| 40 #include "ui/base/l10n/l10n_util.h" | 45 #include "ui/base/l10n/l10n_util.h" | 
| 41 | 46 | 
| 42 using content::UserMetricsAction; | 47 using content::UserMetricsAction; | 
| 43 | 48 | 
| 44 MostVisitedHandler::MostVisitedHandler() | 49 MostVisitedHandler::MostVisitedHandler() | 
| 45     : got_first_most_visited_request_(false) { | 50     : got_first_most_visited_request_(false), | 
|  | 51       most_visited_viewed_(false), | 
|  | 52       user_action_logged_(false) { | 
| 46 } | 53 } | 
| 47 | 54 | 
| 48 MostVisitedHandler::~MostVisitedHandler() { | 55 MostVisitedHandler::~MostVisitedHandler() { | 
|  | 56   if (!user_action_logged_ && most_visited_viewed_) { | 
|  | 57     const GURL ntp_url = GURL(chrome::kChromeUINewTabURL); | 
|  | 58     int action_id = NTP_FOLLOW_ACTION_OTHER; | 
|  | 59     content::NavigationEntry* entry = | 
|  | 60         web_ui()->GetWebContents()->GetController().GetActiveEntry(); | 
|  | 61     if (entry && (entry->GetURL() != ntp_url)) { | 
|  | 62       action_id = | 
|  | 63           content::PageTransitionStripQualifier(entry->GetTransitionType()); | 
|  | 64     } | 
|  | 65 | 
|  | 66     UMA_HISTOGRAM_ENUMERATION("NewTabPage.MostVisitedAction", action_id, | 
|  | 67                               NUM_NTP_FOLLOW_ACTIONS); | 
|  | 68   } | 
| 49 } | 69 } | 
| 50 | 70 | 
| 51 void MostVisitedHandler::RegisterMessages() { | 71 void MostVisitedHandler::RegisterMessages() { | 
| 52   Profile* profile = Profile::FromWebUI(web_ui()); | 72   Profile* profile = Profile::FromWebUI(web_ui()); | 
| 53   // Set up our sources for thumbnail and favicon data. | 73   // Set up our sources for thumbnail and favicon data. | 
| 54   ThumbnailSource* thumbnail_src = new ThumbnailSource(profile); | 74   ThumbnailSource* thumbnail_src = new ThumbnailSource(profile); | 
| 55   profile->GetChromeURLDataManager()->AddDataSource(thumbnail_src); | 75   profile->GetChromeURLDataManager()->AddDataSource(thumbnail_src); | 
| 56 | 76 | 
| 57   profile->GetChromeURLDataManager()->AddDataSource( | 77   profile->GetChromeURLDataManager()->AddDataSource( | 
| 58       new FaviconSource(profile, FaviconSource::FAVICON)); | 78       new FaviconSource(profile, FaviconSource::FAVICON)); | 
| (...skipping 22 matching lines...) Expand all  Loading... | 
| 81   // Register ourselves for any most-visited item blacklisting. | 101   // Register ourselves for any most-visited item blacklisting. | 
| 82   web_ui()->RegisterMessageCallback("blacklistURLFromMostVisited", | 102   web_ui()->RegisterMessageCallback("blacklistURLFromMostVisited", | 
| 83       base::Bind(&MostVisitedHandler::HandleBlacklistURL, | 103       base::Bind(&MostVisitedHandler::HandleBlacklistURL, | 
| 84                  base::Unretained(this))); | 104                  base::Unretained(this))); | 
| 85   web_ui()->RegisterMessageCallback("removeURLsFromMostVisitedBlacklist", | 105   web_ui()->RegisterMessageCallback("removeURLsFromMostVisitedBlacklist", | 
| 86       base::Bind(&MostVisitedHandler::HandleRemoveURLsFromBlacklist, | 106       base::Bind(&MostVisitedHandler::HandleRemoveURLsFromBlacklist, | 
| 87                  base::Unretained(this))); | 107                  base::Unretained(this))); | 
| 88   web_ui()->RegisterMessageCallback("clearMostVisitedURLsBlacklist", | 108   web_ui()->RegisterMessageCallback("clearMostVisitedURLsBlacklist", | 
| 89       base::Bind(&MostVisitedHandler::HandleClearBlacklist, | 109       base::Bind(&MostVisitedHandler::HandleClearBlacklist, | 
| 90                  base::Unretained(this))); | 110                  base::Unretained(this))); | 
|  | 111   web_ui()->RegisterMessageCallback("mostVisitedAction", | 
|  | 112       base::Bind(&MostVisitedHandler::HandleMostVisitedAction, | 
|  | 113                  base::Unretained(this))); | 
|  | 114   web_ui()->RegisterMessageCallback("mostVisitedSelected", | 
|  | 115       base::Bind(&MostVisitedHandler::HandleMostVisitedSelected, | 
|  | 116                  base::Unretained(this))); | 
| 91 } | 117 } | 
| 92 | 118 | 
| 93 void MostVisitedHandler::HandleGetMostVisited(const ListValue* args) { | 119 void MostVisitedHandler::HandleGetMostVisited(const ListValue* args) { | 
| 94   if (!got_first_most_visited_request_) { | 120   if (!got_first_most_visited_request_) { | 
| 95     // If our initial data is already here, return it. | 121     // If our initial data is already here, return it. | 
| 96     SendPagesValue(); | 122     SendPagesValue(); | 
| 97     got_first_most_visited_request_ = true; | 123     got_first_most_visited_request_ = true; | 
| 98   } else { | 124   } else { | 
| 99     StartQueryForMostVisited(); | 125     StartQueryForMostVisited(); | 
| 100   } | 126   } | 
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 151 } | 177 } | 
| 152 | 178 | 
| 153 void MostVisitedHandler::HandleClearBlacklist(const ListValue* args) { | 179 void MostVisitedHandler::HandleClearBlacklist(const ListValue* args) { | 
| 154   content::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); | 180   content::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); | 
| 155 | 181 | 
| 156   history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 182   history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 
| 157   if (ts) | 183   if (ts) | 
| 158     ts->ClearBlacklistedURLs(); | 184     ts->ClearBlacklistedURLs(); | 
| 159 } | 185 } | 
| 160 | 186 | 
|  | 187 void MostVisitedHandler::HandleMostVisitedAction(const base::ListValue* args) { | 
|  | 188   DCHECK(args); | 
|  | 189 | 
|  | 190   double action_id; | 
|  | 191   if (!args->GetDouble(0, &action_id)) | 
|  | 192     NOTREACHED(); | 
|  | 193 | 
|  | 194   UMA_HISTOGRAM_ENUMERATION("NewTabPage.MostVisitedAction", | 
|  | 195                             static_cast<int>(action_id), | 
|  | 196                             NUM_NTP_FOLLOW_ACTIONS); | 
|  | 197   most_visited_viewed_ = true; | 
|  | 198   user_action_logged_ = true; | 
|  | 199 } | 
|  | 200 | 
|  | 201 void MostVisitedHandler::HandleMostVisitedSelected( | 
|  | 202     const base::ListValue* args) { | 
|  | 203   most_visited_viewed_ = true; | 
|  | 204 } | 
|  | 205 | 
| 161 void MostVisitedHandler::SetPagesValueFromTopSites( | 206 void MostVisitedHandler::SetPagesValueFromTopSites( | 
| 162     const history::MostVisitedURLList& data) { | 207     const history::MostVisitedURLList& data) { | 
| 163   pages_value_.reset(new ListValue); | 208   pages_value_.reset(new ListValue); | 
| 164   for (size_t i = 0; i < data.size(); i++) { | 209   for (size_t i = 0; i < data.size(); i++) { | 
| 165     const history::MostVisitedURL& url = data[i]; | 210     const history::MostVisitedURL& url = data[i]; | 
| 166     DictionaryValue* page_value = new DictionaryValue(); | 211     DictionaryValue* page_value = new DictionaryValue(); | 
| 167     if (url.url.is_empty()) { | 212     if (url.url.is_empty()) { | 
| 168       page_value->SetBoolean("filler", true); | 213       page_value->SetBoolean("filler", true); | 
| 169       pages_value_->Append(page_value); | 214       pages_value_->Append(page_value); | 
| 170       continue; | 215       continue; | 
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 203 | 248 | 
| 204 std::string MostVisitedHandler::GetDictionaryKeyForURL(const std::string& url) { | 249 std::string MostVisitedHandler::GetDictionaryKeyForURL(const std::string& url) { | 
| 205   return base::MD5String(url); | 250   return base::MD5String(url); | 
| 206 } | 251 } | 
| 207 | 252 | 
| 208 // static | 253 // static | 
| 209 void MostVisitedHandler::RegisterUserPrefs(PrefService* prefs) { | 254 void MostVisitedHandler::RegisterUserPrefs(PrefService* prefs) { | 
| 210   prefs->RegisterDictionaryPref(prefs::kNtpMostVisitedURLsBlacklist, | 255   prefs->RegisterDictionaryPref(prefs::kNtpMostVisitedURLsBlacklist, | 
| 211                                 PrefService::UNSYNCABLE_PREF); | 256                                 PrefService::UNSYNCABLE_PREF); | 
| 212 } | 257 } | 
| OLD | NEW | 
|---|