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 #include <stdint.h>
10 
11 #include <memory>
12 
13 #include "base/android/jni_weak_ref.h"
14 #include "base/android/scoped_java_ref.h"
15 #include "base/callback_forward.h"
16 #include "base/macros.h"
17 #include "base/observer_list.h"
18 #include "base/strings/string16.h"
19 #include "base/supports_user_data.h"
20 #include "chrome/browser/sync/glue/synced_tab_delegate_android.h"
21 #include "chrome/browser/tab/web_contents_state.h"
22 #include "components/infobars/core/infobar_manager.h"
23 #include "components/omnibox/browser/location_bar_model.h"
24 #include "components/sessions/core/session_id.h"
25 
26 class GURL;
27 class Profile;
28 
29 namespace cc {
30 class Layer;
31 }
32 
33 namespace android {
34 class TabWebContentsDelegateAndroid;
35 }
36 
37 namespace content {
38 class DevToolsAgentHost;
39 class WebContents;
40 }
41 
42 class TabAndroid : public base::SupportsUserData {
43  public:
44   // A Java counterpart will be generated for this enum.
45   // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.chrome.browser
46   enum TabLoadStatus {
47     PAGE_LOAD_FAILED = 0,
48     DEFAULT_PAGE_LOAD = 1,
49     PARTIAL_PRERENDERED_PAGE_LOAD = 2,
50     FULL_PRERENDERED_PAGE_LOAD = 3,
51   };
52 
53   class Observer : public base::CheckedObserver {
54    public:
55     // Called when WebContents is initialized.
56     virtual void OnInitWebContents(TabAndroid* tab) = 0;
57   };
58 
59   // Convenience method to retrieve the Tab associated with the passed
60   // WebContents.  Can return NULL.
61   static TabAndroid* FromWebContents(const content::WebContents* web_contents);
62 
63   // Returns the native TabAndroid stored in the Java Tab represented by
64   // |obj|.
65   static TabAndroid* GetNativeTab(JNIEnv* env,
66                                   const base::android::JavaRef<jobject>& obj);
67 
68   // Function to attach helpers to the contentView.
69   static void AttachTabHelpers(content::WebContents* web_contents);
70 
71   TabAndroid(JNIEnv* env, const base::android::JavaRef<jobject>& obj);
72   ~TabAndroid() override;
73 
74   base::android::ScopedJavaLocalRef<jobject> GetJavaObject();
75 
76   // Return the WebContents, if any, currently owned by this TabAndroid.
web_contents()77   content::WebContents* web_contents() const { return web_contents_.get(); }
78 
79   // Return the cc::Layer that represents the content for this TabAndroid.
80   scoped_refptr<cc::Layer> GetContentLayer() const;
81 
82   // Return specific id information regarding this TabAndroid.
window_id()83   const SessionID& window_id() const { return session_window_id_; }
84 
85   int GetAndroidId() const;
86   bool IsNativePage() const;
87 
88   // Return the tab title.
89   base::string16 GetTitle() const;
90 
91   // Return the tab url.
92   GURL GetURL() const;
93 
94   // Return whether the tab is currently visible and the user can interact with
95   // it.
96   bool IsUserInteractable() const;
97 
98   // Helper methods to make it easier to access objects from the associated
99   // WebContents.  Can return NULL.
100   Profile* GetProfile() const;
101   sync_sessions::SyncedTabDelegate* GetSyncedTabDelegate() const;
102 
103   // Delete navigation entries matching predicate from frozen state.
104   void DeleteFrozenNavigationEntries(
105       const WebContentsState::DeletionPredicate& predicate);
106 
107   void SetWindowSessionID(SessionID window_id);
108 
109   std::unique_ptr<content::WebContents> SwapWebContents(
110       std::unique_ptr<content::WebContents> new_contents,
111       bool did_start_load,
112       bool did_finish_load);
113 
114   bool IsCustomTab();
115   bool IsHidden();
116 
should_add_api2_transition_to_future_navigations()117   bool should_add_api2_transition_to_future_navigations() const {
118     return should_add_api2_transition_to_future_navigations_;
119   }
120 
hide_future_navigations()121   bool hide_future_navigations() const { return hide_future_navigations_; }
122 
should_block_new_notification_requests()123   bool should_block_new_notification_requests() const {
124     return should_block_new_notification_requests_;
125   }
126   // Observers -----------------------------------------------------------------
127 
128   // Adds/Removes an Observer.
129   void AddObserver(Observer* observer);
130   void RemoveObserver(Observer* observer);
131 
132   // Methods called from Java via JNI -----------------------------------------
133 
134   void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj);
135   void InitWebContents(
136       JNIEnv* env,
137       const base::android::JavaParamRef<jobject>& obj,
138       jboolean incognito,
139       jboolean is_background_tab,
140       const base::android::JavaParamRef<jobject>& jweb_contents,
141       jint jparent_tab_id,
142       const base::android::JavaParamRef<jobject>& jweb_contents_delegate,
143       const base::android::JavaParamRef<jobject>&
144           jcontext_menu_populator_factory);
145   void UpdateDelegates(
146       JNIEnv* env,
147       const base::android::JavaParamRef<jobject>& obj,
148       const base::android::JavaParamRef<jobject>& jweb_contents_delegate,
149       const base::android::JavaParamRef<jobject>&
150           jcontext_menu_populator_factory);
151   void DestroyWebContents(JNIEnv* env,
152                           const base::android::JavaParamRef<jobject>& obj);
153   void ReleaseWebContents(JNIEnv* env,
154                           const base::android::JavaParamRef<jobject>& obj);
155   void OnPhysicalBackingSizeChanged(
156       JNIEnv* env,
157       const base::android::JavaParamRef<jobject>& obj,
158       const base::android::JavaParamRef<jobject>& jweb_contents,
159       jint width,
160       jint height);
161   TabLoadStatus LoadUrl(
162       JNIEnv* env,
163       const base::android::JavaParamRef<jobject>& obj,
164       const base::android::JavaParamRef<jstring>& url,
165       const base::android::JavaParamRef<jobject>& j_initiator_origin,
166       const base::android::JavaParamRef<jstring>& j_extra_headers,
167       const base::android::JavaParamRef<jobject>& j_post_data,
168       jint page_transition,
169       const base::android::JavaParamRef<jstring>& j_referrer_url,
170       jint referrer_policy,
171       jboolean is_renderer_initiated,
172       jboolean should_replace_current_entry,
173       jboolean has_user_gesture,
174       jboolean should_clear_history_list,
175       jlong omnibox_input_received_timestamp,
176       jlong intent_received_timestamp);
177   void SetActiveNavigationEntryTitleForUrl(
178       JNIEnv* env,
179       const base::android::JavaParamRef<jobject>& obj,
180       const base::android::JavaParamRef<jstring>& jurl,
181       const base::android::JavaParamRef<jstring>& jtitle);
182 
183   void LoadOriginalImage(JNIEnv* env,
184                          const base::android::JavaParamRef<jobject>& obj);
185   void SetAddApi2TransitionToFutureNavigations(JNIEnv* env,
186                                                jboolean should_add);
GetAddApi2TransitionToFutureNavigations(JNIEnv * env)187   jboolean GetAddApi2TransitionToFutureNavigations(JNIEnv* env) {
188     return should_add_api2_transition_to_future_navigations_;
189   }
190   void SetHideFutureNavigations(JNIEnv* env, jboolean hide);
GetHideFutureNavigations(JNIEnv * env)191   jboolean GetHideFutureNavigations(JNIEnv* env) {
192     return hide_future_navigations_;
193   }
194   void SetShouldBlockNewNotificationRequests(JNIEnv* env, jboolean value);
GetShouldBlockNewNotificationRequests(JNIEnv * env)195   jboolean GetShouldBlockNewNotificationRequests(JNIEnv* env) {
196     return should_block_new_notification_requests_;
197   }
198 
199   scoped_refptr<content::DevToolsAgentHost> GetDevToolsAgentHost();
200 
201   void SetDevToolsAgentHost(scoped_refptr<content::DevToolsAgentHost> host);
202 
203  private:
204   // Calls set_hide_future_navigations() on the HistoryTabHelper associated
205   // with |web_contents_|.
206   void PropagateHideFutureNavigationsToHistoryTabHelper();
207 
208   // Calls SetBlockNewNotificationRequests() on NotificationPermissionContext.
209   void PropagateBlockNewNotificationRequestsToWebContents();
210 
211   JavaObjectWeakGlobalRef weak_java_tab_;
212 
213   // Identifier of the window the tab is in.
214   SessionID session_window_id_;
215 
216   scoped_refptr<cc::Layer> content_layer_;
217 
218   std::unique_ptr<content::WebContents> web_contents_;
219   std::unique_ptr<android::TabWebContentsDelegateAndroid>
220       web_contents_delegate_;
221   scoped_refptr<content::DevToolsAgentHost> devtools_host_;
222   std::unique_ptr<browser_sync::SyncedTabDelegateAndroid> synced_tab_delegate_;
223   bool should_add_api2_transition_to_future_navigations_ = false;
224   bool hide_future_navigations_ = false;
225   bool should_block_new_notification_requests_ = false;
226 
227   base::ObserverList<Observer> observers_;
228 
229   DISALLOW_COPY_AND_ASSIGN(TabAndroid);
230 };
231 
232 #endif  // CHROME_BROWSER_ANDROID_TAB_ANDROID_H_
233