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 |
17 using base::android::AttachCurrentThread; | 21 using base::android::AttachCurrentThread; |
18 using base::android::ConvertUTF16ToJavaString; | 22 using base::android::ConvertUTF16ToJavaString; |
19 using base::android::ConvertUTF8ToJavaString; | 23 using base::android::ConvertUTF8ToJavaString; |
20 using base::android::ScopedJavaLocalRef; | 24 using base::android::ScopedJavaLocalRef; |
21 | 25 |
22 namespace { | 26 namespace { |
23 | 27 |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 session_tab.navigations = tab_entry->navigations; | 123 session_tab.navigations = tab_entry->navigations; |
120 | 124 |
121 WindowOpenDisposition disposition = | 125 WindowOpenDisposition disposition = |
122 static_cast<WindowOpenDisposition>(j_disposition); | 126 static_cast<WindowOpenDisposition>(j_disposition); |
123 SessionRestore::RestoreForeignSessionTab(web_contents, | 127 SessionRestore::RestoreForeignSessionTab(web_contents, |
124 session_tab, | 128 session_tab, |
125 disposition); | 129 disposition); |
126 return true; | 130 return true; |
127 } | 131 } |
128 | 132 |
133 // Open most recently closed tab, don't need parent tab. | |
Theresa
2016/06/30 20:50:25
nit: I don't think this comment is necessary.
xingliu
2016/07/01 00:30:38
Done.
| |
134 jboolean RecentlyClosedTabsBridge::OpenMostRecentlyClosedTab( | |
135 JNIEnv* env, | |
136 const base::android::JavaParamRef<jobject>& obj) { | |
137 EnsureTabRestoreService(); | |
138 if (!tab_restore_service_ || TabModelList::empty()) | |
139 return false; | |
140 | |
141 const sessions::TabRestoreService::Entries& entries = | |
142 tab_restore_service_->entries(); | |
143 if (entries.empty()) | |
144 return false; | |
145 | |
146 // find the correct non-incognito tab model based on TabAndroid.window_id. | |
147 using RestoreTab = const sessions::TabRestoreService::Tab; | |
Theresa
2016/06/30 20:50:25
reminder to move this up to the top of the file.
xingliu
2016/07/01 00:30:38
This is different from using Someclass, I remember
| |
148 RestoreTab* restore_tab = static_cast<RestoreTab*>(entries.front()); | |
149 SessionID::id_type window_id = restore_tab->browser_id; | |
150 TabModel* tab_model = TabModelList::FindTabModelWithId(window_id); | |
151 // if no tab model, fall back to first non-incognito model. | |
Theresa
2016/06/30 20:50:25
nit: "If no tab model matches the window_id, fallb
xingliu
2016/07/01 00:30:38
Done.
| |
152 if (!tab_model || tab_model->IsOffTheRecord()) { | |
153 for (auto it = TabModelList::begin(); it != TabModelList::end(); ++it) { | |
154 TabModel* model = *it; | |
155 if (!model->IsOffTheRecord()) { | |
156 tab_model = model; | |
157 break; | |
158 } | |
159 } | |
160 } | |
161 | |
162 if (!tab_model) | |
163 return false; | |
164 | |
165 // remove last tab restore record. | |
Theresa
2016/06/30 20:50:25
nit: capitalize Remove (same for other code commen
xingliu
2016/07/01 00:30:38
Done.
| |
166 std::unique_ptr<sessions::TabRestoreService::Tab> tab_entry( | |
167 tab_restore_service_->RemoveTabEntryById(entries.front()->id)); | |
Theresa
2016/06/30 20:50:25
use restore_tab->id here instead of entries.front(
xingliu
2016/07/01 00:30:38
Done.
| |
168 if (!tab_entry) | |
169 return false; | |
170 | |
171 // restore navigation history and prepare web content. | |
172 sessions::SessionTab session_tab; | |
173 session_tab.current_navigation_index = tab_entry->current_navigation_index; | |
174 session_tab.navigations = tab_entry->navigations; | |
175 std::vector<std::unique_ptr<content::NavigationEntry>> nav_entries = | |
176 sessions::ContentSerializedNavigationBuilder::ToNavigationEntries( | |
177 session_tab.navigations, profile_); | |
178 content::WebContents* web_contents = content::WebContents::Create( | |
Theresa
2016/06/30 20:50:25
nit: make this a new block? e.g.
// Restore navig
xingliu
2016/07/01 00:30:38
Done.
| |
179 content::WebContents::CreateParams(profile_)); | |
180 web_contents->GetController().Restore( | |
181 session_tab.normalized_navigation_index(), | |
182 content::NavigationController::RESTORE_CURRENT_SESSION, | |
183 &nav_entries); | |
184 | |
185 // create new tab. | |
186 tab_model->CreateTab(NULL, web_contents, -1); | |
187 return true; | |
188 } | |
189 | |
129 void RecentlyClosedTabsBridge::ClearRecentlyClosedTabs( | 190 void RecentlyClosedTabsBridge::ClearRecentlyClosedTabs( |
130 JNIEnv* env, | 191 JNIEnv* env, |
131 const JavaParamRef<jobject>& obj) { | 192 const JavaParamRef<jobject>& obj) { |
132 EnsureTabRestoreService(); | 193 EnsureTabRestoreService(); |
133 if (tab_restore_service_) | 194 if (tab_restore_service_) |
134 tab_restore_service_->ClearEntries(); | 195 tab_restore_service_->ClearEntries(); |
135 } | 196 } |
136 | 197 |
137 void RecentlyClosedTabsBridge::TabRestoreServiceChanged( | 198 void RecentlyClosedTabsBridge::TabRestoreServiceChanged( |
138 sessions::TabRestoreService* service) { | 199 sessions::TabRestoreService* service) { |
(...skipping 29 matching lines...) Expand all Loading... | |
168 const JavaParamRef<jobject>& jprofile) { | 229 const JavaParamRef<jobject>& jprofile) { |
169 RecentlyClosedTabsBridge* bridge = new RecentlyClosedTabsBridge( | 230 RecentlyClosedTabsBridge* bridge = new RecentlyClosedTabsBridge( |
170 ProfileAndroid::FromProfileAndroid(jprofile)); | 231 ProfileAndroid::FromProfileAndroid(jprofile)); |
171 return reinterpret_cast<intptr_t>(bridge); | 232 return reinterpret_cast<intptr_t>(bridge); |
172 } | 233 } |
173 | 234 |
174 // static | 235 // static |
175 bool RecentlyClosedTabsBridge::Register(JNIEnv* env) { | 236 bool RecentlyClosedTabsBridge::Register(JNIEnv* env) { |
176 return RegisterNativesImpl(env); | 237 return RegisterNativesImpl(env); |
177 } | 238 } |
OLD | NEW |