Chromium Code Reviews| 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_live_tab.h" | |
| 16 #include "components/sessions/content/content_serialized_navigation_builder.h" | |
| 17 #include "components/sessions/core/live_tab.h" | |
| 13 #include "components/sessions/core/tab_restore_service.h" | 18 #include "components/sessions/core/tab_restore_service.h" |
| 19 #include "content/public/browser/navigation_entry.h" | |
| 14 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 15 #include "jni/RecentlyClosedBridge_jni.h" | 21 #include "jni/RecentlyClosedBridge_jni.h" |
| 16 | 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; |
| 27 using sessions::ContentLiveTab; | |
| 28 using sessions::LiveTab; | |
| 21 | 29 |
| 22 namespace { | 30 namespace { |
| 23 | 31 |
| 24 void AddTabToList(JNIEnv* env, | 32 void AddTabToList(JNIEnv* env, |
| 25 sessions::TabRestoreService::Entry* entry, | 33 sessions::TabRestoreService::Entry* entry, |
| 26 jobject jtabs_list) { | 34 jobject jtabs_list) { |
| 27 const sessions::TabRestoreService::Tab* tab = | 35 const sessions::TabRestoreService::Tab* tab = |
| 28 static_cast<sessions::TabRestoreService::Tab*>(entry); | 36 static_cast<sessions::TabRestoreService::Tab*>(entry); |
| 29 const sessions::SerializedNavigationEntry& current_navigation = | 37 const sessions::SerializedNavigationEntry& current_navigation = |
| 30 tab->navigations.at(tab->current_navigation_index); | 38 tab->navigations.at(tab->current_navigation_index); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 119 session_tab.navigations = tab_entry->navigations; | 127 session_tab.navigations = tab_entry->navigations; |
| 120 | 128 |
| 121 WindowOpenDisposition disposition = | 129 WindowOpenDisposition disposition = |
| 122 static_cast<WindowOpenDisposition>(j_disposition); | 130 static_cast<WindowOpenDisposition>(j_disposition); |
| 123 SessionRestore::RestoreForeignSessionTab(web_contents, | 131 SessionRestore::RestoreForeignSessionTab(web_contents, |
| 124 session_tab, | 132 session_tab, |
| 125 disposition); | 133 disposition); |
| 126 return true; | 134 return true; |
| 127 } | 135 } |
| 128 | 136 |
| 137 jboolean RecentlyClosedTabsBridge::OpenMostRecentlyClosedTab( | |
| 138 JNIEnv* env, | |
| 139 const base::android::JavaParamRef<jobject>& obj) { | |
| 140 EnsureTabRestoreService(); | |
| 141 if (!tab_restore_service_ || TabModelList::empty()) | |
| 142 return false; | |
| 143 | |
| 144 const sessions::TabRestoreService::Entries& entries = | |
| 145 tab_restore_service_->entries(); | |
| 146 if (entries.empty()) | |
| 147 return false; | |
| 148 | |
| 149 // Find the correct non-incognito tab model based on TabAndroid.window_id. | |
| 150 using RestoreTab = const sessions::TabRestoreService::Tab; | |
| 151 RestoreTab* restore_tab = static_cast<RestoreTab*>(entries.front()); | |
| 152 SessionID::id_type window_id = restore_tab->browser_id; | |
| 153 TabModel* tab_model = TabModelList::FindTabModelWithId(window_id); | |
| 154 // If no tab model matched the window id, fall back to first non-incognito | |
| 155 // model. | |
| 156 if (!tab_model || tab_model->IsOffTheRecord()) { | |
|
Theresa
2016/07/06 18:51:05
I think that rather than finding the correct tab m
xingliu
2016/07/06 22:35:41
Smart..
Thanks for making this code much cleaner!
Theresa
2016/07/07 19:00:12
Thanks for reworking it!
| |
| 157 for (auto it = TabModelList::begin(); it != TabModelList::end(); ++it) { | |
| 158 TabModel* model = *it; | |
| 159 if (!model->IsOffTheRecord()) { | |
| 160 tab_model = model; | |
| 161 break; | |
| 162 } | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 if (!tab_model) | |
| 167 return false; | |
| 168 | |
| 169 // Restore most recent tab data. | |
| 170 std::vector<LiveTab*> restored_tab = tab_restore_service_-> | |
| 171 RestoreMostRecentEntry(tab_model->GetLiveTabContext()); | |
| 172 if (restored_tab.empty()) | |
| 173 return false; | |
| 174 ContentLiveTab* content_tab = static_cast<ContentLiveTab*>(restored_tab[0]); | |
| 175 | |
| 176 // Create new tab. | |
| 177 tab_model->CreateTab(NULL, content_tab->web_contents(), -1); | |
|
Theresa
2016/07/06 18:51:06
This can move to AndroidLiveTabContext::AddRestore
xingliu
2016/07/06 22:35:41
Done.
| |
| 178 return true; | |
| 179 } | |
| 180 | |
| 129 void RecentlyClosedTabsBridge::ClearRecentlyClosedTabs( | 181 void RecentlyClosedTabsBridge::ClearRecentlyClosedTabs( |
| 130 JNIEnv* env, | 182 JNIEnv* env, |
| 131 const JavaParamRef<jobject>& obj) { | 183 const JavaParamRef<jobject>& obj) { |
| 132 EnsureTabRestoreService(); | 184 EnsureTabRestoreService(); |
| 133 if (tab_restore_service_) | 185 if (tab_restore_service_) |
| 134 tab_restore_service_->ClearEntries(); | 186 tab_restore_service_->ClearEntries(); |
| 135 } | 187 } |
| 136 | 188 |
| 137 void RecentlyClosedTabsBridge::TabRestoreServiceChanged( | 189 void RecentlyClosedTabsBridge::TabRestoreServiceChanged( |
| 138 sessions::TabRestoreService* service) { | 190 sessions::TabRestoreService* service) { |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 168 const JavaParamRef<jobject>& jprofile) { | 220 const JavaParamRef<jobject>& jprofile) { |
| 169 RecentlyClosedTabsBridge* bridge = new RecentlyClosedTabsBridge( | 221 RecentlyClosedTabsBridge* bridge = new RecentlyClosedTabsBridge( |
| 170 ProfileAndroid::FromProfileAndroid(jprofile)); | 222 ProfileAndroid::FromProfileAndroid(jprofile)); |
| 171 return reinterpret_cast<intptr_t>(bridge); | 223 return reinterpret_cast<intptr_t>(bridge); |
| 172 } | 224 } |
| 173 | 225 |
| 174 // static | 226 // static |
| 175 bool RecentlyClosedTabsBridge::Register(JNIEnv* env) { | 227 bool RecentlyClosedTabsBridge::Register(JNIEnv* env) { |
| 176 return RegisterNativesImpl(env); | 228 return RegisterNativesImpl(env); |
| 177 } | 229 } |
| OLD | NEW |