tab_android.cc revision 23730a6e56a168d1879203e4b3819bb36e3d8f1f
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/android/tab_android.h"
6c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
7424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "base/android/jni_android.h"
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/android/jni_array.h"
94e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "base/android/jni_string.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/debug/trace_event.h"
11424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/android/chrome_web_contents_delegate_android.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/browser_about_handler.h"
1358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/chrome_notification_types.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/content_settings/tab_specific_content_settings.h"
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/google/google_url_tracker.h"
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/google/google_util.h"
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/prerender/prerender_contents.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/prerender/prerender_manager.h"
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/prerender/prerender_manager_factory.h"
20f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/printing/print_view_manager_basic.h"
21424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
22424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/profiles/profile_android.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sessions/session_tab_helper.h"
2458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/sync/glue/synced_tab_delegate_android.h"
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/ui/android/content_settings/popup_blocked_infobar_delegate.h"
26a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/ui/android/context_menu_helper.h"
274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/browser/ui/android/infobars/infobar_container_android.h"
2858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/ui/android/tab_model/tab_model.h"
2958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/android/window_android_helper.h"
31424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/tab_helpers.h"
342a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/common/net/url_fixer_upper.h"
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/common/url_constants.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/android/content_view_core.h"
384e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "content/public/browser/navigation_entry.h"
3958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "content/public/browser/notification_service.h"
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/browser/user_metrics.h"
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "jni/Tab_jni.h"
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "third_party/WebKit/public/platform/WebReferrerPolicy.h"
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
45424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TabAndroid* TabAndroid::FromWebContents(content::WebContents* web_contents) {
46424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CoreTabHelper* core_tab_helper = CoreTabHelper::FromWebContents(web_contents);
47424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!core_tab_helper)
48424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
49424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
50424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CoreTabHelperDelegate* core_delegate = core_tab_helper->delegate();
51424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!core_delegate)
52424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
53424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
54424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return static_cast<TabAndroid*>(core_delegate);
55424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
56424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
57424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TabAndroid* TabAndroid::GetNativeTab(JNIEnv* env, jobject obj) {
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return reinterpret_cast<TabAndroid*>(Java_Tab_getNativePtr(env, obj));
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)void TabAndroid::AttachTabHelpers(content::WebContents* web_contents) {
6223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  DCHECK(web_contents);
6323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
6423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  TabHelpers::AttachTabHelpers(web_contents);
6523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
6623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
67424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TabAndroid::TabAndroid(JNIEnv* env, jobject obj)
68424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    : weak_java_tab_(env, obj),
6958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      synced_tab_delegate_(new browser_sync::SyncedTabDelegateAndroid(this)) {
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Java_Tab_setNativePtr(env, obj, reinterpret_cast<intptr_t>(this));
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TabAndroid::~TabAndroid() {
7458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = weak_java_tab_.get(env);
7658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (obj.is_null())
7758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return;
7858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Java_Tab_clearNativePtr(env, obj.obj());
8058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
8158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetJavaObject() {
835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return weak_java_tab_.get(env);
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
878bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)int TabAndroid::GetAndroidId() const {
888bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
898bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = weak_java_tab_.get(env);
908bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (obj.is_null())
918bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return -1;
92a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return Java_Tab_getId(env, obj.obj());
938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)int TabAndroid::GetSyncId() const {
960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = weak_java_tab_.get(env);
980f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (obj.is_null())
990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return 0;
100a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return Java_Tab_getSyncId(env, obj.obj());
1010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
103a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 TabAndroid::GetTitle() const {
1048bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1058bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = weak_java_tab_.get(env);
1068bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (obj.is_null())
107a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return base::string16();
1088bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return base::android::ConvertJavaStringToUTF16(
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      Java_Tab_getTitle(env, obj.obj()));
1108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)GURL TabAndroid::GetURL() const {
1138bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = weak_java_tab_.get(env);
1158bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (obj.is_null())
1168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return GURL::EmptyGURL();
1178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return GURL(base::android::ConvertJavaStringToUTF8(
118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      Java_Tab_getUrl(env, obj.obj())));
1198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)bool TabAndroid::RestoreIfNeeded() {
1228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = weak_java_tab_.get(env);
1248bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (obj.is_null())
1258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return false;
126a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return Java_Tab_restoreIfNeeded(env, obj.obj());
1278bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
129424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)content::ContentViewCore* TabAndroid::GetContentViewCore() const {
130424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!web_contents())
131424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
132424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
133424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return content::ContentViewCore::FromWebContents(web_contents());
134424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
135424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
136424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)Profile* TabAndroid::GetProfile() const {
137424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!web_contents())
138424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
139424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
140424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return Profile::FromBrowserContext(web_contents()->GetBrowserContext());
141424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
142424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
14358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)browser_sync::SyncedTabDelegate* TabAndroid::GetSyncedTabDelegate() const {
14458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return synced_tab_delegate_.get();
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void TabAndroid::SetWindowSessionID(SessionID::id_type window_id) {
148a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  session_window_id_.set_id(window_id);
149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!web_contents())
151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
152a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SessionTabHelper* session_tab_helper =
154a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          SessionTabHelper::FromWebContents(web_contents());
155a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  session_tab_helper->SetWindowID(session_window_id_);
156a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
157a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1580f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void TabAndroid::SetSyncId(int sync_id) {
1590f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1600f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = weak_java_tab_.get(env);
1610f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (obj.is_null())
1620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
163a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Java_Tab_setSyncId(env, obj.obj(), sync_id);
1640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TabAndroid::HandlePopupNavigation(chrome::NavigateParams* params) {
1675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NOTIMPLEMENTED();
1685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TabAndroid::OnReceivedHttpAuthRequest(jobject auth_handler,
1715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           const base::string16& host,
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           const base::string16& realm) {
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NOTIMPLEMENTED();
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TabAndroid::AddShortcutToBookmark(const GURL& url,
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       const base::string16& title,
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       const SkBitmap& skbitmap,
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       int r_value,
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       int g_value,
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                       int b_value) {
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NOTREACHED();
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TabAndroid::EditBookmark(int64 node_id,
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              const base::string16& node_title,
1875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              bool is_folder,
1885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              bool is_partner_bookmark) {
1895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NOTREACHED();
1905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TabAndroid::OnNewTabPageReady() {
1935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NOTREACHED();
1945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool TabAndroid::ShouldWelcomePageLinkToTermsOfService() {
1975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NOTIMPLEMENTED();
1985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return false;
1995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
201a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool TabAndroid::HasPrerenderedUrl(GURL gurl) {
202a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  prerender::PrerenderManager* prerender_manager = GetPrerenderManager();
203a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!prerender_manager)
204a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
205a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
206a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::vector<content::WebContents*> contents =
207a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      prerender_manager->GetAllPrerenderingContents();
208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  prerender::PrerenderContents* prerender_contents;
209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (size_t i = 0; i < contents.size(); ++i) {
210a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    prerender_contents = prerender_manager->
211a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        GetPrerenderContents(contents.at(i));
212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (prerender_contents->prerender_url() == gurl &&
213a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        prerender_contents->has_finished_loading()) {
214a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return true;
215a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
216a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
217a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return false;
218a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
219a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
220424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void TabAndroid::SwapTabContents(content::WebContents* old_contents,
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 content::WebContents* new_contents,
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 bool did_start_load,
2235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 bool did_finish_load) {
224424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
2254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // We need to notify the native InfobarContainer so infobars can be swapped.
2274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  InfoBarContainerAndroid* infobar_container =
2284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      reinterpret_cast<InfoBarContainerAndroid*>(
229a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          Java_Tab_getNativeInfoBarContainer(
2304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              env,
2314e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              weak_java_tab_.get(env).obj()));
2324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  InfoBarService* new_infobar_service = new_contents ?
2334e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      InfoBarService::FromWebContents(new_contents) : NULL;
234f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  infobar_container->ChangeInfoBarService(new_infobar_service);
2354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
236a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Java_Tab_swapWebContents(
237424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      env,
238424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      weak_java_tab_.get(env).obj(),
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      reinterpret_cast<intptr_t>(new_contents),
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      did_start_load,
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      did_finish_load);
242424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
243424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
24458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void TabAndroid::Observe(int type,
24558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                         const content::NotificationSource& source,
24658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                         const content::NotificationDetails& details) {
24758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
24858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  switch (type) {
24958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    case chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED: {
25058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      TabSpecificContentSettings* settings =
25158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          TabSpecificContentSettings::FromWebContents(web_contents());
25258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      if (!settings->IsBlockageIndicated(CONTENT_SETTINGS_TYPE_POPUPS)) {
25358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // TODO(dfalcantara): Create an InfoBarDelegate to keep the
25458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // PopupBlockedInfoBar logic native-side instead of straddling the JNI
25558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // boundary.
25658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        int num_popups = 0;
25758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        PopupBlockerTabHelper* popup_blocker_helper =
25858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            PopupBlockerTabHelper::FromWebContents(web_contents());
25958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        if (popup_blocker_helper)
26058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          num_popups = popup_blocker_helper->GetBlockedPopupsCount();
26158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (num_popups > 0)
2635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          PopupBlockedInfoBarDelegate::Create(web_contents(), num_popups);
264f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
26558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        settings->SetBlockageHasBeenIndicated(CONTENT_SETTINGS_TYPE_POPUPS);
26658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      }
26758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
26858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
26958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    case chrome::NOTIFICATION_FAVICON_UPDATED:
270a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      Java_Tab_onFaviconUpdated(env, weak_java_tab_.get(env).obj());
27158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
2725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    case content::NOTIFICATION_NAV_ENTRY_CHANGED:
273a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      Java_Tab_onNavEntryChanged(env, weak_java_tab_.get(env).obj());
2745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      break;
27558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    default:
27658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NOTREACHED() << "Unexpected notification " << type;
27758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
27858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
27958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
28058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TabAndroid::Destroy(JNIEnv* env, jobject obj) {
2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  delete this;
2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
285424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void TabAndroid::InitWebContents(JNIEnv* env,
286424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                 jobject obj,
287424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                 jboolean incognito,
288424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                 jobject jcontent_view_core,
289a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                 jobject jweb_contents_delegate,
290a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                 jobject jcontext_menu_populator) {
291424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  content::ContentViewCore* content_view_core =
292424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      content::ContentViewCore::GetNativeContentViewCore(env,
293424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                         jcontent_view_core);
294424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(content_view_core);
295424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(content_view_core->GetWebContents());
296424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
297424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents_.reset(content_view_core->GetWebContents());
29823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  AttachTabHelpers(web_contents_.get());
299424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
300a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetWindowSessionID(session_window_id_.id());
301a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
302424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  session_tab_id_.set_id(
303424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      SessionTabHelper::FromWebContents(web_contents())->session_id().id());
304a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ContextMenuHelper::FromWebContents(web_contents())->SetPopulator(
305a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      jcontext_menu_populator);
306424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  WindowAndroidHelper::FromWebContents(web_contents())->
307424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      SetWindowAndroid(content_view_core->GetWindowAndroid());
308424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CoreTabHelper::FromWebContents(web_contents())->set_delegate(this);
309424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents_delegate_.reset(
310424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      new chrome::android::ChromeWebContentsDelegateAndroid(
311424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          env, jweb_contents_delegate));
312424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents_delegate_->LoadProgressChanged(web_contents(), 0);
313424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents()->SetDelegate(web_contents_delegate_.get());
314424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
31558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Add(
31658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
31758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
31858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
31958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Add(
32058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
32158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_FAVICON_UPDATED,
32258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
3235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  notification_registrar_.Add(
3245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this,
3255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      content::NOTIFICATION_NAV_ENTRY_CHANGED,
3265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      content::Source<content::NavigationController>(
3275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           &web_contents()->GetController()));
32858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
32958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  synced_tab_delegate_->SetWebContents(web_contents());
33058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
331424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Verify that the WebContents this tab represents matches the expected
332424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // off the record state.
333424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CHECK_EQ(GetProfile()->IsOffTheRecord(), incognito);
334424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
335424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
336424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void TabAndroid::DestroyWebContents(JNIEnv* env,
337424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                    jobject obj,
338424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                    jboolean delete_native) {
33958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK(web_contents());
34058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
34158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Remove(
34258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
34358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
34458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
34558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Remove(
34658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
34758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_FAVICON_UPDATED,
34858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
34923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  notification_registrar_.Remove(
35023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      this,
35123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      content::NOTIFICATION_NAV_ENTRY_CHANGED,
35223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      content::Source<content::NavigationController>(
35323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)           &web_contents()->GetController()));
35458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
355424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents()->SetDelegate(NULL);
356424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
357424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (delete_native) {
358424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    web_contents_.reset();
35958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    synced_tab_delegate_->ResetWebContents();
360424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  } else {
361424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Release the WebContents so it does not get deleted by the scoped_ptr.
362424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ignore_result(web_contents_.release());
363424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
364424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
365424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
3665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetWebContents(
3675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    JNIEnv* env,
3685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    jobject obj) {
3695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!web_contents_.get())
3705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return base::android::ScopedJavaLocalRef<jobject>();
3715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return web_contents_->GetJavaWebContents();
3725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
374424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetProfileAndroid(
375424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    JNIEnv* env,
376424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    jobject obj) {
377424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  Profile* profile = GetProfile();
378424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!profile)
379424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return base::android::ScopedJavaLocalRef<jobject>();
380424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  ProfileAndroid* profile_android = ProfileAndroid::FromProfile(profile);
381424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!profile_android)
382424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return base::android::ScopedJavaLocalRef<jobject>();
383424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
384424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return profile_android->GetJavaObject();
385424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
386424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
387a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TabAndroid::TabLoadStatus TabAndroid::LoadUrl(JNIEnv* env,
388a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jobject obj,
389a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jstring url,
390a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jstring j_extra_headers,
391a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jbyteArray j_post_data,
392a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jint page_transition,
393a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jstring j_referrer_url,
394a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jint referrer_policy) {
395a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  content::ContentViewCore* content_view = GetContentViewCore();
396a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!content_view)
397a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return PAGE_LOAD_FAILED;
398a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
399a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL gurl(base::android::ConvertJavaStringToUTF8(env, url));
400a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (gurl.is_empty())
401a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return PAGE_LOAD_FAILED;
402a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
403a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If the page was prerendered, use it.
404a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Note in incognito mode, we don't have a PrerenderManager.
405a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
406a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  prerender::PrerenderManager* prerender_manager =
407a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      prerender::PrerenderManagerFactory::GetForProfile(GetProfile());
408a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (prerender_manager) {
409a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    bool prefetched_page_loaded = HasPrerenderedUrl(gurl);
410a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Getting the load status before MaybeUsePrerenderedPage() b/c it resets.
411a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    chrome::NavigateParams params(NULL, web_contents());
412a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (prerender_manager->MaybeUsePrerenderedPage(gurl, &params)) {
413a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return prefetched_page_loaded ?
414a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          FULL_PRERENDERED_PAGE_LOAD : PARTIAL_PRERENDERED_PAGE_LOAD;
415a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
416a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
417a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
418a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL fixed_url(URLFixerUpper::FixupURL(gurl.possibly_invalid_spec(),
419a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                         std::string()));
420a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!fixed_url.is_valid())
421a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return PAGE_LOAD_FAILED;
422a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
423a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!HandleNonNavigationAboutURL(fixed_url)) {
424a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Notify the GoogleURLTracker of searches, it might want to change the
425a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // actual Google site used (for instance when in the UK, google.co.uk, when
426a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // in the US google.com).
427a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Note that this needs to happen before we initiate the navigation as the
428a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // GoogleURLTracker uses the navigation pending notification to trigger the
429a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // infobar.
430a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (google_util::IsGoogleSearchUrl(fixed_url) &&
431a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        (page_transition & content::PAGE_TRANSITION_GENERATED)) {
432a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      GoogleURLTracker::GoogleURLSearchCommitted(GetProfile());
433a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
434a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
435a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Record UMA "ShowHistory" here. That way it'll pick up both user
436a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // typing chrome://history as well as selecting from the drop down menu.
437a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (fixed_url.spec() == chrome::kChromeUIHistoryURL) {
438a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      content::RecordAction(base::UserMetricsAction("ShowHistory"));
439a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
440a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
441a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::NavigationController::LoadURLParams load_params(fixed_url);
442a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (j_extra_headers) {
443a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.extra_headers = base::android::ConvertJavaStringToUTF8(
444a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          env,
445a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          j_extra_headers);
446a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
447a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (j_post_data) {
448a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.load_type =
449a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          content::NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST;
450a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      std::vector<uint8> post_data;
451a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::android::JavaByteArrayToByteVector(env, j_post_data, &post_data);
452a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.browser_initiated_post_data =
453a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::RefCountedBytes::TakeVector(&post_data);
454a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
455a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    load_params.transition_type =
456a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        content::PageTransitionFromInt(page_transition);
457a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (j_referrer_url) {
458a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.referrer = content::Referrer(
459a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          GURL(base::android::ConvertJavaStringToUTF8(env, j_referrer_url)),
460a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          static_cast<blink::WebReferrerPolicy>(referrer_policy));
461a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
462a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content_view->LoadUrl(load_params);
463a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
464a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return DEFAULT_PAGE_LOAD;
465a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
466a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
46758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)ToolbarModel::SecurityLevel TabAndroid::GetSecurityLevel(JNIEnv* env,
46858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                                         jobject obj) {
46958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return ToolbarModelImpl::GetSecurityLevelForWebContents(web_contents());
47058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
47158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
4724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void TabAndroid::SetActiveNavigationEntryTitleForUrl(JNIEnv* env,
4734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                     jobject obj,
4744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                     jstring jurl,
4754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                     jstring jtitle) {
4764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  DCHECK(web_contents());
4774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
478a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 title;
4794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (jtitle)
4804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    title = base::android::ConvertJavaStringToUTF16(env, jtitle);
4814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string url;
4834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (jurl)
4844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    url = base::android::ConvertJavaStringToUTF8(env, jurl);
4854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  content::NavigationEntry* entry =
4874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      web_contents()->GetController().GetVisibleEntry();
4884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (entry && url == entry->GetVirtualURL().spec())
4894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    entry->SetTitle(title);
4904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
4914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
492f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool TabAndroid::Print(JNIEnv* env, jobject obj) {
493f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!web_contents())
494f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return false;
495f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
496f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  printing::PrintViewManagerBasic::CreateForWebContents(web_contents());
497f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  printing::PrintViewManagerBasic* print_view_manager =
498f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      printing::PrintViewManagerBasic::FromWebContents(web_contents());
499f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (print_view_manager == NULL)
500f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return false;
501f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
502f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  print_view_manager->PrintNow();
503f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return true;
504f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
505f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
506a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)prerender::PrerenderManager* TabAndroid::GetPrerenderManager() const {
507a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Profile* profile = GetProfile();
508a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!profile)
509a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return NULL;
510a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return prerender::PrerenderManagerFactory::GetForProfile(profile);
511a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
512a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static void Init(JNIEnv* env, jobject obj) {
5145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TRACE_EVENT0("native", "TabAndroid::Init");
5155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This will automatically bind to the Java object and pass ownership there.
5165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  new TabAndroid(env, obj);
5175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
519c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool TabAndroid::RegisterTabAndroid(JNIEnv* env) {
520c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return RegisterNativesImpl(env);
521c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
522