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 "components/sessions/core/tab_restore_service.h" | 13 #include "components/sessions/core/tab_restore_service.h" |
14 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
15 #include "jni/RecentlyClosedBridge_jni.h" | 15 #include "jni/RecentlyClosedBridge_jni.h" |
16 | 16 |
17 using base::android::AttachCurrentThread; | 17 using base::android::AttachCurrentThread; |
18 using base::android::ConvertUTF16ToJavaString; | 18 using base::android::ConvertUTF16ToJavaString; |
19 using base::android::ConvertUTF8ToJavaString; | 19 using base::android::ConvertUTF8ToJavaString; |
20 using base::android::ScopedJavaLocalRef; | 20 using base::android::ScopedJavaLocalRef; |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
24 void AddTabToList(JNIEnv* env, | 24 void AddTabToList(JNIEnv* env, |
25 TabRestoreService::Entry* entry, | 25 sessions::TabRestoreService::Entry* entry, |
26 jobject jtabs_list) { | 26 jobject jtabs_list) { |
27 const TabRestoreService::Tab* tab = | 27 const sessions::TabRestoreService::Tab* tab = |
28 static_cast<TabRestoreService::Tab*>(entry); | 28 static_cast<sessions::TabRestoreService::Tab*>(entry); |
29 const sessions::SerializedNavigationEntry& current_navigation = | 29 const sessions::SerializedNavigationEntry& current_navigation = |
30 tab->navigations.at(tab->current_navigation_index); | 30 tab->navigations.at(tab->current_navigation_index); |
31 Java_RecentlyClosedBridge_pushTab( | 31 Java_RecentlyClosedBridge_pushTab( |
32 env, jtabs_list, entry->id, | 32 env, jtabs_list, entry->id, |
33 ConvertUTF16ToJavaString(env, current_navigation.title()).obj(), | 33 ConvertUTF16ToJavaString(env, current_navigation.title()).obj(), |
34 ConvertUTF8ToJavaString(env, current_navigation.virtual_url().spec()) | 34 ConvertUTF8ToJavaString(env, current_navigation.virtual_url().spec()) |
35 .obj()); | 35 .obj()); |
36 } | 36 } |
37 | 37 |
38 void AddTabsToList(JNIEnv* env, | 38 void AddTabsToList(JNIEnv* env, |
39 const TabRestoreService::Entries& entries, | 39 const sessions::TabRestoreService::Entries& entries, |
40 jobject jtabs_list, | 40 jobject jtabs_list, |
41 int max_tab_count) { | 41 int max_tab_count) { |
42 int added_count = 0; | 42 int added_count = 0; |
43 for (TabRestoreService::Entries::const_iterator it = entries.begin(); | 43 for (sessions::TabRestoreService::Entries::const_iterator it = |
| 44 entries.begin(); |
44 it != entries.end() && added_count < max_tab_count; ++it) { | 45 it != entries.end() && added_count < max_tab_count; ++it) { |
45 TabRestoreService::Entry* entry = *it; | 46 sessions::TabRestoreService::Entry* entry = *it; |
46 DCHECK_EQ(entry->type, TabRestoreService::TAB); | 47 DCHECK_EQ(entry->type, sessions::TabRestoreService::TAB); |
47 if (entry->type == TabRestoreService::TAB) { | 48 if (entry->type == sessions::TabRestoreService::TAB) { |
48 AddTabToList(env, entry, jtabs_list); | 49 AddTabToList(env, entry, jtabs_list); |
49 ++added_count; | 50 ++added_count; |
50 } | 51 } |
51 } | 52 } |
52 } | 53 } |
53 | 54 |
54 } // namespace | 55 } // namespace |
55 | 56 |
56 RecentlyClosedTabsBridge::RecentlyClosedTabsBridge(Profile* profile) | 57 RecentlyClosedTabsBridge::RecentlyClosedTabsBridge(Profile* profile) |
57 : profile_(profile), | 58 : profile_(profile), |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 jboolean RecentlyClosedTabsBridge::OpenRecentlyClosedTab(JNIEnv* env, | 90 jboolean RecentlyClosedTabsBridge::OpenRecentlyClosedTab(JNIEnv* env, |
90 jobject obj, | 91 jobject obj, |
91 jobject jtab, | 92 jobject jtab, |
92 jint recent_tab_id, | 93 jint recent_tab_id, |
93 jint j_disposition) { | 94 jint j_disposition) { |
94 if (!tab_restore_service_) | 95 if (!tab_restore_service_) |
95 return false; | 96 return false; |
96 | 97 |
97 // Find and remove the corresponding tab entry from TabRestoreService. | 98 // Find and remove the corresponding tab entry from TabRestoreService. |
98 // We take ownership of the returned tab. | 99 // We take ownership of the returned tab. |
99 scoped_ptr<TabRestoreService::Tab> tab_entry( | 100 scoped_ptr<sessions::TabRestoreService::Tab> tab_entry( |
100 tab_restore_service_->RemoveTabEntryById(recent_tab_id)); | 101 tab_restore_service_->RemoveTabEntryById(recent_tab_id)); |
101 if (!tab_entry) | 102 if (!tab_entry) |
102 return false; | 103 return false; |
103 | 104 |
104 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, jtab); | 105 TabAndroid* tab_android = TabAndroid::GetNativeTab(env, jtab); |
105 if (!tab_android) | 106 if (!tab_android) |
106 return false; | 107 return false; |
107 content::WebContents* web_contents = tab_android->web_contents(); | 108 content::WebContents* web_contents = tab_android->web_contents(); |
108 if (!web_contents) | 109 if (!web_contents) |
109 return false; | 110 return false; |
(...skipping 12 matching lines...) Expand all Loading... |
122 } | 123 } |
123 | 124 |
124 void RecentlyClosedTabsBridge::ClearRecentlyClosedTabs(JNIEnv* env, | 125 void RecentlyClosedTabsBridge::ClearRecentlyClosedTabs(JNIEnv* env, |
125 jobject obj) { | 126 jobject obj) { |
126 EnsureTabRestoreService(); | 127 EnsureTabRestoreService(); |
127 if (tab_restore_service_) | 128 if (tab_restore_service_) |
128 tab_restore_service_->ClearEntries(); | 129 tab_restore_service_->ClearEntries(); |
129 } | 130 } |
130 | 131 |
131 void RecentlyClosedTabsBridge::TabRestoreServiceChanged( | 132 void RecentlyClosedTabsBridge::TabRestoreServiceChanged( |
132 TabRestoreService* service) { | 133 sessions::TabRestoreService* service) { |
133 if (callback_.is_null()) | 134 if (callback_.is_null()) |
134 return; | 135 return; |
135 JNIEnv* env = AttachCurrentThread(); | 136 JNIEnv* env = AttachCurrentThread(); |
136 Java_RecentlyClosedCallback_onUpdated(env, callback_.obj()); | 137 Java_RecentlyClosedCallback_onUpdated(env, callback_.obj()); |
137 } | 138 } |
138 | 139 |
139 void RecentlyClosedTabsBridge::TabRestoreServiceDestroyed( | 140 void RecentlyClosedTabsBridge::TabRestoreServiceDestroyed( |
140 TabRestoreService* service) { | 141 sessions::TabRestoreService* service) { |
141 tab_restore_service_ = NULL; | 142 tab_restore_service_ = NULL; |
142 } | 143 } |
143 | 144 |
144 void RecentlyClosedTabsBridge::EnsureTabRestoreService() { | 145 void RecentlyClosedTabsBridge::EnsureTabRestoreService() { |
145 if (tab_restore_service_) | 146 if (tab_restore_service_) |
146 return; | 147 return; |
147 | 148 |
148 tab_restore_service_ = TabRestoreServiceFactory::GetForProfile(profile_); | 149 tab_restore_service_ = TabRestoreServiceFactory::GetForProfile(profile_); |
149 | 150 |
150 // TabRestoreServiceFactory::GetForProfile() can return NULL (e.g. in | 151 // TabRestoreServiceFactory::GetForProfile() can return NULL (e.g. in |
(...skipping 11 matching lines...) Expand all Loading... |
162 const JavaParamRef<jobject>& jprofile) { | 163 const JavaParamRef<jobject>& jprofile) { |
163 RecentlyClosedTabsBridge* bridge = new RecentlyClosedTabsBridge( | 164 RecentlyClosedTabsBridge* bridge = new RecentlyClosedTabsBridge( |
164 ProfileAndroid::FromProfileAndroid(jprofile)); | 165 ProfileAndroid::FromProfileAndroid(jprofile)); |
165 return reinterpret_cast<intptr_t>(bridge); | 166 return reinterpret_cast<intptr_t>(bridge); |
166 } | 167 } |
167 | 168 |
168 // static | 169 // static |
169 bool RecentlyClosedTabsBridge::Register(JNIEnv* env) { | 170 bool RecentlyClosedTabsBridge::Register(JNIEnv* env) { |
170 return RegisterNativesImpl(env); | 171 return RegisterNativesImpl(env); |
171 } | 172 } |
OLD | NEW |