OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/dom_ui/new_tab_ui.h" | 5 #include "chrome/browser/dom_ui/new_tab_ui.h" |
6 | 6 |
7 #include "base/histogram.h" | 7 #include "base/histogram.h" |
8 #include "base/string_piece.h" | 8 #include "base/string_piece.h" |
9 #include "chrome/app/locales/locale_settings.h" | 9 #include "chrome/app/locales/locale_settings.h" |
10 #include "chrome/browser/browser.h" | 10 #include "chrome/browser/browser.h" |
(...skipping 626 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 if (content->GetType() == Value::TYPE_LIST) { | 637 if (content->GetType() == Value::TYPE_LIST) { |
638 const ListValue* list_value = static_cast<const ListValue*>(content); | 638 const ListValue* list_value = static_cast<const ListValue*>(content); |
639 Value* list_member; | 639 Value* list_member; |
640 if (list_value->Get(0, &list_member) && | 640 if (list_value->Get(0, &list_member) && |
641 list_member->GetType() == Value::TYPE_STRING) { | 641 list_member->GetType() == Value::TYPE_STRING) { |
642 const StringValue* string_value = | 642 const StringValue* string_value = |
643 static_cast<const StringValue*>(list_member); | 643 static_cast<const StringValue*>(list_member); |
644 std::wstring wstring_value; | 644 std::wstring wstring_value; |
645 if (string_value->GetAsString(&wstring_value)) { | 645 if (string_value->GetAsString(&wstring_value)) { |
646 int session_to_restore = _wtoi(wstring_value.c_str()); | 646 int session_to_restore = _wtoi(wstring_value.c_str()); |
647 | 647 tab_restore_service_->RestoreEntryById(browser, session_to_restore, |
648 const TabRestoreService::Tabs& tabs = tab_restore_service_->tabs(); | 648 true); |
649 for (TabRestoreService::Tabs::const_iterator it = tabs.begin(); | 649 // The current tab has been nuked at this point; don't touch any member |
650 it != tabs.end(); ++it) { | 650 // variables. |
651 if (it->id == session_to_restore) { | |
652 TabRestoreService* tab_restore_service = tab_restore_service_; | |
653 browser->ReplaceRestoredTab( | |
654 it->navigations, it->current_navigation_index); | |
655 tab_restore_service->RemoveHistoricalTabById(session_to_restore); | |
656 // The current tab has been nuked at this point; | |
657 // don't touch any member variables. | |
658 break; | |
659 } | |
660 } | |
661 } | 651 } |
662 } | 652 } |
663 } | 653 } |
664 } | 654 } |
665 | 655 |
666 void RecentlyClosedTabsHandler::HandleGetRecentlyClosedTabs( | 656 void RecentlyClosedTabsHandler::HandleGetRecentlyClosedTabs( |
667 const Value* content) { | 657 const Value* content) { |
668 if (!tab_restore_service_) { | 658 if (!tab_restore_service_) { |
669 tab_restore_service_ = dom_ui_host_->profile()->GetTabRestoreService(); | 659 tab_restore_service_ = dom_ui_host_->profile()->GetTabRestoreService(); |
670 | 660 |
671 // GetTabRestoreService() can return NULL (i.e., when in Off the | 661 // GetTabRestoreService() can return NULL (i.e., when in Off the |
672 // Record mode) | 662 // Record mode) |
673 if (tab_restore_service_) | 663 if (tab_restore_service_) |
674 tab_restore_service_->AddObserver(this); | 664 tab_restore_service_->AddObserver(this); |
675 } | 665 } |
676 | 666 |
677 if (tab_restore_service_) | 667 if (tab_restore_service_) |
678 TabRestoreServiceChanged(tab_restore_service_); | 668 TabRestoreServiceChanged(tab_restore_service_); |
679 } | 669 } |
680 | 670 |
681 void RecentlyClosedTabsHandler::TabRestoreServiceChanged( | 671 void RecentlyClosedTabsHandler::TabRestoreServiceChanged( |
682 TabRestoreService* service) { | 672 TabRestoreService* service) { |
683 const TabRestoreService::Tabs& tabs = service->tabs(); | 673 const TabRestoreService::Entries& entries = service->entries(); |
684 ListValue list_value; | 674 ListValue list_value; |
685 int added_count = 0; | 675 int added_count = 0; |
686 | 676 |
687 // We filter the list of recently closed to only show 'interesting' tabs, | 677 // We filter the list of recently closed to only show 'interesting' entries, |
688 // where an interesting tab navigation is not the new tab ui. | 678 // where an interesting entry is either a closed window or a closed tab |
689 for (TabRestoreService::Tabs::const_iterator it = tabs.begin(); | 679 // whose selected navigation is not the new tab ui. |
690 it != tabs.end() && added_count < 3; ++it) { | 680 for (TabRestoreService::Entries::const_iterator it = entries.begin(); |
691 if (it->navigations.empty()) | 681 it != entries.end() && added_count < 3; ++it) { |
692 continue; | 682 TabRestoreService::Entry* entry = *it; |
693 | 683 DictionaryValue* value = new DictionaryValue(); |
694 const TabNavigation& navigator = | 684 if ((entry->type == TabRestoreService::TAB && |
695 it->navigations.at(it->current_navigation_index); | 685 TabToValue(*static_cast<TabRestoreService::Tab*>(entry), value)) || |
696 if (navigator.url == NewTabUIURL()) | 686 (entry->type == TabRestoreService::WINDOW && |
697 continue; | 687 WindowToValue(*static_cast<TabRestoreService::Window*>(entry), |
698 | 688 value))) { |
699 DictionaryValue* dictionary = new DictionaryValue; | 689 value->SetInteger(L"sessionId", entry->id); |
700 SetURLAndTitle(dictionary, navigator.title, navigator.url); | 690 list_value.Append(value); |
701 dictionary->SetInteger(L"sessionId", it->id); | 691 added_count++; |
702 | 692 } else { |
703 list_value.Append(dictionary); | 693 delete value; |
704 added_count++; | 694 } |
705 } | 695 } |
706 dom_ui_host_->CallJavascriptFunction(L"recentlyClosedTabs", list_value); | 696 dom_ui_host_->CallJavascriptFunction(L"recentlyClosedTabs", list_value); |
707 } | 697 } |
708 | 698 |
709 void RecentlyClosedTabsHandler::TabRestoreServiceDestroyed( | 699 void RecentlyClosedTabsHandler::TabRestoreServiceDestroyed( |
710 TabRestoreService* service) { | 700 TabRestoreService* service) { |
711 tab_restore_service_ = NULL; | 701 tab_restore_service_ = NULL; |
712 } | 702 } |
713 | 703 |
| 704 bool RecentlyClosedTabsHandler::TabToValue( |
| 705 const TabRestoreService::Tab& tab, |
| 706 DictionaryValue* dictionary) { |
| 707 if (tab.navigations.empty()) |
| 708 return false; |
| 709 |
| 710 const TabNavigation& current_navigation = |
| 711 tab.navigations.at(tab.current_navigation_index); |
| 712 if (current_navigation.url == NewTabUIURL()) |
| 713 return false; |
| 714 |
| 715 SetURLAndTitle(dictionary, current_navigation.title, current_navigation.url); |
| 716 dictionary->SetString(L"type", L"tab"); |
| 717 return true; |
| 718 } |
| 719 |
| 720 bool RecentlyClosedTabsHandler::WindowToValue( |
| 721 const TabRestoreService::Window& window, |
| 722 DictionaryValue* dictionary) { |
| 723 if (window.tabs.empty()) { |
| 724 NOTREACHED(); |
| 725 return false; |
| 726 } |
| 727 |
| 728 ListValue* tab_values = new ListValue(); |
| 729 for (size_t i = 0; i < window.tabs.size(); ++i) { |
| 730 DictionaryValue* tab_value = new DictionaryValue(); |
| 731 if (TabToValue(window.tabs[i], tab_value)) |
| 732 tab_values->Append(tab_value); |
| 733 else |
| 734 delete tab_value; |
| 735 } |
| 736 if (tab_values->GetSize() == 0) { |
| 737 delete tab_values; |
| 738 return false; |
| 739 } |
| 740 |
| 741 dictionary->SetString(L"type", L"window"); |
| 742 dictionary->Set(L"tabs", tab_values); |
| 743 return true; |
| 744 } |
| 745 |
714 /////////////////////////////////////////////////////////////////////////////// | 746 /////////////////////////////////////////////////////////////////////////////// |
715 // HistoryHandler | 747 // HistoryHandler |
716 | 748 |
717 HistoryHandler::HistoryHandler(DOMUIHost* dom_ui_host) | 749 HistoryHandler::HistoryHandler(DOMUIHost* dom_ui_host) |
718 : dom_ui_host_(dom_ui_host) { | 750 : dom_ui_host_(dom_ui_host) { |
719 dom_ui_host->RegisterMessageCallback("showHistoryPage", | 751 dom_ui_host->RegisterMessageCallback("showHistoryPage", |
720 NewCallback(this, &HistoryHandler::HandleShowHistoryPage)); | 752 NewCallback(this, &HistoryHandler::HandleShowHistoryPage)); |
721 dom_ui_host->RegisterMessageCallback("searchHistoryPage", | 753 dom_ui_host->RegisterMessageCallback("searchHistoryPage", |
722 NewCallback(this, &HistoryHandler::HandleSearchHistoryPage)); | 754 NewCallback(this, &HistoryHandler::HandleSearchHistoryPage)); |
723 } | 755 } |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
897 void NewTabUIContents::RequestOpenURL(const GURL& url, | 929 void NewTabUIContents::RequestOpenURL(const GURL& url, |
898 const GURL& /*referrer*/, | 930 const GURL& /*referrer*/, |
899 WindowOpenDisposition disposition) { | 931 WindowOpenDisposition disposition) { |
900 // The user opened a URL on the page (including "open in new window"). | 932 // The user opened a URL on the page (including "open in new window"). |
901 // We count all such clicks as AUTO_BOOKMARK, which increments the site's | 933 // We count all such clicks as AUTO_BOOKMARK, which increments the site's |
902 // visit count (which is used for ranking the most visited entries). | 934 // visit count (which is used for ranking the most visited entries). |
903 // Note this means we're including clicks on not only most visited thumbnails, | 935 // Note this means we're including clicks on not only most visited thumbnails, |
904 // but also clicks on recently bookmarked. | 936 // but also clicks on recently bookmarked. |
905 OpenURL(url, GURL(), disposition, PageTransition::AUTO_BOOKMARK); | 937 OpenURL(url, GURL(), disposition, PageTransition::AUTO_BOOKMARK); |
906 } | 938 } |
OLD | NEW |