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 <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/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/metrics/histogram.h" |
14 #include "base/string16.h" | 15 #include "base/string16.h" |
15 #include "base/string_number_conversions.h" | 16 #include "base/string_number_conversions.h" |
16 #include "base/threading/thread.h" | 17 #include "base/threading/thread.h" |
17 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
18 #include "base/values.h" | 19 #include "base/values.h" |
19 #include "chrome/browser/history/page_usage_data.h" | 20 #include "chrome/browser/history/page_usage_data.h" |
20 #include "chrome/browser/history/top_sites.h" | 21 #include "chrome/browser/history/top_sites.h" |
21 #include "chrome/browser/history/visit_filter.h" | 22 #include "chrome/browser/history/visit_filter.h" |
22 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
23 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 24 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
24 #include "chrome/browser/ui/webui/favicon_source.h" | 25 #include "chrome/browser/ui/webui/favicon_source.h" |
25 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 26 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
26 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | 27 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
27 #include "chrome/common/chrome_notification_types.h" | 28 #include "chrome/common/chrome_notification_types.h" |
28 #include "chrome/common/url_constants.h" | 29 #include "chrome/common/url_constants.h" |
29 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/browser/navigation_controller.h" |
| 32 #include "content/public/browser/navigation_entry.h" |
30 #include "content/public/browser/notification_source.h" | 33 #include "content/public/browser/notification_source.h" |
31 #include "content/public/browser/user_metrics.h" | 34 #include "content/public/browser/user_metrics.h" |
| 35 #include "content/public/browser/web_contents.h" |
32 #include "content/public/browser/web_ui.h" | 36 #include "content/public/browser/web_ui.h" |
| 37 #include "content/public/common/page_transition_types.h" |
33 #include "googleurl/src/gurl.h" | 38 #include "googleurl/src/gurl.h" |
34 | 39 |
35 using content::UserMetricsAction; | 40 using content::UserMetricsAction; |
36 | 41 |
| 42 namespace { |
| 43 |
| 44 // Those values are defined in histograms.xml. These represent the action that |
| 45 // the user has taken to leave the Suggested pane, given that they have viewed |
| 46 // that pane on the NTP. |
| 47 enum SuggestedSitesActions { |
| 48 // Values 0 to 10 are the core values from PageTransition (see |
| 49 // page_transition_types.h). |
| 50 |
| 51 // The user has clicked on a Suggested tile. |
| 52 SUGGESTED_SITES_ACTION_CLICKED_SUGGESTED_TILE = 11, |
| 53 // The user has moved to another pane within the NTP. |
| 54 SUGGESTED_SITES_ACTION_CLICKED_OTHER_NTP_PANE, |
| 55 // Any action other than what is listed here (e.g. closing the tab, etc.). |
| 56 SUGGESTED_SITES_ACTION_OTHER, |
| 57 // The number of suggested sites actions that we log. |
| 58 NUM_SUGGESTED_SITES_ACTIONS |
| 59 }; |
| 60 |
| 61 } // namespace |
| 62 |
37 SuggestionsHandler::SuggestionsHandler() | 63 SuggestionsHandler::SuggestionsHandler() |
38 : got_first_suggestions_request_(false) { | 64 : got_first_suggestions_request_(false), |
| 65 suggestions_viewed_(false), |
| 66 user_action_logged_(false) { |
39 } | 67 } |
40 | 68 |
41 SuggestionsHandler::~SuggestionsHandler() { | 69 SuggestionsHandler::~SuggestionsHandler() { |
| 70 // TODO(macourteau): ensure that |suggestions_viewed_| gets set correctly, |
| 71 // once the Suggestions pane gets statically included into the NTP (e.g., |
| 72 // if the Suggestions pane is the one shown by default, the |
| 73 // |suggestions_viewed_| flag might not get set). |
| 74 if (!user_action_logged_ && suggestions_viewed_) { |
| 75 const GURL ntp_url = GURL(chrome::kChromeUINewTabURL); |
| 76 int action_id = SUGGESTED_SITES_ACTION_OTHER; |
| 77 content::NavigationEntry* entry = |
| 78 web_ui()->GetWebContents()->GetController().GetActiveEntry(); |
| 79 if (entry && (entry->GetURL() != ntp_url)) { |
| 80 action_id = |
| 81 content::PageTransitionStripQualifier(entry->GetTransitionType()); |
| 82 } |
| 83 |
| 84 UMA_HISTOGRAM_ENUMERATION("NewTabPage.SuggestedSitesAction", action_id, |
| 85 NUM_SUGGESTED_SITES_ACTIONS); |
| 86 } |
42 } | 87 } |
43 | 88 |
44 void SuggestionsHandler::RegisterMessages() { | 89 void SuggestionsHandler::RegisterMessages() { |
45 Profile* profile = Profile::FromWebUI(web_ui()); | 90 Profile* profile = Profile::FromWebUI(web_ui()); |
46 // Set up our sources for thumbnail and favicon data. | 91 // Set up our sources for thumbnail and favicon data. |
47 profile->GetChromeURLDataManager()->AddDataSource( | 92 profile->GetChromeURLDataManager()->AddDataSource( |
48 new ThumbnailSource(profile)); | 93 new ThumbnailSource(profile)); |
49 profile->GetChromeURLDataManager()->AddDataSource( | 94 profile->GetChromeURLDataManager()->AddDataSource( |
50 new FaviconSource(profile, FaviconSource::FAVICON)); | 95 new FaviconSource(profile, FaviconSource::FAVICON)); |
51 | 96 |
(...skipping 22 matching lines...) Expand all Loading... |
74 // Register ourselves for any suggestions item blacklisting. | 119 // Register ourselves for any suggestions item blacklisting. |
75 web_ui()->RegisterMessageCallback("blacklistURLFromSuggestions", | 120 web_ui()->RegisterMessageCallback("blacklistURLFromSuggestions", |
76 base::Bind(&SuggestionsHandler::HandleBlacklistURL, | 121 base::Bind(&SuggestionsHandler::HandleBlacklistURL, |
77 base::Unretained(this))); | 122 base::Unretained(this))); |
78 web_ui()->RegisterMessageCallback("removeURLsFromSuggestionsBlacklist", | 123 web_ui()->RegisterMessageCallback("removeURLsFromSuggestionsBlacklist", |
79 base::Bind(&SuggestionsHandler::HandleRemoveURLsFromBlacklist, | 124 base::Bind(&SuggestionsHandler::HandleRemoveURLsFromBlacklist, |
80 base::Unretained(this))); | 125 base::Unretained(this))); |
81 web_ui()->RegisterMessageCallback("clearSuggestionsURLsBlacklist", | 126 web_ui()->RegisterMessageCallback("clearSuggestionsURLsBlacklist", |
82 base::Bind(&SuggestionsHandler::HandleClearBlacklist, | 127 base::Bind(&SuggestionsHandler::HandleClearBlacklist, |
83 base::Unretained(this))); | 128 base::Unretained(this))); |
| 129 web_ui()->RegisterMessageCallback("suggestedSitesAction", |
| 130 base::Bind(&SuggestionsHandler::HandleSuggestedSitesAction, |
| 131 base::Unretained(this))); |
| 132 web_ui()->RegisterMessageCallback("suggestedSitesSelected", |
| 133 base::Bind(&SuggestionsHandler::HandleSuggestedSitesSelected, |
| 134 base::Unretained(this))); |
84 } | 135 } |
85 | 136 |
86 void SuggestionsHandler::HandleGetSuggestions(const ListValue* args) { | 137 void SuggestionsHandler::HandleGetSuggestions(const ListValue* args) { |
87 if (!got_first_suggestions_request_) { | 138 if (!got_first_suggestions_request_) { |
88 // If our initial data is already here, return it. | 139 // If our initial data is already here, return it. |
89 SendPagesValue(); | 140 SendPagesValue(); |
90 got_first_suggestions_request_ = true; | 141 got_first_suggestions_request_ = true; |
91 } else { | 142 } else { |
92 StartQueryForSuggestions(); | 143 StartQueryForSuggestions(); |
93 } | 144 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 179 |
129 void SuggestionsHandler::HandleRemoveURLsFromBlacklist(const ListValue* args) { | 180 void SuggestionsHandler::HandleRemoveURLsFromBlacklist(const ListValue* args) { |
130 DCHECK_GT(args->GetSize(), 0U); | 181 DCHECK_GT(args->GetSize(), 0U); |
131 // TODO(georgey) remove URLs from blacklist. | 182 // TODO(georgey) remove URLs from blacklist. |
132 } | 183 } |
133 | 184 |
134 void SuggestionsHandler::HandleClearBlacklist(const ListValue* args) { | 185 void SuggestionsHandler::HandleClearBlacklist(const ListValue* args) { |
135 // TODO(georgey) clear blacklist. | 186 // TODO(georgey) clear blacklist. |
136 } | 187 } |
137 | 188 |
| 189 void SuggestionsHandler::HandleSuggestedSitesAction( |
| 190 const base::ListValue* args) { |
| 191 DCHECK(args); |
| 192 |
| 193 double action_id; |
| 194 if (!args->GetDouble(0, &action_id)) |
| 195 NOTREACHED(); |
| 196 |
| 197 UMA_HISTOGRAM_ENUMERATION("NewTabPage.SuggestedSitesAction", |
| 198 static_cast<int>(action_id), |
| 199 NUM_SUGGESTED_SITES_ACTIONS); |
| 200 suggestions_viewed_ = true; |
| 201 user_action_logged_ = true; |
| 202 } |
| 203 |
| 204 void SuggestionsHandler::HandleSuggestedSitesSelected( |
| 205 const base::ListValue* args) { |
| 206 suggestions_viewed_ = true; |
| 207 } |
| 208 |
138 void SuggestionsHandler::SetPagesValueFromTopSites( | 209 void SuggestionsHandler::SetPagesValueFromTopSites( |
139 const history::MostVisitedURLList& data) { | 210 const history::MostVisitedURLList& data) { |
140 pages_value_.reset(new ListValue()); | 211 pages_value_.reset(new ListValue()); |
141 for (size_t i = 0; i < data.size(); i++) { | 212 for (size_t i = 0; i < data.size(); i++) { |
142 const history::MostVisitedURL& suggested_url = data[i]; | 213 const history::MostVisitedURL& suggested_url = data[i]; |
143 if (suggested_url.url.is_empty()) | 214 if (suggested_url.url.is_empty()) |
144 continue; | 215 continue; |
145 | 216 |
146 DictionaryValue* page_value = new DictionaryValue(); | 217 DictionaryValue* page_value = new DictionaryValue(); |
147 NewTabUI::SetURLTitleAndDirection(page_value, | 218 NewTabUI::SetURLTitleAndDirection(page_value, |
(...skipping 25 matching lines...) Expand all Loading... |
173 } | 244 } |
174 | 245 |
175 std::string SuggestionsHandler::GetDictionaryKeyForURL(const std::string& url) { | 246 std::string SuggestionsHandler::GetDictionaryKeyForURL(const std::string& url) { |
176 return base::MD5String(url); | 247 return base::MD5String(url); |
177 } | 248 } |
178 | 249 |
179 // static | 250 // static |
180 void SuggestionsHandler::RegisterUserPrefs(PrefService* prefs) { | 251 void SuggestionsHandler::RegisterUserPrefs(PrefService* prefs) { |
181 // TODO(georgey) add user preferences (such as own blacklist) as needed. | 252 // TODO(georgey) add user preferences (such as own blacklist) as needed. |
182 } | 253 } |
OLD | NEW |