1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/android/tab_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "chrome/browser/android/chrome_web_contents_delegate_android.h"
10 #include "chrome/browser/android/webapps/single_tab_mode_tab_helper.h"
11 #include "chrome/browser/browser_process.h"
12 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/content_settings/tab_specific_content_settings.h"
14 #include "chrome/browser/extensions/tab_helper.h"
15 #include "chrome/browser/favicon/favicon_tab_helper.h"
16 #include "chrome/browser/history/history_tab_helper.h"
17 #include "chrome/browser/infobars/infobar_service.h"
18 #include "chrome/browser/net/net_error_tab_helper.h"
19 #include "chrome/browser/password_manager/password_manager.h"
20 #include "chrome/browser/password_manager/password_manager_delegate_impl.h"
21 #include "chrome/browser/prerender/prerender_tab_helper.h"
22 #include "chrome/browser/printing/print_view_manager_basic.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/profiles/profile_android.h"
25 #include "chrome/browser/sessions/session_tab_helper.h"
26 #include "chrome/browser/ssl/ssl_tab_helper.h"
27 #include "chrome/browser/sync/glue/synced_tab_delegate_android.h"
28 #include "chrome/browser/tab_contents/navigation_metrics_recorder.h"
29 #include "chrome/browser/translate/translate_tab_helper.h"
30 #include "chrome/browser/ui/alternate_error_tab_observer.h"
31 #include "chrome/browser/ui/android/content_settings/popup_blocked_infobar_delegate.h"
32 #include "chrome/browser/ui/android/context_menu_helper.h"
33 #include "chrome/browser/ui/android/infobars/infobar_container_android.h"
34 #include "chrome/browser/ui/android/tab_model/tab_model.h"
35 #include "chrome/browser/ui/android/tab_model/tab_model_list.h"
36 #include "chrome/browser/ui/android/window_android_helper.h"
37 #include "chrome/browser/ui/autofill/tab_autofill_manager_delegate.h"
38 #include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
39 #include "chrome/browser/ui/bookmarks/bookmark_tab_helper.h"
40 #include "chrome/browser/ui/browser_tab_contents.h"
41 #include "chrome/browser/ui/find_bar/find_tab_helper.h"
42 #include "chrome/browser/ui/prefs/prefs_tab_helper.h"
43 #include "chrome/browser/ui/tab_contents/core_tab_helper.h"
44 #include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
45 #include "components/autofill/content/browser/autofill_driver_impl.h"
46 #include "content/public/browser/android/content_view_core.h"
47 #include "content/public/browser/navigation_entry.h"
48 #include "content/public/browser/notification_service.h"
49 #include "content/public/browser/web_contents.h"
50 #include "extensions/browser/view_type_utils.h"
51 #include "jni/TabBase_jni.h"
53 #if defined(ENABLE_MANAGED_USERS)
54 #include "chrome/browser/managed_mode/managed_mode_navigation_observer.h"
59 const char kTabHelpersInitializedUserDataKey
[] =
60 "TabAndroidTabHelpersInitialized";
64 void BrowserTabContents::AttachTabHelpers(content::WebContents
* contents
) {
65 // If already initialized, nothing to be done.
66 base::SupportsUserData::Data
* initialization_tag
=
67 contents
->GetUserData(&kTabHelpersInitializedUserDataKey
);
68 if (initialization_tag
)
71 // Mark as initialized.
72 contents
->SetUserData(&kTabHelpersInitializedUserDataKey
,
73 new base::SupportsUserData::Data());
76 extensions::SetViewType(contents
, extensions::VIEW_TYPE_TAB_CONTENTS
);
78 Profile
* profile
= Profile::FromBrowserContext(contents
->GetBrowserContext());
80 // SessionTabHelper comes first because it sets up the tab ID, and other
81 // helpers may rely on that.
82 SessionTabHelper::CreateForWebContents(contents
);
84 AlternateErrorPageTabObserver::CreateForWebContents(contents
);
85 autofill::TabAutofillManagerDelegate::CreateForWebContents(contents
);
86 autofill::AutofillDriverImpl::CreateForWebContentsAndDelegate(
88 autofill::TabAutofillManagerDelegate::FromWebContents(contents
),
89 g_browser_process
->GetApplicationLocale(),
90 autofill::AutofillManager::ENABLE_AUTOFILL_DOWNLOAD_MANAGER
);
91 BookmarkTabHelper::CreateForWebContents(contents
);
92 ContextMenuHelper::CreateForWebContents(contents
);
93 CoreTabHelper::CreateForWebContents(contents
);
94 extensions::TabHelper::CreateForWebContents(contents
);
95 FaviconTabHelper::CreateForWebContents(contents
);
96 FindTabHelper::CreateForWebContents(contents
);
97 HistoryTabHelper::CreateForWebContents(contents
);
98 InfoBarService::CreateForWebContents(contents
);
99 NavigationMetricsRecorder::CreateForWebContents(contents
);
100 chrome_browser_net::NetErrorTabHelper::CreateForWebContents(contents
);
101 PasswordManagerDelegateImpl::CreateForWebContents(contents
);
102 PasswordManager::CreateForWebContentsAndDelegate(
103 contents
, PasswordManagerDelegateImpl::FromWebContents(contents
));
104 PopupBlockerTabHelper::CreateForWebContents(contents
);
105 PrefsTabHelper::CreateForWebContents(contents
);
106 prerender::PrerenderTabHelper::CreateForWebContentsWithPasswordManager(
107 contents
, PasswordManager::FromWebContents(contents
));
108 SingleTabModeTabHelper::CreateForWebContents(contents
);
109 SSLTabHelper::CreateForWebContents(contents
);
110 TabSpecificContentSettings::CreateForWebContents(contents
);
111 TranslateTabHelper::CreateForWebContents(contents
);
112 WindowAndroidHelper::CreateForWebContents(contents
);
114 #if defined(ENABLE_MANAGED_USERS)
115 if (profile
->IsManaged())
116 ManagedModeNavigationObserver::CreateForWebContents(contents
);
120 // TODO(dtrainor): Refactor so we do not need this method.
121 void TabAndroid::InitTabHelpers(content::WebContents
* contents
) {
122 BrowserTabContents::AttachTabHelpers(contents
);
125 TabAndroid
* TabAndroid::FromWebContents(content::WebContents
* web_contents
) {
126 CoreTabHelper
* core_tab_helper
= CoreTabHelper::FromWebContents(web_contents
);
127 if (!core_tab_helper
)
130 CoreTabHelperDelegate
* core_delegate
= core_tab_helper
->delegate();
134 return static_cast<TabAndroid
*>(core_delegate
);
137 TabAndroid
* TabAndroid::GetNativeTab(JNIEnv
* env
, jobject obj
) {
138 return reinterpret_cast<TabAndroid
*>(Java_TabBase_getNativePtr(env
, obj
));
141 TabAndroid::TabAndroid(JNIEnv
* env
, jobject obj
)
142 : weak_java_tab_(env
, obj
),
144 synced_tab_delegate_(new browser_sync::SyncedTabDelegateAndroid(this)) {
145 Java_TabBase_setNativePtr(env
, obj
, reinterpret_cast<intptr_t>(this));
148 TabAndroid::~TabAndroid() {
149 JNIEnv
* env
= base::android::AttachCurrentThread();
150 ScopedJavaLocalRef
<jobject
> obj
= weak_java_tab_
.get(env
);
154 Java_TabBase_clearNativePtr(env
, obj
.obj());
157 int TabAndroid::GetAndroidId() const {
158 JNIEnv
* env
= base::android::AttachCurrentThread();
159 ScopedJavaLocalRef
<jobject
> obj
= weak_java_tab_
.get(env
);
162 return Java_TabBase_getId(env
, obj
.obj());
165 int TabAndroid::GetSyncId() const {
166 JNIEnv
* env
= base::android::AttachCurrentThread();
167 ScopedJavaLocalRef
<jobject
> obj
= weak_java_tab_
.get(env
);
170 return Java_TabBase_getSyncId(env
, obj
.obj());
173 base::string16
TabAndroid::GetTitle() const {
174 JNIEnv
* env
= base::android::AttachCurrentThread();
175 ScopedJavaLocalRef
<jobject
> obj
= weak_java_tab_
.get(env
);
177 return base::string16();
178 return base::android::ConvertJavaStringToUTF16(
179 Java_TabBase_getTitle(env
, obj
.obj()));
182 GURL
TabAndroid::GetURL() const {
183 JNIEnv
* env
= base::android::AttachCurrentThread();
184 ScopedJavaLocalRef
<jobject
> obj
= weak_java_tab_
.get(env
);
186 return GURL::EmptyGURL();
187 return GURL(base::android::ConvertJavaStringToUTF8(
188 Java_TabBase_getUrl(env
, obj
.obj())));
191 bool TabAndroid::RestoreIfNeeded() {
192 JNIEnv
* env
= base::android::AttachCurrentThread();
193 ScopedJavaLocalRef
<jobject
> obj
= weak_java_tab_
.get(env
);
196 return Java_TabBase_restoreIfNeeded(env
, obj
.obj());
199 content::ContentViewCore
* TabAndroid::GetContentViewCore() const {
203 return content::ContentViewCore::FromWebContents(web_contents());
206 Profile
* TabAndroid::GetProfile() const {
210 return Profile::FromBrowserContext(web_contents()->GetBrowserContext());
213 browser_sync::SyncedTabDelegate
* TabAndroid::GetSyncedTabDelegate() const {
214 return synced_tab_delegate_
.get();
217 void TabAndroid::SetSyncId(int sync_id
) {
218 JNIEnv
* env
= base::android::AttachCurrentThread();
219 ScopedJavaLocalRef
<jobject
> obj
= weak_java_tab_
.get(env
);
222 Java_TabBase_setSyncId(env
, obj
.obj(), sync_id
);
225 void TabAndroid::HandlePopupNavigation(chrome::NavigateParams
* params
) {
229 void TabAndroid::OnReceivedHttpAuthRequest(jobject auth_handler
,
230 const base::string16
& host
,
231 const base::string16
& realm
) {
235 void TabAndroid::AddShortcutToBookmark(const GURL
& url
,
236 const base::string16
& title
,
237 const SkBitmap
& skbitmap
,
244 void TabAndroid::EditBookmark(int64 node_id
,
245 const base::string16
& node_title
,
247 bool is_partner_bookmark
) {
251 void TabAndroid::OnNewTabPageReady() {
255 bool TabAndroid::ShouldWelcomePageLinkToTermsOfService() {
260 void TabAndroid::SwapTabContents(content::WebContents
* old_contents
,
261 content::WebContents
* new_contents
) {
262 JNIEnv
* env
= base::android::AttachCurrentThread();
264 // We need to notify the native InfobarContainer so infobars can be swapped.
265 InfoBarContainerAndroid
* infobar_container
=
266 reinterpret_cast<InfoBarContainerAndroid
*>(
267 Java_TabBase_getNativeInfoBarContainer(
269 weak_java_tab_
.get(env
).obj()));
270 InfoBarService
* new_infobar_service
= new_contents
?
271 InfoBarService::FromWebContents(new_contents
) : NULL
;
272 infobar_container
->ChangeInfoBarService(new_infobar_service
);
274 Java_TabBase_swapWebContents(
276 weak_java_tab_
.get(env
).obj(),
277 reinterpret_cast<intptr_t>(new_contents
));
280 void TabAndroid::Observe(int type
,
281 const content::NotificationSource
& source
,
282 const content::NotificationDetails
& details
) {
283 JNIEnv
* env
= base::android::AttachCurrentThread();
285 case chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED
: {
286 TabSpecificContentSettings
* settings
=
287 TabSpecificContentSettings::FromWebContents(web_contents());
288 if (!settings
->IsBlockageIndicated(CONTENT_SETTINGS_TYPE_POPUPS
)) {
289 // TODO(dfalcantara): Create an InfoBarDelegate to keep the
290 // PopupBlockedInfoBar logic native-side instead of straddling the JNI
293 PopupBlockerTabHelper
* popup_blocker_helper
=
294 PopupBlockerTabHelper::FromWebContents(web_contents());
295 if (popup_blocker_helper
)
296 num_popups
= popup_blocker_helper
->GetBlockedPopupsCount();
298 if (num_popups
> 0) {
299 PopupBlockedInfoBarDelegate::Create(
300 InfoBarService::FromWebContents(web_contents()),
304 settings
->SetBlockageHasBeenIndicated(CONTENT_SETTINGS_TYPE_POPUPS
);
308 case chrome::NOTIFICATION_FAVICON_UPDATED
:
309 Java_TabBase_onFaviconUpdated(env
, weak_java_tab_
.get(env
).obj());
312 NOTREACHED() << "Unexpected notification " << type
;
317 void TabAndroid::Destroy(JNIEnv
* env
, jobject obj
) {
321 void TabAndroid::InitWebContents(JNIEnv
* env
,
324 jobject jcontent_view_core
,
325 jobject jweb_contents_delegate
,
326 jobject jcontext_menu_populator
) {
327 content::ContentViewCore
* content_view_core
=
328 content::ContentViewCore::GetNativeContentViewCore(env
,
330 DCHECK(content_view_core
);
331 DCHECK(content_view_core
->GetWebContents());
333 web_contents_
.reset(content_view_core
->GetWebContents());
334 InitTabHelpers(web_contents_
.get());
336 session_tab_id_
.set_id(
337 SessionTabHelper::FromWebContents(web_contents())->session_id().id());
338 ContextMenuHelper::FromWebContents(web_contents())->SetPopulator(
339 jcontext_menu_populator
);
340 WindowAndroidHelper::FromWebContents(web_contents())->
341 SetWindowAndroid(content_view_core
->GetWindowAndroid());
342 CoreTabHelper::FromWebContents(web_contents())->set_delegate(this);
343 web_contents_delegate_
.reset(
344 new chrome::android::ChromeWebContentsDelegateAndroid(
345 env
, jweb_contents_delegate
));
346 web_contents_delegate_
->LoadProgressChanged(web_contents(), 0);
347 web_contents()->SetDelegate(web_contents_delegate_
.get());
349 notification_registrar_
.Add(
351 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED
,
352 content::Source
<content::WebContents
>(web_contents()));
353 notification_registrar_
.Add(
355 chrome::NOTIFICATION_FAVICON_UPDATED
,
356 content::Source
<content::WebContents
>(web_contents()));
358 synced_tab_delegate_
->SetWebContents(web_contents());
360 // Set the window ID if there is a valid TabModel.
361 TabModel
* model
= TabModelList::GetTabModelWithProfile(GetProfile());
364 window_id
.set_id(model
->GetSessionId());
366 SessionTabHelper
* session_tab_helper
=
367 SessionTabHelper::FromWebContents(web_contents());
368 session_tab_helper
->SetWindowID(window_id
);
371 // Verify that the WebContents this tab represents matches the expected
372 // off the record state.
373 CHECK_EQ(GetProfile()->IsOffTheRecord(), incognito
);
376 void TabAndroid::DestroyWebContents(JNIEnv
* env
,
378 jboolean delete_native
) {
379 DCHECK(web_contents());
381 notification_registrar_
.Remove(
383 chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED
,
384 content::Source
<content::WebContents
>(web_contents()));
385 notification_registrar_
.Remove(
387 chrome::NOTIFICATION_FAVICON_UPDATED
,
388 content::Source
<content::WebContents
>(web_contents()));
390 web_contents()->SetDelegate(NULL
);
393 web_contents_
.reset();
394 synced_tab_delegate_
->ResetWebContents();
396 // Release the WebContents so it does not get deleted by the scoped_ptr.
397 ignore_result(web_contents_
.release());
401 base::android::ScopedJavaLocalRef
<jobject
> TabAndroid::GetWebContents(
404 if (!web_contents_
.get())
405 return base::android::ScopedJavaLocalRef
<jobject
>();
406 return web_contents_
->GetJavaWebContents();
409 base::android::ScopedJavaLocalRef
<jobject
> TabAndroid::GetProfileAndroid(
412 Profile
* profile
= GetProfile();
414 return base::android::ScopedJavaLocalRef
<jobject
>();
415 ProfileAndroid
* profile_android
= ProfileAndroid::FromProfile(profile
);
416 if (!profile_android
)
417 return base::android::ScopedJavaLocalRef
<jobject
>();
419 return profile_android
->GetJavaObject();
422 ToolbarModel::SecurityLevel
TabAndroid::GetSecurityLevel(JNIEnv
* env
,
424 return ToolbarModelImpl::GetSecurityLevelForWebContents(web_contents());
427 void TabAndroid::SetActiveNavigationEntryTitleForUrl(JNIEnv
* env
,
431 DCHECK(web_contents());
433 base::string16 title
;
435 title
= base::android::ConvertJavaStringToUTF16(env
, jtitle
);
439 url
= base::android::ConvertJavaStringToUTF8(env
, jurl
);
441 content::NavigationEntry
* entry
=
442 web_contents()->GetController().GetVisibleEntry();
443 if (entry
&& url
== entry
->GetVirtualURL().spec())
444 entry
->SetTitle(title
);
447 bool TabAndroid::Print(JNIEnv
* env
, jobject obj
) {
451 printing::PrintViewManagerBasic::CreateForWebContents(web_contents());
452 printing::PrintViewManagerBasic
* print_view_manager
=
453 printing::PrintViewManagerBasic::FromWebContents(web_contents());
454 if (print_view_manager
== NULL
)
457 print_view_manager
->PrintNow();
461 static void Init(JNIEnv
* env
, jobject obj
) {
462 TRACE_EVENT0("native", "TabAndroid::Init");
463 // This will automatically bind to the Java object and pass ownership there.
464 new TabAndroid(env
, obj
);
467 bool TabAndroid::RegisterTabAndroid(JNIEnv
* env
) {
468 return RegisterNativesImpl(env
);