| 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/recently_closed_tabs_handler.h" | 5 #include "chrome/browser/ui/webui/ntp/recently_closed_tabs_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/sessions/tab_restore_service_delegate.h" | 11 #include "chrome/browser/sessions/tab_restore_service_delegate.h" |
| 12 #include "chrome/browser/sessions/tab_restore_service_factory.h" | 12 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
| 13 #include "chrome/browser/ui/host_desktop.h" | 13 #include "chrome/browser/ui/host_desktop.h" |
| 14 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 14 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
| 15 #include "chrome/common/url_constants.h" | 15 #include "chrome/common/url_constants.h" |
| 16 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
| 17 #include "content/public/browser/web_contents_view.h" | 17 #include "content/public/browser/web_contents_view.h" |
| 18 #include "content/public/browser/web_ui.h" | 18 #include "content/public/browser/web_ui.h" |
| 19 #include "ui/base/webui/web_ui_util.h" | 19 #include "ui/base/webui/web_ui_util.h" |
| 20 | 20 |
| 21 #if defined(OS_ANDROID) | |
| 22 #include "chrome/browser/sessions/session_restore.h" | |
| 23 #endif | |
| 24 | |
| 25 namespace { | 21 namespace { |
| 26 | 22 |
| 27 void TabToValue(const TabRestoreService::Tab& tab, | 23 void TabToValue(const TabRestoreService::Tab& tab, |
| 28 base::DictionaryValue* dictionary) { | 24 base::DictionaryValue* dictionary) { |
| 29 const sessions::SerializedNavigationEntry& current_navigation = | 25 const sessions::SerializedNavigationEntry& current_navigation = |
| 30 tab.navigations.at(tab.current_navigation_index); | 26 tab.navigations.at(tab.current_navigation_index); |
| 31 NewTabUI::SetUrlTitleAndDirection(dictionary, current_navigation.title(), | 27 NewTabUI::SetUrlTitleAndDirection(dictionary, current_navigation.title(), |
| 32 current_navigation.virtual_url()); | 28 current_navigation.virtual_url()); |
| 33 dictionary->SetString("type", "tab"); | 29 dictionary->SetString("type", "tab"); |
| 34 dictionary->SetDouble("timestamp", tab.timestamp.ToDoubleT()); | 30 dictionary->SetDouble("timestamp", tab.timestamp.ToDoubleT()); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 tab_restore_service_->RemoveObserver(this); | 65 tab_restore_service_->RemoveObserver(this); |
| 70 } | 66 } |
| 71 | 67 |
| 72 void RecentlyClosedTabsHandler::HandleReopenTab(const base::ListValue* args) { | 68 void RecentlyClosedTabsHandler::HandleReopenTab(const base::ListValue* args) { |
| 73 if (!tab_restore_service_) | 69 if (!tab_restore_service_) |
| 74 return; | 70 return; |
| 75 | 71 |
| 76 double session_to_restore = 0.0; | 72 double session_to_restore = 0.0; |
| 77 CHECK(args->GetDouble(0, &session_to_restore)); | 73 CHECK(args->GetDouble(0, &session_to_restore)); |
| 78 | 74 |
| 79 #if defined(OS_ANDROID) | |
| 80 // Find and remove the corresponding tab entry from TabRestoreService. | |
| 81 // We take ownership of the returned tab. | |
| 82 scoped_ptr<TabRestoreService::Tab> tab_entry( | |
| 83 tab_restore_service_->RemoveTabEntryById(static_cast<int>( | |
| 84 session_to_restore))); | |
| 85 if (tab_entry.get() == NULL) | |
| 86 return; | |
| 87 | |
| 88 // RestoreForeignSessionTab needs a SessionTab. | |
| 89 SessionTab session_tab; | |
| 90 session_tab.current_navigation_index = tab_entry->current_navigation_index; | |
| 91 session_tab.navigations = tab_entry->navigations; | |
| 92 | |
| 93 SessionRestore::RestoreForeignSessionTab(web_ui()->GetWebContents(), | |
| 94 session_tab, NEW_FOREGROUND_TAB); | |
| 95 #else | |
| 96 double index = -1.0; | 75 double index = -1.0; |
| 97 CHECK(args->GetDouble(1, &index)); | 76 CHECK(args->GetDouble(1, &index)); |
| 98 | 77 |
| 99 // There are actually less than 20 restore tab items displayed in the UI. | 78 // There are actually less than 20 restore tab items displayed in the UI. |
| 100 UMA_HISTOGRAM_ENUMERATION("NewTabPage.SessionRestore", | 79 UMA_HISTOGRAM_ENUMERATION("NewTabPage.SessionRestore", |
| 101 static_cast<int>(index), 20); | 80 static_cast<int>(index), 20); |
| 102 | 81 |
| 103 TabRestoreServiceDelegate* delegate = | 82 TabRestoreServiceDelegate* delegate = |
| 104 TabRestoreServiceDelegate::FindDelegateForWebContents( | 83 TabRestoreServiceDelegate::FindDelegateForWebContents( |
| 105 web_ui()->GetWebContents()); | 84 web_ui()->GetWebContents()); |
| 106 if (!delegate) | 85 if (!delegate) |
| 107 return; | 86 return; |
| 108 chrome::HostDesktopType host_desktop_type = | 87 chrome::HostDesktopType host_desktop_type = |
| 109 chrome::GetHostDesktopTypeForNativeView( | 88 chrome::GetHostDesktopTypeForNativeView( |
| 110 web_ui()->GetWebContents()->GetView()->GetNativeView()); | 89 web_ui()->GetWebContents()->GetView()->GetNativeView()); |
| 111 WindowOpenDisposition disposition = webui::GetDispositionFromClick(args, 2); | 90 WindowOpenDisposition disposition = webui::GetDispositionFromClick(args, 2); |
| 112 tab_restore_service_->RestoreEntryById(delegate, | 91 tab_restore_service_->RestoreEntryById(delegate, |
| 113 static_cast<int>(session_to_restore), | 92 static_cast<int>(session_to_restore), |
| 114 host_desktop_type, | 93 host_desktop_type, |
| 115 disposition); | 94 disposition); |
| 116 // The current tab has been nuked at this point; don't touch any member | 95 // The current tab has been nuked at this point; don't touch any member |
| 117 // variables. | 96 // variables. |
| 118 #endif | |
| 119 } | 97 } |
| 120 | 98 |
| 121 void RecentlyClosedTabsHandler::HandleClearRecentlyClosed( | 99 void RecentlyClosedTabsHandler::HandleClearRecentlyClosed( |
| 122 const base::ListValue* args) { | 100 const base::ListValue* args) { |
| 123 EnsureTabRestoreService(); | 101 EnsureTabRestoreService(); |
| 124 if (tab_restore_service_) | 102 if (tab_restore_service_) |
| 125 tab_restore_service_->ClearEntries(); | 103 tab_restore_service_->ClearEntries(); |
| 126 } | 104 } |
| 127 | 105 |
| 128 void RecentlyClosedTabsHandler::HandleGetRecentlyClosedTabs( | 106 void RecentlyClosedTabsHandler::HandleGetRecentlyClosedTabs( |
| 129 const base::ListValue* args) { | 107 const base::ListValue* args) { |
| 130 EnsureTabRestoreService(); | 108 EnsureTabRestoreService(); |
| 131 if (tab_restore_service_) | 109 if (tab_restore_service_) |
| 132 TabRestoreServiceChanged(tab_restore_service_); | 110 TabRestoreServiceChanged(tab_restore_service_); |
| 133 } | 111 } |
| 134 | 112 |
| 135 void RecentlyClosedTabsHandler::TabRestoreServiceChanged( | 113 void RecentlyClosedTabsHandler::TabRestoreServiceChanged( |
| 136 TabRestoreService* service) { | 114 TabRestoreService* service) { |
| 137 base::ListValue list_value; | 115 base::ListValue list_value; |
| 138 const int max_count = 10; | 116 const int max_count = 10; |
| 139 int added_count = 0; | 117 int added_count = 0; |
| 140 // We filter the list of recently closed to only show 'interesting' entries, | 118 // We filter the list of recently closed to only show 'interesting' entries, |
| 141 // where an interesting entry is either a closed window or a closed tab | 119 // where an interesting entry is either a closed window or a closed tab |
| 142 // whose selected navigation is not the new tab ui. | 120 // whose selected navigation is not the new tab ui. |
| 143 for (TabRestoreService::Entries::const_iterator it = | 121 for (TabRestoreService::Entries::const_iterator it = |
| 144 service->entries().begin(); | 122 service->entries().begin(); |
| 145 it != service->entries().end() && added_count < max_count; ++it) { | 123 it != service->entries().end() && added_count < max_count; ++it) { |
| 146 TabRestoreService::Entry* entry = *it; | 124 TabRestoreService::Entry* entry = *it; |
| 147 scoped_ptr<base::DictionaryValue> entry_dict(new base::DictionaryValue()); | 125 scoped_ptr<base::DictionaryValue> entry_dict(new base::DictionaryValue()); |
| (...skipping 28 matching lines...) Expand all Loading... |
| 176 | 154 |
| 177 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in | 155 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in |
| 178 // Off the Record mode) | 156 // Off the Record mode) |
| 179 if (tab_restore_service_) { | 157 if (tab_restore_service_) { |
| 180 // This does nothing if the tabs have already been loaded or they | 158 // This does nothing if the tabs have already been loaded or they |
| 181 // shouldn't be loaded. | 159 // shouldn't be loaded. |
| 182 tab_restore_service_->LoadTabsFromLastSession(); | 160 tab_restore_service_->LoadTabsFromLastSession(); |
| 183 tab_restore_service_->AddObserver(this); | 161 tab_restore_service_->AddObserver(this); |
| 184 } | 162 } |
| 185 } | 163 } |
| OLD | NEW |