tab_android.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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"
156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/browser/favicon/favicon_tab_helper.h"
1646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)#include "chrome/browser/google/google_url_tracker_factory.h"
17c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "chrome/browser/infobars/infobar_service.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/prerender/prerender_contents.h"
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/prerender/prerender_manager.h"
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/prerender/prerender_manager_factory.h"
21f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/printing/print_view_manager_basic.h"
22424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
23424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/profiles/profile_android.h"
245c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/search/search.h"
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sessions/session_tab_helper.h"
2658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/sync/glue/synced_tab_delegate_android.h"
27116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/tab_contents/tab_util.h"
28f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/ui/android/content_settings/popup_blocked_infobar_delegate.h"
29a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/ui/android/context_menu_helper.h"
304e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/browser/ui/android/infobars/infobar_container_android.h"
3158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/ui/android/tab_model/tab_model.h"
3258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/android/window_android_helper.h"
34424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
35f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/ui/search/instant_search_prerenderer.h"
365c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/ui/search/search_tab_helper.h"
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/tab_helpers.h"
392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/common/url_constants.h"
41f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/google/core/browser/google_url_tracker.h"
426d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "components/google/core/browser/google_util.h"
430529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/infobars/core/infobar_container.h"
44f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/url_fixer/url_fixer.h"
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/android/content_view_core.h"
464e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "content/public/browser/navigation_entry.h"
4758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "content/public/browser/notification_service.h"
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/browser/user_metrics.h"
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "jni/Tab_jni.h"
516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "skia/ext/image_operations.h"
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "third_party/WebKit/public/platform/WebReferrerPolicy.h"
536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
54116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "ui/base/window_open_disposition.h"
556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "ui/gfx/android/device_display_info.h"
566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "ui/gfx/android/java_bitmap.h"
576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "ui/gfx/favicon_size.h"
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "ui/gfx/image/image_skia.h"
59116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
60116680a4aac90f2aa7413d9095a592090648e557Ben Murdochusing content::GlobalRequestID;
61116680a4aac90f2aa7413d9095a592090648e557Ben Murdochusing content::NavigationController;
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdochusing content::WebContents;
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace {
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
66116680a4aac90f2aa7413d9095a592090648e557Ben MurdochWebContents* CreateTargetContents(const chrome::NavigateParams& params,
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                  const GURL& url) {
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  Profile* profile = params.initiating_profile;
69116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (profile->IsOffTheRecord() || params.disposition == OFF_THE_RECORD) {
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    profile = profile->GetOffTheRecordProfile();
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  WebContents::CreateParams create_params(
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      profile, tab_util::GetSiteInstanceForNewTab(profile, url));
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (params.source_contents) {
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    create_params.initial_size =
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        params.source_contents->GetContainerBounds().size();
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (params.should_set_opener)
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      create_params.opener = params.source_contents;
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (params.disposition == NEW_BACKGROUND_TAB)
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    create_params.initially_hidden = true;
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  WebContents* target_contents = WebContents::Create(create_params);
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return target_contents;
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)bool MaybeSwapWithPrerender(const GURL& url, chrome::NavigateParams* params) {
905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  Profile* profile =
915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      Profile::FromBrowserContext(params->target_contents->GetBrowserContext());
925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  prerender::PrerenderManager* prerender_manager =
945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      prerender::PrerenderManagerFactory::GetForProfile(profile);
955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (!prerender_manager)
965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return false;
975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return prerender_manager->MaybeUsePrerenderedPage(url, params);
985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
100116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
102424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TabAndroid* TabAndroid::FromWebContents(content::WebContents* web_contents) {
103424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CoreTabHelper* core_tab_helper = CoreTabHelper::FromWebContents(web_contents);
104424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!core_tab_helper)
105424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
106424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
107424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CoreTabHelperDelegate* core_delegate = core_tab_helper->delegate();
108424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!core_delegate)
109424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
110424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
111424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return static_cast<TabAndroid*>(core_delegate);
112424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
113424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
114424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TabAndroid* TabAndroid::GetNativeTab(JNIEnv* env, jobject obj) {
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return reinterpret_cast<TabAndroid*>(Java_Tab_getNativePtr(env, obj));
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
11823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)void TabAndroid::AttachTabHelpers(content::WebContents* web_contents) {
11923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  DCHECK(web_contents);
12023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
12123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  TabHelpers::AttachTabHelpers(web_contents);
12223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
12323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
124424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TabAndroid::TabAndroid(JNIEnv* env, jobject obj)
125424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    : weak_java_tab_(env, obj),
12658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      synced_tab_delegate_(new browser_sync::SyncedTabDelegateAndroid(this)) {
127a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Java_Tab_setNativePtr(env, obj, reinterpret_cast<intptr_t>(this));
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TabAndroid::~TabAndroid() {
13158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1325f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  Java_Tab_clearNativePtr(env, weak_java_tab_.get(env).obj());
13358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
13458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetJavaObject() {
1365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return weak_java_tab_.get(env);
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1408bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)int TabAndroid::GetAndroidId() const {
1418bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1425f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return Java_Tab_getId(env, weak_java_tab_.get(env).obj());
1438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1450f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)int TabAndroid::GetSyncId() const {
1460f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1475f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return Java_Tab_getSyncId(env, weak_java_tab_.get(env).obj());
1480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
150a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 TabAndroid::GetTitle() const {
1518bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1528bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return base::android::ConvertJavaStringToUTF16(
1535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      Java_Tab_getTitle(env, weak_java_tab_.get(env).obj()));
1548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1568bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)GURL TabAndroid::GetURL() const {
1578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return GURL(base::android::ConvertJavaStringToUTF8(
1595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      Java_Tab_getUrl(env, weak_java_tab_.get(env).obj())));
1608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
162e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochbool TabAndroid::LoadIfNeeded() {
1638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return Java_Tab_loadIfNeeded(env, weak_java_tab_.get(env).obj());
1658bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
167424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)content::ContentViewCore* TabAndroid::GetContentViewCore() const {
168424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!web_contents())
169424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
170424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
171424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return content::ContentViewCore::FromWebContents(web_contents());
172424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
173424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
174424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)Profile* TabAndroid::GetProfile() const {
175424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!web_contents())
176424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
177424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
178424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return Profile::FromBrowserContext(web_contents()->GetBrowserContext());
179424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
180424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
18158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)browser_sync::SyncedTabDelegate* TabAndroid::GetSyncedTabDelegate() const {
18258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return synced_tab_delegate_.get();
1832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
185a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void TabAndroid::SetWindowSessionID(SessionID::id_type window_id) {
186a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  session_window_id_.set_id(window_id);
187a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!web_contents())
189a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SessionTabHelper* session_tab_helper =
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          SessionTabHelper::FromWebContents(web_contents());
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  session_tab_helper->SetWindowID(session_window_id_);
194a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1960f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void TabAndroid::SetSyncId(int sync_id) {
1970f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  Java_Tab_setSyncId(env, weak_java_tab_.get(env).obj(), sync_id);
1990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
2000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TabAndroid::HandlePopupNavigation(chrome::NavigateParams* params) {
202116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (params->disposition != SUPPRESS_OPEN &&
203116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      params->disposition != SAVE_TO_DISK &&
204116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      params->disposition != IGNORE_ACTION) {
205116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (!params->url.is_empty()) {
206116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      bool was_blocked = false;
207116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      GURL url(params->url);
208116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      if (params->disposition == CURRENT_TAB) {
2095f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        params->target_contents = web_contents_.get();
2105f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        if (!MaybeSwapWithPrerender(url, params)) {
2115f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          NavigationController::LoadURLParams load_url_params(url);
2125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          MakeLoadURLParams(params, &load_url_params);
2135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          params->target_contents->GetController().LoadURLWithParams(
2145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)              load_url_params);
2155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        }
216116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      } else {
217116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        params->target_contents = CreateTargetContents(*params, url);
2185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        NavigationController::LoadURLParams load_url_params(url);
2195f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        MakeLoadURLParams(params, &load_url_params);
220116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        params->target_contents->GetController().LoadURLWithParams(
221116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            load_url_params);
222116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        web_contents_delegate_->AddNewContents(params->source_contents,
223116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                               params->target_contents,
224116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                               params->disposition,
225116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                               params->window_bounds,
226116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                               params->user_gesture,
227116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                               &was_blocked);
228116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        if (was_blocked)
229116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          params->target_contents = NULL;
230116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      }
231116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    }
232116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
2335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool TabAndroid::ShouldWelcomePageLinkToTermsOfService() {
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NOTIMPLEMENTED();
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return false;
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
240a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool TabAndroid::HasPrerenderedUrl(GURL gurl) {
241a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  prerender::PrerenderManager* prerender_manager = GetPrerenderManager();
242a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!prerender_manager)
243a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
244a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
245a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::vector<content::WebContents*> contents =
246a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      prerender_manager->GetAllPrerenderingContents();
247a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  prerender::PrerenderContents* prerender_contents;
248a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (size_t i = 0; i < contents.size(); ++i) {
249a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    prerender_contents = prerender_manager->
250a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        GetPrerenderContents(contents.at(i));
251a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (prerender_contents->prerender_url() == gurl &&
252a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        prerender_contents->has_finished_loading()) {
253a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return true;
254a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
255a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
256a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return false;
257a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
258a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
259116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid TabAndroid::MakeLoadURLParams(
260116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    chrome::NavigateParams* params,
261116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    NavigationController::LoadURLParams* load_url_params) {
262116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  load_url_params->referrer = params->referrer;
263116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  load_url_params->frame_tree_node_id = params->frame_tree_node_id;
264116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  load_url_params->redirect_chain = params->redirect_chain;
265116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  load_url_params->transition_type = params->transition;
266116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  load_url_params->extra_headers = params->extra_headers;
267116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  load_url_params->should_replace_current_entry =
268116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      params->should_replace_current_entry;
269116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
270116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (params->transferred_global_request_id != GlobalRequestID()) {
271116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    load_url_params->transferred_global_request_id =
272116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        params->transferred_global_request_id;
273116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
274116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  load_url_params->is_renderer_initiated = params->is_renderer_initiated;
275116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
276116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Only allows the browser-initiated navigation to use POST.
277116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (params->uses_post && !params->is_renderer_initiated) {
278116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    load_url_params->load_type =
279116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST;
280116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    load_url_params->browser_initiated_post_data =
281116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        params->browser_initiated_post_data;
282116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
283116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
284116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
285424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void TabAndroid::SwapTabContents(content::WebContents* old_contents,
2865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 content::WebContents* new_contents,
2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 bool did_start_load,
2885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 bool did_finish_load) {
289424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
2904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // We need to notify the native InfobarContainer so infobars can be swapped.
2924e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  InfoBarContainerAndroid* infobar_container =
2934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      reinterpret_cast<InfoBarContainerAndroid*>(
294a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          Java_Tab_getNativeInfoBarContainer(
2954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              env,
2964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              weak_java_tab_.get(env).obj()));
297a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  InfoBarService* new_infobar_service =
298a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      new_contents ? InfoBarService::FromWebContents(new_contents) : NULL;
299a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  infobar_container->ChangeInfoBarManager(new_infobar_service);
3004e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
301a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Java_Tab_swapWebContents(
302424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      env,
303424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      weak_java_tab_.get(env).obj(),
3045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      reinterpret_cast<intptr_t>(new_contents),
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      did_start_load,
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      did_finish_load);
307424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
308424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
3091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid TabAndroid::OnWebContentsInstantSupportDisabled(
3101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const content::WebContents* contents) {
3111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(contents);
3121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (web_contents() != contents)
3131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return;
3141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  JNIEnv* env = base::android::AttachCurrentThread();
3161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  Java_Tab_onWebContentsInstantSupportDisabled(env,
3171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                               weak_java_tab_.get(env).obj());
3181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
3191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
32058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void TabAndroid::Observe(int type,
32158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                         const content::NotificationSource& source,
32258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                         const content::NotificationDetails& details) {
32358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
32458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  switch (type) {
32558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    case chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED: {
32658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      TabSpecificContentSettings* settings =
32758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          TabSpecificContentSettings::FromWebContents(web_contents());
32858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      if (!settings->IsBlockageIndicated(CONTENT_SETTINGS_TYPE_POPUPS)) {
32958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // TODO(dfalcantara): Create an InfoBarDelegate to keep the
33058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // PopupBlockedInfoBar logic native-side instead of straddling the JNI
33158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // boundary.
33258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        int num_popups = 0;
33358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        PopupBlockerTabHelper* popup_blocker_helper =
33458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            PopupBlockerTabHelper::FromWebContents(web_contents());
33558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        if (popup_blocker_helper)
33658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          num_popups = popup_blocker_helper->GetBlockedPopupsCount();
33758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (num_popups > 0)
3395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          PopupBlockedInfoBarDelegate::Create(web_contents(), num_popups);
340f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
34158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        settings->SetBlockageHasBeenIndicated(CONTENT_SETTINGS_TYPE_POPUPS);
34258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      }
34358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
34458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
34558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    case chrome::NOTIFICATION_FAVICON_UPDATED:
346a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      Java_Tab_onFaviconUpdated(env, weak_java_tab_.get(env).obj());
34758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
3485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    case content::NOTIFICATION_NAV_ENTRY_CHANGED:
349a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      Java_Tab_onNavEntryChanged(env, weak_java_tab_.get(env).obj());
3505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      break;
35158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    default:
35258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NOTREACHED() << "Unexpected notification " << type;
35358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
35458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
35558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
35658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TabAndroid::Destroy(JNIEnv* env, jobject obj) {
3585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  delete this;
3595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
361424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void TabAndroid::InitWebContents(JNIEnv* env,
362424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                 jobject obj,
363424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                 jboolean incognito,
364424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                 jobject jcontent_view_core,
365a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                 jobject jweb_contents_delegate,
366a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                 jobject jcontext_menu_populator) {
367424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  content::ContentViewCore* content_view_core =
368424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      content::ContentViewCore::GetNativeContentViewCore(env,
369424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                         jcontent_view_core);
370424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(content_view_core);
371424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(content_view_core->GetWebContents());
372424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
373424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents_.reset(content_view_core->GetWebContents());
37423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  AttachTabHelpers(web_contents_.get());
375424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
376a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetWindowSessionID(session_window_id_.id());
377a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
378424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  session_tab_id_.set_id(
379424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      SessionTabHelper::FromWebContents(web_contents())->session_id().id());
380a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ContextMenuHelper::FromWebContents(web_contents())->SetPopulator(
381a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      jcontext_menu_populator);
382424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  WindowAndroidHelper::FromWebContents(web_contents())->
383424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      SetWindowAndroid(content_view_core->GetWindowAndroid());
384424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CoreTabHelper::FromWebContents(web_contents())->set_delegate(this);
3851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  SearchTabHelper::FromWebContents(web_contents())->set_delegate(this);
386424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents_delegate_.reset(
387424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      new chrome::android::ChromeWebContentsDelegateAndroid(
388424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          env, jweb_contents_delegate));
389424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents_delegate_->LoadProgressChanged(web_contents(), 0);
390424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents()->SetDelegate(web_contents_delegate_.get());
391424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
39258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Add(
39358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
39458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
39558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
39658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Add(
39758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
39858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_FAVICON_UPDATED,
39958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
4005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  notification_registrar_.Add(
4015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this,
4025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      content::NOTIFICATION_NAV_ENTRY_CHANGED,
4035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      content::Source<content::NavigationController>(
4045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           &web_contents()->GetController()));
40558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
40658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  synced_tab_delegate_->SetWebContents(web_contents());
40758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
408424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Verify that the WebContents this tab represents matches the expected
409424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // off the record state.
410424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CHECK_EQ(GetProfile()->IsOffTheRecord(), incognito);
411424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
412424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
413424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void TabAndroid::DestroyWebContents(JNIEnv* env,
414424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                    jobject obj,
415424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                    jboolean delete_native) {
41658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK(web_contents());
41758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
41858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Remove(
41958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
42058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
42158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
42258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Remove(
42358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
42458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_FAVICON_UPDATED,
42558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
42623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  notification_registrar_.Remove(
42723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      this,
42823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      content::NOTIFICATION_NAV_ENTRY_CHANGED,
42923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      content::Source<content::NavigationController>(
43023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)           &web_contents()->GetController()));
43158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
432424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents()->SetDelegate(NULL);
433424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
434424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (delete_native) {
435424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    web_contents_.reset();
43658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    synced_tab_delegate_->ResetWebContents();
437424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  } else {
438424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Release the WebContents so it does not get deleted by the scoped_ptr.
439424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ignore_result(web_contents_.release());
440424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
441424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
442424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
443424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetProfileAndroid(
444424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    JNIEnv* env,
445424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    jobject obj) {
446424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  Profile* profile = GetProfile();
447424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!profile)
448424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return base::android::ScopedJavaLocalRef<jobject>();
449424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  ProfileAndroid* profile_android = ProfileAndroid::FromProfile(profile);
450424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!profile_android)
451424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return base::android::ScopedJavaLocalRef<jobject>();
452424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
453424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return profile_android->GetJavaObject();
454424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
455424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
456a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TabAndroid::TabLoadStatus TabAndroid::LoadUrl(JNIEnv* env,
457a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jobject obj,
458a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jstring url,
459a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jstring j_extra_headers,
460a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jbyteArray j_post_data,
461a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jint page_transition,
462a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jstring j_referrer_url,
463cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                              jint referrer_policy,
464cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                              jboolean is_renderer_initiated) {
4651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!web_contents())
466a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return PAGE_LOAD_FAILED;
467a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
468a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL gurl(base::android::ConvertJavaStringToUTF8(env, url));
469a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (gurl.is_empty())
470a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return PAGE_LOAD_FAILED;
471a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
472a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If the page was prerendered, use it.
473a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Note in incognito mode, we don't have a PrerenderManager.
474a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
475a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  prerender::PrerenderManager* prerender_manager =
476a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      prerender::PrerenderManagerFactory::GetForProfile(GetProfile());
477a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (prerender_manager) {
478a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    bool prefetched_page_loaded = HasPrerenderedUrl(gurl);
479a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Getting the load status before MaybeUsePrerenderedPage() b/c it resets.
480a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    chrome::NavigateParams params(NULL, web_contents());
481f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    InstantSearchPrerenderer* prerenderer =
482f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        InstantSearchPrerenderer::GetForProfile(GetProfile());
483f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (prerenderer) {
484f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      const base::string16& search_terms =
485f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          chrome::ExtractSearchTermsFromURL(GetProfile(), gurl);
486f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      if (!search_terms.empty() &&
487f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          prerenderer->CanCommitQuery(web_contents_.get(), search_terms)) {
488f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        prerenderer->Commit(search_terms);
489f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
490f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        if (prerenderer->UsePrerenderedPage(gurl, &params))
491f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          return FULL_PRERENDERED_PAGE_LOAD;
492f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      }
493f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      prerenderer->Cancel();
494f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
495a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (prerender_manager->MaybeUsePrerenderedPage(gurl, &params)) {
496a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return prefetched_page_loaded ?
497a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          FULL_PRERENDERED_PAGE_LOAD : PARTIAL_PRERENDERED_PAGE_LOAD;
498a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
499a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
500a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
501f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  GURL fixed_url(
502f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      url_fixer::FixupURL(gurl.possibly_invalid_spec(), std::string()));
503a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!fixed_url.is_valid())
504a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return PAGE_LOAD_FAILED;
505a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
506a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!HandleNonNavigationAboutURL(fixed_url)) {
507a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Notify the GoogleURLTracker of searches, it might want to change the
508a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // actual Google site used (for instance when in the UK, google.co.uk, when
509a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // in the US google.com).
510a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Note that this needs to happen before we initiate the navigation as the
511a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // GoogleURLTracker uses the navigation pending notification to trigger the
512a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // infobar.
513a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (google_util::IsGoogleSearchUrl(fixed_url) &&
5141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        (page_transition & ui::PAGE_TRANSITION_GENERATED)) {
51546d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GoogleURLTracker* tracker =
51646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          GoogleURLTrackerFactory::GetForProfile(GetProfile());
51746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      if (tracker)
51846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        tracker->SearchCommitted();
519a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
520a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
521a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Record UMA "ShowHistory" here. That way it'll pick up both user
522a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // typing chrome://history as well as selecting from the drop down menu.
523a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (fixed_url.spec() == chrome::kChromeUIHistoryURL) {
524a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      content::RecordAction(base::UserMetricsAction("ShowHistory"));
525a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
526a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
527a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::NavigationController::LoadURLParams load_params(fixed_url);
528a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (j_extra_headers) {
529a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.extra_headers = base::android::ConvertJavaStringToUTF8(
530a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          env,
531a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          j_extra_headers);
532a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
533a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (j_post_data) {
534a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.load_type =
535a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          content::NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST;
536a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      std::vector<uint8> post_data;
537a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::android::JavaByteArrayToByteVector(env, j_post_data, &post_data);
538a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.browser_initiated_post_data =
539a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::RefCountedBytes::TakeVector(&post_data);
540a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
541a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    load_params.transition_type =
5421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        ui::PageTransitionFromInt(page_transition);
543a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (j_referrer_url) {
544a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.referrer = content::Referrer(
545a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          GURL(base::android::ConvertJavaStringToUTF8(env, j_referrer_url)),
546a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          static_cast<blink::WebReferrerPolicy>(referrer_policy));
547a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
5485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    const base::string16 search_terms =
5495c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        chrome::ExtractSearchTermsFromURL(GetProfile(), gurl);
5505c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    SearchTabHelper* search_tab_helper =
5515c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        SearchTabHelper::FromWebContents(web_contents_.get());
5525c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    if (!search_terms.empty() && search_tab_helper &&
5535c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        search_tab_helper->SupportsInstant()) {
5545c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      search_tab_helper->Submit(search_terms);
5555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      return DEFAULT_PAGE_LOAD;
5565c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    }
557cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    load_params.is_renderer_initiated = is_renderer_initiated;
5581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    web_contents()->GetController().LoadURLWithParams(load_params);
559a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
560a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return DEFAULT_PAGE_LOAD;
561a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
562a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
56358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)ToolbarModel::SecurityLevel TabAndroid::GetSecurityLevel(JNIEnv* env,
56458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                                         jobject obj) {
56558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return ToolbarModelImpl::GetSecurityLevelForWebContents(web_contents());
56658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
56758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
5684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void TabAndroid::SetActiveNavigationEntryTitleForUrl(JNIEnv* env,
5694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                     jobject obj,
5704e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                     jstring jurl,
5714e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                     jstring jtitle) {
5724e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  DCHECK(web_contents());
5734e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
574a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 title;
5754e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (jtitle)
5764e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    title = base::android::ConvertJavaStringToUTF16(env, jtitle);
5774e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5784e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string url;
5794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (jurl)
5804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    url = base::android::ConvertJavaStringToUTF8(env, jurl);
5814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
5824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  content::NavigationEntry* entry =
5834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      web_contents()->GetController().GetVisibleEntry();
5844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (entry && url == entry->GetVirtualURL().spec())
5854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    entry->SetTitle(title);
5864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
5874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
588f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool TabAndroid::Print(JNIEnv* env, jobject obj) {
589f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!web_contents())
590f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return false;
591f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
592f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  printing::PrintViewManagerBasic::CreateForWebContents(web_contents());
593f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  printing::PrintViewManagerBasic* print_view_manager =
594f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      printing::PrintViewManagerBasic::FromWebContents(web_contents());
595f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (print_view_manager == NULL)
596f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return false;
597f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
598f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  print_view_manager->PrintNow();
599f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return true;
600f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
601f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
6026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)ScopedJavaLocalRef<jobject> TabAndroid::GetFavicon(JNIEnv* env, jobject obj) {
6036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  ScopedJavaLocalRef<jobject> bitmap;
6046e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  FaviconTabHelper* favicon_tab_helper =
6056e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      FaviconTabHelper::FromWebContents(web_contents_.get());
6066e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6076e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!favicon_tab_helper)
6086e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return bitmap;
6091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // If the favicon isn't valid, it will return a default bitmap.
6116e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6126e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  SkBitmap favicon =
6136e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      favicon_tab_helper->GetFavicon()
6146e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          .AsImageSkia()
6156e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          .GetRepresentation(
6166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)               ResourceBundle::GetSharedInstance().GetMaxScaleFactor())
6176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          .sk_bitmap();
6186e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (favicon.empty()) {
6206e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    favicon = favicon_tab_helper->GetFavicon().AsBitmap();
6216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
6226e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!favicon.empty()) {
6246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    gfx::DeviceDisplayInfo device_info;
6256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    const float device_scale_factor = device_info.GetDIPScale();
6266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    int target_size_dip = device_scale_factor * gfx::kFaviconSize;
6276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if (favicon.width() != target_size_dip ||
6286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        favicon.height() != target_size_dip) {
6296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      favicon =
6306e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          skia::ImageOperations::Resize(favicon,
6316e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                        skia::ImageOperations::RESIZE_BEST,
6326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                        target_size_dip,
6336e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                        target_size_dip);
6346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    }
6356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6366e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    bitmap = gfx::ConvertToJavaBitmap(&favicon);
6376e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
6386e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return bitmap;
6396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
6406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccijboolean TabAndroid::IsFaviconValid(JNIEnv* env, jobject jobj) {
6421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return web_contents() &&
6431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      FaviconTabHelper::FromWebContents(web_contents())->FaviconIsValid();
6441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
6451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
646a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)prerender::PrerenderManager* TabAndroid::GetPrerenderManager() const {
647a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Profile* profile = GetProfile();
648a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!profile)
649a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return NULL;
650a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return prerender::PrerenderManagerFactory::GetForProfile(profile);
651a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
652a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
6535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static void Init(JNIEnv* env, jobject obj) {
6545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TRACE_EVENT0("native", "TabAndroid::Init");
6555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This will automatically bind to the Java object and pass ownership there.
6565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  new TabAndroid(env, obj);
6575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
6585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
659c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool TabAndroid::RegisterTabAndroid(JNIEnv* env) {
660c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return RegisterNativesImpl(env);
661c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
662