tab_android.cc revision 6d86b77056ed63eb6871182f42a9fd5f07550f90
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"
1546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/google/google_url_tracker_factory.h"
16c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "chrome/browser/infobars/infobar_service.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"
235c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/search/search.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sessions/session_tab_helper.h"
2558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/sync/glue/synced_tab_delegate_android.h"
26f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/ui/android/content_settings/popup_blocked_infobar_delegate.h"
27a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/ui/android/context_menu_helper.h"
284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/browser/ui/android/infobars/infobar_container_android.h"
2958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/ui/android/tab_model/tab_model.h"
3058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/android/window_android_helper.h"
32424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
33f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/ui/search/instant_search_prerenderer.h"
345c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/ui/search/search_tab_helper.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/tab_helpers.h"
372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/common/url_constants.h"
39f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/google/core/browser/google_url_tracker.h"
406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "components/google/core/browser/google_util.h"
410529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/infobars/core/infobar_container.h"
42f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/url_fixer/url_fixer.h"
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/android/content_view_core.h"
444e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "content/public/browser/navigation_entry.h"
4558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "content/public/browser/notification_service.h"
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/browser/user_metrics.h"
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "jni/Tab_jni.h"
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "third_party/WebKit/public/platform/WebReferrerPolicy.h"
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
51424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TabAndroid* TabAndroid::FromWebContents(content::WebContents* web_contents) {
52424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CoreTabHelper* core_tab_helper = CoreTabHelper::FromWebContents(web_contents);
53424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!core_tab_helper)
54424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
55424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
56424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CoreTabHelperDelegate* core_delegate = core_tab_helper->delegate();
57424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!core_delegate)
58424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
59424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
60424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return static_cast<TabAndroid*>(core_delegate);
61424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
62424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
63424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TabAndroid* TabAndroid::GetNativeTab(JNIEnv* env, jobject obj) {
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return reinterpret_cast<TabAndroid*>(Java_Tab_getNativePtr(env, obj));
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)void TabAndroid::AttachTabHelpers(content::WebContents* web_contents) {
6823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  DCHECK(web_contents);
6923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
7023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  TabHelpers::AttachTabHelpers(web_contents);
7123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
7223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
73424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TabAndroid::TabAndroid(JNIEnv* env, jobject obj)
74424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    : weak_java_tab_(env, obj),
7558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      synced_tab_delegate_(new browser_sync::SyncedTabDelegateAndroid(this)) {
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Java_Tab_setNativePtr(env, obj, reinterpret_cast<intptr_t>(this));
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TabAndroid::~TabAndroid() {
8058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
8158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = weak_java_tab_.get(env);
8258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  if (obj.is_null())
8358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    return;
8458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Java_Tab_clearNativePtr(env, obj.obj());
8658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
8758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetJavaObject() {
895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return weak_java_tab_.get(env);
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
938bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)int TabAndroid::GetAndroidId() const {
948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
958bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = weak_java_tab_.get(env);
968bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (obj.is_null())
978bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return -1;
98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return Java_Tab_getId(env, obj.obj());
998bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1008bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)int TabAndroid::GetSyncId() const {
1020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = weak_java_tab_.get(env);
1040f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (obj.is_null())
1050f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return 0;
106a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return Java_Tab_getSyncId(env, obj.obj());
1070f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
109a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 TabAndroid::GetTitle() const {
1108bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1118bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = weak_java_tab_.get(env);
1128bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (obj.is_null())
113a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)    return base::string16();
1148bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return base::android::ConvertJavaStringToUTF16(
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      Java_Tab_getTitle(env, obj.obj()));
1168bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1178bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1188bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)GURL TabAndroid::GetURL() const {
1198bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1208bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = weak_java_tab_.get(env);
1218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (obj.is_null())
1228bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return GURL::EmptyGURL();
1238bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return GURL(base::android::ConvertJavaStringToUTF8(
124a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      Java_Tab_getUrl(env, obj.obj())));
1258bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1268bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
127e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochbool TabAndroid::LoadIfNeeded() {
1288bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1298bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = weak_java_tab_.get(env);
1308bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  if (obj.is_null())
1318bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)    return false;
132e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  return Java_Tab_loadIfNeeded(env, obj.obj());
1338bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1348bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
135424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)content::ContentViewCore* TabAndroid::GetContentViewCore() const {
136424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!web_contents())
137424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
138424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
139424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return content::ContentViewCore::FromWebContents(web_contents());
140424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
141424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
142424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)Profile* TabAndroid::GetProfile() const {
143424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!web_contents())
144424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
145424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
146424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return Profile::FromBrowserContext(web_contents()->GetBrowserContext());
147424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
148424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
14958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)browser_sync::SyncedTabDelegate* TabAndroid::GetSyncedTabDelegate() const {
15058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return synced_tab_delegate_.get();
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
153a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void TabAndroid::SetWindowSessionID(SessionID::id_type window_id) {
154a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  session_window_id_.set_id(window_id);
155a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
156a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!web_contents())
157a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
158a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
159a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SessionTabHelper* session_tab_helper =
160a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          SessionTabHelper::FromWebContents(web_contents());
161a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  session_tab_helper->SetWindowID(session_window_id_);
162a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
163a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1640f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void TabAndroid::SetSyncId(int sync_id) {
1650f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  ScopedJavaLocalRef<jobject> obj = weak_java_tab_.get(env);
1670f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  if (obj.is_null())
1680f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)    return;
169a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Java_Tab_setSyncId(env, obj.obj(), sync_id);
1700f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TabAndroid::HandlePopupNavigation(chrome::NavigateParams* params) {
1735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NOTIMPLEMENTED();
1745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TabAndroid::OnReceivedHttpAuthRequest(jobject auth_handler,
1775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           const base::string16& host,
1785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                           const base::string16& realm) {
1795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NOTIMPLEMENTED();
1805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool TabAndroid::ShouldWelcomePageLinkToTermsOfService() {
1835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NOTIMPLEMENTED();
1845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return false;
1855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
187a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool TabAndroid::HasPrerenderedUrl(GURL gurl) {
188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  prerender::PrerenderManager* prerender_manager = GetPrerenderManager();
189a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!prerender_manager)
190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::vector<content::WebContents*> contents =
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      prerender_manager->GetAllPrerenderingContents();
194a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  prerender::PrerenderContents* prerender_contents;
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (size_t i = 0; i < contents.size(); ++i) {
196a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    prerender_contents = prerender_manager->
197a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        GetPrerenderContents(contents.at(i));
198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (prerender_contents->prerender_url() == gurl &&
199a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        prerender_contents->has_finished_loading()) {
200a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return true;
201a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
202a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
203a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return false;
204a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
205a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
206424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void TabAndroid::SwapTabContents(content::WebContents* old_contents,
2075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 content::WebContents* new_contents,
2085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 bool did_start_load,
2095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 bool did_finish_load) {
210424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
2114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // We need to notify the native InfobarContainer so infobars can be swapped.
2134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  InfoBarContainerAndroid* infobar_container =
2144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      reinterpret_cast<InfoBarContainerAndroid*>(
215a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          Java_Tab_getNativeInfoBarContainer(
2164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              env,
2174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              weak_java_tab_.get(env).obj()));
218a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  InfoBarService* new_infobar_service =
219a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      new_contents ? InfoBarService::FromWebContents(new_contents) : NULL;
220a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  infobar_container->ChangeInfoBarManager(new_infobar_service);
2214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
222a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Java_Tab_swapWebContents(
223424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      env,
224424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      weak_java_tab_.get(env).obj(),
2255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      reinterpret_cast<intptr_t>(new_contents),
2265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      did_start_load,
2275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      did_finish_load);
228424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
229424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
23058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void TabAndroid::Observe(int type,
23158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                         const content::NotificationSource& source,
23258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                         const content::NotificationDetails& details) {
23358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
23458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  switch (type) {
23558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    case chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED: {
23658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      TabSpecificContentSettings* settings =
23758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          TabSpecificContentSettings::FromWebContents(web_contents());
23858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      if (!settings->IsBlockageIndicated(CONTENT_SETTINGS_TYPE_POPUPS)) {
23958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // TODO(dfalcantara): Create an InfoBarDelegate to keep the
24058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // PopupBlockedInfoBar logic native-side instead of straddling the JNI
24158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // boundary.
24258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        int num_popups = 0;
24358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        PopupBlockerTabHelper* popup_blocker_helper =
24458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            PopupBlockerTabHelper::FromWebContents(web_contents());
24558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        if (popup_blocker_helper)
24658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          num_popups = popup_blocker_helper->GetBlockedPopupsCount();
24758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (num_popups > 0)
2495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          PopupBlockedInfoBarDelegate::Create(web_contents(), num_popups);
250f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
25158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        settings->SetBlockageHasBeenIndicated(CONTENT_SETTINGS_TYPE_POPUPS);
25258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      }
25358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
25458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
25558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    case chrome::NOTIFICATION_FAVICON_UPDATED:
256a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      Java_Tab_onFaviconUpdated(env, weak_java_tab_.get(env).obj());
25758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
2585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    case content::NOTIFICATION_NAV_ENTRY_CHANGED:
259a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      Java_Tab_onNavEntryChanged(env, weak_java_tab_.get(env).obj());
2605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      break;
26158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    default:
26258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NOTREACHED() << "Unexpected notification " << type;
26358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
26458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
26558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
26658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TabAndroid::Destroy(JNIEnv* env, jobject obj) {
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  delete this;
2695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
271424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void TabAndroid::InitWebContents(JNIEnv* env,
272424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                 jobject obj,
273424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                 jboolean incognito,
274424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                 jobject jcontent_view_core,
275a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                 jobject jweb_contents_delegate,
276a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                 jobject jcontext_menu_populator) {
277424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  content::ContentViewCore* content_view_core =
278424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      content::ContentViewCore::GetNativeContentViewCore(env,
279424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                         jcontent_view_core);
280424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(content_view_core);
281424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(content_view_core->GetWebContents());
282424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
283424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents_.reset(content_view_core->GetWebContents());
28423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  AttachTabHelpers(web_contents_.get());
285424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
286a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetWindowSessionID(session_window_id_.id());
287a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
288424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  session_tab_id_.set_id(
289424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      SessionTabHelper::FromWebContents(web_contents())->session_id().id());
290a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ContextMenuHelper::FromWebContents(web_contents())->SetPopulator(
291a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      jcontext_menu_populator);
292424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  WindowAndroidHelper::FromWebContents(web_contents())->
293424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      SetWindowAndroid(content_view_core->GetWindowAndroid());
294424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CoreTabHelper::FromWebContents(web_contents())->set_delegate(this);
295424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents_delegate_.reset(
296424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      new chrome::android::ChromeWebContentsDelegateAndroid(
297424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          env, jweb_contents_delegate));
298424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents_delegate_->LoadProgressChanged(web_contents(), 0);
299424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents()->SetDelegate(web_contents_delegate_.get());
300424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
30158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Add(
30258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
30358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
30458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
30558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Add(
30658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
30758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_FAVICON_UPDATED,
30858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  notification_registrar_.Add(
3105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this,
3115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      content::NOTIFICATION_NAV_ENTRY_CHANGED,
3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      content::Source<content::NavigationController>(
3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           &web_contents()->GetController()));
31458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
31558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  synced_tab_delegate_->SetWebContents(web_contents());
31658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
317424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Verify that the WebContents this tab represents matches the expected
318424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // off the record state.
319424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CHECK_EQ(GetProfile()->IsOffTheRecord(), incognito);
320424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
321424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
322424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void TabAndroid::DestroyWebContents(JNIEnv* env,
323424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                    jobject obj,
324424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                    jboolean delete_native) {
32558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK(web_contents());
32658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
32758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Remove(
32858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
32958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
33058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
33158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Remove(
33258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
33358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_FAVICON_UPDATED,
33458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
33523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  notification_registrar_.Remove(
33623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      this,
33723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      content::NOTIFICATION_NAV_ENTRY_CHANGED,
33823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      content::Source<content::NavigationController>(
33923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)           &web_contents()->GetController()));
34058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
341424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents()->SetDelegate(NULL);
342424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
343424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (delete_native) {
344424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    web_contents_.reset();
34558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    synced_tab_delegate_->ResetWebContents();
346424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  } else {
347424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Release the WebContents so it does not get deleted by the scoped_ptr.
348424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ignore_result(web_contents_.release());
349424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
350424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
351424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
3525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetWebContents(
3535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    JNIEnv* env,
3545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    jobject obj) {
3555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!web_contents_.get())
3565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return base::android::ScopedJavaLocalRef<jobject>();
3575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return web_contents_->GetJavaWebContents();
3585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
360424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetProfileAndroid(
361424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    JNIEnv* env,
362424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    jobject obj) {
363424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  Profile* profile = GetProfile();
364424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!profile)
365424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return base::android::ScopedJavaLocalRef<jobject>();
366424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  ProfileAndroid* profile_android = ProfileAndroid::FromProfile(profile);
367424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!profile_android)
368424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return base::android::ScopedJavaLocalRef<jobject>();
369424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
370424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return profile_android->GetJavaObject();
371424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
372424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
373a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TabAndroid::TabLoadStatus TabAndroid::LoadUrl(JNIEnv* env,
374a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jobject obj,
375a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jstring url,
376a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jstring j_extra_headers,
377a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jbyteArray j_post_data,
378a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jint page_transition,
379a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jstring j_referrer_url,
380cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                              jint referrer_policy,
381cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                              jboolean is_renderer_initiated) {
382a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  content::ContentViewCore* content_view = GetContentViewCore();
383a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!content_view)
384a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return PAGE_LOAD_FAILED;
385a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
386a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL gurl(base::android::ConvertJavaStringToUTF8(env, url));
387a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (gurl.is_empty())
388a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return PAGE_LOAD_FAILED;
389a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
390a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If the page was prerendered, use it.
391a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Note in incognito mode, we don't have a PrerenderManager.
392a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
393a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  prerender::PrerenderManager* prerender_manager =
394a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      prerender::PrerenderManagerFactory::GetForProfile(GetProfile());
395a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (prerender_manager) {
396a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    bool prefetched_page_loaded = HasPrerenderedUrl(gurl);
397a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Getting the load status before MaybeUsePrerenderedPage() b/c it resets.
398a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    chrome::NavigateParams params(NULL, web_contents());
399f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    InstantSearchPrerenderer* prerenderer =
400f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        InstantSearchPrerenderer::GetForProfile(GetProfile());
401f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (prerenderer) {
402f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      const base::string16& search_terms =
403f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          chrome::ExtractSearchTermsFromURL(GetProfile(), gurl);
404f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      if (!search_terms.empty() &&
405f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          prerenderer->CanCommitQuery(web_contents_.get(), search_terms)) {
406f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        prerenderer->Commit(search_terms);
407f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
408f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        if (prerenderer->UsePrerenderedPage(gurl, &params))
409f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          return FULL_PRERENDERED_PAGE_LOAD;
410f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      }
411f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      prerenderer->Cancel();
412f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
413a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (prerender_manager->MaybeUsePrerenderedPage(gurl, &params)) {
414a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return prefetched_page_loaded ?
415a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          FULL_PRERENDERED_PAGE_LOAD : PARTIAL_PRERENDERED_PAGE_LOAD;
416a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
417a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
418a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
419f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  GURL fixed_url(
420f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      url_fixer::FixupURL(gurl.possibly_invalid_spec(), std::string()));
421a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!fixed_url.is_valid())
422a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return PAGE_LOAD_FAILED;
423a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
424a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!HandleNonNavigationAboutURL(fixed_url)) {
425a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Notify the GoogleURLTracker of searches, it might want to change the
426a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // actual Google site used (for instance when in the UK, google.co.uk, when
427a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // in the US google.com).
428a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Note that this needs to happen before we initiate the navigation as the
429a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // GoogleURLTracker uses the navigation pending notification to trigger the
430a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // infobar.
431a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (google_util::IsGoogleSearchUrl(fixed_url) &&
432a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        (page_transition & content::PAGE_TRANSITION_GENERATED)) {
43346d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GoogleURLTracker* tracker =
43446d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          GoogleURLTrackerFactory::GetForProfile(GetProfile());
43546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      if (tracker)
43646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        tracker->SearchCommitted();
437a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
438a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
439a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Record UMA "ShowHistory" here. That way it'll pick up both user
440a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // typing chrome://history as well as selecting from the drop down menu.
441a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (fixed_url.spec() == chrome::kChromeUIHistoryURL) {
442a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      content::RecordAction(base::UserMetricsAction("ShowHistory"));
443a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
444a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
445a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::NavigationController::LoadURLParams load_params(fixed_url);
446a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (j_extra_headers) {
447a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.extra_headers = base::android::ConvertJavaStringToUTF8(
448a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          env,
449a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          j_extra_headers);
450a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
451a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (j_post_data) {
452a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.load_type =
453a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          content::NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST;
454a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      std::vector<uint8> post_data;
455a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::android::JavaByteArrayToByteVector(env, j_post_data, &post_data);
456a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.browser_initiated_post_data =
457a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::RefCountedBytes::TakeVector(&post_data);
458a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
459a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    load_params.transition_type =
460a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        content::PageTransitionFromInt(page_transition);
461a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (j_referrer_url) {
462a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.referrer = content::Referrer(
463a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          GURL(base::android::ConvertJavaStringToUTF8(env, j_referrer_url)),
464a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          static_cast<blink::WebReferrerPolicy>(referrer_policy));
465a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
4665c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    const base::string16 search_terms =
4675c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        chrome::ExtractSearchTermsFromURL(GetProfile(), gurl);
4685c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    SearchTabHelper* search_tab_helper =
4695c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        SearchTabHelper::FromWebContents(web_contents_.get());
4705c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    if (!search_terms.empty() && search_tab_helper &&
4715c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        search_tab_helper->SupportsInstant()) {
4725c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      search_tab_helper->Submit(search_terms);
4735c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      return DEFAULT_PAGE_LOAD;
4745c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    }
475cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    load_params.is_renderer_initiated = is_renderer_initiated;
476a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content_view->LoadUrl(load_params);
477a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
478a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return DEFAULT_PAGE_LOAD;
479a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
480a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
48158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)ToolbarModel::SecurityLevel TabAndroid::GetSecurityLevel(JNIEnv* env,
48258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                                         jobject obj) {
48358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return ToolbarModelImpl::GetSecurityLevelForWebContents(web_contents());
48458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
48558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
4864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void TabAndroid::SetActiveNavigationEntryTitleForUrl(JNIEnv* env,
4874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                     jobject obj,
4884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                     jstring jurl,
4894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                     jstring jtitle) {
4904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  DCHECK(web_contents());
4914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
492a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 title;
4934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (jtitle)
4944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    title = base::android::ConvertJavaStringToUTF16(env, jtitle);
4954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
4964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string url;
4974e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (jurl)
4984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    url = base::android::ConvertJavaStringToUTF8(env, jurl);
4994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  content::NavigationEntry* entry =
5014e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      web_contents()->GetController().GetVisibleEntry();
5024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (entry && url == entry->GetVirtualURL().spec())
5034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    entry->SetTitle(title);
5044e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
5054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
506f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool TabAndroid::Print(JNIEnv* env, jobject obj) {
507f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!web_contents())
508f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return false;
509f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
510f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  printing::PrintViewManagerBasic::CreateForWebContents(web_contents());
511f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  printing::PrintViewManagerBasic* print_view_manager =
512f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      printing::PrintViewManagerBasic::FromWebContents(web_contents());
513f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (print_view_manager == NULL)
514f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return false;
515f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
516f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  print_view_manager->PrintNow();
517f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return true;
518f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
519f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
520a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)prerender::PrerenderManager* TabAndroid::GetPrerenderManager() const {
521a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Profile* profile = GetProfile();
522a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!profile)
523a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return NULL;
524a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return prerender::PrerenderManagerFactory::GetForProfile(profile);
525a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
526a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static void Init(JNIEnv* env, jobject obj) {
5285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TRACE_EVENT0("native", "TabAndroid::Init");
5295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This will automatically bind to the Java object and pass ownership there.
5305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  new TabAndroid(env, obj);
5315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
5325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
533c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool TabAndroid::RegisterTabAndroid(JNIEnv* env) {
534c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return RegisterNativesImpl(env);
535c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
536