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"
2429b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch#include "chrome/browser/search/instant_service.h"
2529b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch#include "chrome/browser/search/instant_service_factory.h"
265c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/search/search.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/sessions/session_tab_helper.h"
2858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/sync/glue/synced_tab_delegate_android.h"
29116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/tab_contents/tab_util.h"
30f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "chrome/browser/ui/android/content_settings/popup_blocked_infobar_delegate.h"
31a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)#include "chrome/browser/ui/android/context_menu_helper.h"
324e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "chrome/browser/ui/android/infobars/infobar_container_android.h"
3358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/ui/android/tab_model/tab_model.h"
3458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/android/window_android_helper.h"
36424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)#include "chrome/browser/ui/blocked_content/popup_blocker_tab_helper.h"
37f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/ui/search/instant_search_prerenderer.h"
385c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu#include "chrome/browser/ui/search/search_tab_helper.h"
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/tab_contents/core_tab_helper.h"
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/tab_helpers.h"
412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/ui/toolbar/toolbar_model_impl.h"
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/common/url_constants.h"
43f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/google/core/browser/google_url_tracker.h"
446d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "components/google/core/browser/google_util.h"
450529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "components/infobars/core/infobar_container.h"
46f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "components/url_fixer/url_fixer.h"
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/android/content_view_core.h"
484e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)#include "content/public/browser/navigation_entry.h"
4958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "content/public/browser/notification_service.h"
5029b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch#include "content/public/browser/render_process_host.h"
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/browser/user_metrics.h"
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/public/browser/web_contents.h"
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "jni/Tab_jni.h"
546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "skia/ext/image_operations.h"
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "third_party/WebKit/public/platform/WebReferrerPolicy.h"
566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h"
57116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "ui/base/window_open_disposition.h"
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "ui/gfx/android/device_display_info.h"
596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "ui/gfx/android/java_bitmap.h"
606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "ui/gfx/favicon_size.h"
616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "ui/gfx/image/image_skia.h"
62116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
63116680a4aac90f2aa7413d9095a592090648e557Ben Murdochusing content::GlobalRequestID;
64116680a4aac90f2aa7413d9095a592090648e557Ben Murdochusing content::NavigationController;
65116680a4aac90f2aa7413d9095a592090648e557Ben Murdochusing content::WebContents;
66116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
67116680a4aac90f2aa7413d9095a592090648e557Ben Murdochnamespace {
68116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
69116680a4aac90f2aa7413d9095a592090648e557Ben MurdochWebContents* CreateTargetContents(const chrome::NavigateParams& params,
70116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                  const GURL& url) {
71116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  Profile* profile = params.initiating_profile;
72116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
73116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (profile->IsOffTheRecord() || params.disposition == OFF_THE_RECORD) {
74116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    profile = profile->GetOffTheRecordProfile();
75116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
76116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  WebContents::CreateParams create_params(
77116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      profile, tab_util::GetSiteInstanceForNewTab(profile, url));
78116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (params.source_contents) {
79116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    create_params.initial_size =
80116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        params.source_contents->GetContainerBounds().size();
81116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (params.should_set_opener)
82116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      create_params.opener = params.source_contents;
83116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
84116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (params.disposition == NEW_BACKGROUND_TAB)
85116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    create_params.initially_hidden = true;
86116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
87116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  WebContents* target_contents = WebContents::Create(create_params);
88116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
89116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  return target_contents;
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
925f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)bool MaybeSwapWithPrerender(const GURL& url, chrome::NavigateParams* params) {
935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  Profile* profile =
945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      Profile::FromBrowserContext(params->target_contents->GetBrowserContext());
955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  prerender::PrerenderManager* prerender_manager =
975f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      prerender::PrerenderManagerFactory::GetForProfile(profile);
985f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  if (!prerender_manager)
995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    return false;
1005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return prerender_manager->MaybeUsePrerenderedPage(url, params);
1015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)}
1025f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
103116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}  // namespace
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TabAndroid* TabAndroid::FromWebContents(content::WebContents* web_contents) {
106424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CoreTabHelper* core_tab_helper = CoreTabHelper::FromWebContents(web_contents);
107424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!core_tab_helper)
108424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
109424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
110424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CoreTabHelperDelegate* core_delegate = core_tab_helper->delegate();
111424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!core_delegate)
112424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
113424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
114424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return static_cast<TabAndroid*>(core_delegate);
115424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
116424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
117424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TabAndroid* TabAndroid::GetNativeTab(JNIEnv* env, jobject obj) {
118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return reinterpret_cast<TabAndroid*>(Java_Tab_getNativePtr(env, obj));
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
12123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)void TabAndroid::AttachTabHelpers(content::WebContents* web_contents) {
12223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  DCHECK(web_contents);
12323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
12423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  TabHelpers::AttachTabHelpers(web_contents);
12523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)}
12623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)
127424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)TabAndroid::TabAndroid(JNIEnv* env, jobject obj)
128424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    : weak_java_tab_(env, obj),
12958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      synced_tab_delegate_(new browser_sync::SyncedTabDelegateAndroid(this)) {
130a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Java_Tab_setNativePtr(env, obj, reinterpret_cast<intptr_t>(this));
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
13358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)TabAndroid::~TabAndroid() {
13458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  Java_Tab_clearNativePtr(env, weak_java_tab_.get(env).obj());
13658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
13758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
1385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetJavaObject() {
1395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return weak_java_tab_.get(env);
1415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
1425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
1438bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)int TabAndroid::GetAndroidId() const {
1448bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1455f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return Java_Tab_getId(env, weak_java_tab_.get(env).obj());
1468bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1480f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)int TabAndroid::GetSyncId() const {
1490f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1505f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return Java_Tab_getSyncId(env, weak_java_tab_.get(env).obj());
1510f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
1520f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
153a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)base::string16 TabAndroid::GetTitle() const {
1548bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1558bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return base::android::ConvertJavaStringToUTF16(
1565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      Java_Tab_getTitle(env, weak_java_tab_.get(env).obj()));
1578bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1588bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
1598bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)GURL TabAndroid::GetURL() const {
1608bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1618bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  return GURL(base::android::ConvertJavaStringToUTF8(
1625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      Java_Tab_getUrl(env, weak_java_tab_.get(env).obj())));
1638bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1648bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
165e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdochbool TabAndroid::LoadIfNeeded() {
1668bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
1675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  return Java_Tab_loadIfNeeded(env, weak_java_tab_.get(env).obj());
1688bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)}
1698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
170424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)content::ContentViewCore* TabAndroid::GetContentViewCore() const {
171424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!web_contents())
172424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
173424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
174424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return content::ContentViewCore::FromWebContents(web_contents());
175424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
176424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
177424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)Profile* TabAndroid::GetProfile() const {
178424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!web_contents())
179424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return NULL;
180424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
181424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return Profile::FromBrowserContext(web_contents()->GetBrowserContext());
182424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
183424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
18458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)browser_sync::SyncedTabDelegate* TabAndroid::GetSyncedTabDelegate() const {
18558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return synced_tab_delegate_.get();
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void TabAndroid::SetWindowSessionID(SessionID::id_type window_id) {
189a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  session_window_id_.set_id(window_id);
190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!web_contents())
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
194a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SessionTabHelper* session_tab_helper =
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          SessionTabHelper::FromWebContents(web_contents());
196a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  session_tab_helper->SetWindowID(session_window_id_);
197a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1990f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)void TabAndroid::SetSyncId(int sync_id) {
2000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
2015f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  Java_Tab_setSyncId(env, weak_java_tab_.get(env).obj(), sync_id);
2020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)}
2030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TabAndroid::HandlePopupNavigation(chrome::NavigateParams* params) {
205116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (params->disposition != SUPPRESS_OPEN &&
206116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      params->disposition != SAVE_TO_DISK &&
207116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      params->disposition != IGNORE_ACTION) {
208116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    if (!params->url.is_empty()) {
209116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      bool was_blocked = false;
210116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      GURL url(params->url);
211116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      if (params->disposition == CURRENT_TAB) {
2125f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        params->target_contents = web_contents_.get();
2135f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        if (!MaybeSwapWithPrerender(url, params)) {
2145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          NavigationController::LoadURLParams load_url_params(url);
2155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          MakeLoadURLParams(params, &load_url_params);
2165f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          params->target_contents->GetController().LoadURLWithParams(
2175f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)              load_url_params);
2185f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        }
219116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      } else {
220116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        params->target_contents = CreateTargetContents(*params, url);
2215f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        NavigationController::LoadURLParams load_url_params(url);
2225f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)        MakeLoadURLParams(params, &load_url_params);
223116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        params->target_contents->GetController().LoadURLWithParams(
224116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch            load_url_params);
225116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        web_contents_delegate_->AddNewContents(params->source_contents,
226116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                               params->target_contents,
227116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                               params->disposition,
228116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                               params->window_bounds,
229116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                               params->user_gesture,
230116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                                               &was_blocked);
231116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        if (was_blocked)
232116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch          params->target_contents = NULL;
233116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      }
234116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    }
235116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
2365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)bool TabAndroid::ShouldWelcomePageLinkToTermsOfService() {
2395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  NOTIMPLEMENTED();
2405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  return false;
2415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
243a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)bool TabAndroid::HasPrerenderedUrl(GURL gurl) {
244a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  prerender::PrerenderManager* prerender_manager = GetPrerenderManager();
245a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!prerender_manager)
246a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return false;
247a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
248a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::vector<content::WebContents*> contents =
249a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      prerender_manager->GetAllPrerenderingContents();
250a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  prerender::PrerenderContents* prerender_contents;
251a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  for (size_t i = 0; i < contents.size(); ++i) {
252a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    prerender_contents = prerender_manager->
253a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        GetPrerenderContents(contents.at(i));
254a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (prerender_contents->prerender_url() == gurl &&
255a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        prerender_contents->has_finished_loading()) {
256a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return true;
257a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
258a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
259a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return false;
260a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
261a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
262116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid TabAndroid::MakeLoadURLParams(
263116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    chrome::NavigateParams* params,
264116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    NavigationController::LoadURLParams* load_url_params) {
265116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  load_url_params->referrer = params->referrer;
266116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  load_url_params->frame_tree_node_id = params->frame_tree_node_id;
267116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  load_url_params->redirect_chain = params->redirect_chain;
268116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  load_url_params->transition_type = params->transition;
269116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  load_url_params->extra_headers = params->extra_headers;
270116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  load_url_params->should_replace_current_entry =
271116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      params->should_replace_current_entry;
272116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
273116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (params->transferred_global_request_id != GlobalRequestID()) {
274116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    load_url_params->transferred_global_request_id =
275116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        params->transferred_global_request_id;
276116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
277116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  load_url_params->is_renderer_initiated = params->is_renderer_initiated;
278116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
279116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // Only allows the browser-initiated navigation to use POST.
280116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  if (params->uses_post && !params->is_renderer_initiated) {
281116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    load_url_params->load_type =
282116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST;
283116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    load_url_params->browser_initiated_post_data =
284116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch        params->browser_initiated_post_data;
285116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  }
286116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch}
287116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
288424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void TabAndroid::SwapTabContents(content::WebContents* old_contents,
2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 content::WebContents* new_contents,
2905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 bool did_start_load,
2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 bool did_finish_load) {
292424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
2934e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
2944e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // We need to notify the native InfobarContainer so infobars can be swapped.
2954e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  InfoBarContainerAndroid* infobar_container =
2964e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      reinterpret_cast<InfoBarContainerAndroid*>(
297a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          Java_Tab_getNativeInfoBarContainer(
2984e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              env,
2994e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)              weak_java_tab_.get(env).obj()));
300a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  InfoBarService* new_infobar_service =
301a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch      new_contents ? InfoBarService::FromWebContents(new_contents) : NULL;
302a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch  infobar_container->ChangeInfoBarManager(new_infobar_service);
3034e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
304a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Java_Tab_swapWebContents(
305424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      env,
306424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      weak_java_tab_.get(env).obj(),
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      reinterpret_cast<intptr_t>(new_contents),
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      did_start_load,
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      did_finish_load);
310424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
311424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
31229b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdochvoid TabAndroid::DefaultSearchProviderChanged() {
31329b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  // TODO(kmadhusu): Move this function definition to a common place and update
31429b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  // BrowserInstantController::DefaultSearchProviderChanged to use the same.
31529b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  if (!web_contents())
31629b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch    return;
31729b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch
31829b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  InstantService* instant_service =
31929b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch      InstantServiceFactory::GetForProfile(GetProfile());
32029b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  if (!instant_service)
32129b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch    return;
32229b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch
32329b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  // Send new search URLs to the renderer.
32429b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  content::RenderProcessHost* rph = web_contents()->GetRenderProcessHost();
32529b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  instant_service->SendSearchURLsToRenderer(rph);
32629b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch
32729b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  // Reload the contents to ensure that it gets assigned to a non-previledged
32829b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  // renderer.
32929b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  if (!instant_service->IsInstantProcess(rph->GetID()))
33029b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch    return;
33129b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  web_contents()->GetController().Reload(false);
33229b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch
33329b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  // As the reload was not triggered by the user we don't want to close any
33429b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  // infobars. We have to tell the InfoBarService after the reload, otherwise it
33529b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  // would ignore this call when
33629b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  // WebContentsObserver::DidStartNavigationToPendingEntry is invoked.
33729b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  InfoBarService::FromWebContents(web_contents())->set_ignore_next_reload();
33829b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch}
33929b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch
3401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid TabAndroid::OnWebContentsInstantSupportDisabled(
3411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const content::WebContents* contents) {
3421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(contents);
3431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (web_contents() != contents)
3441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    return;
3451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
3461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  JNIEnv* env = base::android::AttachCurrentThread();
3471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  Java_Tab_onWebContentsInstantSupportDisabled(env,
3481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                               weak_java_tab_.get(env).obj());
3491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
3501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
35158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)void TabAndroid::Observe(int type,
35258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                         const content::NotificationSource& source,
35358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                         const content::NotificationDetails& details) {
35458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  JNIEnv* env = base::android::AttachCurrentThread();
35558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  switch (type) {
35658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    case chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED: {
35758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      TabSpecificContentSettings* settings =
35858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          TabSpecificContentSettings::FromWebContents(web_contents());
35958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      if (!settings->IsBlockageIndicated(CONTENT_SETTINGS_TYPE_POPUPS)) {
36058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // TODO(dfalcantara): Create an InfoBarDelegate to keep the
36158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // PopupBlockedInfoBar logic native-side instead of straddling the JNI
36258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        // boundary.
36358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        int num_popups = 0;
36458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        PopupBlockerTabHelper* popup_blocker_helper =
36558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)            PopupBlockerTabHelper::FromWebContents(web_contents());
36658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        if (popup_blocker_helper)
36758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)          num_popups = popup_blocker_helper->GetBlockedPopupsCount();
36858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        if (num_popups > 0)
3705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          PopupBlockedInfoBarDelegate::Create(web_contents(), num_popups);
371f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
37258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)        settings->SetBlockageHasBeenIndicated(CONTENT_SETTINGS_TYPE_POPUPS);
37358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      }
37458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
37558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    }
37658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    case chrome::NOTIFICATION_FAVICON_UPDATED:
377a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      Java_Tab_onFaviconUpdated(env, weak_java_tab_.get(env).obj());
37858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
3795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    case content::NOTIFICATION_NAV_ENTRY_CHANGED:
380a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      Java_Tab_onNavEntryChanged(env, weak_java_tab_.get(env).obj());
3815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      break;
38258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    default:
38358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      NOTREACHED() << "Unexpected notification " << type;
38458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      break;
38558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  }
38658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
38758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
3885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void TabAndroid::Destroy(JNIEnv* env, jobject obj) {
3895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  delete this;
3905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
392424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void TabAndroid::InitWebContents(JNIEnv* env,
393424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                 jobject obj,
394424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                 jboolean incognito,
395424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                 jobject jcontent_view_core,
396a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                 jobject jweb_contents_delegate,
397a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                 jobject jcontext_menu_populator) {
398424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  content::ContentViewCore* content_view_core =
399424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      content::ContentViewCore::GetNativeContentViewCore(env,
400424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                                         jcontent_view_core);
401424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(content_view_core);
402424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  DCHECK(content_view_core->GetWebContents());
403424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
404424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents_.reset(content_view_core->GetWebContents());
40523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  AttachTabHelpers(web_contents_.get());
406424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
407a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  SetWindowSessionID(session_window_id_.id());
408a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
409424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  session_tab_id_.set_id(
410424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      SessionTabHelper::FromWebContents(web_contents())->session_id().id());
411a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  ContextMenuHelper::FromWebContents(web_contents())->SetPopulator(
412a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      jcontext_menu_populator);
413424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  WindowAndroidHelper::FromWebContents(web_contents())->
414424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      SetWindowAndroid(content_view_core->GetWindowAndroid());
415424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CoreTabHelper::FromWebContents(web_contents())->set_delegate(this);
4161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  SearchTabHelper::FromWebContents(web_contents())->set_delegate(this);
417424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents_delegate_.reset(
418424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)      new chrome::android::ChromeWebContentsDelegateAndroid(
419424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)          env, jweb_contents_delegate));
420424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents_delegate_->LoadProgressChanged(web_contents(), 0);
421424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents()->SetDelegate(web_contents_delegate_.get());
422424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
42358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Add(
42458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
42558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
42658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
42758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Add(
42858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
42958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_FAVICON_UPDATED,
43058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  notification_registrar_.Add(
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      this,
4335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      content::NOTIFICATION_NAV_ENTRY_CHANGED,
4345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      content::Source<content::NavigationController>(
4355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)           &web_contents()->GetController()));
43658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
43758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  synced_tab_delegate_->SetWebContents(web_contents());
43858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
439424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // Verify that the WebContents this tab represents matches the expected
440424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  // off the record state.
441424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  CHECK_EQ(GetProfile()->IsOffTheRecord(), incognito);
44229b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch
44329b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  InstantService* instant_service =
44429b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch      InstantServiceFactory::GetForProfile(GetProfile());
44529b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  if (instant_service)
44629b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch    instant_service->AddObserver(this);
447424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
448424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
449424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)void TabAndroid::DestroyWebContents(JNIEnv* env,
450424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                    jobject obj,
451424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)                                    jboolean delete_native) {
45258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  DCHECK(web_contents());
45358537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
45458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Remove(
45558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
45658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_WEB_CONTENT_SETTINGS_CHANGED,
45758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
45858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  notification_registrar_.Remove(
45958537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      this,
46058537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      chrome::NOTIFICATION_FAVICON_UPDATED,
46158537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)      content::Source<content::WebContents>(web_contents()));
46223730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)  notification_registrar_.Remove(
46323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      this,
46423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      content::NOTIFICATION_NAV_ENTRY_CHANGED,
46523730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)      content::Source<content::NavigationController>(
46623730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles)           &web_contents()->GetController()));
46758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
46829b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  InstantService* instant_service =
46929b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch      InstantServiceFactory::GetForProfile(GetProfile());
47029b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch  if (instant_service)
47129b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch    instant_service->RemoveObserver(this);
47229b820f8d84e3bc97d62552e54923c42407f2f29Ben Murdoch
473424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  web_contents()->SetDelegate(NULL);
474424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
475424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (delete_native) {
476424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    web_contents_.reset();
47758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)    synced_tab_delegate_->ResetWebContents();
478424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  } else {
479424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    // Release the WebContents so it does not get deleted by the scoped_ptr.
480424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    ignore_result(web_contents_.release());
481424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  }
482424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
483424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
484424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)base::android::ScopedJavaLocalRef<jobject> TabAndroid::GetProfileAndroid(
485424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    JNIEnv* env,
486424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    jobject obj) {
487424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  Profile* profile = GetProfile();
488424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!profile)
489424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return base::android::ScopedJavaLocalRef<jobject>();
490424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  ProfileAndroid* profile_android = ProfileAndroid::FromProfile(profile);
491424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  if (!profile_android)
492424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)    return base::android::ScopedJavaLocalRef<jobject>();
493424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
494424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)  return profile_android->GetJavaObject();
495424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)}
496424c4d7b64af9d0d8fd9624f381f469654d5e3d2Torne (Richard Coles)
497a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)TabAndroid::TabLoadStatus TabAndroid::LoadUrl(JNIEnv* env,
498a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jobject obj,
499a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jstring url,
500a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jstring j_extra_headers,
501a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jbyteArray j_post_data,
502a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jint page_transition,
503a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                                              jstring j_referrer_url,
504cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                              jint referrer_policy,
505cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                              jboolean is_renderer_initiated) {
5061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!web_contents())
507a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return PAGE_LOAD_FAILED;
508a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
509a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL gurl(base::android::ConvertJavaStringToUTF8(env, url));
510a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (gurl.is_empty())
511a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return PAGE_LOAD_FAILED;
512a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
513a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // If the page was prerendered, use it.
514a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Note in incognito mode, we don't have a PrerenderManager.
515a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
516a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  prerender::PrerenderManager* prerender_manager =
517a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      prerender::PrerenderManagerFactory::GetForProfile(GetProfile());
518a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (prerender_manager) {
519a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    bool prefetched_page_loaded = HasPrerenderedUrl(gurl);
520a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Getting the load status before MaybeUsePrerenderedPage() b/c it resets.
521a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    chrome::NavigateParams params(NULL, web_contents());
522f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    InstantSearchPrerenderer* prerenderer =
523f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        InstantSearchPrerenderer::GetForProfile(GetProfile());
524f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (prerenderer) {
525f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      const base::string16& search_terms =
526f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          chrome::ExtractSearchTermsFromURL(GetProfile(), gurl);
527f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      if (!search_terms.empty() &&
528f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          prerenderer->CanCommitQuery(web_contents_.get(), search_terms)) {
529f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        prerenderer->Commit(search_terms);
530f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
531f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)        if (prerenderer->UsePrerenderedPage(gurl, &params))
532f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          return FULL_PRERENDERED_PAGE_LOAD;
533f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      }
534f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      prerenderer->Cancel();
535f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
536a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (prerender_manager->MaybeUsePrerenderedPage(gurl, &params)) {
537a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      return prefetched_page_loaded ?
538a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          FULL_PRERENDERED_PAGE_LOAD : PARTIAL_PRERENDERED_PAGE_LOAD;
539a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
540a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
541a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
542f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  GURL fixed_url(
543f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      url_fixer::FixupURL(gurl.possibly_invalid_spec(), std::string()));
544a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!fixed_url.is_valid())
545a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return PAGE_LOAD_FAILED;
546a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
547a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!HandleNonNavigationAboutURL(fixed_url)) {
548a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Notify the GoogleURLTracker of searches, it might want to change the
549a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // actual Google site used (for instance when in the UK, google.co.uk, when
550a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // in the US google.com).
551a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Note that this needs to happen before we initiate the navigation as the
552a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // GoogleURLTracker uses the navigation pending notification to trigger the
553a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // infobar.
554a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (google_util::IsGoogleSearchUrl(fixed_url) &&
5551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        (page_transition & ui::PAGE_TRANSITION_GENERATED)) {
55646d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      GoogleURLTracker* tracker =
55746d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)          GoogleURLTrackerFactory::GetForProfile(GetProfile());
55846d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)      if (tracker)
55946d4c2bc3267f3f028f39e7e311b0f89aba2e4fdTorne (Richard Coles)        tracker->SearchCommitted();
560a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
561a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
562a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Record UMA "ShowHistory" here. That way it'll pick up both user
563a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // typing chrome://history as well as selecting from the drop down menu.
564a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (fixed_url.spec() == chrome::kChromeUIHistoryURL) {
565a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      content::RecordAction(base::UserMetricsAction("ShowHistory"));
566a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
567a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
568a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    content::NavigationController::LoadURLParams load_params(fixed_url);
569a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (j_extra_headers) {
570a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.extra_headers = base::android::ConvertJavaStringToUTF8(
571a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          env,
572a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          j_extra_headers);
573a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
574a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (j_post_data) {
575a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.load_type =
576a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          content::NavigationController::LOAD_TYPE_BROWSER_INITIATED_HTTP_POST;
577a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      std::vector<uint8> post_data;
578a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::android::JavaByteArrayToByteVector(env, j_post_data, &post_data);
579a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.browser_initiated_post_data =
580a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::RefCountedBytes::TakeVector(&post_data);
581a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
582a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    load_params.transition_type =
5831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        ui::PageTransitionFromInt(page_transition);
584a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (j_referrer_url) {
585a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      load_params.referrer = content::Referrer(
586a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          GURL(base::android::ConvertJavaStringToUTF8(env, j_referrer_url)),
587a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          static_cast<blink::WebReferrerPolicy>(referrer_policy));
588a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
5895c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    const base::string16 search_terms =
5905c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        chrome::ExtractSearchTermsFromURL(GetProfile(), gurl);
5915c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    SearchTabHelper* search_tab_helper =
5925c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        SearchTabHelper::FromWebContents(web_contents_.get());
5935c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    if (!search_terms.empty() && search_tab_helper &&
5945c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu        search_tab_helper->SupportsInstant()) {
5955c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      search_tab_helper->Submit(search_terms);
5965c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu      return DEFAULT_PAGE_LOAD;
5975c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu    }
598cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    load_params.is_renderer_initiated = is_renderer_initiated;
5991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    web_contents()->GetController().LoadURLWithParams(load_params);
600a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
601a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return DEFAULT_PAGE_LOAD;
602a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
603a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
60458537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)ToolbarModel::SecurityLevel TabAndroid::GetSecurityLevel(JNIEnv* env,
60558537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)                                                         jobject obj) {
60658537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)  return ToolbarModelImpl::GetSecurityLevelForWebContents(web_contents());
60758537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)}
60858537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)
6094e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void TabAndroid::SetActiveNavigationEntryTitleForUrl(JNIEnv* env,
6104e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                     jobject obj,
6114e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                     jstring jurl,
6124e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                                                     jstring jtitle) {
6134e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  DCHECK(web_contents());
6144e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
615a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 title;
6164e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (jtitle)
6174e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    title = base::android::ConvertJavaStringToUTF16(env, jtitle);
6184e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
6194e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  std::string url;
6204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (jurl)
6214e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    url = base::android::ConvertJavaStringToUTF8(env, jurl);
6224e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
6234e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  content::NavigationEntry* entry =
6244e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)      web_contents()->GetController().GetVisibleEntry();
6254e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  if (entry && url == entry->GetVirtualURL().spec())
6264e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    entry->SetTitle(title);
6274e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
6284e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
629f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)bool TabAndroid::Print(JNIEnv* env, jobject obj) {
630f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (!web_contents())
631f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return false;
632f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
633f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  printing::PrintViewManagerBasic::CreateForWebContents(web_contents());
634f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  printing::PrintViewManagerBasic* print_view_manager =
635f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      printing::PrintViewManagerBasic::FromWebContents(web_contents());
636f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  if (print_view_manager == NULL)
637f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)    return false;
638f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
639f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  print_view_manager->PrintNow();
640f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  return true;
641f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)}
642f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
6436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)ScopedJavaLocalRef<jobject> TabAndroid::GetFavicon(JNIEnv* env, jobject obj) {
6446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  ScopedJavaLocalRef<jobject> bitmap;
6456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  FaviconTabHelper* favicon_tab_helper =
6466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      FaviconTabHelper::FromWebContents(web_contents_.get());
6476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!favicon_tab_helper)
6496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return bitmap;
6501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // If the favicon isn't valid, it will return a default bitmap.
6526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  SkBitmap favicon =
6546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      favicon_tab_helper->GetFavicon()
6556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          .AsImageSkia()
6566e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          .GetRepresentation(
6576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)               ResourceBundle::GetSharedInstance().GetMaxScaleFactor())
6586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          .sk_bitmap();
6596e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6606e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (favicon.empty()) {
6616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    favicon = favicon_tab_helper->GetFavicon().AsBitmap();
6626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
6636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!favicon.empty()) {
6656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    gfx::DeviceDisplayInfo device_info;
6666e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    const float device_scale_factor = device_info.GetDIPScale();
6676e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    int target_size_dip = device_scale_factor * gfx::kFaviconSize;
6686e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if (favicon.width() != target_size_dip ||
6696e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)        favicon.height() != target_size_dip) {
6706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      favicon =
6716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          skia::ImageOperations::Resize(favicon,
6726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                        skia::ImageOperations::RESIZE_BEST,
6736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                        target_size_dip,
6746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                                        target_size_dip);
6756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    }
6766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    bitmap = gfx::ConvertToJavaBitmap(&favicon);
6786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
6796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  return bitmap;
6806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
6816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
6821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccijboolean TabAndroid::IsFaviconValid(JNIEnv* env, jobject jobj) {
6831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return web_contents() &&
6841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      FaviconTabHelper::FromWebContents(web_contents())->FaviconIsValid();
6851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
6861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
687a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)prerender::PrerenderManager* TabAndroid::GetPrerenderManager() const {
688a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Profile* profile = GetProfile();
689a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!profile)
690a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return NULL;
691a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return prerender::PrerenderManagerFactory::GetForProfile(profile);
692a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
693a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
6945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)static void Init(JNIEnv* env, jobject obj) {
6955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  TRACE_EVENT0("native", "TabAndroid::Init");
6965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // This will automatically bind to the Java object and pass ownership there.
6975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  new TabAndroid(env, obj);
6985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
6995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
700c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)bool TabAndroid::RegisterTabAndroid(JNIEnv* env) {
701c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  return RegisterNativesImpl(env);
702c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
703