Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / content / shell / android / java / src / org / chromium / content_shell / Shell.java
index 3a442a0..cb8c995 100644 (file)
@@ -10,6 +10,7 @@ import android.text.TextUtils;
 import android.util.AttributeSet;
 import android.view.KeyEvent;
 import android.view.View;
+import android.view.ViewGroup;
 import android.view.inputmethod.EditorInfo;
 import android.view.inputmethod.InputMethodManager;
 import android.widget.EditText;
@@ -22,8 +23,12 @@ import android.widget.TextView.OnEditorActionListener;
 import org.chromium.base.CalledByNative;
 import org.chromium.base.JNINamespace;
 import org.chromium.content.browser.ContentView;
+import org.chromium.content.browser.ContentViewClient;
+import org.chromium.content.browser.ContentViewCore;
 import org.chromium.content.browser.ContentViewRenderView;
-import org.chromium.content.browser.LoadUrlParams;
+import org.chromium.content_public.browser.LoadUrlParams;
+import org.chromium.content_public.browser.NavigationController;
+import org.chromium.content_public.browser.WebContents;
 import org.chromium.ui.base.WindowAndroid;
 
 /**
@@ -41,11 +46,15 @@ public class Shell extends LinearLayout {
         }
     };
 
-    // TODO(jrg): a mContentView.destroy() call is needed, both upstream and downstream.
-    private ContentView mContentView;
+    private ContentViewCore mContentViewCore;
+    private WebContents mWebContents;
+    private NavigationController mNavigationController;
+    private ContentViewClient mContentViewClient;
     private EditText mUrlTextView;
     private ImageButton mPrevButton;
     private ImageButton mNextButton;
+    private ImageButton mStopButton;
+    private ImageButton mReloadButton;
 
     private ClipDrawable mProgressDrawable;
 
@@ -85,10 +94,13 @@ public class Shell extends LinearLayout {
      *
      * @param nativeShell The pointer to the native Shell object.
      * @param window The owning window for this shell.
+     * @param client The {@link ContentViewClient} to be bound to any current or new
+     *               {@link ContentViewCore}s associated with this shell.
      */
-    public void initialize(long nativeShell, WindowAndroid window) {
+    public void initialize(long nativeShell, WindowAndroid window, ContentViewClient client) {
         mNativeShell = nativeShell;
         mWindow = window;
+        mContentViewClient = client;
     }
 
     /**
@@ -104,7 +116,7 @@ public class Shell extends LinearLayout {
     private void onNativeDestroyed() {
         mWindow = null;
         mNativeShell = 0;
-        mContentView.destroy();
+        mContentViewCore.destroy();
     }
 
     /**
@@ -143,7 +155,7 @@ public class Shell extends LinearLayout {
                 }
                 loadUrl(mUrlTextView.getText().toString());
                 setKeyboardVisibilityForUrl(false);
-                mContentView.requestFocus();
+                mContentViewCore.getContainerView().requestFocus();
                 return true;
             }
         });
@@ -154,10 +166,20 @@ public class Shell extends LinearLayout {
                 mNextButton.setVisibility(hasFocus ? GONE : VISIBLE);
                 mPrevButton.setVisibility(hasFocus ? GONE : VISIBLE);
                 if (!hasFocus) {
-                    mUrlTextView.setText(mContentView.getUrl());
+                    mUrlTextView.setText(mWebContents.getUrl());
                 }
             }
         });
+        mUrlTextView.setOnKeyListener(new OnKeyListener() {
+            @Override
+            public boolean onKey(View v, int keyCode, KeyEvent event) {
+                if (keyCode == KeyEvent.KEYCODE_BACK) {
+                    mContentViewCore.getContainerView().requestFocus();
+                    return true;
+                }
+                return false;
+            }
+        });
     }
 
     /**
@@ -169,15 +191,15 @@ public class Shell extends LinearLayout {
     public void loadUrl(String url) {
         if (url == null) return;
 
-        if (TextUtils.equals(url, mContentView.getUrl())) {
-            mContentView.getContentViewCore().reload(true);
+        if (TextUtils.equals(url, mWebContents.getUrl())) {
+            mNavigationController.reload(true);
         } else {
-            mContentView.loadUrl(new LoadUrlParams(sanitizeUrl(url)));
+            mNavigationController.loadUrl(new LoadUrlParams(sanitizeUrl(url)));
         }
         mUrlTextView.clearFocus();
         // TODO(aurimas): Remove this when crbug.com/174541 is fixed.
-        mContentView.clearFocus();
-        mContentView.requestFocus();
+        mContentViewCore.getContainerView().clearFocus();
+        mContentViewCore.getContainerView().requestFocus();
     }
 
     /**
@@ -186,7 +208,7 @@ public class Shell extends LinearLayout {
      * @return The sanitized URL.
      */
     public static String sanitizeUrl(String url) {
-        if (url == null) return url;
+        if (url == null) return null;
         if (url.startsWith("www.") || url.indexOf(":") == -1) url = "http://" + url;
         return url;
     }
@@ -196,7 +218,7 @@ public class Shell extends LinearLayout {
         mPrevButton.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
-                if (mContentView.canGoBack()) mContentView.goBack();
+                if (mNavigationController.canGoBack()) mNavigationController.goBack();
             }
         });
 
