| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/android/recently_closed_tabs_bridge.h" | 5 #include "chrome/browser/android/recently_closed_tabs_bridge.h" |
| 6 | 6 |
| 7 #include "base/android/jni_string.h" | 7 #include "base/android/jni_string.h" |
| 8 #include "chrome/browser/android/tab_android.h" | 8 #include "chrome/browser/android/tab_android.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/profiles/profile_android.h" | 10 #include "chrome/browser/profiles/profile_android.h" |
| 11 #include "chrome/browser/sessions/session_restore.h" | 11 #include "chrome/browser/sessions/session_restore.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/android/tab_model/tab_model.h" |
| 14 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" |
| 15 #include "components/sessions/content/content_serialized_navigation_builder.h" |
| 13 #include "components/sessions/core/tab_restore_service.h" | 16 #include "components/sessions/core/tab_restore_service.h" |
| 17 #include "content/public/browser/navigation_entry.h" |
| 14 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| 15 #include "jni/RecentlyClosedBridge_jni.h" | 19 #include "jni/RecentlyClosedBridge_jni.h" |
| 16 | 20 |
| 21 |
| 17 using base::android::AttachCurrentThread; | 22 using base::android::AttachCurrentThread; |
| 18 using base::android::ConvertUTF16ToJavaString; | 23 using base::android::ConvertUTF16ToJavaString; |
| 19 using base::android::ConvertUTF8ToJavaString; | 24 using base::android::ConvertUTF8ToJavaString; |
| 20 using base::android::ScopedJavaLocalRef; | 25 using base::android::ScopedJavaLocalRef; |
| 21 | 26 |
| 22 namespace { | 27 namespace { |
| 23 | 28 |
| 24 void AddTabToList(JNIEnv* env, | 29 void AddTabToList(JNIEnv* env, |
| 25 sessions::TabRestoreService::Entry* entry, | 30 sessions::TabRestoreService::Entry* entry, |
| 26 jobject jtabs_list) { | 31 jobject jtabs_list) { |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 session_tab.navigations = tab_entry->navigations; | 124 session_tab.navigations = tab_entry->navigations; |
| 120 | 125 |
| 121 WindowOpenDisposition disposition = | 126 WindowOpenDisposition disposition = |
| 122 static_cast<WindowOpenDisposition>(j_disposition); | 127 static_cast<WindowOpenDisposition>(j_disposition); |
| 123 SessionRestore::RestoreForeignSessionTab(web_contents, | 128 SessionRestore::RestoreForeignSessionTab(web_contents, |
| 124 session_tab, | 129 session_tab, |
| 125 disposition); | 130 disposition); |
| 126 return true; | 131 return true; |
| 127 } | 132 } |
| 128 | 133 |
| 134 // Open most recently closed tab, don't need parent tab. |
| 135 jboolean RecentlyClosedTabsBridge::OpenMostRecentlyClosedTab( |
| 136 JNIEnv* env, |
| 137 const base::android::JavaParamRef<jobject>& obj) { |
| 138 EnsureTabRestoreService(); |
| 139 if (!tab_restore_service_ || TabModelList::empty()) |
| 140 return false; |
| 141 |
| 142 const sessions::TabRestoreService::Entries& entries = |
| 143 tab_restore_service_->entries(); |
| 144 if (entries.empty()) |
| 145 return false; |
| 146 |
| 147 using RestoreTab = sessions::TabRestoreService::Tab; |
| 148 RestoreTab* restore_tab = |
| 149 static_cast<RestoreTab*>(tab_restore_service_->GetMostRecentEntry()); |
| 150 SessionID::id_type window_id = restore_tab->browser_id; |
| 151 |
| 152 // find the correct tab model based on window id. if nothing, fall back to |
| 153 // the first available tab model. |
| 154 TabModel* tab_model = TabModelList::FindTabModelWithId(window_id); |
| 155 if (!tab_model) |
| 156 tab_model = TabModelList::get(0); |
| 157 |
| 158 if (!tab_model) |
| 159 return false; |
| 160 |
| 161 // remove last tab restore record. |
| 162 std::unique_ptr<sessions::TabRestoreService::Tab> tab_entry( |
| 163 tab_restore_service_->RemoveTabEntryById(entries.front()->id)); |
| 164 if (!tab_entry) |
| 165 return false; |
| 166 |
| 167 // restore navigation history and prepare web content. |
| 168 sessions::SessionTab session_tab; |
| 169 session_tab.current_navigation_index = tab_entry->current_navigation_index; |
| 170 session_tab.navigations = tab_entry->navigations; |
| 171 std::vector<std::unique_ptr<content::NavigationEntry>> nav_entries = |
| 172 sessions::ContentSerializedNavigationBuilder::ToNavigationEntries( |
| 173 session_tab.navigations, profile_); |
| 174 content::WebContents* web_contents = content::WebContents::Create( |
| 175 content::WebContents::CreateParams(profile_)); |
| 176 web_contents->GetController().Restore( |
| 177 session_tab.normalized_navigation_index(), |
| 178 content::NavigationController::RESTORE_CURRENT_SESSION, |
| 179 &nav_entries); |
| 180 |
| 181 // create new tab. |
| 182 tab_model->CreateTab(NULL, web_contents, -1); |
| 183 return true; |
| 184 } |
| 185 |
| 129 void RecentlyClosedTabsBridge::ClearRecentlyClosedTabs( | 186 void RecentlyClosedTabsBridge::ClearRecentlyClosedTabs( |
| 130 JNIEnv* env, | 187 JNIEnv* env, |
| 131 const JavaParamRef<jobject>& obj) { | 188 const JavaParamRef<jobject>& obj) { |
| 132 EnsureTabRestoreService(); | 189 EnsureTabRestoreService(); |
| 133 if (tab_restore_service_) | 190 if (tab_restore_service_) |
| 134 tab_restore_service_->ClearEntries(); | 191 tab_restore_service_->ClearEntries(); |
| 135 } | 192 } |
| 136 | 193 |
| 137 void RecentlyClosedTabsBridge::TabRestoreServiceChanged( | 194 void RecentlyClosedTabsBridge::TabRestoreServiceChanged( |
| 138 sessions::TabRestoreService* service) { | 195 sessions::TabRestoreService* service) { |
| (...skipping 29 matching lines...) Expand all Loading... |
| 168 const JavaParamRef<jobject>& jprofile) { | 225 const JavaParamRef<jobject>& jprofile) { |
| 169 RecentlyClosedTabsBridge* bridge = new RecentlyClosedTabsBridge( | 226 RecentlyClosedTabsBridge* bridge = new RecentlyClosedTabsBridge( |
| 170 ProfileAndroid::FromProfileAndroid(jprofile)); | 227 ProfileAndroid::FromProfileAndroid(jprofile)); |
| 171 return reinterpret_cast<intptr_t>(bridge); | 228 return reinterpret_cast<intptr_t>(bridge); |
| 172 } | 229 } |
| 173 | 230 |
| 174 // static | 231 // static |
| 175 bool RecentlyClosedTabsBridge::Register(JNIEnv* env) { | 232 bool RecentlyClosedTabsBridge::Register(JNIEnv* env) { |
| 176 return RegisterNativesImpl(env); | 233 return RegisterNativesImpl(env); |
| 177 } | 234 } |
| OLD | NEW |