| 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" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 | 50 |
| 51 } // namespace | 51 } // namespace |
| 52 | 52 |
| 53 void RecentlyClosedTabsHandler::RegisterMessages() { | 53 void RecentlyClosedTabsHandler::RegisterMessages() { |
| 54 web_ui()->RegisterMessageCallback("getRecentlyClosedTabs", | 54 web_ui()->RegisterMessageCallback("getRecentlyClosedTabs", |
| 55 base::Bind(&RecentlyClosedTabsHandler::HandleGetRecentlyClosedTabs, | 55 base::Bind(&RecentlyClosedTabsHandler::HandleGetRecentlyClosedTabs, |
| 56 base::Unretained(this))); | 56 base::Unretained(this))); |
| 57 web_ui()->RegisterMessageCallback("reopenTab", | 57 web_ui()->RegisterMessageCallback("reopenTab", |
| 58 base::Bind(&RecentlyClosedTabsHandler::HandleReopenTab, | 58 base::Bind(&RecentlyClosedTabsHandler::HandleReopenTab, |
| 59 base::Unretained(this))); | 59 base::Unretained(this))); |
| 60 web_ui()->RegisterMessageCallback("clearRecentlyClosed", |
| 61 base::Bind(&RecentlyClosedTabsHandler::HandleClearRecentlyClosed, |
| 62 base::Unretained(this))); |
| 60 } | 63 } |
| 61 | 64 |
| 62 RecentlyClosedTabsHandler::~RecentlyClosedTabsHandler() { | 65 RecentlyClosedTabsHandler::~RecentlyClosedTabsHandler() { |
| 63 if (tab_restore_service_) | 66 if (tab_restore_service_) |
| 64 tab_restore_service_->RemoveObserver(this); | 67 tab_restore_service_->RemoveObserver(this); |
| 65 } | 68 } |
| 66 | 69 |
| 67 void RecentlyClosedTabsHandler::HandleReopenTab(const ListValue* args) { | 70 void RecentlyClosedTabsHandler::HandleReopenTab(const ListValue* args) { |
| 68 if (!tab_restore_service_) | 71 if (!tab_restore_service_) |
| 69 return; | 72 return; |
| 70 | 73 |
| 71 double index = -1.0; | |
| 72 CHECK(args->GetDouble(1, &index)); | |
| 73 | |
| 74 // There are actually less than 20 restore tab items displayed in the UI. | |
| 75 UMA_HISTOGRAM_ENUMERATION("NewTabPage.SessionRestore", | |
| 76 static_cast<int>(index), 20); | |
| 77 | |
| 78 double session_to_restore = 0.0; | 74 double session_to_restore = 0.0; |
| 79 CHECK(args->GetDouble(0, &session_to_restore)); | 75 CHECK(args->GetDouble(0, &session_to_restore)); |
| 80 | 76 |
| 81 #if defined(OS_ANDROID) | 77 #if defined(OS_ANDROID) |
| 82 // Find and remove the corresponding tab entry from TabRestoreService. | 78 // Find and remove the corresponding tab entry from TabRestoreService. |
| 83 // We take ownership of the returned tab. | 79 // We take ownership of the returned tab. |
| 84 scoped_ptr<TabRestoreService::Tab> tab_entry( | 80 scoped_ptr<TabRestoreService::Tab> tab_entry( |
| 85 tab_restore_service_->RemoveTabEntryById(static_cast<int>( | 81 tab_restore_service_->RemoveTabEntryById(static_cast<int>( |
| 86 session_to_restore))); | 82 session_to_restore))); |
| 87 if (tab_entry.get() == NULL) | 83 if (tab_entry.get() == NULL) |
| 88 return; | 84 return; |
| 89 | 85 |
| 90 // RestoreForeignSessionTab needs a SessionTab. | 86 // RestoreForeignSessionTab needs a SessionTab. |
| 91 SessionTab session_tab; | 87 SessionTab session_tab; |
| 92 session_tab.current_navigation_index = tab_entry->current_navigation_index; | 88 session_tab.current_navigation_index = tab_entry->current_navigation_index; |
| 93 session_tab.navigations = tab_entry->navigations; | 89 session_tab.navigations = tab_entry->navigations; |
| 94 | 90 |
| 95 SessionRestore::RestoreForeignSessionTab(web_ui()->GetWebContents(), | 91 SessionRestore::RestoreForeignSessionTab(web_ui()->GetWebContents(), |
| 96 session_tab, NEW_FOREGROUND_TAB); | 92 session_tab, NEW_FOREGROUND_TAB); |
| 97 #else | 93 #else |
| 94 double index = -1.0; |
| 95 CHECK(args->GetDouble(1, &index)); |
| 96 |
| 97 // There are actually less than 20 restore tab items displayed in the UI. |
| 98 UMA_HISTOGRAM_ENUMERATION("NewTabPage.SessionRestore", |
| 99 static_cast<int>(index), 20); |
| 100 |
| 98 TabRestoreServiceDelegate* delegate = | 101 TabRestoreServiceDelegate* delegate = |
| 99 TabRestoreServiceDelegate::FindDelegateForWebContents( | 102 TabRestoreServiceDelegate::FindDelegateForWebContents( |
| 100 web_ui()->GetWebContents()); | 103 web_ui()->GetWebContents()); |
| 101 if (!delegate) | 104 if (!delegate) |
| 102 return; | 105 return; |
| 103 WindowOpenDisposition disposition = | 106 WindowOpenDisposition disposition = |
| 104 web_ui_util::GetDispositionFromClick(args, 2); | 107 web_ui_util::GetDispositionFromClick(args, 2); |
| 105 tab_restore_service_->RestoreEntryById(delegate, | 108 tab_restore_service_->RestoreEntryById(delegate, |
| 106 static_cast<int>(session_to_restore), | 109 static_cast<int>(session_to_restore), |
| 107 disposition); | 110 disposition); |
| 108 // The current tab has been nuked at this point; don't touch any member | 111 // The current tab has been nuked at this point; don't touch any member |
| 109 // variables. | 112 // variables. |
| 110 #endif | 113 #endif |
| 111 } | 114 } |
| 112 | 115 |
| 116 void RecentlyClosedTabsHandler::HandleClearRecentlyClosed( |
| 117 const ListValue* args) { |
| 118 EnsureTabRestoreService(); |
| 119 if (tab_restore_service_) |
| 120 tab_restore_service_->ClearEntries(); |
| 121 } |
| 122 |
| 113 void RecentlyClosedTabsHandler::HandleGetRecentlyClosedTabs( | 123 void RecentlyClosedTabsHandler::HandleGetRecentlyClosedTabs( |
| 114 const ListValue* args) { | 124 const ListValue* args) { |
| 115 if (!tab_restore_service_) { | 125 EnsureTabRestoreService(); |
| 116 tab_restore_service_ = | |
| 117 TabRestoreServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); | |
| 118 | |
| 119 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in | |
| 120 // Off the Record mode) | |
| 121 if (tab_restore_service_) { | |
| 122 // This does nothing if the tabs have already been loaded or they | |
| 123 // shouldn't be loaded. | |
| 124 tab_restore_service_->LoadTabsFromLastSession(); | |
| 125 | |
| 126 tab_restore_service_->AddObserver(this); | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 if (tab_restore_service_) | 126 if (tab_restore_service_) |
| 131 TabRestoreServiceChanged(tab_restore_service_); | 127 TabRestoreServiceChanged(tab_restore_service_); |
| 132 } | 128 } |
| 133 | 129 |
| 134 void RecentlyClosedTabsHandler::TabRestoreServiceChanged( | 130 void RecentlyClosedTabsHandler::TabRestoreServiceChanged( |
| 135 TabRestoreService* service) { | 131 TabRestoreService* service) { |
| 136 ListValue list_value; | 132 ListValue list_value; |
| 137 TabRestoreService::Entries entries = service->entries(); | 133 TabRestoreService::Entries entries = service->entries(); |
| 138 CreateRecentlyClosedValues(entries, &list_value); | 134 CreateRecentlyClosedValues(entries, &list_value); |
| 139 | 135 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 164 DCHECK_EQ(entry->type, TabRestoreService::WINDOW); | 160 DCHECK_EQ(entry->type, TabRestoreService::WINDOW); |
| 165 WindowToValue(*static_cast<TabRestoreService::Window*>(entry), | 161 WindowToValue(*static_cast<TabRestoreService::Window*>(entry), |
| 166 entry_dict.get()); | 162 entry_dict.get()); |
| 167 } | 163 } |
| 168 | 164 |
| 169 entry_dict->SetInteger("sessionId", entry->id); | 165 entry_dict->SetInteger("sessionId", entry->id); |
| 170 entry_list_value->Append(entry_dict.release()); | 166 entry_list_value->Append(entry_dict.release()); |
| 171 added_count++; | 167 added_count++; |
| 172 } | 168 } |
| 173 } | 169 } |
| 170 |
| 171 void RecentlyClosedTabsHandler::EnsureTabRestoreService() { |
| 172 if (tab_restore_service_) |
| 173 return; |
| 174 |
| 175 tab_restore_service_ = |
| 176 TabRestoreServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
| 177 |
| 178 // TabRestoreServiceFactory::GetForProfile() can return NULL (i.e., when in |
| 179 // Off the Record mode) |
| 180 if (tab_restore_service_) { |
| 181 // This does nothing if the tabs have already been loaded or they |
| 182 // shouldn't be loaded. |
| 183 tab_restore_service_->LoadTabsFromLastSession(); |
| 184 tab_restore_service_->AddObserver(this); |
| 185 } |
| 186 } |
| OLD | NEW |