• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/search/instant_service_observer.h"
15 #include "chrome/browser/sync/glue/synced_tab_delegate_android.h"
16 #include "chrome/browser/ui/search/search_tab_helper_delegate.h"
17 #include "chrome/browser/ui/tab_contents/core_tab_helper_delegate.h"
18 #include "chrome/browser/ui/toolbar/toolbar_model.h"
19 #include "components/sessions/session_id.h"
20 #include "content/public/browser/notification_observer.h"
21 #include "content/public/browser/notification_registrar.h"
22 
23 class GURL;
24 class Profile;
25 class SkBitmap;
26 
27 namespace chrome {
28 struct NavigateParams;
29 }
30 
31 namespace chrome {
32 namespace android {
33 class ChromeWebContentsDelegateAndroid;
34 }
35 }
36 
37 namespace content {
38 class ContentViewCore;
39 class WebContents;
40 }
41 
42 namespace prerender {
43 class PrerenderManager;
44 }
45 
46 class TabAndroid : public CoreTabHelperDelegate,
47                    public InstantServiceObserver,
48                    public SearchTabHelperDelegate,
49                    public content::NotificationObserver {
50  public:
51   enum TabLoadStatus {
52 #define DEFINE_TAB_LOAD_STATUS(name, value)  name = value,
53 #include "chrome/browser/android/tab_load_status.h"
54 #undef DEFINE_TAB_LOAD_STATUS
55   };
56 
57   // Convenience method to retrieve the Tab associated with the passed
58   // WebContents.  Can return NULL.
59   static TabAndroid* FromWebContents(content::WebContents* web_contents);
60 
61   // Returns the native TabAndroid stored in the Java Tab represented by
62   // |obj|.
63   static TabAndroid* GetNativeTab(JNIEnv* env, jobject obj);
64 
65   // Function to attach helpers to the contentView.
66   static void AttachTabHelpers(content::WebContents* web_contents);
67 
68   TabAndroid(JNIEnv* env, jobject obj);
69   virtual ~TabAndroid();
70 
71   base::android::ScopedJavaLocalRef<jobject> GetJavaObject();
72 
73   // Return the WebContents, if any, currently owned by this TabAndroid.
web_contents()74   content::WebContents* web_contents() const { return web_contents_.get(); }
75 
76   // Return specific id information regarding this TabAndroid.
session_id()77   const SessionID& session_id() const { return session_tab_id_; }
78   int GetAndroidId() const;
79   int GetSyncId() const;
80 
81   // Return the tab title.
82   base::string16 GetTitle() const;
83 
84   // Return the tab url.
85   GURL GetURL() const;
86 
87   // Load the tab if it was unloaded from memory.
88   bool LoadIfNeeded();
89 
90   // Helper methods to make it easier to access objects from the associated
91   // WebContents.  Can return NULL.
92   content::ContentViewCore* GetContentViewCore() const;
93   Profile* GetProfile() const;
94   browser_sync::SyncedTabDelegate* GetSyncedTabDelegate() const;
95 
96   void SetWindowSessionID(SessionID::id_type window_id);
97   void SetSyncId(int sync_id);
98 
99   virtual void HandlePopupNavigation(chrome::NavigateParams* params);
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   void MakeLoadURLParams(
108       chrome::NavigateParams* params,
109       content::NavigationController::LoadURLParams* load_url_params);
110 
111   // CoreTabHelperDelegate ----------------------------------------------------
112 
113   virtual void SwapTabContents(content::WebContents* old_contents,
114                                content::WebContents* new_contents,
115                                bool did_start_load,
116                                bool did_finish_load) OVERRIDE;
117 
118   // Overridden from InstantServiceObserver:
119   void DefaultSearchProviderChanged() OVERRIDE;
120 
121   // Overridden from SearchTabHelperDelegate:
122   virtual void OnWebContentsInstantSupportDisabled(
123       const content::WebContents* web_contents) OVERRIDE;
124 
125   // NotificationObserver -----------------------------------------------------
126   virtual void Observe(int type,
127                        const content::NotificationSource& source,
128                        const content::NotificationDetails& details) OVERRIDE;
129 
130   // Methods called from Java via JNI -----------------------------------------
131 
132   virtual void Destroy(JNIEnv* env, jobject obj);
133   virtual void InitWebContents(JNIEnv* env,
134                                jobject obj,
135                                jboolean incognito,
136                                jobject jcontent_view_core,
137                                jobject jweb_contents_delegate,
138                                jobject jcontext_menu_populator);
139   virtual void DestroyWebContents(JNIEnv* env,
140                                   jobject obj,
141                                   jboolean delete_native);
142   base::android::ScopedJavaLocalRef<jobject> GetProfileAndroid(JNIEnv* env,
143                                                                jobject obj);
144   virtual TabLoadStatus LoadUrl(JNIEnv* env,
145                                 jobject obj,
146                                 jstring url,
147                                 jstring j_extra_headers,
148                                 jbyteArray j_post_data,
149                                 jint page_transition,
150                                 jstring j_referrer_url,
151                                 jint referrer_policy,
152                                 jboolean is_renderer_initiated);
153   ToolbarModel::SecurityLevel GetSecurityLevel(JNIEnv* env, jobject obj);
154   void SetActiveNavigationEntryTitleForUrl(JNIEnv* env,
155                                            jobject obj,
156                                            jstring jurl,
157                                            jstring jtitle);
158   bool Print(JNIEnv* env, jobject obj);
159   // Called to get favicon of current tab, return null if no favicon is
160   // avaliable for current tab.
161   base::android::ScopedJavaLocalRef<jobject> GetFavicon(JNIEnv* env,
162                                                         jobject obj);
163   jboolean IsFaviconValid(JNIEnv* env, jobject jobj);
164 
165   // Register the Tab's native methods through JNI.
166   static bool RegisterTabAndroid(JNIEnv* env);
167 
168  private:
169   prerender::PrerenderManager* GetPrerenderManager() const;
170 
171   JavaObjectWeakGlobalRef weak_java_tab_;
172 
173   // The identifier used by session restore for this tab.
174   SessionID session_tab_id_;
175 
176   // Identifier of the window the tab is in.
177   SessionID session_window_id_;
178 
179   content::NotificationRegistrar notification_registrar_;
180 
181   scoped_ptr<content::WebContents> web_contents_;
182   scoped_ptr<chrome::android::ChromeWebContentsDelegateAndroid>
183       web_contents_delegate_;
184 
185   scoped_ptr<browser_sync::SyncedTabDelegateAndroid> synced_tab_delegate_;
186 
187   DISALLOW_COPY_AND_ASSIGN(TabAndroid);
188 };
189 
190 #endif  // CHROME_BROWSER_ANDROID_TAB_ANDROID_H_
191