Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(466)

Side by Side Diff: chrome/browser/ui/webui/ntp/new_tab_ui.cc

Issue 9358031: Added new adaptive "Suggest" tab on the New Tab Page, behing the flag, for the experiments. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed bug in card_slider.js which prevented correct card be selected in dome cases Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" 7 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
8 8
9 #include <set> 9 #include <set>
10 10
(...skipping 20 matching lines...) Expand all
31 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h" 31 #include "chrome/browser/ui/webui/ntp/app_launcher_handler.h"
32 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h" 32 #include "chrome/browser/ui/webui/ntp/favicon_webui_handler.h"
33 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h" 33 #include "chrome/browser/ui/webui/ntp/foreign_session_handler.h"
34 #include "chrome/browser/ui/webui/ntp/most_visited_handler.h" 34 #include "chrome/browser/ui/webui/ntp/most_visited_handler.h"
35 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h" 35 #include "chrome/browser/ui/webui/ntp/new_tab_page_handler.h"
36 #include "chrome/browser/ui/webui/ntp/new_tab_page_sync_handler.h" 36 #include "chrome/browser/ui/webui/ntp/new_tab_page_sync_handler.h"
37 #include "chrome/browser/ui/webui/ntp/ntp_login_handler.h" 37 #include "chrome/browser/ui/webui/ntp/ntp_login_handler.h"
38 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h" 38 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache_factory.h"
39 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h" 39 #include "chrome/browser/ui/webui/ntp/ntp_resource_cache.h"
40 #include "chrome/browser/ui/webui/ntp/recently_closed_tabs_handler.h" 40 #include "chrome/browser/ui/webui/ntp/recently_closed_tabs_handler.h"
41 #include "chrome/browser/ui/webui/ntp/suggestions_page_handler.h"
41 #include "chrome/browser/ui/webui/theme_source.h" 42 #include "chrome/browser/ui/webui/theme_source.h"
42 #include "chrome/common/chrome_notification_types.h" 43 #include "chrome/common/chrome_notification_types.h"
43 #include "chrome/common/chrome_switches.h" 44 #include "chrome/common/chrome_switches.h"
44 #include "chrome/common/extensions/extension.h" 45 #include "chrome/common/extensions/extension.h"
45 #include "chrome/common/pref_names.h" 46 #include "chrome/common/pref_names.h"
46 #include "chrome/common/url_constants.h" 47 #include "chrome/common/url_constants.h"
47 #include "content/browser/renderer_host/render_view_host.h" 48 #include "content/browser/renderer_host/render_view_host.h"
48 #include "content/public/browser/browser_thread.h" 49 #include "content/public/browser/browser_thread.h"
49 #include "content/public/browser/notification_service.h" 50 #include "content/public/browser/notification_service.h"
50 #include "content/public/browser/user_metrics.h" 51 #include "content/public/browser/user_metrics.h"
51 #include "content/public/browser/web_contents.h" 52 #include "content/public/browser/web_contents.h"
52 #include "content/public/browser/web_ui.h" 53 #include "content/public/browser/web_ui.h"
54 #include "grit/browser_resources.h"
53 #include "grit/generated_resources.h" 55 #include "grit/generated_resources.h"
54 #include "grit/theme_resources.h" 56 #include "grit/theme_resources.h"
55 #include "ui/base/l10n/l10n_util.h" 57 #include "ui/base/l10n/l10n_util.h"
56 58
57 using content::BrowserThread; 59 using content::BrowserThread;
58 using content::UserMetricsAction; 60 using content::UserMetricsAction;
59 using content::WebContents; 61 using content::WebContents;
60 using content::WebUIController; 62 using content::WebUIController;
61 63
62 namespace { 64 namespace {
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 web_ui->OverrideTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE)); 103 web_ui->OverrideTitle(l10n_util::GetStringUTF16(IDS_NEW_TAB_TITLE));
102 104
103 // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more 105 // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more
104 // highly. Note this means we're including clicks on not only most visited 106 // highly. Note this means we're including clicks on not only most visited
105 // thumbnails, but also clicks on recently bookmarked. 107 // thumbnails, but also clicks on recently bookmarked.
106 web_ui->SetLinkTransitionType(content::PAGE_TRANSITION_AUTO_BOOKMARK); 108 web_ui->SetLinkTransitionType(content::PAGE_TRANSITION_AUTO_BOOKMARK);
107 109
108 if (!GetProfile()->IsOffTheRecord()) { 110 if (!GetProfile()->IsOffTheRecord()) {
109 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); 111 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler());
110 web_ui->AddMessageHandler(new MostVisitedHandler()); 112 web_ui->AddMessageHandler(new MostVisitedHandler());
113 if (NewTabUI::IsSuggestionsPageEnabled())
114 web_ui->AddMessageHandler(new SuggestionsHandler());
111 web_ui->AddMessageHandler(new RecentlyClosedTabsHandler()); 115 web_ui->AddMessageHandler(new RecentlyClosedTabsHandler());
112 web_ui->AddMessageHandler(new MetricsHandler()); 116 web_ui->AddMessageHandler(new MetricsHandler());
113 if (GetProfile()->IsSyncAccessible()) 117 if (GetProfile()->IsSyncAccessible())
114 web_ui->AddMessageHandler(new NewTabPageSyncHandler()); 118 web_ui->AddMessageHandler(new NewTabPageSyncHandler());
115 ExtensionService* service = GetProfile()->GetExtensionService(); 119 ExtensionService* service = GetProfile()->GetExtensionService();
116 // We might not have an ExtensionService (on ChromeOS when not logged in 120 // We might not have an ExtensionService (on ChromeOS when not logged in
117 // for example). 121 // for example).
118 if (service) 122 if (service)
119 web_ui->AddMessageHandler(new AppLauncherHandler(service)); 123 web_ui->AddMessageHandler(new AppLauncherHandler(service));
120 124
121 web_ui->AddMessageHandler(new NewTabPageHandler()); 125 web_ui->AddMessageHandler(new NewTabPageHandler());
122 web_ui->AddMessageHandler(new FaviconWebUIHandler()); 126 web_ui->AddMessageHandler(new FaviconWebUIHandler());
123 } 127 }
124 128
125 if (NTPLoginHandler::ShouldShow(GetProfile())) 129 if (NTPLoginHandler::ShouldShow(GetProfile()))
126 web_ui->AddMessageHandler(new NTPLoginHandler()); 130 web_ui->AddMessageHandler(new NTPLoginHandler());
127 131
128 // Initializing the CSS and HTML can require some CPU, so do it after 132 // Initializing the CSS and HTML can require some CPU, so do it after
129 // we've hooked up the most visited handler. This allows the DB query 133 // we've hooked up the most visited handler. This allows the DB query
130 // for the new tab thumbs to happen earlier. 134 // for the new tab thumbs to happen earlier.
131 InitializeCSSCaches(); 135 InitializeCSSCaches();
132 NewTabHTMLSource* html_source = 136 NewTabHTMLSource* html_source =
133 new NewTabHTMLSource(GetProfile()->GetOriginalProfile()); 137 new NewTabHTMLSource(GetProfile()->GetOriginalProfile());
134 GetProfile()->GetChromeURLDataManager()->AddDataSource(html_source); 138 GetProfile()->GetChromeURLDataManager()->AddDataSource(html_source);
139 // These two resources should be loaded only if suggestions NTP is enabled.
140 html_source->AddResource("suggestions_page.css", "text/css",
141 NewTabUI::IsSuggestionsPageEnabled() ? IDR_SUGGESTIONS_PAGE_CSS : 0);
142 if (NewTabUI::IsSuggestionsPageEnabled()) {
143 html_source->AddResource("suggestions_page.js", "text/javascript",
Dan Beam 2012/02/28 20:14:24 nit: should this be application/javascript? if yo
GeorgeY 2012/02/28 21:17:42 sure
144 IDR_SUGGESTIONS_PAGE_JS);
145 }
135 146
136 // Listen for theme installation. 147 // Listen for theme installation.
137 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED, 148 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_THEME_CHANGED,
138 content::Source<ThemeService>( 149 content::Source<ThemeService>(
139 ThemeServiceFactory::GetForProfile(GetProfile()))); 150 ThemeServiceFactory::GetForProfile(GetProfile())));
140 } 151 }
141 152
142 NewTabUI::~NewTabUI() { 153 NewTabUI::~NewTabUI() {
143 g_live_new_tabs.Pointer()->erase(this); 154 g_live_new_tabs.Pointer()->erase(this);
144 } 155 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 Profile* profile = GetProfile(); 231 Profile* profile = GetProfile();
221 ThemeSource* theme = new ThemeSource(profile); 232 ThemeSource* theme = new ThemeSource(profile);
222 profile->GetChromeURLDataManager()->AddDataSource(theme); 233 profile->GetChromeURLDataManager()->AddDataSource(theme);
223 } 234 }
224 235
225 // static 236 // static
226 void NewTabUI::RegisterUserPrefs(PrefService* prefs) { 237 void NewTabUI::RegisterUserPrefs(PrefService* prefs) {
227 NewTabPageHandler::RegisterUserPrefs(prefs); 238 NewTabPageHandler::RegisterUserPrefs(prefs);
228 AppLauncherHandler::RegisterUserPrefs(prefs); 239 AppLauncherHandler::RegisterUserPrefs(prefs);
229 MostVisitedHandler::RegisterUserPrefs(prefs); 240 MostVisitedHandler::RegisterUserPrefs(prefs);
241 if (NewTabUI::IsSuggestionsPageEnabled())
242 SuggestionsHandler::RegisterUserPrefs(prefs);
230 } 243 }
231 244
232 // static 245 // static
233 void NewTabUI::SetupFieldTrials() { 246 void NewTabUI::SetupFieldTrials() {
234 scoped_refptr<base::FieldTrial> trial( 247 scoped_refptr<base::FieldTrial> trial(
235 new base::FieldTrial(kWebStoreLinkExperiment, 1000, "Disabled", 248 new base::FieldTrial(kWebStoreLinkExperiment, 1000, "Disabled",
236 2012, 6, 1)); 249 2012, 6, 1));
237 250
238 // Try to give the user a consistent experience, if possible. 251 // Try to give the user a consistent experience, if possible.
239 if (base::FieldTrialList::IsOneTimeRandomizationEnabled()) 252 if (base::FieldTrialList::IsOneTimeRandomizationEnabled())
(...skipping 13 matching lines...) Expand all
253 } 266 }
254 267
255 // static 268 // static
256 bool NewTabUI::ShouldShowAppInstallHint() { 269 bool NewTabUI::ShouldShowAppInstallHint() {
257 const CommandLine* cli = CommandLine::ForCurrentProcess(); 270 const CommandLine* cli = CommandLine::ForCurrentProcess();
258 return cli->HasSwitch(switches::kNtpAppInstallHint) || 271 return cli->HasSwitch(switches::kNtpAppInstallHint) ||
259 WebStoreLinkExperimentGroupIs(g_hint_group); 272 WebStoreLinkExperimentGroupIs(g_hint_group);
260 } 273 }
261 274
262 // static 275 // static
276 bool NewTabUI::IsSuggestionsPageEnabled() {
277 return CommandLine::ForCurrentProcess()->HasSwitch(
278 switches::kEnableSuggestionsTabPage);
279 }
280
281 // static
263 void NewTabUI::SetURLTitleAndDirection(DictionaryValue* dictionary, 282 void NewTabUI::SetURLTitleAndDirection(DictionaryValue* dictionary,
264 const string16& title, 283 const string16& title,
265 const GURL& gurl) { 284 const GURL& gurl) {
266 dictionary->SetString("url", gurl.spec()); 285 dictionary->SetString("url", gurl.spec());
267 286
268 bool using_url_as_the_title = false; 287 bool using_url_as_the_title = false;
269 string16 title_to_set(title); 288 string16 title_to_set(title);
270 if (title_to_set.empty()) { 289 if (title_to_set.empty()) {
271 using_url_as_the_title = true; 290 using_url_as_the_title = true;
272 title_to_set = UTF8ToUTF16(gurl.spec()); 291 title_to_set = UTF8ToUTF16(gurl.spec());
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
312 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile) 331 NewTabUI::NewTabHTMLSource::NewTabHTMLSource(Profile* profile)
313 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()), 332 : DataSource(chrome::kChromeUINewTabHost, MessageLoop::current()),
314 profile_(profile) { 333 profile_(profile) {
315 } 334 }
316 335
317 void NewTabUI::NewTabHTMLSource::StartDataRequest(const std::string& path, 336 void NewTabUI::NewTabHTMLSource::StartDataRequest(const std::string& path,
318 bool is_incognito, 337 bool is_incognito,
319 int request_id) { 338 int request_id) {
320 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 339 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
321 340
341 std::map<std::string, std::pair<std::string, int> >::iterator it =
342 resource_map_.find(path);
343 if (it != resource_map_.end()) {
344 scoped_refptr<RefCountedStaticMemory> resource_bytes(
Dan Beam 2012/02/28 20:14:24 nit: indent is weird
GeorgeY 2012/02/28 21:17:42 Four spaces - what is the problem?
Dan Beam 2012/02/28 22:30:12 It's fine.
345 it->second.second ?
346 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
347 it->second.second) :
348 new RefCountedStaticMemory);
349 SendResponse(request_id, resource_bytes);
350 return;
351 }
352
322 if (!path.empty() && path[0] != '#') { 353 if (!path.empty() && path[0] != '#') {
323 // A path under new-tab was requested; it's likely a bad relative 354 // A path under new-tab was requested; it's likely a bad relative
324 // URL from the new tab page, but in any case it's an error. 355 // URL from the new tab page, but in any case it's an error.
325 NOTREACHED() << path << " should not have been requested on the NTP"; 356 NOTREACHED() << path << " should not have been requested on the NTP";
326 return; 357 return;
327 } 358 }
328 359
329 scoped_refptr<RefCountedMemory> html_bytes( 360 scoped_refptr<RefCountedMemory> html_bytes(
330 NTPResourceCacheFactory::GetForProfile(profile_)-> 361 NTPResourceCacheFactory::GetForProfile(profile_)->
331 GetNewTabHTML(is_incognito)); 362 GetNewTabHTML(is_incognito));
332 363
333 SendResponse(request_id, html_bytes); 364 SendResponse(request_id, html_bytes);
334 } 365 }
335 366
336 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string&) const { 367 std::string NewTabUI::NewTabHTMLSource::GetMimeType(const std::string& s)
Dan Beam 2012/02/28 20:14:24 can you rename |s| to something more descriptive?
GeorgeY 2012/02/28 21:17:42 Done.
368 const {
369 std::map<std::string, std::pair<std::string, int> >::const_iterator it =
370 resource_map_.find(s);
371 if (it != resource_map_.end())
372 return it->second.first;
337 return "text/html"; 373 return "text/html";
338 } 374 }
339 375
340 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const { 376 bool NewTabUI::NewTabHTMLSource::ShouldReplaceExistingSource() const {
341 return false; 377 return false;
342 } 378 }
379
380 void NewTabUI::NewTabHTMLSource::AddResource(const char* resource,
381 const char* mime_type,
382 int resource_id) {
383 DCHECK(resource);
384 DCHECK(mime_type);
385 resource_map_[std::string(resource)] =
386 std::make_pair(std::string(mime_type), resource_id);
387 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698