| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
| 8 #include "chrome/browser/profiles/profile.h" | 8 #include "chrome/browser/profiles/profile.h" |
| 9 #include "chrome/browser/sessions/session_utils.h" |
| 9 #include "chrome/browser/sessions/tab_restore_service_delegate.h" | 10 #include "chrome/browser/sessions/tab_restore_service_delegate.h" |
| 10 #include "chrome/browser/sessions/tab_restore_service_factory.h" | 11 #include "chrome/browser/sessions/tab_restore_service_factory.h" |
| 11 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 12 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
| 12 #include "chrome/common/url_constants.h" | 13 #include "chrome/common/url_constants.h" |
| 13 #include "content/browser/tab_contents/tab_contents.h" | 14 #include "content/browser/tab_contents/tab_contents.h" |
| 14 | 15 |
| 15 namespace { | 16 namespace { |
| 16 | 17 |
| 17 bool IsTabUnique(const DictionaryValue* tab, | |
| 18 std::set<std::string>* unique_items) { | |
| 19 DCHECK(unique_items); | |
| 20 std::string title; | |
| 21 std::string url; | |
| 22 if (tab->GetString("title", &title) && | |
| 23 tab->GetString("url", &url)) { | |
| 24 // TODO(viettrungluu): this isn't obviously reliable, since different | |
| 25 // combinations of titles/urls may conceivably yield the same string. | |
| 26 std::string unique_key = title + url; | |
| 27 if (unique_items->find(unique_key) != unique_items->end()) | |
| 28 return false; | |
| 29 else | |
| 30 unique_items->insert(unique_key); | |
| 31 } | |
| 32 return true; | |
| 33 } | |
| 34 | |
| 35 bool TabToValue(const TabRestoreService::Tab& tab, | 18 bool TabToValue(const TabRestoreService::Tab& tab, |
| 36 DictionaryValue* dictionary) { | 19 DictionaryValue* dictionary) { |
| 37 if (tab.navigations.empty()) | 20 if (tab.navigations.empty()) |
| 38 return false; | 21 return false; |
| 39 | 22 |
| 40 const TabNavigation& current_navigation = | 23 const TabNavigation& current_navigation = |
| 41 tab.navigations.at(tab.current_navigation_index); | 24 tab.navigations.at(tab.current_navigation_index); |
| 42 if (current_navigation.virtual_url() == GURL(chrome::kChromeUINewTabURL)) | 25 if (current_navigation.virtual_url() == GURL(chrome::kChromeUINewTabURL)) |
| 43 return false; | 26 return false; |
| 44 NewTabUI::SetURLTitleAndDirection(dictionary, current_navigation.title(), | 27 NewTabUI::SetURLTitleAndDirection(dictionary, current_navigation.title(), |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 TabRestoreServiceDelegate* delegate = | 70 TabRestoreServiceDelegate* delegate = |
| 88 TabRestoreServiceDelegate::FindDelegateForController( | 71 TabRestoreServiceDelegate::FindDelegateForController( |
| 89 &web_ui_->tab_contents()->controller(), NULL); | 72 &web_ui_->tab_contents()->controller(), NULL); |
| 90 if (!delegate || !tab_restore_service_) | 73 if (!delegate || !tab_restore_service_) |
| 91 return; | 74 return; |
| 92 | 75 |
| 93 int session_to_restore; | 76 int session_to_restore; |
| 94 if (!ExtractIntegerValue(args, &session_to_restore)) | 77 if (!ExtractIntegerValue(args, &session_to_restore)) |
| 95 return; | 78 return; |
| 96 | 79 |
| 97 const TabRestoreService::Entries& entries = tab_restore_service_->entries(); | 80 TabRestoreService::Entries entries; |
| 81 SessionUtils::FilteredEntries(tab_restore_service_, &entries); |
| 98 int index = 0; | 82 int index = 0; |
| 99 for (TabRestoreService::Entries::const_iterator iter = entries.begin(); | 83 for (TabRestoreService::Entries::const_iterator iter = entries.begin(); |
| 100 iter != entries.end(); ++iter, ++index) { | 84 iter != entries.end(); ++iter, ++index) { |
| 101 if (session_to_restore == (*iter)->id) | 85 if (session_to_restore == (*iter)->id) |
| 102 break; | 86 break; |
| 103 } | 87 } |
| 104 // There are actually less than 20 restore tab items displayed in the UI. | 88 // There are actually less than 20 restore tab items displayed in the UI. |
| 105 UMA_HISTOGRAM_ENUMERATION("NewTabPage.SessionRestore", index, 20); | 89 UMA_HISTOGRAM_ENUMERATION("NewTabPage.SessionRestore", index, 20); |
| 106 | 90 |
| 107 tab_restore_service_->RestoreEntryById(delegate, session_to_restore, true); | 91 tab_restore_service_->RestoreEntryById(delegate, session_to_restore, true); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 126 } | 110 } |
| 127 } | 111 } |
| 128 | 112 |
| 129 if (tab_restore_service_) | 113 if (tab_restore_service_) |
| 130 TabRestoreServiceChanged(tab_restore_service_); | 114 TabRestoreServiceChanged(tab_restore_service_); |
| 131 } | 115 } |
| 132 | 116 |
| 133 void RecentlyClosedTabsHandler::TabRestoreServiceChanged( | 117 void RecentlyClosedTabsHandler::TabRestoreServiceChanged( |
| 134 TabRestoreService* service) { | 118 TabRestoreService* service) { |
| 135 ListValue list_value; | 119 ListValue list_value; |
| 136 AddRecentlyClosedEntries(service->entries(), &list_value); | 120 TabRestoreService::Entries entries; |
| 121 SessionUtils::FilteredEntries(service, &entries); |
| 122 |
| 123 AddRecentlyClosedEntries(entries, &list_value); |
| 137 | 124 |
| 138 web_ui_->CallJavascriptFunction("recentlyClosedTabs", list_value); | 125 web_ui_->CallJavascriptFunction("recentlyClosedTabs", list_value); |
| 139 } | 126 } |
| 140 | 127 |
| 141 void RecentlyClosedTabsHandler::TabRestoreServiceDestroyed( | 128 void RecentlyClosedTabsHandler::TabRestoreServiceDestroyed( |
| 142 TabRestoreService* service) { | 129 TabRestoreService* service) { |
| 143 tab_restore_service_ = NULL; | 130 tab_restore_service_ = NULL; |
| 144 } | 131 } |
| 145 | 132 |
| 146 // static | 133 // static |
| 147 void RecentlyClosedTabsHandler::AddRecentlyClosedEntries( | 134 void RecentlyClosedTabsHandler::AddRecentlyClosedEntries( |
| 148 const TabRestoreService::Entries& entries, ListValue* entry_list_value) { | 135 const TabRestoreService::Entries& entries, ListValue* entry_list_value) { |
| 149 const int max_count = 10; | 136 const int max_count = 10; |
| 150 int added_count = 0; | 137 int added_count = 0; |
| 151 std::set<std::string> unique_items; | |
| 152 // We filter the list of recently closed to only show 'interesting' entries, | 138 // We filter the list of recently closed to only show 'interesting' entries, |
| 153 // where an interesting entry is either a closed window or a closed tab | 139 // where an interesting entry is either a closed window or a closed tab |
| 154 // whose selected navigation is not the new tab ui. | 140 // whose selected navigation is not the new tab ui. |
| 155 for (TabRestoreService::Entries::const_iterator it = entries.begin(); | 141 for (TabRestoreService::Entries::const_iterator it = entries.begin(); |
| 156 it != entries.end() && added_count < max_count; ++it) { | 142 it != entries.end() && added_count < max_count; ++it) { |
| 157 TabRestoreService::Entry* entry = *it; | 143 TabRestoreService::Entry* entry = *it; |
| 158 scoped_ptr<DictionaryValue> entry_dict(new DictionaryValue()); | 144 scoped_ptr<DictionaryValue> entry_dict(new DictionaryValue()); |
| 159 if ((entry->type == TabRestoreService::TAB && | 145 if ((entry->type == TabRestoreService::TAB && |
| 160 TabToValue( | 146 TabToValue( |
| 161 *static_cast<TabRestoreService::Tab*>(entry), | 147 *static_cast<TabRestoreService::Tab*>(entry), |
| 162 entry_dict.get()) && | 148 entry_dict.get())) || |
| 163 IsTabUnique(entry_dict.get(), &unique_items)) || | |
| 164 (entry->type == TabRestoreService::WINDOW && | 149 (entry->type == TabRestoreService::WINDOW && |
| 165 WindowToValue( | 150 WindowToValue( |
| 166 *static_cast<TabRestoreService::Window*>(entry), | 151 *static_cast<TabRestoreService::Window*>(entry), |
| 167 entry_dict.get()))) { | 152 entry_dict.get()))) { |
| 168 entry_dict->SetInteger("sessionId", entry->id); | 153 entry_dict->SetInteger("sessionId", entry->id); |
| 169 entry_list_value->Append(entry_dict.release()); | 154 entry_list_value->Append(entry_dict.release()); |
| 170 added_count++; | 155 added_count++; |
| 171 } | 156 } |
| 172 } | 157 } |
| 173 } | 158 } |
| OLD | NEW |