| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/dom_ui/new_tab_ui.h" | 7 #include "chrome/browser/dom_ui/new_tab_ui.h" |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| 11 #include "app/l10n_util.h" | 11 #include "app/l10n_util.h" |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/histogram.h" | 14 #include "base/histogram.h" |
| 15 #include "base/i18n/rtl.h" | 15 #include "base/i18n/rtl.h" |
| 16 #include "base/singleton.h" | 16 #include "base/singleton.h" |
| 17 #include "base/thread.h" | 17 #include "base/thread.h" |
| 18 #include "chrome/browser/bookmarks/bookmark_model.h" |
| 18 #include "chrome/browser/browser.h" | 19 #include "chrome/browser/browser.h" |
| 20 #include "chrome/browser/browser_window.h" |
| 19 #include "chrome/browser/chrome_thread.h" | 21 #include "chrome/browser/chrome_thread.h" |
| 20 #include "chrome/browser/dom_ui/app_launcher_handler.h" | 22 #include "chrome/browser/dom_ui/app_launcher_handler.h" |
| 21 #include "chrome/browser/dom_ui/dom_ui_theme_source.h" | 23 #include "chrome/browser/dom_ui/dom_ui_theme_source.h" |
| 22 #include "chrome/browser/dom_ui/most_visited_handler.h" | 24 #include "chrome/browser/dom_ui/most_visited_handler.h" |
| 23 #include "chrome/browser/dom_ui/new_tab_page_sync_handler.h" | 25 #include "chrome/browser/dom_ui/new_tab_page_sync_handler.h" |
| 24 #include "chrome/browser/dom_ui/ntp_resource_cache.h" | 26 #include "chrome/browser/dom_ui/ntp_resource_cache.h" |
| 25 #include "chrome/browser/dom_ui/shown_sections_handler.h" | 27 #include "chrome/browser/dom_ui/shown_sections_handler.h" |
| 26 #include "chrome/browser/dom_ui/tips_handler.h" | 28 #include "chrome/browser/dom_ui/tips_handler.h" |
| 29 #include "chrome/browser/importer/importer_data_types.h" |
| 27 #include "chrome/browser/metrics/user_metrics.h" | 30 #include "chrome/browser/metrics/user_metrics.h" |
| 28 #include "chrome/browser/pref_service.h" | 31 #include "chrome/browser/pref_service.h" |
| 29 #include "chrome/browser/profile.h" | 32 #include "chrome/browser/profile.h" |
| 30 #include "chrome/browser/renderer_host/render_view_host.h" | 33 #include "chrome/browser/renderer_host/render_view_host.h" |
| 31 #include "chrome/browser/sessions/session_types.h" | 34 #include "chrome/browser/sessions/session_types.h" |
| 32 #include "chrome/browser/sessions/tab_restore_service.h" | 35 #include "chrome/browser/sessions/tab_restore_service.h" |
| 33 #include "chrome/browser/sync/profile_sync_service.h" | 36 #include "chrome/browser/sync/profile_sync_service.h" |
| 34 #include "chrome/browser/tab_contents/tab_contents.h" | 37 #include "chrome/browser/tab_contents/tab_contents.h" |
| 35 #include "chrome/common/chrome_switches.h" | 38 #include "chrome/common/chrome_switches.h" |
| 36 #include "chrome/common/notification_service.h" | 39 #include "chrome/common/notification_service.h" |
| 37 #include "chrome/common/pref_names.h" | 40 #include "chrome/common/pref_names.h" |
| 38 #include "chrome/common/url_constants.h" | 41 #include "chrome/common/url_constants.h" |
| 39 #include "grit/generated_resources.h" | 42 #include "grit/generated_resources.h" |
| 40 | 43 |
| 44 #if defined(OS_WIN) |
| 45 #include "chrome/browser/views/importer_view.h" |
| 46 #include "views/window/window.h" |
| 47 #endif |
| 48 |
| 41 namespace { | 49 namespace { |
| 42 | 50 |
| 43 // The number of recent bookmarks we show. | 51 // The number of recent bookmarks we show. |
| 44 const int kRecentBookmarks = 9; | 52 const int kRecentBookmarks = 9; |
| 45 | 53 |
| 46 // The number of search URLs to show. | 54 // The number of search URLs to show. |
| 47 const int kSearchURLs = 3; | 55 const int kSearchURLs = 3; |
| 48 | 56 |
| 49 // Strings sent to the page via jstemplates used to set the direction of the | 57 // Strings sent to the page via jstemplates used to set the direction of the |
| 50 // HTML document based on locale. | 58 // HTML document based on locale. |
| (...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 dom_ui_->GetProfile()->GetPrefs()->SetBoolean(prefs::kHomePageIsNewTabPage, | 430 dom_ui_->GetProfile()->GetPrefs()->SetBoolean(prefs::kHomePageIsNewTabPage, |
| 423 true); | 431 true); |
| 424 ListValue list_value; | 432 ListValue list_value; |
| 425 list_value.Append(new StringValue( | 433 list_value.Append(new StringValue( |
| 426 l10n_util::GetString(IDS_NEW_TAB_HOME_PAGE_SET_NOTIFICATION))); | 434 l10n_util::GetString(IDS_NEW_TAB_HOME_PAGE_SET_NOTIFICATION))); |
| 427 list_value.Append(new StringValue( | 435 list_value.Append(new StringValue( |
| 428 l10n_util::GetString(IDS_NEW_TAB_HOME_PAGE_HIDE_NOTIFICATION))); | 436 l10n_util::GetString(IDS_NEW_TAB_HOME_PAGE_HIDE_NOTIFICATION))); |
| 429 dom_ui_->CallJavascriptFunction(L"onHomePageSet", list_value); | 437 dom_ui_->CallJavascriptFunction(L"onHomePageSet", list_value); |
| 430 } | 438 } |
| 431 | 439 |
| 440 /////////////////////////////////////////////////////////////////////////////// |
| 441 // NewTabPageImportBookmarksHandler |
| 442 class NewTabPageImportBookmarksHandler : public DOMMessageHandler { |
| 443 public: |
| 444 NewTabPageImportBookmarksHandler() {} |
| 445 virtual ~NewTabPageImportBookmarksHandler() {} |
| 446 |
| 447 // DOMMessageHandler implementation. |
| 448 virtual void RegisterMessages(); |
| 449 |
| 450 // Callback for "importBookmarks". |
| 451 void HandleImportBookmarks(const Value* value); |
| 452 private: |
| 453 |
| 454 DISALLOW_COPY_AND_ASSIGN(NewTabPageImportBookmarksHandler); |
| 455 }; |
| 456 |
| 457 void NewTabPageImportBookmarksHandler::RegisterMessages() { |
| 458 dom_ui_->RegisterMessageCallback("importBookmarks", NewCallback( |
| 459 this, &NewTabPageImportBookmarksHandler::HandleImportBookmarks)); |
| 460 } |
| 461 |
| 462 void NewTabPageImportBookmarksHandler::HandleImportBookmarks( |
| 463 const Value* value) { |
| 464 Browser* browser = NULL; |
| 465 TabContentsDelegate* delegate = dom_ui_->tab_contents()->delegate(); |
| 466 if (delegate) |
| 467 browser = delegate->GetBrowser(); |
| 468 DCHECK(browser); |
| 469 #if defined(OS_WIN) |
| 470 views::Window::CreateChromeWindow( |
| 471 browser->window()->GetNativeHandle(), |
| 472 gfx::Rect(), |
| 473 new ImporterView(dom_ui_->GetProfile(), importer::FAVORITES))->Show(); |
| 474 #endif |
| 475 } |
| 476 |
| 432 } // namespace | 477 } // namespace |
| 433 | 478 |
| 434 /////////////////////////////////////////////////////////////////////////////// | 479 /////////////////////////////////////////////////////////////////////////////// |
| 435 // NewTabUI | 480 // NewTabUI |
| 436 | 481 |
| 437 NewTabUI::NewTabUI(TabContents* contents) | 482 NewTabUI::NewTabUI(TabContents* contents) |
| 438 : DOMUI(contents) { | 483 : DOMUI(contents) { |
| 439 // Override some options on the DOM UI. | 484 // Override some options on the DOM UI. |
| 440 hide_favicon_ = true; | 485 hide_favicon_ = true; |
| 441 force_bookmark_bar_visible_ = true; | 486 force_bookmark_bar_visible_ = true; |
| 442 force_extension_shelf_visible_ = true; | 487 force_extension_shelf_visible_ = true; |
| 443 focus_location_bar_by_default_ = true; | 488 focus_location_bar_by_default_ = true; |
| 444 should_hide_url_ = true; | 489 should_hide_url_ = true; |
| 445 overridden_title_ = WideToUTF16Hack(l10n_util::GetString(IDS_NEW_TAB_TITLE)); | 490 overridden_title_ = WideToUTF16Hack(l10n_util::GetString(IDS_NEW_TAB_TITLE)); |
| 446 | 491 |
| 447 // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more | 492 // We count all link clicks as AUTO_BOOKMARK, so that site can be ranked more |
| 448 // highly. Note this means we're including clicks on not only most visited | 493 // highly. Note this means we're including clicks on not only most visited |
| 449 // thumbnails, but also clicks on recently bookmarked. | 494 // thumbnails, but also clicks on recently bookmarked. |
| 450 link_transition_type_ = PageTransition::AUTO_BOOKMARK; | 495 link_transition_type_ = PageTransition::AUTO_BOOKMARK; |
| 451 | 496 |
| 452 if (NewTabUI::FirstRunDisabled()) | 497 if (NewTabUI::FirstRunDisabled()) |
| 453 NewTabHTMLSource::set_first_run(false); | 498 NewTabHTMLSource::set_first_run(false); |
| 454 | 499 |
| 455 static bool first_view = true; | 500 static bool first_view = true; |
| 456 if (first_view) { | 501 if (first_view) { |
| 502 Profile* profile = GetProfile(); |
| 503 profile->GetPrefs()->SetInteger(prefs::kNTPPromoViewsRemaining, |
| 504 profile->GetPrefs()->GetInteger(prefs::kNTPPromoViewsRemaining) - 1); |
| 505 profile->GetBookmarkModel()->AddObserver(this); |
| 457 first_view = false; | 506 first_view = false; |
| 458 } | 507 } |
| 459 | 508 |
| 460 if (!GetProfile()->IsOffTheRecord()) { | 509 if (!GetProfile()->IsOffTheRecord()) { |
| 461 PrefService* pref_service = GetProfile()->GetPrefs(); | 510 PrefService* pref_service = GetProfile()->GetPrefs(); |
| 462 AddMessageHandler((new ShownSectionsHandler(pref_service))->Attach(this)); | 511 AddMessageHandler((new ShownSectionsHandler(pref_service))->Attach(this)); |
| 463 AddMessageHandler((new MostVisitedHandler())->Attach(this)); | 512 AddMessageHandler((new MostVisitedHandler())->Attach(this)); |
| 464 AddMessageHandler((new RecentlyClosedTabsHandler())->Attach(this)); | 513 AddMessageHandler((new RecentlyClosedTabsHandler())->Attach(this)); |
| 465 AddMessageHandler((new MetricsHandler())->Attach(this)); | 514 AddMessageHandler((new MetricsHandler())->Attach(this)); |
| 466 if (WebResourcesEnabled()) | 515 if (WebResourcesEnabled()) |
| 467 AddMessageHandler((new TipsHandler())->Attach(this)); | 516 AddMessageHandler((new TipsHandler())->Attach(this)); |
| 468 if (GetProfile()->IsSyncAccessible()) | 517 if (GetProfile()->IsSyncAccessible()) |
| 469 AddMessageHandler((new NewTabPageSyncHandler())->Attach(this)); | 518 AddMessageHandler((new NewTabPageSyncHandler())->Attach(this)); |
| 470 if (Extension::AppsAreEnabled()) { | 519 if (Extension::AppsAreEnabled()) { |
| 471 ExtensionsService* service = GetProfile()->GetExtensionsService(); | 520 ExtensionsService* service = GetProfile()->GetExtensionsService(); |
| 472 // We might not have an ExtensionsService (on ChromeOS when not logged in | 521 // We might not have an ExtensionsService (on ChromeOS when not logged in |
| 473 // for example). | 522 // for example). |
| 474 if (service) | 523 if (service) |
| 475 AddMessageHandler((new AppLauncherHandler(service))->Attach(this)); | 524 AddMessageHandler((new AppLauncherHandler(service))->Attach(this)); |
| 476 } | 525 } |
| 477 | 526 |
| 478 AddMessageHandler((new NewTabPageSetHomePageHandler())->Attach(this)); | 527 AddMessageHandler((new NewTabPageSetHomePageHandler())->Attach(this)); |
| 528 AddMessageHandler((new NewTabPageImportBookmarksHandler())->Attach(this)); |
| 479 } | 529 } |
| 480 | 530 |
| 481 // Initializing the CSS and HTML can require some CPU, so do it after | 531 // Initializing the CSS and HTML can require some CPU, so do it after |
| 482 // we've hooked up the most visited handler. This allows the DB query | 532 // we've hooked up the most visited handler. This allows the DB query |
| 483 // for the new tab thumbs to happen earlier. | 533 // for the new tab thumbs to happen earlier. |
| 484 InitializeCSSCaches(); | 534 InitializeCSSCaches(); |
| 485 NewTabHTMLSource* html_source = | 535 NewTabHTMLSource* html_source = |
| 486 new NewTabHTMLSource(GetProfile()->GetOriginalProfile()); | 536 new NewTabHTMLSource(GetProfile()->GetOriginalProfile()); |
| 487 ChromeThread::PostTask( | 537 ChromeThread::PostTask( |
| 488 ChromeThread::IO, FROM_HERE, | 538 ChromeThread::IO, FROM_HERE, |
| 489 NewRunnableMethod( | 539 NewRunnableMethod( |
| 490 Singleton<ChromeURLDataManager>::get(), | 540 Singleton<ChromeURLDataManager>::get(), |
| 491 &ChromeURLDataManager::AddDataSource, | 541 &ChromeURLDataManager::AddDataSource, |
| 492 make_scoped_refptr(html_source))); | 542 make_scoped_refptr(html_source))); |
| 493 | 543 |
| 494 // Listen for theme installation. | 544 // Listen for theme installation. |
| 495 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, | 545 registrar_.Add(this, NotificationType::BROWSER_THEME_CHANGED, |
| 496 NotificationService::AllSources()); | 546 NotificationService::AllSources()); |
| 497 // Listen for bookmark bar visibility changes. | 547 // Listen for bookmark bar visibility changes. |
| 498 registrar_.Add(this, NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, | 548 registrar_.Add(this, NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED, |
| 499 NotificationService::AllSources()); | 549 NotificationService::AllSources()); |
| 500 } | 550 } |
| 501 | 551 |
| 502 NewTabUI::~NewTabUI() { | 552 NewTabUI::~NewTabUI() { |
| 553 BookmarkModel* bookmark_model = GetProfile()->GetBookmarkModel(); |
| 554 if (bookmark_model) |
| 555 bookmark_model->RemoveObserver(this); |
| 503 } | 556 } |
| 504 | 557 |
| 505 void NewTabUI::RenderViewCreated(RenderViewHost* render_view_host) { | 558 void NewTabUI::RenderViewCreated(RenderViewHost* render_view_host) { |
| 506 render_view_host->set_paint_observer(new PaintTimer); | 559 render_view_host->set_paint_observer(new PaintTimer); |
| 507 } | 560 } |
| 508 | 561 |
| 509 void NewTabUI::RenderViewReused(RenderViewHost* render_view_host) { | 562 void NewTabUI::RenderViewReused(RenderViewHost* render_view_host) { |
| 510 render_view_host->set_paint_observer(new PaintTimer); | 563 render_view_host->set_paint_observer(new PaintTimer); |
| 511 } | 564 } |
| 512 | 565 |
| 566 void NewTabUI::BookmarkNodeAdded(BookmarkModel* model, |
| 567 const BookmarkNode* parent, |
| 568 int index) { |
| 569 // Stop showing the promo, and no longer observe the bookmark model. |
| 570 GetProfile()->GetPrefs()->SetInteger(prefs::kNTPPromoViewsRemaining, 0); |
| 571 GetProfile()->GetBookmarkModel()->RemoveObserver(this); |
| 572 } |
| 573 |
| 513 void NewTabUI::Observe(NotificationType type, | 574 void NewTabUI::Observe(NotificationType type, |
| 514 const NotificationSource& source, | 575 const NotificationSource& source, |
| 515 const NotificationDetails& details) { | 576 const NotificationDetails& details) { |
| 516 if (NotificationType::BROWSER_THEME_CHANGED == type) { | 577 if (NotificationType::BROWSER_THEME_CHANGED == type) { |
| 517 InitializeCSSCaches(); | 578 InitializeCSSCaches(); |
| 518 CallJavascriptFunction(L"themeChanged"); | 579 CallJavascriptFunction(L"themeChanged"); |
| 519 } else if (NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED) { | 580 } else if (NotificationType::BOOKMARK_BAR_VISIBILITY_PREF_CHANGED) { |
| 520 if (GetProfile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar)) | 581 if (GetProfile()->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar)) |
| 521 CallJavascriptFunction(L"bookmarkBarAttached"); | 582 CallJavascriptFunction(L"bookmarkBarAttached"); |
| 522 else | 583 else |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 649 // URL from the new tab page, but in any case it's an error. | 710 // URL from the new tab page, but in any case it's an error. |
| 650 NOTREACHED(); | 711 NOTREACHED(); |
| 651 return; | 712 return; |
| 652 } | 713 } |
| 653 | 714 |
| 654 scoped_refptr<RefCountedBytes> html_bytes = | 715 scoped_refptr<RefCountedBytes> html_bytes = |
| 655 profile_->GetNTPResourceCache()->GetNewTabHTML(is_off_the_record); | 716 profile_->GetNTPResourceCache()->GetNewTabHTML(is_off_the_record); |
| 656 | 717 |
| 657 SendResponse(request_id, html_bytes); | 718 SendResponse(request_id, html_bytes); |
| 658 } | 719 } |
| OLD | NEW |