Restructuring WebContents functions from ContentViewCore to WebContents.
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
This commit is contained in:
android_webview/java/src/org/chromium/android_webview
content
browser
android
web_contents
public
android
java
src
org
chromium
content
browser
content_public
javatests
src
org
chromium
content
test
android
javatests
src
org
chromium
content
browser
test
@ -56,6 +56,7 @@ import org.chromium.content.browser.WebContentsObserverAndroid;
|
||||
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;
|
||||
@ -1676,12 +1677,12 @@ public class AwContents {
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
@ -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"
|
||||
@ -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;
|
||||
@ -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
|
||||
|
@ -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);
|
||||
|
@ -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"
|
||||
@ -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 {
|
||||
|
||||
@ -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
|
||||
|
@ -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();
|
||||
|
@ -76,6 +76,7 @@ import org.chromium.content.browser.input.SelectPopupItem;
|
||||
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;
|
||||
@ -1354,11 +1355,6 @@ public class ContentViewCore
|
||||
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.
|
||||
@ -1371,8 +1367,8 @@ public class ContentViewCore
|
||||
* 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);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -1382,8 +1378,8 @@ public class ContentViewCore
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -2408,13 +2404,6 @@ public class ContentViewCore
|
||||
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) {
|
||||
@ -3175,9 +3164,6 @@ public class ContentViewCore
|
||||
|
||||
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);
|
||||
|
||||
|
@ -6,6 +6,7 @@ package org.chromium.content.browser.webcontents;
|
||||
|
||||
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;
|
||||
@ -167,6 +168,7 @@ import org.chromium.content_public.browser.WebContents;
|
||||
/**
|
||||
* Inserts the provided markup sandboxed into the frame.
|
||||
*/
|
||||
@Override
|
||||
public void setupTransitionView(String markup) {
|
||||
nativeSetupTransitionView(mNativeWebContentsAndroid, markup);
|
||||
}
|
||||
@ -175,6 +177,7 @@ import org.chromium.content_public.browser.WebContents;
|
||||
* Hides transition elements specified by the selector, and activates any
|
||||
* exiting-transition stylesheets.
|
||||
*/
|
||||
@Override
|
||||
public void beginExitTransition(String cssSelector) {
|
||||
nativeBeginExitTransition(mNativeWebContentsAndroid, cssSelector);
|
||||
}
|
||||
@ -208,6 +211,18 @@ import org.chromium.content_public.browser.WebContents;
|
||||
}
|
||||
}
|
||||
|
||||
@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);
|
||||
@ -236,4 +251,6 @@ import org.chromium.content_public.browser.WebContents;
|
||||
String markup);
|
||||
private native void nativeBeginExitTransition(long nativeWebContentsAndroid,
|
||||
String cssSelector);
|
||||
private native void nativeEvaluateJavaScript(long nativeWebContentsAndroid,
|
||||
String script, JavaScriptCallback callback, boolean startRenderer);
|
||||
}
|
||||
|
14
content/public/android/java/src/org/chromium/content_public/browser/JavaScriptCallback.java
Normal file
14
content/public/android/java/src/org/chromium/content_public/browser/JavaScriptCallback.java
Normal file
@ -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);
|
||||
}
|
@ -146,4 +146,18 @@ public interface WebContents {
|
||||
*/
|
||||
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);
|
||||
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ package org.chromium.content.browser;
|
||||
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.
|
||||
@ -103,7 +104,7 @@ public class JavaBridgeChildFrameTest extends JavaBridgeTestBase {
|
||||
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;
|
||||
|
@ -7,6 +7,7 @@ package org.chromium.content.browser.test.util;
|
||||
|
||||
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;
|
||||
@ -109,8 +110,8 @@ public class TestCallbackHelperContainer {
|
||||
* @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);
|
||||
|
Reference in New Issue
Block a user