Upstream version 10.39.233.0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / android / core_internal / src / org / xwalk / core / internal / XWalkViewInternal.java
index ad93b27..f57dc48 100644 (file)
@@ -25,14 +25,14 @@ import java.io.File;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.lang.ref.WeakReference;
+import java.net.MalformedURLException;
+import java.net.URL;
 
 import org.chromium.base.ActivityState;
 import org.chromium.base.ApplicationStatus;
 import org.chromium.base.ApplicationStatus.ActivityStateListener;
 import org.chromium.base.CommandLine;
-
-import org.xwalk.core.internal.extension.XWalkExtensionManager;
-import org.xwalk.core.internal.extension.XWalkPathHelper;
+import org.xwalk.core.internal.extension.BuiltinXWalkExtensions;
 
 /**
  * <p>XWalkViewInternal represents an Android view for web apps/pages. Thus most of attributes
@@ -128,6 +128,7 @@ import org.xwalk.core.internal.extension.XWalkPathHelper;
  *   }
  * </pre>
  */
+@XWalkAPI(extendClass = FrameLayout.class, createExternally = true)
 public class XWalkViewInternal extends android.widget.FrameLayout {
 
     private class XWalkActivityStateListener implements ActivityStateListener {
@@ -150,7 +151,6 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
     private XWalkContent mContent;
     private Activity mActivity;
     private Context mContext;
-    private XWalkExtensionManager mExtensionManager;
     private boolean mIsHidden;
     private XWalkActivityStateListener mActivityStateListener;
 
@@ -158,11 +158,13 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * Normal reload mode as default.
      * @since 1.0
      */
+    @XWalkAPI
     public static final int RELOAD_NORMAL = 0;
     /**
      * Reload mode with bypassing the cache.
      * @since 1.0
      */
+    @XWalkAPI
     public static final int RELOAD_IGNORE_CACHE = 1;
 
     /**
@@ -171,12 +173,20 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @param attrs    an AttributeSet passed to our parent.
      * @since 1.0
      */
+    @XWalkAPI(preWrapperLines = {
+                  "        super(${param1}, ${param2});"},
+              postWrapperLines = {
+                  "        if (bridge == null) return;",
+                  "        addView((FrameLayout)bridge, new FrameLayout.LayoutParams(",
+                  "                FrameLayout.LayoutParams.MATCH_PARENT,",
+                  "                FrameLayout.LayoutParams.MATCH_PARENT));"})
     public XWalkViewInternal(Context context, AttributeSet attrs) {
-        super(context, attrs);
+        super(convertContext(context), attrs);
 
         checkThreadSafety();
-        mContext = context;
-        init(context, attrs);
+        mActivity = (Activity) context;
+        mContext = getContext();
+        init(mContext, attrs);
     }
 
     /**
@@ -186,14 +196,34 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @param activity the activity for this XWalkViewInternal.
      * @since 1.0
      */
+    @XWalkAPI(preWrapperLines = {
+                  "        super(${param1}, null);"},
+              postWrapperLines = {
+                  "        if (bridge == null) return;",
+                  "        addView((FrameLayout)bridge, new FrameLayout.LayoutParams(",
+                  "                FrameLayout.LayoutParams.MATCH_PARENT,",
+                  "                FrameLayout.LayoutParams.MATCH_PARENT));"})
     public XWalkViewInternal(Context context, Activity activity) {
-        super(context, null);
-        checkThreadSafety();
+        super(convertContext(context), null);
 
+        checkThreadSafety();
         // Make sure mActivity is initialized before calling 'init' method.
         mActivity = activity;
-        mContext = context;
-        init(context, null);
+        mContext = getContext();
+        init(mContext, null);
+    }
+
+    private static Context convertContext(Context context) {
+        Context ret = context;
+        Context bridgeContext = ReflectionHelper.getBridgeContext();
+        if (bridgeContext == null || context == null ||
+                bridgeContext.getPackageName().equals(context.getPackageName())) {
+            // Not acrossing package
+            ret = context;
+        } else {
+            ret = new MixedContext(bridgeContext, context);
+        }
+        return ret;
     }
 
     /**
@@ -222,6 +252,10 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
         return mContext;
     }
 
+    public void completeWindowCreation(XWalkViewInternal newXWalkView) {
+        mContent.supplyContentsForPopup(newXWalkView == null ? null : newXWalkView.mContent);
+    }
+
     private void init(Context context, AttributeSet attrs) {
         // Initialize chromium resources. Assign them the correct ids in
         // xwalk core.
@@ -332,10 +366,9 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
         setNotificationService(new XWalkNotificationServiceImpl(context, this));
 
         if (!CommandLine.getInstance().hasSwitch("disable-xwalk-extensions")) {
-            // Enable xwalk extension mechanism and start load extensions here.
-            // Note that it has to be after above initialization.
-            mExtensionManager = new XWalkExtensionManager(context, getActivity());
-            mExtensionManager.loadExtensions();
+            BuiltinXWalkExtensions.load(context, getActivity());
+        } else {
+            XWalkPreferencesInternal.setValue(XWalkPreferencesInternal.ENABLE_EXTENSIONS, false);
         }
 
         XWalkPathHelper.initialize();
@@ -367,6 +400,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @param content the content for the web page/app. Could be empty.
      * @since 1.0
      */
+    @XWalkAPI
     public void load(String url, String content) {
         if (mContent == null) return;
         checkThreadSafety();
@@ -385,6 +419,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @param content the content for manifest.json.
      * @since 1.0
      */
+    @XWalkAPI
     public void loadAppFromManifest(String url, String content) {
         if (mContent == null) return;
         checkThreadSafety();
@@ -396,6 +431,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @param mode the reload mode.
      * @since 1.0
      */
+    @XWalkAPI
     public void reload(int mode) {
         if (mContent == null) return;
         checkThreadSafety();
@@ -406,6 +442,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * Stop current loading progress.
      * @since 1.0
      */
+    @XWalkAPI
     public void stopLoading() {
         if (mContent == null) return;
         checkThreadSafety();
@@ -418,6 +455,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @return the url for current web page/app.
      * @since 1.0
      */
+    @XWalkAPI
     public String getUrl() {
         if (mContent == null) return null;
         checkThreadSafety();
@@ -430,6 +468,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @return the title for current web page/app.
      * @since 1.0
      */
+    @XWalkAPI
     public String getTitle() {
         if (mContent == null) return null;
         checkThreadSafety();
@@ -441,6 +480,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @return the original url.
      * @since 1.0
      */
+    @XWalkAPI
     public String getOriginalUrl() {
         if (mContent == null) return null;
         checkThreadSafety();
@@ -453,6 +493,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @return the navigation history.
      * @since 1.0
      */
+    @XWalkAPI
     public XWalkNavigationHistoryInternal getNavigationHistory() {
         if (mContent == null) return null;
         checkThreadSafety();
@@ -467,6 +508,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @param name the name injected in JavaScript.
      * @since 1.0
      */
+    @XWalkAPI
     public void addJavascriptInterface(Object object, String name) {
         if (mContent == null) return;
         checkThreadSafety();
@@ -479,6 +521,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @param callback the callback to handle the evaluated result.
      * @since 1.0
      */
+    @XWalkAPI
     public void evaluateJavascript(String script, ValueCallback<String> callback) {
         if (mContent == null) return;
         checkThreadSafety();
@@ -491,6 +534,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @param includeDiskFiles indicate whether to clear disk files for cache.
      * @since 1.0
      */
+    @XWalkAPI
     public void clearCache(boolean includeDiskFiles) {
         if (mContent == null) return;
         checkThreadSafety();
@@ -502,6 +546,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @return true if any HTML element is occupying the whole screen.
      * @since 1.0
      */
+    @XWalkAPI
     public boolean hasEnteredFullscreen() {
         if (mContent == null) return false;
         checkThreadSafety();
@@ -513,6 +558,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * in fullscreen.
      * @since 1.0
      */
+    @XWalkAPI
     public void leaveFullscreen() {
         if (mContent == null) return;
         checkThreadSafety();
@@ -529,6 +575,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      *
      * @since 1.0
      */
+    @XWalkAPI
     public void pauseTimers() {
         if (mContent == null) return;
         checkThreadSafety();
@@ -545,6 +592,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      *
      * @since 1.0
      */
+    @XWalkAPI
     public void resumeTimers() {
         if (mContent == null) return;
         checkThreadSafety();
@@ -559,9 +607,9 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * called to pause above things.
      * @since 1.0
      */
+    @XWalkAPI
     public void onHide() {
         if (mContent == null || mIsHidden) return;
-        if (null != mExtensionManager) mExtensionManager.onPause();
         mContent.onPause();
         mIsHidden = true;
     }
@@ -574,9 +622,9 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * called to resume above things.
      * @since 1.0
      */
+    @XWalkAPI
     public void onShow() {
         if (mContent == null || !mIsHidden ) return;
-        if (null != mExtensionManager) mExtensionManager.onResume();
         mContent.onResume();
         mIsHidden = false;
     }
@@ -587,6 +635,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * called to release resources.
      * @since 1.0
      */
+    @XWalkAPI
     public void onDestroy() {
         destroy();
     }
@@ -601,10 +650,9 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @param data passed from android.app.Activity.onActivityResult().
      * @since 1.0
      */
+    @XWalkAPI
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         if (mContent == null) return;
-        if (null != mExtensionManager)
-                mExtensionManager.onActivityResult(requestCode, resultCode, data);
         mContent.onActivityResult(requestCode, resultCode, data);
     }
 
@@ -616,6 +664,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @param intent passed from android.app.Activity.onNewIntent().
      * @since 1.0
      */
+    @XWalkAPI
     public boolean onNewIntent(Intent intent) {
         if (mContent == null) return false;
         return mContent.onNewIntent(intent);
@@ -627,6 +676,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @param outState the saved state for restoring.
      * @since 1.0
      */
+    @XWalkAPI
     public boolean saveState(Bundle outState) {
         if (mContent == null) return false;
         mContent.saveState(outState);
@@ -639,6 +689,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @return true if it can restore the state.
      * @since 1.0
      */
+    @XWalkAPI
     public boolean restoreState(Bundle inState) {
         if (mContent == null) return false;
         if (mContent.restoreState(inState) != null) return true;
@@ -651,8 +702,9 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @since 1.0
      */
     // TODO(yongsheng): make it static?
+    @XWalkAPI
     public String getAPIVersion() {
-        return "2.1";
+        return "3.0";
     }
 
     /**
@@ -661,6 +713,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @since 1.0
      */
     // TODO(yongsheng): make it static?
+    @XWalkAPI
     public String getXWalkVersion() {
         if (mContent == null) return null;
         return mContent.getXWalkVersion();
@@ -672,6 +725,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @param client the XWalkUIClientInternal defined by callers.
      * @since 1.0
      */
+    @XWalkAPI
     public void setUIClient(XWalkUIClientInternal client) {
         if (mContent == null) return;
         checkThreadSafety();
@@ -684,6 +738,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      * @param client the XWalkResourceClientInternal defined by callers.
      * @since 1.0
      */
+    @XWalkAPI
     public void setResourceClient(XWalkResourceClientInternal client) {
         if (mContent == null) return;
         checkThreadSafety();
@@ -706,6 +761,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
      *
      * @hide
      */
+    @XWalkAPI
     public void setNetworkAvailable(boolean networkUp) {
         if (mContent == null) return;
         checkThreadSafety();
@@ -713,17 +769,35 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
     }
 
     /**
-     * Enables remote debugging and returns the URL at which the dev tools server is listening
-     * for commands. The allowedUid argument can be used to specify the uid of the process that is
-     * permitted to connect.
-     * TODO(yongsheng): how to enable this in XWalkPreferencesInternal?
-     *
-     * @hide
+     * Enables remote debugging and returns the URL at which the dev tools
+     * server is listening for commands.
+     * The allowedUid argument can be used to specify the uid of the process
+     * that is permitted to connect.
      */
-    public String enableRemoteDebugging(int allowedUid) {
+    public void enableRemoteDebugging(int allowedUid) {
+        if (mContent == null) return;
+        checkThreadSafety();
+        mContent.enableRemoteDebugging(allowedUid);
+    }
+
+    /**
+     * Get the websocket url for remote debugging.
+     * @return the web socket url to remote debug this xwalk view.
+     * null will be returned if remote debugging is not enabled.
+     * @since 4.0
+     */
+    @XWalkAPI
+    public URL getRemoteDebuggingUrl() {
         if (mContent == null) return null;
         checkThreadSafety();
-        return mContent.enableRemoteDebugging(allowedUid);
+        String wsUrl = mContent.getRemoteDebuggingUrl();
+        if (wsUrl == null || wsUrl.isEmpty()) return null;
+
+        try {
+            return new URL(wsUrl);
+        } catch (MalformedURLException e) {
+            return null;
+        }
     }
 
     /**
@@ -769,15 +843,14 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
         if (mContent == null) return;
         ApplicationStatus.unregisterActivityStateListener(mActivityStateListener);
         mActivityStateListener = null;
-        if (null != mExtensionManager) mExtensionManager.onDestroy();
         mContent.destroy();
         disableRemoteDebugging();
     }
 
     // Enables remote debugging and returns the URL at which the dev tools server is listening
     // for commands. Only the current process is allowed to connect to the server.
-    String enableRemoteDebugging() {
-        return enableRemoteDebugging(mContext.getApplicationInfo().uid);
+    void enableRemoteDebugging() {
+        enableRemoteDebugging(mContext.getApplicationInfo().uid);
     }
 
     void disableRemoteDebugging() {
@@ -881,7 +954,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
     private void onActivityStateChange(Activity activity, int newState) {
         assert(getActivity() == activity);
         switch (newState) {
-           case ActivityState.STARTED:
+            case ActivityState.STARTED:
                 onShow();
                 break;
             case ActivityState.PAUSED:
@@ -893,7 +966,7 @@ public class XWalkViewInternal extends android.widget.FrameLayout {
             case ActivityState.DESTROYED:
                 onDestroy();
                 break;
-           case ActivityState.STOPPED:
+            case ActivityState.STOPPED:
                 onHide();
                 break;
             default: