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