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_weak_ref.h" 12 #include "base/compiler_specific.h" 13 #include "base/i18n/rtl.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/process/process.h" 16 #include "content/browser/renderer_host/render_widget_host_view_android.h" 17 #include "content/browser/web_contents/web_contents_impl.h" 18 #include "content/public/browser/android/content_view_core.h" 19 #include "content/public/browser/web_contents_observer.h" 20 #include "third_party/WebKit/public/web/WebInputEvent.h" 21 #include "ui/gfx/rect.h" 22 #include "ui/gfx/rect_f.h" 23 #include "url/gurl.h" 24 25 namespace ui { 26 class ViewAndroid; 27 class WindowAndroid; 28 } 29 30 namespace content { 31 class GinJavaBridgeDispatcherHost; 32 class RenderWidgetHostViewAndroid; 33 struct MenuItem; 34 35 // TODO(jrg): this is a shell. Upstream the rest. 36 class ContentViewCoreImpl : public ContentViewCore, 37 public WebContentsObserver { 38 public: 39 static ContentViewCoreImpl* FromWebContents(WebContents* web_contents); 40 ContentViewCoreImpl(JNIEnv* env, 41 jobject obj, 42 WebContents* web_contents, 43 ui::ViewAndroid* view_android, 44 ui::WindowAndroid* window_android, 45 jobject java_bridge_retained_object_set); 46 47 // ContentViewCore implementation. 48 virtual base::android::ScopedJavaLocalRef<jobject> GetJavaObject() OVERRIDE; 49 virtual WebContents* GetWebContents() const OVERRIDE; 50 virtual ui::ViewAndroid* GetViewAndroid() const OVERRIDE; 51 virtual ui::WindowAndroid* GetWindowAndroid() const OVERRIDE; 52 virtual scoped_refptr<cc::Layer> GetLayer() const OVERRIDE; 53 virtual void LoadUrl(NavigationController::LoadURLParams& params) OVERRIDE; 54 virtual void ShowPastePopup(int x, int y) OVERRIDE; 55 virtual void GetScaledContentBitmap( 56 float scale, 57 jobject bitmap_config, 58 gfx::Rect src_subrect, 59 const base::Callback<void(bool, const SkBitmap&)>& result_callback) 60 OVERRIDE; 61 virtual float GetDpiScale() const OVERRIDE; 62 virtual void PauseVideo() OVERRIDE; 63 virtual void PauseOrResumeGeolocation(bool should_pause) OVERRIDE; 64 virtual void PauseOrResumeVideoCaptureStream(bool should_pause) OVERRIDE; 65 virtual void RequestTextSurroundingSelection( 66 int max_length, 67 const base::Callback<void(const base::string16& content, 68 int start_offset, 69 int end_offset)>& callback) OVERRIDE; 70 71 // -------------------------------------------------------------------------- 72 // Methods called from Java via JNI 73 // -------------------------------------------------------------------------- 74 75 base::android::ScopedJavaLocalRef<jobject> GetWebContentsAndroid(JNIEnv* env, 76 jobject obj); 77 78 void OnJavaContentViewCoreDestroyed(JNIEnv* env, jobject obj); 79 80 // Notifies the ContentViewCore that items were selected in the currently 81 // showing select popup. 82 void SelectPopupMenuItems(JNIEnv* env, jobject obj, jintArray indices); 83 84 void LoadUrl( 85 JNIEnv* env, jobject obj, 86 jstring url, 87 jint load_url_type, 88 jint transition_type, 89 jstring j_referrer_url, 90 jint referrer_policy, 91 jint ua_override_option, 92 jstring extra_headers, 93 jbyteArray post_data, 94 jstring base_url_for_data_url, 95 jstring virtual_url_for_data_url, 96 jboolean can_load_local_resources, 97 jboolean is_renderer_initiated); 98 base::android::ScopedJavaLocalRef<jstring> GetURL(JNIEnv* env, jobject) const; 99 jboolean IsIncognito(JNIEnv* env, jobject obj); 100 void SendOrientationChangeEvent(JNIEnv* env, jobject obj, jint orientation); 101 jboolean OnTouchEvent(JNIEnv* env, 102 jobject obj, 103 jobject motion_event, 104 jlong time_ms, 105 jint android_action, 106 jint pointer_count, 107 jint history_size, 108 jint action_index, 109 jfloat pos_x_0, 110 jfloat pos_y_0, 111 jfloat pos_x_1, 112 jfloat pos_y_1, 113 jint pointer_id_0, 114 jint pointer_id_1, 115 jfloat touch_major_0, 116 jfloat touch_major_1, 117 jfloat raw_pos_x, 118 jfloat raw_pos_y, 119 jint android_tool_type_0, 120 jint android_tool_type_1, 121 jint android_button_state); 122 jboolean SendMouseMoveEvent(JNIEnv* env, 123 jobject obj, 124 jlong time_ms, 125 jfloat x, 126 jfloat y); 127 jboolean SendMouseWheelEvent(JNIEnv* env, 128 jobject obj, 129 jlong time_ms, 130 jfloat x, 131 jfloat y, 132 jfloat vertical_axis); 133 void ScrollBegin(JNIEnv* env, jobject obj, jlong time_ms, 134 jfloat x, jfloat y, jfloat hintx, jfloat hinty); 135 void ScrollEnd(JNIEnv* env, jobject obj, jlong time_ms); 136 void ScrollBy(JNIEnv* env, jobject obj, jlong time_ms, 137 jfloat x, jfloat y, jfloat dx, jfloat dy); 138 void FlingStart(JNIEnv* env, jobject obj, jlong time_ms, 139 jfloat x, jfloat y, jfloat vx, jfloat vy); 140 void FlingCancel(JNIEnv* env, jobject obj, jlong time_ms); 141 void SingleTap(JNIEnv* env, jobject obj, jlong time_ms, 142 jfloat x, jfloat y); 143 void DoubleTap(JNIEnv* env, jobject obj, jlong time_ms, 144 jfloat x, jfloat y) ; 145 void LongPress(JNIEnv* env, jobject obj, jlong time_ms, 146 jfloat x, jfloat y); 147 void PinchBegin(JNIEnv* env, jobject obj, jlong time_ms, jfloat x, jfloat y); 148 void PinchEnd(JNIEnv* env, jobject obj, jlong time_ms); 149 void PinchBy(JNIEnv* env, jobject obj, jlong time_ms, 150 jfloat x, jfloat y, jfloat delta); 151 void SelectBetweenCoordinates(JNIEnv* env, jobject obj, 152 jfloat x1, jfloat y1, 153 jfloat x2, jfloat y2); 154 void MoveCaret(JNIEnv* env, jobject obj, jfloat x, jfloat y); 155 156 void ResetGestureDetection(JNIEnv* env, jobject obj); 157 void SetDoubleTapSupportEnabled(JNIEnv* env, jobject obj, jboolean enabled); 158 void SetMultiTouchZoomSupportEnabled(JNIEnv* env, 159 jobject obj, 160 jboolean enabled); 161 162 void LoadIfNecessary(JNIEnv* env, jobject obj); 163 void RequestRestoreLoad(JNIEnv* env, jobject obj); 164 void Reload(JNIEnv* env, jobject obj, jboolean check_for_repost); 165 void ReloadIgnoringCache(JNIEnv* env, jobject obj, jboolean check_for_repost); 166 void CancelPendingReload(JNIEnv* env, jobject obj); 167 void ContinuePendingReload(JNIEnv* env, jobject obj); 168 void AddStyleSheetByURL(JNIEnv* env, jobject obj, jstring url); 169 void ClearHistory(JNIEnv* env, jobject obj); 170 void EvaluateJavaScript(JNIEnv* env, 171 jobject obj, 172 jstring script, 173 jobject callback, 174 jboolean start_renderer); 175 long GetNativeImeAdapter(JNIEnv* env, jobject obj); 176 void SetFocus(JNIEnv* env, jobject obj, jboolean focused); 177 void ScrollFocusedEditableNodeIntoView(JNIEnv* env, jobject obj); 178 void SelectWordAroundCaret(JNIEnv* env, jobject obj); 179 180 jint GetBackgroundColor(JNIEnv* env, jobject obj); 181 void SetBackgroundColor(JNIEnv* env, jobject obj, jint color); 182 void OnShow(JNIEnv* env, jobject obj); 183 void OnHide(JNIEnv* env, jobject obj); 184 void ClearSslPreferences(JNIEnv* env, jobject /* obj */); 185 void SetUseDesktopUserAgent(JNIEnv* env, 186 jobject /* obj */, 187 jboolean state, 188 jboolean reload_on_state_change); 189 bool GetUseDesktopUserAgent(JNIEnv* env, jobject /* obj */); 190 void Show(); 191 void Hide(); 192 void SetAllowJavascriptInterfacesInspection(JNIEnv* env, 193 jobject obj, 194 jboolean allow); 195 void AddJavascriptInterface(JNIEnv* env, 196 jobject obj, 197 jobject object, 198 jstring name, 199 jclass safe_annotation_clazz); 200 void RemoveJavascriptInterface(JNIEnv* env, jobject obj, jstring name); 201 int GetNavigationHistory(JNIEnv* env, jobject obj, jobject history); 202 void GetDirectedNavigationHistory(JNIEnv* env, 203 jobject obj, 204 jobject history, 205 jboolean is_forward, 206 jint max_entries); 207 base::android::ScopedJavaLocalRef<jstring> 208 GetOriginalUrlForActiveNavigationEntry(JNIEnv* env, jobject obj); 209 void WasResized(JNIEnv* env, jobject obj); 210 jboolean IsRenderWidgetHostViewReady(JNIEnv* env, jobject obj); 211 void ExitFullscreen(JNIEnv* env, jobject obj); 212 void UpdateTopControlsState(JNIEnv* env, 213 jobject obj, 214 bool enable_hiding, 215 bool enable_showing, 216 bool animate); 217 void ShowImeIfNeeded(JNIEnv* env, jobject obj); 218 219 void ShowInterstitialPage(JNIEnv* env, 220 jobject obj, 221 jstring jurl, 222 jlong delegate); 223 jboolean IsShowingInterstitialPage(JNIEnv* env, jobject obj); 224 225 void SetAccessibilityEnabled(JNIEnv* env, jobject obj, bool enabled); 226 227 void ExtractSmartClipData(JNIEnv* env, 228 jobject obj, 229 jint x, 230 jint y, 231 jint width, 232 jint height); 233 234 void SetBackgroundOpaque(JNIEnv* env, jobject jobj, jboolean opaque); 235 236 jint GetCurrentRenderProcessId(JNIEnv* env, jobject obj); 237 238 // -------------------------------------------------------------------------- 239 // Public methods that call to Java via JNI 240 // -------------------------------------------------------------------------- 241 242 void OnSmartClipDataExtracted(const base::string16& text, 243 const base::string16& html, 244 const gfx::Rect& clip_rect); 245 246 // Creates a popup menu with |items|. 247 // |multiple| defines if it should support multi-select. 248 // If not |multiple|, |selected_item| sets the initially selected item. 249 // Otherwise, item's "checked" flag selects it. 250 void ShowSelectPopupMenu(const gfx::Rect& bounds, 251 const std::vector<MenuItem>& items, 252 int selected_item, 253 bool multiple); 254 // Hides a visible popup menu. 255 void HideSelectPopupMenu(); 256 257 // All sizes and offsets are in CSS pixels as cached by the renderer. 258 void UpdateFrameInfo(const gfx::Vector2dF& scroll_offset, 259 float page_scale_factor, 260 const gfx::Vector2dF& page_scale_factor_limits, 261 const gfx::SizeF& content_size, 262 const gfx::SizeF& viewport_size, 263 const gfx::Vector2dF& controls_offset, 264 const gfx::Vector2dF& content_offset, 265 float overdraw_bottom_height); 266 267 void UpdateImeAdapter(long native_ime_adapter, int text_input_type, 268 const std::string& text, 269 int selection_start, int selection_end, 270 int composition_start, int composition_end, 271 bool show_ime_if_needed, bool is_non_ime_change); 272 void SetTitle(const base::string16& title); 273 void OnBackgroundColorChanged(SkColor color); 274 275 bool HasFocus(); 276 void OnGestureEventAck(const blink::WebGestureEvent& event, 277 InputEventAckState ack_result); 278 bool FilterInputEvent(const blink::WebInputEvent& event); 279 void OnSelectionChanged(const std::string& text); 280 void OnSelectionBoundsChanged( 281 const ViewHostMsg_SelectionBounds_Params& params); 282 283 void StartContentIntent(const GURL& content_url); 284 285 // Shows the disambiguation popup 286 // |target_rect| --> window coordinates which |zoomed_bitmap| represents 287 // |zoomed_bitmap| --> magnified image of potential touch targets 288 void ShowDisambiguationPopup( 289 const gfx::Rect& target_rect, const SkBitmap& zoomed_bitmap); 290 291 // Creates a java-side touch event, used for injecting touch event for 292 // testing/benchmarking purposes 293 base::android::ScopedJavaLocalRef<jobject> CreateTouchEventSynthesizer(); 294 295 base::android::ScopedJavaLocalRef<jobject> GetContentVideoViewClient(); 296 297 // Returns the context that the ContentViewCore was created with, it would 298 // typically be an Activity context for an on screen view. 299 base::android::ScopedJavaLocalRef<jobject> GetContext(); 300 301 // Returns True if the given media should be blocked to load. 302 bool ShouldBlockMediaRequest(const GURL& url); 303 304 void DidStopFlinging(); 305 306 // Returns the viewport size after accounting for the viewport offset. 307 gfx::Size GetViewSize() const; 308 309 void SetAccessibilityEnabledInternal(bool enabled); 310 311 void ShowSelectionHandlesAutomatically() const; 312 313 // -------------------------------------------------------------------------- 314 // Methods called from native code 315 // -------------------------------------------------------------------------- 316 317 gfx::Size GetPhysicalBackingSize() const; 318 gfx::Size GetViewportSizeDip() const; 319 gfx::Size GetViewportSizeOffsetDip() const; 320 float GetOverdrawBottomHeightDip() const; 321 322 void AttachLayer(scoped_refptr<cc::Layer> layer); 323 void RemoveLayer(scoped_refptr<cc::Layer> layer); 324 325 private: 326 class ContentViewUserData; 327 328 friend class ContentViewUserData; 329 virtual ~ContentViewCoreImpl(); 330 331 // WebContentsObserver implementation. 332 virtual void RenderViewReady() OVERRIDE; 333 virtual void RenderViewHostChanged(RenderViewHost* old_host, 334 RenderViewHost* new_host) OVERRIDE; 335 virtual void WebContentsDestroyed() OVERRIDE; 336 337 // -------------------------------------------------------------------------- 338 // Other private methods and data 339 // -------------------------------------------------------------------------- 340 341 void InitWebContents(); 342 343 RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid(); 344 345 blink::WebGestureEvent MakeGestureEvent( 346 blink::WebInputEvent::Type type, int64 time_ms, float x, float y) const; 347 348 gfx::Size GetViewportSizePix() const; 349 gfx::Size GetViewportSizeOffsetPix() const; 350 351 void DeleteScaledSnapshotTexture(); 352 353 bool OnMotionEvent(const ui::MotionEvent& event); 354 void SendGestureEvent(const blink::WebGestureEvent& event); 355 356 // Update focus state of the RenderWidgetHostView. 357 void SetFocusInternal(bool focused); 358 359 // Send device_orientation_ to renderer. 360 void SendOrientationChangeEventInternal(); 361 362 float dpi_scale() const { return dpi_scale_; } 363 364 // A weak reference to the Java ContentViewCore object. 365 JavaObjectWeakGlobalRef java_ref_; 366 367 // Reference to the current WebContents used to determine how and what to 368 // display in the ContentViewCore. 369 WebContentsImpl* web_contents_; 370 371 // A compositor layer containing any layer that should be shown. 372 scoped_refptr<cc::Layer> root_layer_; 373 374 // Device scale factor. 375 float dpi_scale_; 376 377 // The Android view that can be used to add and remove decoration layers 378 // like AutofillPopup. 379 ui::ViewAndroid* view_android_; 380 381 // The owning window that has a hold of main application activity. 382 ui::WindowAndroid* window_android_; 383 384 // The cache of device's current orientation set from Java side, this value 385 // will be sent to Renderer once it is ready. 386 int device_orientation_; 387 388 bool accessibility_enabled_; 389 390 // Manages injecting Java objects. 391 scoped_ptr<GinJavaBridgeDispatcherHost> 392 java_bridge_dispatcher_host_; 393 394 DISALLOW_COPY_AND_ASSIGN(ContentViewCoreImpl); 395 }; 396 397 bool RegisterContentViewCore(JNIEnv* env); 398 399 } // namespace content 400 401 #endif // CONTENT_BROWSER_ANDROID_CONTENT_VIEW_CORE_IMPL_H_ 402