1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef CHROME_BROWSER_ANDROID_TAB_ANDROID_H_ 6 #define CHROME_BROWSER_ANDROID_TAB_ANDROID_H_ 7 8 #include <jni.h> 9 10 #include "base/android/jni_weak_ref.h" 11 #include "base/callback_forward.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/strings/string16.h" 14 #include "chrome/browser/sessions/session_id.h" 15 #include "chrome/browser/sync/glue/synced_tab_delegate_android.h" 16 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h" 17 #include "chrome/browser/ui/toolbar/toolbar_model.h" 18 #include "content/public/browser/notification_observer.h" 19 #include "content/public/browser/notification_registrar.h" 20 21 class GURL; 22 class Profile; 23 class SkBitmap; 24 25 namespace chrome { 26 struct NavigateParams; 27 } 28 29 namespace chrome { 30 namespace android { 31 class ChromeWebContentsDelegateAndroid; 32 } 33 } 34 35 namespace content { 36 class ContentViewCore; 37 class WebContents; 38 } 39 40 namespace prerender { 41 class PrerenderManager; 42 } 43 44 class TabAndroid : public CoreTabHelperDelegate, 45 public content::NotificationObserver { 46 public: 47 enum TabLoadStatus { 48 #define DEFINE_TAB_LOAD_STATUS(name, value) name = value, 49 #include "chrome/browser/android/tab_load_status.h" 50 #undef DEFINE_TAB_LOAD_STATUS 51 }; 52 53 // Convenience method to retrieve the Tab associated with the passed 54 // WebContents. Can return NULL. 55 static TabAndroid* FromWebContents(content::WebContents* web_contents); 56 57 // Returns the native TabAndroid stored in the Java Tab represented by 58 // |obj|. 59 static TabAndroid* GetNativeTab(JNIEnv* env, jobject obj); 60 61 // Function to attach helpers to the contentView. 62 static void AttachTabHelpers(content::WebContents* web_contents); 63 64 TabAndroid(JNIEnv* env, jobject obj); 65 virtual ~TabAndroid(); 66 67 base::android::ScopedJavaLocalRef<jobject> GetJavaObject(); 68 69 // Return the WebContents, if any, currently owned by this TabAndroid. 70 content::WebContents* web_contents() const { return web_contents_.get(); } 71 72 // Return specific id information regarding this TabAndroid. 73 const SessionID& session_id() const { return session_tab_id_; } 74 int GetAndroidId() const; 75 int GetSyncId() const; 76 77 // Return the tab title. 78 base::string16 GetTitle() const; 79 80 // Return the tab url. 81 GURL GetURL() const; 82 83 // Load the tab if it was unloaded from memory. 84 bool LoadIfNeeded(); 85 86 // Helper methods to make it easier to access objects from the associated 87 // WebContents. Can return NULL. 88 content::ContentViewCore* GetContentViewCore() const; 89 Profile* GetProfile() const; 90 browser_sync::SyncedTabDelegate* GetSyncedTabDelegate() const; 91 92 void SetWindowSessionID(SessionID::id_type window_id); 93 void SetSyncId(int sync_id); 94 95 virtual void HandlePopupNavigation(chrome::NavigateParams* params); 96 97 virtual void OnReceivedHttpAuthRequest(jobject auth_handler, 98 const base::string16& host, 99 const base::string16& realm); 100 101 // Called to determine if chrome://welcome should contain links to the terms 102 // of service and the privacy notice. 103 virtual bool ShouldWelcomePageLinkToTermsOfService(); 104 105 bool HasPrerenderedUrl(GURL gurl); 106 107 // CoreTabHelperDelegate ---------------------------------------------------- 108 109 virtual void SwapTabContents(content::WebContents* old_contents, 110 content::WebContents* new_contents, 111 bool did_start_load, 112 bool did_finish_load) OVERRIDE; 113 114 // NotificationObserver ----------------------------------------------------- 115 virtual void Observe(int type, 116 const content::NotificationSource& source, 117 const content::NotificationDetails& details) OVERRIDE; 118 119 // Methods called from Java via JNI ----------------------------------------- 120 121 virtual void Destroy(JNIEnv* env, jobject obj); 122 virtual void InitWebContents(JNIEnv* env, 123 jobject obj, 124 jboolean incognito, 125 jobject jcontent_view_core, 126 jobject jweb_contents_delegate, 127 jobject jcontext_menu_populator); 128 virtual void DestroyWebContents(JNIEnv* env, 129 jobject obj, 130 jboolean delete_native); 131 base::android::ScopedJavaLocalRef<jobject> GetWebContents(JNIEnv* env, 132 jobject obj); 133 base::android::ScopedJavaLocalRef<jobject> GetProfileAndroid(JNIEnv* env, 134 jobject obj); 135 virtual TabLoadStatus LoadUrl(JNIEnv* env, 136 jobject obj, 137 jstring url, 138 jstring j_extra_headers, 139 jbyteArray j_post_data, 140 jint page_transition, 141 jstring j_referrer_url, 142 jint referrer_policy, 143 jboolean is_renderer_initiated); 144 ToolbarModel::SecurityLevel GetSecurityLevel(JNIEnv* env, jobject obj); 145 void SetActiveNavigationEntryTitleForUrl(JNIEnv* env, 146 jobject obj, 147 jstring jurl, 148 jstring jtitle); 149 bool Print(JNIEnv* env, jobject obj); 150 151 // Register the Tab's native methods through JNI. 152 static bool RegisterTabAndroid(JNIEnv* env); 153 154 private: 155 prerender::PrerenderManager* GetPrerenderManager() const; 156 157 JavaObjectWeakGlobalRef weak_java_tab_; 158 159 // The identifier used by session restore for this tab. 160 SessionID session_tab_id_; 161 162 // Identifier of the window the tab is in. 163 SessionID session_window_id_; 164 165 content::NotificationRegistrar notification_registrar_; 166 167 scoped_ptr<content::WebContents> web_contents_; 168 scoped_ptr<chrome::android::ChromeWebContentsDelegateAndroid> 169 web_contents_delegate_; 170 171 scoped_ptr<browser_sync::SyncedTabDelegateAndroid> synced_tab_delegate_; 172 173 DISALLOW_COPY_AND_ASSIGN(TabAndroid); 174 }; 175 176 #endif // CHROME_BROWSER_ANDROID_TAB_ANDROID_H_ 177