1 // Copyright 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 CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_ 6 #define CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_ 7 8 #include <vector> 9 10 #include "base/android/jni_android.h" 11 #include "base/android/jni_helper.h" 12 #include "base/basictypes.h" 13 #include "base/compiler_specific.h" 14 #include "base/i18n/rtl.h" 15 #include "base/memory/scoped_ptr.h" 16 #include "base/process/process.h" 17 #include "content/browser/renderer_host/render_widget_host_view_android.h" 18 #include "content/browser/web_contents/web_contents_impl.h" 19 #include "content/public/browser/android/content_view_core.h" 20 #include "content/public/browser/notification_observer.h" 21 #include "content/public/browser/notification_registrar.h" 22 #include "third_party/WebKit/public/web/WebInputEvent.h" 23 #include "ui/gfx/rect.h" 24 #include "ui/gfx/rect_f.h" 25 #include "url/gurl.h" 26 27 namespace ui { 28 class ViewAndroid; 29 class WindowAndroid; 30 } 31 32 namespace content { 33 class RenderWidgetHostViewAndroid; 34 struct MenuItem; 35 36 // TODO(jrg): this is a shell. Upstream the rest. 37 class ContentViewCoreImpl : public ContentViewCore, 38 public NotificationObserver { 39 public: 40 static ContentViewCoreImpl* FromWebContents(WebContents* web_contents); 41 ContentViewCoreImpl(JNIEnv* env, 42 jobject obj, 43 bool hardware_accelerated, 44 WebContents* web_contents, 45 ui::ViewAndroid* view_android, 46 ui::WindowAndroid* window_android); 47 48 // ContentViewCore implementation. 49 virtual base::android::ScopedJavaLocalRef<jobject> GetJavaObject() OVERRIDE; 50 virtual WebContents* GetWebContents() const OVERRIDE; 51 virtual ui::ViewAndroid* GetViewAndroid() const OVERRIDE; 52 virtual ui::WindowAndroid* GetWindowAndroid() const OVERRIDE; 53 virtual scoped_refptr<cc::Layer> GetLayer() const OVERRIDE; 54 virtual void LoadUrl(NavigationController::LoadURLParams& params) OVERRIDE; 55 virtual jint GetCurrentRenderProcessId(JNIEnv* env, jobject obj) OVERRIDE; 56 virtual void ShowPastePopup(int x, int y) OVERRIDE; 57 virtual unsigned int GetScaledContentTexture( 58 float scale, 59 gfx::Size* out_size) OVERRIDE; 60 virtual float GetDpiScale() const OVERRIDE; 61 virtual void RequestContentClipping(const gfx::Rect& clipping, 62 const gfx::Size& content_size) OVERRIDE; 63 virtual void PauseVideo() OVERRIDE; 64 65 // -------------------------------------------------------------------------- 66 // Methods called from Java via JNI 67 // -------------------------------------------------------------------------- 68 69 void OnJavaContentViewCoreDestroyed(JNIEnv* env, jobject obj); 70 71 // Notifies the ContentViewCore that items were selected in the currently 72 // showing select popup. 73 void SelectPopupMenuItems(JNIEnv* env, jobject obj, jintArray indices); 74 75 void LoadUrl( 76 JNIEnv* env, jobject obj, 77 jstring url, 78 jint load_url_type, 79 jint transition_type, 80 jint ua_override_option, 81 jstring extra_headers, 82 jbyteArray post_data, 83 jstring base_url_for_data_url, 84 jstring virtual_url_for_data_url, 85 jboolean can_load_local_resources); 86 base::android::ScopedJavaLocalRef<jstring> GetURL(JNIEnv* env, jobject) const; 87 base::android::ScopedJavaLocalRef<jstring> GetTitle( 88 JNIEnv* env, jobject obj) const; 89 jboolean IsIncognito(JNIEnv* env, jobject obj); 90 jboolean Crashed(JNIEnv* env, jobject obj) const { return tab_crashed_; } 91 void SendOrientationChangeEvent(JNIEnv* env, jobject obj, jint orientation); 92 jboolean SendTouchEvent(JNIEnv* env, 93 jobject obj, 94 jlong time_ms, 95 jint type, 96 jobjectArray pts); 97 jboolean SendMouseMoveEvent(JNIEnv* env, 98 jobject obj, 99 jlong time_ms, 100 jfloat x, 101 jfloat y); 102 jboolean SendMouseWheelEvent(JNIEnv* env, 103 jobject obj, 104 jlong time_ms, 105 jfloat x, 106 jfloat y, 107 jfloat vertical_axis); 108 void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y); 109 void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms); 110 void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms, 111 jfloat x, jfloat y, jfloat dx, jfloat dy, 112 jboolean last_input_event_for_vsync); 113 void FlingStart(JNIEnv* env, jobject obj, jlong time_ms, 114 jfloat x, jfloat y, jfloat vx, jfloat vy); 115 void FlingCancel(JNIEnv* env, jobject obj, jlong time_ms); 116 void SingleTap(JNIEnv* env, jobject obj, jlong time_ms, 117 jfloat x, jfloat y, 118 jboolean disambiguation_popup_tap); 119 void SingleTapUnconfirmed(JNIEnv* env, jobject obj, jlong time_ms, 120 jfloat x, jfloat y); 121 void ShowPressState(JNIEnv* env, jobject obj, jlong time_ms, 122 jfloat x, jfloat y); 123 void ShowPressCancel(JNIEnv* env, jobject obj, jlong time_ms, 124 jfloat x, jfloat y); 125 void DoubleTap(JNIEnv* env, jobject obj, jlong time_ms, 126 jfloat x, jfloat y) ; 127 void LongPress(JNIEnv* env, jobject obj, jlong time_ms, 128 jfloat x, jfloat y, 129 jboolean disambiguation_popup_tap); 130 void LongTap(JNIEnv* env, jobject obj, jlong time_ms, 131 jfloat x, jfloat y, 132 jboolean disambiguation_popup_tap); 133 void PinchBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y); 134 void PinchEnd(JNIEnv* env, jobject obj, jlong time_ms); 135 void PinchBy(JNIEnv* env, jobject obj, jlong time_ms, 136 jfloat x, jfloat y, jfloat delta, 137 jboolean last_input_event_for_vsync); 138 void SelectBetweenCoordinates(JNIEnv* env, jobject obj, 139 jfloat x1, jfloat y1, 140 jfloat x2, jfloat y2); 141 void MoveCaret(JNIEnv* env, jobject obj, jfloat x, jfloat y); 142 143 jboolean CanGoBack(JNIEnv* env, jobject obj); 144 jboolean CanGoForward(JNIEnv* env, jobject obj); 145 jboolean CanGoToOffset(JNIEnv* env, jobject obj, jint offset); 146 void GoBack(JNIEnv* env, jobject obj); 147 void GoForward(JNIEnv* env, jobject obj); 148 void GoToOffset(JNIEnv* env, jobject obj, jint offset); 149 void GoToNavigationIndex(JNIEnv* env, jobject obj, jint index); 150 void StopLoading(JNIEnv* env, jobject obj); 151 void Reload(JNIEnv* env, jobject obj); 152 void CancelPendingReload(JNIEnv* env, jobject obj); 153 void ContinuePendingReload(JNIEnv* env, jobject obj); 154 jboolean NeedsReload(JNIEnv* env, jobject obj); 155 void ClearHistory(JNIEnv* env, jobject obj); 156 void EvaluateJavaScript(JNIEnv* env, 157 jobject obj, 158 jstring script, 159 jobject callback, 160 jboolean start_renderer); 161 int GetNativeImeAdapter(JNIEnv* env, jobject obj); 162 void SetFocus(JNIEnv* env, jobject obj, jboolean focused); 163 void ScrollFocusedEditableNodeIntoView(JNIEnv* env, jobject obj); 164 void UndoScrollFocusedEditableNodeIntoView(JNIEnv* env, jobject obj); 165 166 jint GetBackgroundColor(JNIEnv* env, jobject obj); 167 void SetBackgroundColor(JNIEnv* env, jobject obj, jint color); 168 void OnShow(JNIEnv* env, jobject obj); 169 void OnHide(JNIEnv* env, jobject obj); 170 void ClearSslPreferences(JNIEnv* env, jobject /* obj */); 171 void SetUseDesktopUserAgent(JNIEnv* env, 172 jobject /* obj */, 173 jboolean state, 174 jboolean reload_on_state_change); 175 bool GetUseDesktopUserAgent(JNIEnv* env, jobject /* obj */); 176 void Show(); 177 void Hide(); 178 void AddJavascriptInterface(JNIEnv* env, 179 jobject obj, 180 jobject object, 181 jstring name, 182 jclass safe_annotation_clazz, 183 jobject retained_object_set); 184 void RemoveJavascriptInterface(JNIEnv* env, jobject obj, jstring name); 185 int GetNavigationHistory(JNIEnv* env, jobject obj, jobject history); 186 void GetDirectedNavigationHistory(JNIEnv* env, 187 jobject obj, 188 jobject history, 189 jboolean is_forward, 190 jint max_entries); 191 base::android::ScopedJavaLocalRef<jstring> 192 GetOriginalUrlForActiveNavigationEntry(JNIEnv* env, jobject obj); 193 void UpdateVSyncParameters(JNIEnv* env, jobject obj, jlong timebase_micros, 194 jlong interval_micros); 195 void OnVSync(JNIEnv* env, jobject /* obj */, jlong frame_time_micros); 196 jboolean OnAnimate(JNIEnv* env, jobject /* obj */, jlong frame_time_micros); 197 jboolean PopulateBitmapFromCompositor(JNIEnv* env, 198 jobject obj, 199 jobject jbitmap); 200 void WasResized(JNIEnv* env, jobject obj); 201 jboolean IsRenderWidgetHostViewReady(JNIEnv* env, jobject obj); 202 void ExitFullscreen(JNIEnv* env, jobject obj); 203 void UpdateTopControlsState(JNIEnv* env, 204 jobject obj, 205 bool enable_hiding, 206 bool enable_showing, 207 bool animate); 208 void ShowImeIfNeeded(JNIEnv* env, jobject obj); 209 210 void ShowInterstitialPage(JNIEnv* env, 211 jobject obj, 212 jstring jurl, 213 jint delegate); 214 jboolean IsShowingInterstitialPage(JNIEnv* env, jobject obj); 215 216 void AttachExternalVideoSurface(JNIEnv* env, 217 jobject obj, 218 jint player_id, 219 jobject jsurface); 220 void DetachExternalVideoSurface(JNIEnv* env, jobject obj, jint player_id); 221 void SetAccessibilityEnabled(JNIEnv* env, jobject obj, bool enabled); 222 223 // -------------------------------------------------------------------------- 224 // Public methods that call to Java via JNI 225 // -------------------------------------------------------------------------- 226 227 // Creates a popup menu with |items|. 228 // |multiple| defines if it should support multi-select. 229 // If not |multiple|, |selected_item| sets the initially selected item. 230 // Otherwise, item's "checked" flag selects it. 231 void ShowSelectPopupMenu(const std::vector<MenuItem>& items, 232 int selected_item, 233 bool multiple); 234 235 void OnTabCrashed(); 236 237 // All sizes and offsets are in CSS pixels as cached by the renderer. 238 void UpdateFrameInfo(const gfx::Vector2dF& scroll_offset, 239 float page_scale_factor, 240 const gfx::Vector2dF& page_scale_factor_limits, 241 const gfx::SizeF& content_size, 242 const gfx::SizeF& viewport_size, 243 const gfx::Vector2dF& controls_offset, 244 const gfx::Vector2dF& content_offset, 245 float overdraw_bottom_height); 246 247 void UpdateImeAdapter(int native_ime_adapter, int text_input_type, 248 const std::string& text, 249 int selection_start, int selection_end, 250 int composition_start, int composition_end, 251 bool show_ime_if_needed); 252 void ProcessImeBatchStateAck(bool is_begin); 253 void SetTitle(const string16& title); 254 void OnBackgroundColorChanged(SkColor color); 255 256 bool HasFocus(); 257 void ConfirmTouchEvent(InputEventAckState ack_result); 258 void UnhandledFlingStartEvent(); 259 void HasTouchEventHandlers(bool need_touch_events); 260 void OnSelectionChanged(const std::string& text); 261 void OnSelectionBoundsChanged( 262 const ViewHostMsg_SelectionBounds_Params& params); 263 264 void StartContentIntent(const GURL& content_url); 265 266 // Shows the disambiguation popup 267 // |target_rect| --> window coordinates which |zoomed_bitmap| represents 268 // |zoomed_bitmap| --> magnified image of potential touch targets 269 void ShowDisambiguationPopup( 270 const gfx::Rect& target_rect, const SkBitmap& zoomed_bitmap); 271 272 // Creates a java-side smooth scroller. Used by 273 // chrome.gpuBenchmarking.smoothScrollBy. 274 base::android::ScopedJavaLocalRef<jobject> CreateSmoothScroller( 275 bool scroll_down, int mouse_event_x, int mouse_event_y); 276 277 // Notifies the java object about the external surface, requesting for one if 278 // necessary. 279 void NotifyExternalSurface( 280 int player_id, bool is_request, const gfx::RectF& rect); 281 282 base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient(); 283 284 // Returns the context that the ContentViewCore was created with, it would 285 // typically be an Activity context for an on screen view. 286 base::android::ScopedJavaLocalRef<jobject> GetContext(); 287 288 // -------------------------------------------------------------------------- 289 // Methods called from native code 290 // -------------------------------------------------------------------------- 291 292 gfx::Size GetPhysicalBackingSize() const; 293 gfx::Size GetViewportSizeDip() const; 294 gfx::Size GetViewportSizeOffsetDip() const; 295 float GetOverdrawBottomHeightDip() const; 296 297 void AttachLayer(scoped_refptr<cc::Layer> layer); 298 void RemoveLayer(scoped_refptr<cc::Layer> layer); 299 void SetNeedsBeginFrame(bool enabled); 300 void SetNeedsAnimate(); 301 302 private: 303 class ContentViewUserData; 304 305 friend class ContentViewUserData; 306 virtual ~ContentViewCoreImpl(); 307 308 // NotificationObserver implementation. 309 virtual void Observe(int type, 310 const NotificationSource& source, 311 const NotificationDetails& details) OVERRIDE; 312 313 // -------------------------------------------------------------------------- 314 // Other private methods and data 315 // -------------------------------------------------------------------------- 316 317 void InitWebContents(); 318 319 RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid(); 320 321 float GetTouchPaddingDip(); 322 323 WebKit::WebGestureEvent MakeGestureEvent( 324 WebKit::WebInputEvent::Type type, long time_ms, float x, float y) const; 325 326 void SendBeginFrame(base::TimeTicks frame_time); 327 328 gfx::Size GetViewportSizePix() const; 329 gfx::Size GetViewportSizeOffsetPix() const; 330 331 void DeleteScaledSnapshotTexture(); 332 333 void SendGestureEvent(const WebKit::WebGestureEvent& event); 334 335 // Checks if there there is a corresponding renderer process and updates 336 // |tab_crashed_| accordingly. 337 void UpdateTabCrashedFlag(); 338 339 // Update focus state of the RenderWidgetHostView. 340 void SetFocusInternal(bool focused); 341 342 // A weak reference to the Java ContentViewCore object. 343 JavaObjectWeakGlobalRef java_ref_; 344 345 NotificationRegistrar notification_registrar_; 346 347 // Reference to the current WebContents used to determine how and what to 348 // display in the ContentViewCore. 349 WebContentsImpl* web_contents_; 350 351 // A compositor layer containing any layer that should be shown. 352 scoped_refptr<cc::Layer> root_layer_; 353 354 // Whether the renderer backing this ContentViewCore has crashed. 355 bool tab_crashed_; 356 357 // Device scale factor. 358 float dpi_scale_; 359 360 // Variables used to keep track of frame timestamps and deadlines. 361 base::TimeDelta vsync_interval_; 362 base::TimeDelta expected_browser_composite_time_; 363 364 // The Android view that can be used to add and remove decoration layers 365 // like AutofillPopup. 366 ui::ViewAndroid* view_android_; 367 368 // The owning window that has a hold of main application activity. 369 ui::WindowAndroid* window_android_; 370 371 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl); 372 }; 373 374 bool RegisterContentViewCore(JNIEnv* env); 375 376 } // namespace content 377 378 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_ 379