| 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/suggestions_page_handler.h" | 5 #include "chrome/browser/ui/webui/ntp/suggestions_page_handler.h" |
| 6 | 6 |
| 7 #include <math.h> | 7 #include <math.h> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/md5.h" | 12 #include "base/md5.h" |
| 13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
| 14 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "chrome/browser/chrome_notification_types.h" | |
| 19 #include "chrome/browser/history/top_sites.h" | 18 #include "chrome/browser/history/top_sites.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 21 #include "chrome/browser/ui/webui/favicon_source.h" | 20 #include "chrome/browser/ui/webui/favicon_source.h" |
| 22 #include "chrome/browser/ui/webui/ntp/ntp_stats.h" | 21 #include "chrome/browser/ui/webui/ntp/ntp_stats.h" |
| 23 #include "chrome/browser/ui/webui/ntp/suggestions_combiner.h" | 22 #include "chrome/browser/ui/webui/ntp/suggestions_combiner.h" |
| 24 #include "chrome/browser/ui/webui/ntp/suggestions_source_top_sites.h" | 23 #include "chrome/browser/ui/webui/ntp/suggestions_source_top_sites.h" |
| 25 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | 24 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
| 26 #include "chrome/common/url_constants.h" | 25 #include "chrome/common/url_constants.h" |
| 27 #include "components/history/core/browser/page_usage_data.h" | 26 #include "components/history/core/browser/page_usage_data.h" |
| 28 #include "components/pref_registry/pref_registry_syncable.h" | 27 #include "components/pref_registry/pref_registry_syncable.h" |
| 29 #include "content/public/browser/navigation_controller.h" | 28 #include "content/public/browser/navigation_controller.h" |
| 30 #include "content/public/browser/navigation_entry.h" | 29 #include "content/public/browser/navigation_entry.h" |
| 31 #include "content/public/browser/notification_source.h" | |
| 32 #include "content/public/browser/url_data_source.h" | 30 #include "content/public/browser/url_data_source.h" |
| 33 #include "content/public/browser/user_metrics.h" | 31 #include "content/public/browser/user_metrics.h" |
| 34 #include "content/public/browser/web_contents.h" | 32 #include "content/public/browser/web_contents.h" |
| 35 #include "content/public/browser/web_ui.h" | 33 #include "content/public/browser/web_ui.h" |
| 36 #include "ui/base/page_transition_types.h" | 34 #include "ui/base/page_transition_types.h" |
| 37 #include "url/gurl.h" | 35 #include "url/gurl.h" |
| 38 | 36 |
| 39 using base::UserMetricsAction; | 37 using base::UserMetricsAction; |
| 40 | 38 |
| 41 SuggestionsHandler::SuggestionsHandler() | 39 SuggestionsHandler::SuggestionsHandler() |
| 42 : got_first_suggestions_request_(false), | 40 : scoped_observer_(this), |
| 41 got_first_suggestions_request_(false), |
| 43 suggestions_viewed_(false), | 42 suggestions_viewed_(false), |
| 44 user_action_logged_(false) { | 43 user_action_logged_(false) { |
| 45 } | 44 } |
| 46 | 45 |
| 47 SuggestionsHandler::~SuggestionsHandler() { | 46 SuggestionsHandler::~SuggestionsHandler() { |
| 48 if (!user_action_logged_ && suggestions_viewed_) { | 47 if (!user_action_logged_ && suggestions_viewed_) { |
| 49 const GURL ntp_url = GURL(chrome::kChromeUINewTabURL); | 48 const GURL ntp_url = GURL(chrome::kChromeUINewTabURL); |
| 50 int action_id = NTP_FOLLOW_ACTION_OTHER; | 49 int action_id = NTP_FOLLOW_ACTION_OTHER; |
| 51 content::NavigationEntry* entry = | 50 content::NavigationEntry* entry = |
| 52 web_ui()->GetWebContents()->GetController().GetLastCommittedEntry(); | 51 web_ui()->GetWebContents()->GetController().GetLastCommittedEntry(); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 69 | 68 |
| 70 // TODO(georgey) change the source of the web-sites to provide our data. | 69 // TODO(georgey) change the source of the web-sites to provide our data. |
| 71 // Initial commit uses top sites as a data source. | 70 // Initial commit uses top sites as a data source. |
| 72 history::TopSites* top_sites = profile->GetTopSites(); | 71 history::TopSites* top_sites = profile->GetTopSites(); |
| 73 if (top_sites) { | 72 if (top_sites) { |
| 74 // TopSites updates itself after a delay. This is especially noticable when | 73 // TopSites updates itself after a delay. This is especially noticable when |
| 75 // your profile is empty. Ask TopSites to update itself when we're about to | 74 // your profile is empty. Ask TopSites to update itself when we're about to |
| 76 // show the new tab page. | 75 // show the new tab page. |
| 77 top_sites->SyncWithHistory(); | 76 top_sites->SyncWithHistory(); |
| 78 | 77 |
| 79 // Register for notification when TopSites changes so that we can update | 78 // Register as TopSitesObserver so that we can update ourselves when the |
| 80 // ourself. | 79 // TopSites changes. |
| 81 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, | 80 scoped_observer_.Add(top_sites); |
| 82 content::Source<history::TopSites>(top_sites)); | |
| 83 } | 81 } |
| 84 | 82 |
| 85 // Setup the suggestions sources. | 83 // Setup the suggestions sources. |
| 86 SuggestionsCombiner* combiner = new SuggestionsCombiner(this, profile); | 84 SuggestionsCombiner* combiner = new SuggestionsCombiner(this, profile); |
| 87 combiner->AddSource(new SuggestionsSourceTopSites()); | 85 combiner->AddSource(new SuggestionsSourceTopSites()); |
| 88 suggestions_combiner_.reset(combiner); | 86 suggestions_combiner_.reset(combiner); |
| 89 | 87 |
| 90 // We pre-emptively make a fetch for suggestions so we have the results | 88 // We pre-emptively make a fetch for suggestions so we have the results |
| 91 // sooner. | 89 // sooner. |
| 92 suggestions_combiner_->FetchItems(profile); | 90 suggestions_combiner_->FetchItems(profile); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 NUM_NTP_FOLLOW_ACTIONS); | 166 NUM_NTP_FOLLOW_ACTIONS); |
| 169 suggestions_viewed_ = true; | 167 suggestions_viewed_ = true; |
| 170 user_action_logged_ = true; | 168 user_action_logged_ = true; |
| 171 } | 169 } |
| 172 | 170 |
| 173 void SuggestionsHandler::HandleSuggestedSitesSelected( | 171 void SuggestionsHandler::HandleSuggestedSitesSelected( |
| 174 const base::ListValue* args) { | 172 const base::ListValue* args) { |
| 175 suggestions_viewed_ = true; | 173 suggestions_viewed_ = true; |
| 176 } | 174 } |
| 177 | 175 |
| 178 void SuggestionsHandler::Observe(int type, | 176 void SuggestionsHandler::TopSitesLoaded(history::TopSites* top_sites) { |
| 179 const content::NotificationSource& source, | 177 } |
| 180 const content::NotificationDetails& details) { | |
| 181 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); | |
| 182 | 178 |
| 179 void SuggestionsHandler::TopSitesChanged(history::TopSites* top_sites) { |
| 183 // Suggestions urls changed, query again. | 180 // Suggestions urls changed, query again. |
| 184 suggestions_combiner_->FetchItems(Profile::FromWebUI(web_ui())); | 181 suggestions_combiner_->FetchItems(Profile::FromWebUI(web_ui())); |
| 185 } | 182 } |
| 186 | 183 |
| 187 void SuggestionsHandler::BlacklistURL(const GURL& url) { | 184 void SuggestionsHandler::BlacklistURL(const GURL& url) { |
| 188 // TODO(georgey) blacklist an URL. | 185 // TODO(georgey) blacklist an URL. |
| 189 } | 186 } |
| 190 | 187 |
| 191 std::string SuggestionsHandler::GetDictionaryKeyForURL(const std::string& url) { | 188 std::string SuggestionsHandler::GetDictionaryKeyForURL(const std::string& url) { |
| 192 return base::MD5String(url); | 189 return base::MD5String(url); |
| 193 } | 190 } |
| 194 | 191 |
| 195 // static | 192 // static |
| 196 void SuggestionsHandler::RegisterProfilePrefs( | 193 void SuggestionsHandler::RegisterProfilePrefs( |
| 197 user_prefs::PrefRegistrySyncable* registry) { | 194 user_prefs::PrefRegistrySyncable* registry) { |
| 198 // TODO(georgey) add user preferences (such as own blacklist) as needed. | 195 // TODO(georgey) add user preferences (such as own blacklist) as needed. |
| 199 } | 196 } |
| OLD | NEW |