Skip to content

Commit

Permalink
Restructuring WebContents functions from ContentViewCore to WebContents.
Browse files Browse the repository at this point in the history
In this patch we are trying to position the WebContents functionalities to web_contents_android
file from ContentViewCoreImpl to make it functionally readable. Ensuring there is a one-to-one
mapping between WebContents Java and native counterparts.

BUG=398263

Review URL: https://codereview.chromium.org/464393002

Cr-Commit-Position: refs/heads/master@{#289796}
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289796 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
ajith.v@samsung.com committed Aug 15, 2014
1 parent 6ad9023 commit 585c6dd
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import org.chromium.content.common.CleanupReference;
import org.chromium.content_public.Referrer;
import org.chromium.content_public.browser.GestureStateListener;
import org.chromium.content_public.browser.JavaScriptCallback;
import org.chromium.ui.base.ActivityWindowAndroid;
import org.chromium.ui.base.WindowAndroid;
import org.chromium.ui.gfx.DeviceDisplayInfo;
Expand Down Expand Up @@ -1676,12 +1677,12 @@ public void preauthorizePermission(Uri origin, long resources) {
}

/**
* @see ContentViewCore.evaluateJavaScript(String, ContentViewCore.JavaScriptCallback)
* @see ContentViewCore.evaluateJavaScript(String, JavaScriptCallback)
*/
public void evaluateJavaScript(String script, final ValueCallback<String> callback) {
ContentViewCore.JavaScriptCallback jsCallback = null;
JavaScriptCallback jsCallback = null;
if (callback != null) {
jsCallback = new ContentViewCore.JavaScriptCallback() {
jsCallback = new JavaScriptCallback() {
@Override
public void handleJavaScriptResult(String jsonResult) {
callback.onReceiveValue(jsonResult);
Expand Down
49 changes: 0 additions & 49 deletions content/browser/android/content_view_core_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include "base/android/jni_string.h"
#include "base/android/scoped_java_ref.h"
#include "base/command_line.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/metrics/histogram.h"
#include "base/strings/utf_string_conversions.h"
Expand Down Expand Up @@ -67,7 +66,6 @@ using base::android::ConvertJavaStringToUTF16;
using base::android::ConvertJavaStringToUTF8;
using base::android::ConvertUTF16ToJavaString;
using base::android::ConvertUTF8ToJavaString;
using base::android::ScopedJavaGlobalRef;
using base::android::ScopedJavaLocalRef;
using blink::WebGestureEvent;
using blink::WebInputEvent;
Expand Down Expand Up @@ -1309,53 +1307,6 @@ long ContentViewCoreImpl::GetNativeImeAdapter(JNIEnv* env, jobject obj) {
return rwhva->GetNativeImeAdapter();
}

namespace {
void JavaScriptResultCallback(const ScopedJavaGlobalRef<jobject>& callback,
const base::Value* result) {
JNIEnv* env = base::android::AttachCurrentThread();
std::string json;
base::JSONWriter::Write(result, &json);
ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json);
Java_ContentViewCore_onEvaluateJavaScriptResult(env,
j_json.obj(),
callback.obj());
}
} // namespace

void ContentViewCoreImpl::EvaluateJavaScript(JNIEnv* env,
jobject obj,
jstring script,
jobject callback,
jboolean start_renderer) {
RenderViewHost* rvh = web_contents_->GetRenderViewHost();
DCHECK(rvh);

if (start_renderer && !rvh->IsRenderViewLive()) {
if (!web_contents_->CreateRenderViewForInitialEmptyDocument()) {
LOG(ERROR) << "Failed to create RenderView in EvaluateJavaScript";
return;
}
}

if (!callback) {
// No callback requested.
web_contents_->GetMainFrame()->ExecuteJavaScript(
ConvertJavaStringToUTF16(env, script));
return;
}

// Secure the Java callback in a scoped object and give ownership of it to the
// base::Callback.
ScopedJavaGlobalRef<jobject> j_callback;
j_callback.Reset(env, callback);
content::RenderFrameHost::JavaScriptResultCallback c_callback =
base::Bind(&JavaScriptResultCallback, j_callback);

web_contents_->GetMainFrame()->ExecuteJavaScript(
ConvertJavaStringToUTF16(env, script),
c_callback);
}

// TODO(sgurun) add support for posting a frame whose name is known (only
// main frame is supported at this time, see crbug.com/389721)
// TODO(sgurun) add support for passing message ports
Expand Down
5 changes: 0 additions & 5 deletions content/browser/android/content_view_core_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,6 @@ class ContentViewCoreImpl : public ContentViewCore,
jboolean enabled);

void ClearHistory(JNIEnv* env, jobject obj);
void EvaluateJavaScript(JNIEnv* env,
jobject obj,
jstring script,
jobject callback,
jboolean start_renderer);
void PostMessageToFrame(JNIEnv* env, jobject obj, jstring frame_id,
jstring message, jstring source_origin, jstring target_origin);
long GetNativeImeAdapter(JNIEnv* env, jobject obj);
Expand Down
51 changes: 51 additions & 0 deletions content/browser/web_contents/web_contents_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "base/command_line.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "content/browser/android/interstitial_page_delegate_android.h"
#include "content/browser/frame_host/interstitial_page_impl.h"
Expand All @@ -24,7 +25,23 @@

using base::android::AttachCurrentThread;
using base::android::ConvertJavaStringToUTF8;
using base::android::ConvertJavaStringToUTF16;
using base::android::ConvertUTF8ToJavaString;
using base::android::ScopedJavaGlobalRef;

namespace {

void JavaScriptResultCallback(const ScopedJavaGlobalRef<jobject>& callback,
const base::Value* result) {
JNIEnv* env = base::android::AttachCurrentThread();
std::string json;
base::JSONWriter::Write(result, &json);
ScopedJavaLocalRef<jstring> j_json = ConvertUTF8ToJavaString(env, json);
content::Java_WebContentsImpl_onEvaluateJavaScriptResult(
env, j_json.obj(), callback.obj());
}

} // namespace

namespace content {

Expand Down Expand Up @@ -307,4 +324,38 @@ void WebContentsAndroid::DidStartNavigationTransitionForFrame(int64 frame_id) {
env, obj_.obj(), frame_id);
}

void WebContentsAndroid::EvaluateJavaScript(JNIEnv* env,
jobject obj,
jstring script,
jobject callback,
jboolean start_renderer) {
RenderViewHost* rvh = web_contents_->GetRenderViewHost();
DCHECK(rvh);

if (start_renderer && !rvh->IsRenderViewLive()) {
if (!static_cast<WebContentsImpl*>(web_contents_)->
CreateRenderViewForInitialEmptyDocument()) {
LOG(ERROR) << "Failed to create RenderView in EvaluateJavaScript";
return;
}
}

if (!callback) {
// No callback requested.
web_contents_->GetMainFrame()->ExecuteJavaScript(
ConvertJavaStringToUTF16(env, script));
return;
}

// Secure the Java callback in a scoped object and give ownership of it to the
// base::Callback.
ScopedJavaGlobalRef<jobject> j_callback;
j_callback.Reset(env, callback);
content::RenderFrameHost::JavaScriptResultCallback js_callback =
base::Bind(&JavaScriptResultCallback, j_callback);

web_contents_->GetMainFrame()->ExecuteJavaScript(
ConvertJavaStringToUTF16(env, script), js_callback);
}

} // namespace content
5 changes: 5 additions & 0 deletions content/browser/web_contents/web_contents_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class CONTENT_EXPORT WebContentsAndroid
void SelectWordAroundCaret(JNIEnv* env, jobject obj);

void InsertCSS(JNIEnv* env, jobject jobj, jstring jcss);
void EvaluateJavaScript(JNIEnv* env,
jobject obj,
jstring script,
jobject callback,
jboolean start_renderer);

private:
RenderWidgetHostViewAndroid* GetRenderWidgetHostViewAndroid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.chromium.content.browser.input.SelectionEventType;
import org.chromium.content.common.ContentSwitches;
import org.chromium.content_public.browser.GestureStateListener;
import org.chromium.content_public.browser.JavaScriptCallback;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.DeviceFormFactor;
import org.chromium.ui.base.ViewAndroid;
Expand Down Expand Up @@ -1354,11 +1355,6 @@ public void addStyleSheetByURL(String url) {
mWebContents.addStyleSheetByURL(url);
}

/** Callback interface for evaluateJavaScript(). */
public interface JavaScriptCallback {
void handleJavaScriptResult(String jsonResult);
}

/**
* Injects the passed Javascript code in the current page and evaluates it.
* If a result is required, pass in a callback.
Expand All @@ -1371,8 +1367,8 @@ public interface JavaScriptCallback {
* If no result is required, pass null.
*/
public void evaluateJavaScript(String script, JavaScriptCallback callback) {
if (mNativeContentViewCore == 0) return;
nativeEvaluateJavaScript(mNativeContentViewCore, script, callback, false);
assert mWebContents != null;
mWebContents.evaluateJavaScript(script, callback, false);
}

/**
Expand All @@ -1382,8 +1378,8 @@ public void evaluateJavaScript(String script, JavaScriptCallback callback) {
* @param script The Javascript to execute.
*/
public void evaluateJavaScriptEvenIfNotYetNavigated(String script) {
if (mNativeContentViewCore == 0) return;
nativeEvaluateJavaScript(mNativeContentViewCore, script, null, true);
assert mWebContents != null;
mWebContents.evaluateJavaScript(script, null, true);
}

/**
Expand Down Expand Up @@ -2408,13 +2404,6 @@ private void onSelectionChanged(String text) {
getContentViewClient().onSelectionChanged(text);
}

@SuppressWarnings("unused")
@CalledByNative
private static void onEvaluateJavaScriptResult(
String jsonResult, JavaScriptCallback callback) {
callback.handleJavaScriptResult(jsonResult);
}

@SuppressWarnings("unused")
@CalledByNative
private void showPastePopup(int xDip, int yDip) {
Expand Down Expand Up @@ -3175,9 +3164,6 @@ private native void nativeSetMultiTouchZoomSupportEnabled(

private native void nativeClearHistory(long nativeContentViewCoreImpl);

private native void nativeEvaluateJavaScript(long nativeContentViewCoreImpl,
String script, JavaScriptCallback callback, boolean startRenderer);

private native void nativePostMessageToFrame(long nativeContentViewCoreImpl, String frameId,
String message, String sourceOrigin, String targetOrigin);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import org.chromium.content_public.browser.JavaScriptCallback;
import org.chromium.content_public.browser.NavigationController;
import org.chromium.content_public.browser.NavigationTransitionDelegate;
import org.chromium.content_public.browser.WebContents;
Expand Down Expand Up @@ -167,6 +168,7 @@ public void setNavigationTransitionDelegate(NavigationTransitionDelegate delegat
/**
* Inserts the provided markup sandboxed into the frame.
*/
@Override
public void setupTransitionView(String markup) {
nativeSetupTransitionView(mNativeWebContentsAndroid, markup);
}
Expand All @@ -175,6 +177,7 @@ public void setupTransitionView(String markup) {
* Hides transition elements specified by the selector, and activates any
* exiting-transition stylesheets.
*/
@Override
public void beginExitTransition(String cssSelector) {
nativeBeginExitTransition(mNativeWebContentsAndroid, cssSelector);
}
Expand Down Expand Up @@ -208,6 +211,18 @@ private void didStartNavigationTransitionForFrame(long frameId) {
}
}

@Override
public void evaluateJavaScript(String script, JavaScriptCallback callback,
boolean startRenderer) {
nativeEvaluateJavaScript(mNativeWebContentsAndroid, script, callback, true);
}

@CalledByNative
private static void onEvaluateJavaScriptResult(
String jsonResult, JavaScriptCallback callback) {
callback.handleJavaScriptResult(jsonResult);
}

private native String nativeGetTitle(long nativeWebContentsAndroid);
private native String nativeGetVisibleURL(long nativeWebContentsAndroid);
private native void nativeStop(long nativeWebContentsAndroid);
Expand Down Expand Up @@ -236,4 +251,6 @@ private native void nativeSetupTransitionView(long nativeWebContentsAndroid,
String markup);
private native void nativeBeginExitTransition(long nativeWebContentsAndroid,
String cssSelector);
private native void nativeEvaluateJavaScript(long nativeWebContentsAndroid,
String script, JavaScriptCallback callback, boolean startRenderer);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.content_public.browser;

/** Callback interface for WebContents evaluateJavaScript(). */
public interface JavaScriptCallback {
/**
* Called from native in response to evaluateJavaScript().
* @param jsonResult json result curresponds to JS execution
*/
void handleJavaScriptResult(String jsonResult);
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,18 @@ public void updateTopControlsState(boolean enableHiding, boolean enableShowing,
*/
public void beginExitTransition(String cssSelector);

/**
* Injects the passed Javascript code in the current page and evaluates it.
* If a result is required, pass in a callback.
*
* @param script The Javascript to execute.
* @param callback The callback to be fired off when a result is ready. The script's
* result will be json encoded and passed as the parameter, and the call
* will be made on the main thread.
* If no result is required, pass null.
* @param startRenderer Tells whether to start Renderer or not for initial empty document
*/
public void evaluateJavaScript(String script, JavaScriptCallback callback,
boolean startRenderer);

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.test.suitebuilder.annotation.SmallTest;

import org.chromium.base.test.util.Feature;
import org.chromium.content_public.browser.JavaScriptCallback;

/**
* Part of the test suite for the WebView's Java Bridge.
Expand Down Expand Up @@ -103,7 +104,7 @@ private String executeJavaScriptAndGetResult(final ContentViewCore contentViewCo
final String script) throws Throwable {
final String[] result = new String[1];
class ResultCallback extends JavaBridgeTestBase.Controller
implements ContentViewCore.JavaScriptCallback {
implements JavaScriptCallback {
@Override
public void handleJavaScriptResult(String jsonResult) {
result[0] = jsonResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.chromium.base.ThreadUtils;
import org.chromium.content.browser.ContentViewCore;
import org.chromium.content_public.browser.JavaScriptCallback;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -109,8 +110,8 @@ public static class OnEvaluateJavaScriptResultHelper extends CallbackHelper {
* @param code A JavaScript code to be evaluated.
*/
public void evaluateJavaScript(ContentViewCore contentViewCore, String code) {
ContentViewCore.JavaScriptCallback callback =
new ContentViewCore.JavaScriptCallback() {
JavaScriptCallback callback =
new JavaScriptCallback() {
@Override
public void handleJavaScriptResult(String jsonResult) {
notifyCalled(jsonResult);
Expand Down

0 comments on commit 585c6dd

Please sign in to comment.