Chromium Code Reviews| Index: chrome/browser/android/recently_closed_tabs_bridge.cc |
| diff --git a/chrome/browser/android/recently_closed_tabs_bridge.cc b/chrome/browser/android/recently_closed_tabs_bridge.cc |
| index 771c815e70b9b7e507f46b5ea43618d09bc9e015..b2482d68eb8536bb319157ac512f104ad60d0416 100644 |
| --- a/chrome/browser/android/recently_closed_tabs_bridge.cc |
| +++ b/chrome/browser/android/recently_closed_tabs_bridge.cc |
| @@ -10,10 +10,15 @@ |
| #include "chrome/browser/profiles/profile_android.h" |
| #include "chrome/browser/sessions/session_restore.h" |
| #include "chrome/browser/sessions/tab_restore_service_factory.h" |
| +#include "chrome/browser/ui/android/tab_model/tab_model.h" |
| +#include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| +#include "components/sessions/content/content_serialized_navigation_builder.h" |
| #include "components/sessions/core/tab_restore_service.h" |
| +#include "content/public/browser/navigation_entry.h" |
| #include "content/public/browser/web_contents.h" |
| #include "jni/RecentlyClosedBridge_jni.h" |
| + |
|
Theresa
2016/06/29 23:53:30
nit: remove extra blank line
|
| using base::android::AttachCurrentThread; |
| using base::android::ConvertUTF16ToJavaString; |
| using base::android::ConvertUTF8ToJavaString; |
| @@ -126,6 +131,64 @@ jboolean RecentlyClosedTabsBridge::OpenRecentlyClosedTab( |
| return true; |
| } |
| +// Open most recently closed tab, don't need parent tab. |
| +jboolean RecentlyClosedTabsBridge::OpenMostRecentlyClosedTab( |
| + JNIEnv* env, |
| + const base::android::JavaParamRef<jobject>& obj) { |
| + EnsureTabRestoreService(); |
| + if (!tab_restore_service_ || TabModelList::empty()) |
| + return false; |
| + |
| + const sessions::TabRestoreService::Entries& entries = |
| + tab_restore_service_->entries(); |
| + if (entries.empty()) |
| + return false; |
| + |
| + // find the correct non-incognito tab model based on TabAndroid.window_id. |
| + using RestoreTab = const sessions::TabRestoreService::Tab; |
|
Theresa
2016/06/29 23:53:30
put this using line up with the rest.
|
| + RestoreTab* restore_tab = |
| + static_cast<RestoreTab*>(entries.front()); |
| + SessionID::id_type window_id = restore_tab->browser_id; |
| + TabModel* tab_model = TabModelList::FindTabModelWithId(window_id); |
| + // if no tab model, fall back to first non-incognito model. |
| + if (!tab_model || tab_model->IsOffTheRecord()) { |
| + for (auto it = TabModelList::begin(); it != TabModelList::end(); ++it) { |
| + TabModel* model = *it; |
| + if (!model->IsOffTheRecord()) { |
| + tab_model = model; |
| + break; |
| + } |
| + } |
| + } |
| + |
| + if (!tab_model) |
| + return false; |
| + |
| + // remove last tab restore record. |
| + std::unique_ptr<sessions::TabRestoreService::Tab> tab_entry( |
| + tab_restore_service_->RemoveTabEntryById(entries.front()->id)); |
| + if (!tab_entry) |
| + return false; |
| + |
| + // restore navigation history and prepare web content. |
| + sessions::SessionTab session_tab; |
| + session_tab.current_navigation_index = tab_entry->current_navigation_index; |
| + session_tab.navigations = tab_entry->navigations; |
| + std::vector<std::unique_ptr<content::NavigationEntry>> nav_entries = |
| + sessions::ContentSerializedNavigationBuilder::ToNavigationEntries( |
| + session_tab.navigations, profile_); |
| + content::WebContents* web_contents = content::WebContents::Create( |
| + content::WebContents::CreateParams(profile_)); |
| + web_contents->GetController().Restore( |
| + session_tab.normalized_navigation_index(), |
| + content::NavigationController::RESTORE_CURRENT_SESSION, |
| + &nav_entries); |
| + |
| + // create new tab. |
| + tab_model->CreateTab(NULL, web_contents, -1); |
| + return true; |
| +} |
| + |
| void RecentlyClosedTabsBridge::ClearRecentlyClosedTabs( |
| JNIEnv* env, |
| const JavaParamRef<jobject>& obj) { |