@@ -204,7 +226,21 @@ public class Shell extends LinearLayout {
         mNextButton.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
-                if (mContentView.canGoForward()) mContentView.goForward();
+                if (mNavigationController.canGoForward()) mNavigationController.goForward();
+            }
+        });
+        mStopButton = (ImageButton) findViewById(R.id.stop);
+        mStopButton.setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                if (mLoading) mWebContents.stop();
+            }
+        });
+        mReloadButton = (ImageButton) findViewById(R.id.reload);
+        mReloadButton.setOnClickListener(new OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                mNavigationController.reload(true);
             }
         });
     }
@@ -240,26 +276,58 @@ public class Shell extends LinearLayout {
 
     /**
      * Initializes the ContentView based on the native tab contents pointer passed in.
-     * @param nativeTabContents The pointer to the native tab contents object.
+     * @param nativeWebContents The pointer to the native tab contents object.
      */
     @SuppressWarnings("unused")
     @CalledByNative
-    private void initFromNativeTabContents(long nativeTabContents) {
-        mContentView = ContentView.newInstance(getContext(), nativeTabContents, mWindow);
-        if (mContentView.getUrl() != null) mUrlTextView.setText(mContentView.getUrl());
-        ((FrameLayout) findViewById(R.id.contentview_holder)).addView(mContentView,
+    private void initFromNativeTabContents(long nativeWebContents) {
+        Context context = getContext();
+        mContentViewCore = new ContentViewCore(context);
+        ContentView cv = ContentView.newInstance(context, mContentViewCore);
+        mContentViewCore.initialize(cv, cv, nativeWebContents, mWindow);
+        mContentViewCore.setContentViewClient(mContentViewClient);
+        mWebContents = mContentViewCore.getWebContents();
+        mNavigationController = mWebContents.getNavigationController();
+        if (getParent() != null) mContentViewCore.onShow();
+        if (mWebContents.getUrl() != null) {
+            mUrlTextView.setText(mWebContents.getUrl());
+        }
+        ((FrameLayout) findViewById(R.id.contentview_holder)).addView(cv,
                 new FrameLayout.LayoutParams(
                         FrameLayout.LayoutParams.MATCH_PARENT,
                         FrameLayout.LayoutParams.MATCH_PARENT));
-        mContentView.requestFocus();
-        mContentViewRenderView.setCurrentContentView(mContentView);
+        cv.requestFocus();
+        mContentViewRenderView.setCurrentContentViewCore(mContentViewCore);
+    }
+
+    /**
+     * Enable/Disable navigation(Prev/Next) button if navigation is allowed/disallowed
+     * in respective direction.
+     * @param controlId Id of button to update
+     * @param enabled enable/disable value
+     */
+    @CalledByNative
+    private void enableUiControl(int controlId, boolean enabled) {
+        if (controlId == 0) mPrevButton.setEnabled(enabled);
+        else if (controlId == 1) mNextButton.setEnabled(enabled);
+        else if (controlId == 2) {
+            mStopButton.setVisibility(enabled ? VISIBLE : GONE);
+            mReloadButton.setVisibility(enabled ? GONE : VISIBLE);
+        }
+    }
+
+    /**
+     * @return The {@link ViewGroup} currently shown by this Shell.
+     */
+    public ViewGroup getContentView() {
+        return mContentViewCore.getContainerView();
     }
 
     /**
-     * @return The {@link ContentView} currently shown by this Shell.
+     * @return The {@link ContentViewCore} currently managing the view shown by this Shell.
      */
-    public ContentView getContentView() {
-        return mContentView;
+    public ContentViewCore getContentViewCore() {
+        return mContentViewCore;
     }
 
     private void setKeyboardVisibilityForUrl(boolean visible) {