Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: android_webview/java/src/org/chromium/android_webview/AwContents.java

Issue 2103243002: Factor out ContentViewAndroidDelegate (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed comments Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.android_webview; 5 package org.chromium.android_webview;
6 6
7 import android.annotation.SuppressLint; 7 import android.annotation.SuppressLint;
8 import android.annotation.TargetApi; 8 import android.annotation.TargetApi;
9 import android.app.Activity; 9 import android.app.Activity;
10 import android.content.ComponentCallbacks2; 10 import android.content.ComponentCallbacks2;
(...skipping 24 matching lines...) Expand all
35 import android.view.ViewGroup; 35 import android.view.ViewGroup;
36 import android.view.ViewStructure; 36 import android.view.ViewStructure;
37 import android.view.accessibility.AccessibilityEvent; 37 import android.view.accessibility.AccessibilityEvent;
38 import android.view.accessibility.AccessibilityNodeInfo; 38 import android.view.accessibility.AccessibilityNodeInfo;
39 import android.view.accessibility.AccessibilityNodeProvider; 39 import android.view.accessibility.AccessibilityNodeProvider;
40 import android.view.animation.AnimationUtils; 40 import android.view.animation.AnimationUtils;
41 import android.view.inputmethod.EditorInfo; 41 import android.view.inputmethod.EditorInfo;
42 import android.view.inputmethod.InputConnection; 42 import android.view.inputmethod.InputConnection;
43 import android.webkit.JavascriptInterface; 43 import android.webkit.JavascriptInterface;
44 import android.webkit.ValueCallback; 44 import android.webkit.ValueCallback;
45 import android.widget.FrameLayout;
45 46
46 import org.chromium.android_webview.permission.AwGeolocationCallback; 47 import org.chromium.android_webview.permission.AwGeolocationCallback;
47 import org.chromium.android_webview.permission.AwPermissionRequest; 48 import org.chromium.android_webview.permission.AwPermissionRequest;
48 import org.chromium.base.LocaleUtils; 49 import org.chromium.base.LocaleUtils;
49 import org.chromium.base.Log; 50 import org.chromium.base.Log;
50 import org.chromium.base.ThreadUtils; 51 import org.chromium.base.ThreadUtils;
51 import org.chromium.base.TraceEvent; 52 import org.chromium.base.TraceEvent;
52 import org.chromium.base.VisibleForTesting; 53 import org.chromium.base.VisibleForTesting;
53 import org.chromium.base.annotations.CalledByNative; 54 import org.chromium.base.annotations.CalledByNative;
54 import org.chromium.base.annotations.JNINamespace; 55 import org.chromium.base.annotations.JNINamespace;
55 import org.chromium.base.annotations.SuppressFBWarnings; 56 import org.chromium.base.annotations.SuppressFBWarnings;
56 import org.chromium.components.navigation_interception.InterceptNavigationDelega te; 57 import org.chromium.components.navigation_interception.InterceptNavigationDelega te;
57 import org.chromium.components.navigation_interception.NavigationParams; 58 import org.chromium.components.navigation_interception.NavigationParams;
58 import org.chromium.content.browser.ContentViewClient; 59 import org.chromium.content.browser.ContentViewClient;
59 import org.chromium.content.browser.ContentViewCore; 60 import org.chromium.content.browser.ContentViewCore;
60 import org.chromium.content.browser.ContentViewStatics; 61 import org.chromium.content.browser.ContentViewStatics;
62 import org.chromium.content.browser.RenderCoordinates;
61 import org.chromium.content.browser.SmartClipProvider; 63 import org.chromium.content.browser.SmartClipProvider;
62 import org.chromium.content.common.CleanupReference; 64 import org.chromium.content.common.CleanupReference;
63 import org.chromium.content_public.browser.GestureStateListener; 65 import org.chromium.content_public.browser.GestureStateListener;
64 import org.chromium.content_public.browser.JavaScriptCallback; 66 import org.chromium.content_public.browser.JavaScriptCallback;
65 import org.chromium.content_public.browser.LoadUrlParams; 67 import org.chromium.content_public.browser.LoadUrlParams;
66 import org.chromium.content_public.browser.NavigationController; 68 import org.chromium.content_public.browser.NavigationController;
67 import org.chromium.content_public.browser.NavigationHistory; 69 import org.chromium.content_public.browser.NavigationHistory;
68 import org.chromium.content_public.browser.WebContents; 70 import org.chromium.content_public.browser.WebContents;
69 import org.chromium.content_public.browser.navigation_controller.LoadURLType; 71 import org.chromium.content_public.browser.navigation_controller.LoadURLType;
70 import org.chromium.content_public.browser.navigation_controller.UserAgentOverri deOption; 72 import org.chromium.content_public.browser.navigation_controller.UserAgentOverri deOption;
71 import org.chromium.content_public.common.Referrer; 73 import org.chromium.content_public.common.Referrer;
72 import org.chromium.net.NetError; 74 import org.chromium.net.NetError;
73 import org.chromium.net.NetworkChangeNotifier; 75 import org.chromium.net.NetworkChangeNotifier;
74 import org.chromium.ui.base.ActivityWindowAndroid; 76 import org.chromium.ui.base.ActivityWindowAndroid;
75 import org.chromium.ui.base.PageTransition; 77 import org.chromium.ui.base.PageTransition;
78 import org.chromium.ui.base.ViewAndroidDelegate;
76 import org.chromium.ui.base.WindowAndroid; 79 import org.chromium.ui.base.WindowAndroid;
77 import org.chromium.ui.gfx.DeviceDisplayInfo; 80 import org.chromium.ui.gfx.DeviceDisplayInfo;
78 81
79 import java.io.File; 82 import java.io.File;
80 import java.lang.annotation.Annotation; 83 import java.lang.annotation.Annotation;
84 import java.lang.ref.WeakReference;
81 import java.net.MalformedURLException; 85 import java.net.MalformedURLException;
82 import java.net.URL; 86 import java.net.URL;
83 import java.util.HashMap; 87 import java.util.HashMap;
88 import java.util.LinkedHashMap;
84 import java.util.Locale; 89 import java.util.Locale;
85 import java.util.Map; 90 import java.util.Map;
91 import java.util.Map.Entry;
86 import java.util.WeakHashMap; 92 import java.util.WeakHashMap;
87 import java.util.concurrent.Callable; 93 import java.util.concurrent.Callable;
88 94
89 /** 95 /**
90 * Exposes the native AwContents class, and together these classes wrap the Cont entViewCore 96 * Exposes the native AwContents class, and together these classes wrap the Cont entViewCore
91 * and Browser components that are required to implement Android WebView API. Th is is the 97 * and Browser components that are required to implement Android WebView API. Th is is the
92 * primary entry point for the WebViewProvider implementation; it holds a 1:1 ob ject 98 * primary entry point for the WebViewProvider implementation; it holds a 1:1 ob ject
93 * relationship with application WebView instances. 99 * relationship with application WebView instances.
94 * (We define this class independent of the hidden WebViewProvider interfaces, t o allow 100 * (We define this class independent of the hidden WebViewProvider interfaces, t o allow
95 * continuous build & test in the open source SDK-based tree). 101 * continuous build & test in the open source SDK-based tree).
96 */ 102 */
97 @JNINamespace("android_webview") 103 @JNINamespace("android_webview")
98 public class AwContents implements SmartClipProvider, 104 public class AwContents implements SmartClipProvider,
99 PostMessageSender.PostMessageSenderDelegate { 105 PostMessageSender.PostMessageSenderDelegate {
100 private static final String TAG = "AwContents"; 106 private static final String TAG = "AwContents";
101 private static final boolean TRACE = false; 107 private static final boolean TRACE = false;
102 private static final int NO_WARN = 0; 108 private static final int NO_WARN = 0;
103 private static final int WARN = 1; 109 private static final int WARN = 1;
104 110
105 private static final String WEB_ARCHIVE_EXTENSION = ".mht"; 111 private static final String WEB_ARCHIVE_EXTENSION = ".mht";
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
257 private long mNativeAwContents; 263 private long mNativeAwContents;
258 private final AwBrowserContext mBrowserContext; 264 private final AwBrowserContext mBrowserContext;
259 // mContainerView and mCurrentFunctor form a pair that needs to stay in sync . 265 // mContainerView and mCurrentFunctor form a pair that needs to stay in sync .
260 private ViewGroup mContainerView; 266 private ViewGroup mContainerView;
261 private AwGLFunctor mCurrentFunctor; 267 private AwGLFunctor mCurrentFunctor;
262 private AwGLFunctor mInitialFunctor; 268 private AwGLFunctor mInitialFunctor;
263 private AwGLFunctor mFullScreenFunctor; // Only non-null when in fullscreen mode. 269 private AwGLFunctor mFullScreenFunctor; // Only non-null when in fullscreen mode.
264 private final Context mContext; 270 private final Context mContext;
265 private final int mAppTargetSdkVersion; 271 private final int mAppTargetSdkVersion;
266 private ContentViewCore mContentViewCore; 272 private ContentViewCore mContentViewCore;
273 private AwViewAndroidDelegate mViewAndroidDelegate;
267 private WindowAndroidWrapper mWindowAndroid; 274 private WindowAndroidWrapper mWindowAndroid;
268 private WebContents mWebContents; 275 private WebContents mWebContents;
269 private NavigationController mNavigationController; 276 private NavigationController mNavigationController;
270 private final AwContentsClient mContentsClient; 277 private final AwContentsClient mContentsClient;
271 private final AwContentViewClient mContentViewClient; 278 private final AwContentViewClient mContentViewClient;
272 private AwWebContentsObserver mWebContentsObserver; 279 private AwWebContentsObserver mWebContentsObserver;
273 private final AwContentsClientBridge mContentsClientBridge; 280 private final AwContentsClientBridge mContentsClientBridge;
274 private final AwWebContentsDelegateAdapter mWebContentsDelegate; 281 private final AwWebContentsDelegateAdapter mWebContentsDelegate;
275 private final AwContentsBackgroundThreadClient mBackgroundThreadClient; 282 private final AwContentsBackgroundThreadClient mBackgroundThreadClient;
276 private final AwContentsIoThreadClient mIoThreadClient; 283 private final AwContentsIoThreadClient mIoThreadClient;
(...skipping 519 matching lines...) Expand 10 before | Expand all | Expand 10 after
796 mScrollAccessibilityHelper = new ScrollAccessibilityHelper(mContainerVie w); 803 mScrollAccessibilityHelper = new ScrollAccessibilityHelper(mContainerVie w);
797 804
798 setOverScrollMode(mContainerView.getOverScrollMode()); 805 setOverScrollMode(mContainerView.getOverScrollMode());
799 setScrollBarStyle(mInternalAccessAdapter.super_getScrollBarStyle()); 806 setScrollBarStyle(mInternalAccessAdapter.super_getScrollBarStyle());
800 807
801 setNewAwContents(nativeInit(mBrowserContext)); 808 setNewAwContents(nativeInit(mBrowserContext));
802 809
803 onContainerViewChanged(); 810 onContainerViewChanged();
804 } 811 }
805 812
806 private static ContentViewCore createAndInitializeContentViewCore(ViewGroup containerView, 813 private static void initializeContentViewCore(ContentViewCore contentViewCor e,
807 Context context, InternalAccessDelegate internalDispatcher, WebConte nts webContents, 814 ViewGroup containerView, Context context, ViewAndroidDelegate viewDe legate,
808 GestureStateListener gestureStateListener, 815 InternalAccessDelegate internalDispatcher, WebContents webContents,
809 ContentViewClient contentViewClient, 816 GestureStateListener gestureStateListener, ContentViewClient content ViewClient,
810 ContentViewCore.ZoomControlsDelegate zoomControlsDelegate, 817 ContentViewCore.ZoomControlsDelegate zoomControlsDelegate,
811 WindowAndroid windowAndroid) { 818 WindowAndroid windowAndroid) {
812 ContentViewCore contentViewCore = new ContentViewCore(context); 819 contentViewCore.initialize(containerView, viewDelegate, internalDispatch er, webContents,
813 contentViewCore.initialize(containerView, internalDispatcher, webContent s,
814 windowAndroid); 820 windowAndroid);
815 contentViewCore.addGestureStateListener(gestureStateListener); 821 contentViewCore.addGestureStateListener(gestureStateListener);
816 contentViewCore.setContentViewClient(contentViewClient); 822 contentViewCore.setContentViewClient(contentViewClient);
817 contentViewCore.setZoomControlsDelegate(zoomControlsDelegate); 823 contentViewCore.setZoomControlsDelegate(zoomControlsDelegate);
818 return contentViewCore; 824 }
825
826 /**
827 * Implementation of the interface {@link org.chromium.ui.base.ViewAndroid.V iewAndroidDelegate}
828 * for WebView.
829 */
830 public static class AwViewAndroidDelegate extends ViewAndroidDelegate {
831 /**
832 * The current container view. This view can be updated with
833 * {@link #updateCurrentContainerView()}. This needs to be a WeakReferen ce
834 * because ViewAndroidDelegate is held strongly native side, which other wise
835 * indefinitely prevents Android WebView from being garbage collected.
836 */
837 private WeakReference<ViewGroup> mContainerView;
838
839 /**
840 * List of anchor views stored in the order in which they were acquired mapped
841 * to their position.
842 */
843 private final Map<View, Position> mAnchorViews = new LinkedHashMap<>();
844
845 private final RenderCoordinates mRenderCoordinates;
846
847 /**
848 * Represents the position of an anchor view.
849 */
850 @VisibleForTesting
851 private static class Position {
852 public final float mX;
853 public final float mY;
854 public final float mWidth;
855 public final float mHeight;
856 public final int mLeftMargin;
857 public final int mTopMargin;
858
859 public Position(float x, float y, float width, float height, int lef tMargin,
860 int topMargin) {
861 mX = x;
862 mY = y;
863 mWidth = width;
864 mHeight = height;
865 mLeftMargin = leftMargin;
866 mTopMargin = topMargin;
867 }
868 }
869
870 AwViewAndroidDelegate(ViewGroup containerView, RenderCoordinates renderC oordinates) {
871 mContainerView = new WeakReference<>(containerView);
872 mRenderCoordinates = renderCoordinates;
873 }
874
875 @Override
876 public View acquireView() {
877 ViewGroup containerView = mContainerView.get();
878 if (containerView == null) return null;
879 View anchorView = new View(containerView.getContext());
880 containerView.addView(anchorView);
881 // |mAnchorViews| will be updated with the right view position in |s etViewPosition|.
882 mAnchorViews.put(anchorView, null);
883 return anchorView;
884 }
885
886 @Override
887 public void removeView(View anchorView) {
888 mAnchorViews.remove(anchorView);
889 ViewGroup containerView = mContainerView.get();
890 if (containerView != null) {
891 containerView.removeView(anchorView);
892 }
893 }
894
895 /**
896 * Updates the current container view to which this class delegates. Exis ting anchor views
897 * are transferred from the old to the new container view.
898 */
899 public void updateCurrentContainerView(ViewGroup containerView) {
900 ViewGroup oldContainerView = mContainerView.get();
901 mContainerView = new WeakReference<>(containerView);
902 for (Entry<View, Position> entry : mAnchorViews.entrySet()) {
903 View anchorView = entry.getKey();
904 Position position = entry.getValue();
905 if (oldContainerView != null) {
906 oldContainerView.removeView(anchorView);
907 }
908 containerView.addView(anchorView);
909 if (position != null) {
910 float scale = (float) DeviceDisplayInfo.create(containerView .getContext())
911 .getDIPScale();
912 setViewPosition(anchorView, position.mX, position.mY,
913 position.mWidth, position.mHeight, scale,
914 position.mLeftMargin, position.mTopMargin);
915 }
916 }
917 }
918
919 @SuppressWarnings("deprecation") // AbsoluteLayout
920 @Override
921 public void setViewPosition(View anchorView, float x, float y, float wid th, float height,
922 float scale, int leftMargin, int topMargin) {
923 if (!mAnchorViews.containsKey(anchorView)) return;
924 mAnchorViews.put(anchorView, new Position(x, y, width, height, leftM argin, topMargin));
925
926 ViewGroup containerView = getContainerView();
927 if (containerView instanceof FrameLayout) {
928 super.setViewPosition(anchorView, x, y, width, height, scale, le ftMargin,
929 topMargin);
930 return;
931 }
932 // This fixes the offset due to a difference in
933 // scrolling model of WebView vs. Chrome.
934 // TODO(sgurun) fix this to use mContainerViewAtCreation.getScroll[X /Y]()
935 // as it naturally accounts for scroll differences between
936 // these models.
937 leftMargin += mRenderCoordinates.getScrollXPixInt();
938 topMargin += mRenderCoordinates.getScrollYPixInt();
939
940 int scaledWidth = Math.round(width * scale);
941 int scaledHeight = Math.round(height * scale);
942 android.widget.AbsoluteLayout.LayoutParams lp =
943 new android.widget.AbsoluteLayout.LayoutParams(
944 scaledWidth, scaledHeight, leftMargin, topMargin);
945 anchorView.setLayoutParams(lp);
946 }
947
948 @Override
949 protected ViewGroup getContainerView() {
950 return mContainerView.get();
951 }
819 } 952 }
820 953
821 boolean isFullScreen() { 954 boolean isFullScreen() {
822 return mFullScreenTransitionsState.isFullScreen(); 955 return mFullScreenTransitionsState.isFullScreen();
823 } 956 }
824 957
825 /** 958 /**
826 * Transitions this {@link AwContents} to fullscreen mode and returns the 959 * Transitions this {@link AwContents} to fullscreen mode and returns the
827 * {@link View} where the contents will be drawn while in fullscreen, or nul l 960 * {@link View} where the contents will be drawn while in fullscreen, or nul l
828 * if this AwContents has already been destroyed. 961 * if this AwContents has already been destroyed.
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
910 1043
911 private void setContainerView(ViewGroup newContainerView, AwGLFunctor curren tFunctor) { 1044 private void setContainerView(ViewGroup newContainerView, AwGLFunctor curren tFunctor) {
912 // setWillNotDraw(false) is required since WebView draws it's own conten ts using it's 1045 // setWillNotDraw(false) is required since WebView draws it's own conten ts using it's
913 // container view. If this is ever not the case we should remove this, a s it removes 1046 // container view. If this is ever not the case we should remove this, a s it removes
914 // Android's gatherTransparentRegion optimization for the view. 1047 // Android's gatherTransparentRegion optimization for the view.
915 mContainerView = newContainerView; 1048 mContainerView = newContainerView;
916 mCurrentFunctor = currentFunctor; 1049 mCurrentFunctor = currentFunctor;
917 updateNativeAwGLFunctor(); 1050 updateNativeAwGLFunctor();
918 mContainerView.setWillNotDraw(false); 1051 mContainerView.setWillNotDraw(false);
919 1052
1053 mViewAndroidDelegate.updateCurrentContainerView(mContainerView);
920 mContentViewCore.setContainerView(mContainerView); 1054 mContentViewCore.setContainerView(mContainerView);
921 if (mAwPdfExporter != null) { 1055 if (mAwPdfExporter != null) {
922 mAwPdfExporter.setContainerView(mContainerView); 1056 mAwPdfExporter.setContainerView(mContainerView);
923 } 1057 }
924 mWebContentsDelegate.setContainerView(mContainerView); 1058 mWebContentsDelegate.setContainerView(mContainerView);
925 onContainerViewChanged(); 1059 onContainerViewChanged();
926 } 1060 }
927 1061
928 /** 1062 /**
929 * Reconciles the state of this AwContents object with the state of the new container view. 1063 * Reconciles the state of this AwContents object with the state of the new container view.
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
1030 mNativeAwContents = newAwContentsPtr; 1164 mNativeAwContents = newAwContentsPtr;
1031 nativeSetAwGLFunctor(mNativeAwContents, mInitialFunctor.getNativeAwGLFun ctor()); 1165 nativeSetAwGLFunctor(mNativeAwContents, mInitialFunctor.getNativeAwGLFun ctor());
1032 updateNativeAwGLFunctor(); 1166 updateNativeAwGLFunctor();
1033 // TODO(joth): when the native and java counterparts of AwBrowserContext are hooked up to 1167 // TODO(joth): when the native and java counterparts of AwBrowserContext are hooked up to
1034 // each other, we should update |mBrowserContext| according to the newly received native 1168 // each other, we should update |mBrowserContext| according to the newly received native
1035 // WebContent's browser context. 1169 // WebContent's browser context.
1036 1170
1037 WebContents webContents = nativeGetWebContents(mNativeAwContents); 1171 WebContents webContents = nativeGetWebContents(mNativeAwContents);
1038 1172
1039 mWindowAndroid = getWindowAndroid(mContext); 1173 mWindowAndroid = getWindowAndroid(mContext);
1040 mContentViewCore = createAndInitializeContentViewCore(mContainerView, mC ontext, 1174 mContentViewCore = new ContentViewCore(mContext);
1041 mInternalAccessAdapter, webContents, new AwGestureStateListener( ), 1175 mViewAndroidDelegate = new AwViewAndroidDelegate(mContainerView,
1042 mContentViewClient, mZoomControls, mWindowAndroid.getWindowAndro id()); 1176 mContentViewCore.getRenderCoordinates());
1177 initializeContentViewCore(mContentViewCore, mContainerView, mContext,
1178 mViewAndroidDelegate, mInternalAccessAdapter, webContents,
1179 new AwGestureStateListener(), mContentViewClient, mZoomControls,
1180 mWindowAndroid.getWindowAndroid());
1043 nativeSetJavaPeers(mNativeAwContents, this, mWebContentsDelegate, mConte ntsClientBridge, 1181 nativeSetJavaPeers(mNativeAwContents, this, mWebContentsDelegate, mConte ntsClientBridge,
1044 mIoThreadClient, mInterceptNavigationDelegate); 1182 mIoThreadClient, mInterceptNavigationDelegate);
1045 mWebContents = mContentViewCore.getWebContents(); 1183 mWebContents = mContentViewCore.getWebContents();
1046 mNavigationController = mWebContents.getNavigationController(); 1184 mNavigationController = mWebContents.getNavigationController();
1047 installWebContentsObserver(); 1185 installWebContentsObserver();
1048 mSettings.setWebContents(webContents); 1186 mSettings.setWebContents(webContents);
1049 nativeSetDipScale(mNativeAwContents, (float) mDIPScale); 1187 nativeSetDipScale(mNativeAwContents, (float) mDIPScale);
1050 updateContentViewCoreVisibility(); 1188 updateContentViewCoreVisibility();
1051 1189
1052 // The native side object has been bound to this java instance, so now i s the time to 1190 // The native side object has been bound to this java instance, so now i s the time to
(...skipping 2302 matching lines...) Expand 10 before | Expand all | Expand 10 after
3355 long resources); 3493 long resources);
3356 3494
3357 private native void nativePostMessageToFrame(long nativeAwContents, String f rameId, 3495 private native void nativePostMessageToFrame(long nativeAwContents, String f rameId,
3358 String message, String targetOrigin, int[] msgPorts); 3496 String message, String targetOrigin, int[] msgPorts);
3359 3497
3360 private native void nativeCreateMessageChannel(long nativeAwContents, AwMess agePort[] ports); 3498 private native void nativeCreateMessageChannel(long nativeAwContents, AwMess agePort[] ports);
3361 3499
3362 private native void nativeGrantFileSchemeAccesstoChildProcess(long nativeAwC ontents); 3500 private native void nativeGrantFileSchemeAccesstoChildProcess(long nativeAwC ontents);
3363 private native void nativeResumeLoadingCreatedPopupWebContents(long nativeAw Contents); 3501 private native void nativeResumeLoadingCreatedPopupWebContents(long nativeAw Contents);
3364 } 3502 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698