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