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