Skip to content

Commit

Permalink
[core][eagle] support httpmodule in android
Browse files Browse the repository at this point in the history
  • Loading branch information
yxping committed Nov 19, 2018
1 parent 34a9fec commit ff165ef
Show file tree
Hide file tree
Showing 21 changed files with 526 additions and 95 deletions.
Binary file modified android/sdk/libs/armeabi-v7a/libweexjss.so
Binary file not shown.
Binary file modified android/sdk/libs/armeabi/libweexjss.so
Binary file not shown.
101 changes: 101 additions & 0 deletions android/sdk/src/main/java/com/taobao/weex/bridge/RequestHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.taobao.weex.bridge;

import android.net.Uri;
import android.text.TextUtils;

import com.taobao.weex.WXEnvironment;
import com.taobao.weex.WXHttpListener;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.WXSDKManager;
import com.taobao.weex.adapter.IWXHttpAdapter;
import com.taobao.weex.adapter.URIAdapter;
import com.taobao.weex.base.CalledByNative;
import com.taobao.weex.common.WXRequest;
import com.taobao.weex.common.WXResponse;
import com.taobao.weex.http.WXHttpUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.taobao.weex.http.WXHttpUtil.KEY_USER_AGENT;

public class RequestHandler {

native void nativeInvokeOnSuccess(long callback, String result);
native void nativeInvokeOnFailed(long callback);

@CalledByNative
public static RequestHandler create() {
return new RequestHandler();
}

@CalledByNative
public void send(String instanceId, String url, long nativeCallback) {
if (TextUtils.isEmpty(instanceId) || TextUtils.isEmpty(url) ||
nativeCallback == 0 ||
!WXSDKManager.getInstance().getAllInstanceMap().containsKey(
instanceId)) {
return;
}

WXSDKManager manager = WXSDKManager.getInstance();
WXSDKInstance instance =
WXSDKManager.getInstance().getSDKInstance(instanceId);
if (instance == null)
return;
IWXHttpAdapter adapter = WXSDKManager.getInstance().getIWXHttpAdapter();

WXRequest wxRequest = new WXRequest();
wxRequest.url = manager.getURIAdapter()
.rewrite(instance, URIAdapter.BUNDLE, Uri.parse(url))
.toString();

if (wxRequest.paramMap == null) {
wxRequest.paramMap = new HashMap<>();
}
wxRequest.paramMap.put(
KEY_USER_AGENT, WXHttpUtil.assembleUserAgent(
instance.getContext(), WXEnvironment.getConfig()));
wxRequest.paramMap.put("isBundleRequest", "true");
adapter.sendRequest(wxRequest, new OnHttpListenerInner(instance, nativeCallback, url));
}

class OnHttpListenerInner extends WXHttpListener {
private long sNativeCallback;

OnHttpListenerInner(WXSDKInstance instance, long nativeCallback, String bundlUrl) {
super(instance, bundlUrl);
sNativeCallback = nativeCallback;
}

@Override
public void onSuccess(WXResponse response) {
String script = new String(response.originalData);
nativeInvokeOnSuccess(sNativeCallback, script);
}

@Override
public void onFail(WXResponse response) {
nativeInvokeOnFailed(sNativeCallback);
}
}
}
14 changes: 9 additions & 5 deletions android/sdk/src/main/java/com/taobao/weex/bridge/WXBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public class WXBridge implements IWXBridge {

public native String nativeExecJSOnInstance(String instanceId, String script, int type);

public native void nativeFireEventOnDataRenderNode(String instanceId, String ref, String type, String data);
public native void nativeFireEventOnDataRenderNode(String instanceId, String ref, String type, String data, String domChanges);

public native void nativeRegisterModuleOnDataRenderNode( String data);

Expand Down Expand Up @@ -256,7 +256,12 @@ public Object callNativeModule(String instanceId, String module, String method,
// TODO use a better way
if (instance!=null && (instance.getRenderStrategy()== WXRenderStrategy.DATA_RENDER
|| instance.getRenderStrategy()== WXRenderStrategy.DATA_RENDER_BINARY)){
argArray = (JSONArray) JSON.parse(new String(arguments, "UTF-8"));
try {
argArray = (JSONArray) JSON.parse(new String(arguments, "UTF-8"));
} catch (Exception e) {
// For wson use in data render mode
argArray = (JSONArray) WXWsonJSONSwitch.parseWsonOrJSON(arguments);
}
} else {
argArray = (JSONArray) WXWsonJSONSwitch.parseWsonOrJSON(arguments);
}
Expand Down Expand Up @@ -705,9 +710,8 @@ public void reportNativeInitStatus(String statusCode, String errorMsg) {
}
}

@Override
public void fireEventOnDataRenderNode(String instanceId, String ref, String type, String data) {
nativeFireEventOnDataRenderNode(instanceId,ref,type,data);
public void fireEventOnDataRenderNode(String instanceId, String ref, String type, String data, String domChanges) {
nativeFireEventOnDataRenderNode(instanceId,ref,type,data, domChanges);
}

public void registerModuleOnDataRenderNode(String data) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ public void fireEventOnNode(final String instanceId, final String ref,
WXSDKInstance instance = WXSDKManager.getInstance().getAllInstanceMap().get(instanceId);
if (instance != null && (instance.getRenderStrategy() == WXRenderStrategy.DATA_RENDER ||
instance.getRenderStrategy() == WXRenderStrategy.DATA_RENDER_BINARY)) {
fireEventOnDataRenderNode(instanceId, ref, type, data);
fireEventOnDataRenderNode(instanceId, ref, type, data, domChanges);
} else {
if(callback == null) {
addJSEventTask(METHOD_FIRE_EVENT, instanceId, params, ref, type, data, domChanges);
Expand All @@ -1143,7 +1143,7 @@ public void fireEventOnNode(final String instanceId, final String ref,
}

private void fireEventOnDataRenderNode(final String instanceId, final String ref,
final String type, final Map<String, Object> data) {
final String type, final Map<String, Object> data, final Map<String, Object> domChanges) {
mJSHandler.postDelayed(WXThread.secure(new Runnable() {
@Override
public void run() {
Expand All @@ -1154,7 +1154,9 @@ public void run() {
WXLogUtils.d("fireEventOnDataRenderNode >>>> instanceId:" + instanceId
+ ", data:" + data);
}
mWXBridge.fireEventOnDataRenderNode(instanceId, ref,type,JSON.toJSONString(data));
if (mWXBridge instanceof WXBridge) {
((WXBridge) mWXBridge).fireEventOnDataRenderNode(instanceId, ref,type,JSON.toJSONString(data), JSON.toJSONString(domChanges));
}
WXLogUtils.renderPerformanceLog("fireEventOnDataRenderNode", System.currentTimeMillis() - start);
} catch (Throwable e) {
String err = "[WXBridgeManager] fireEventOnDataRenderNode " + WXLogUtils.getStackTrace(e);
Expand Down
10 changes: 8 additions & 2 deletions ios/sdk/WeexSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -4205,7 +4205,10 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
VALID_ARCHS = "arm64 armv7 x86_64 i386";
WARNING_CFLAGS = "-Wno-documentation";
WARNING_CFLAGS = (
"-Wno-documentation",
"-Wno-c++14-extensions",
);
};
name = Debug;
};
Expand Down Expand Up @@ -4259,7 +4262,10 @@
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
VALID_ARCHS = "arm64 armv7 x86_64 i386";
WARNING_CFLAGS = "-Wno-documentation";
WARNING_CFLAGS = (
"-Wno-documentation",
"-Wno-c++14-extensions",
);
};
name = Release;
};
Expand Down
10 changes: 7 additions & 3 deletions weex_core/Source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,11 @@ set(COMMON_SRCS
./core/data_render/binary_file.cc
./core/data_render/vnode/vnode.cc
./core/data_render/vnode/vcomponent.cc
./core/data_render/vnode/android/vcomponent_lifecycle_listener.cc
./core/data_render/vnode/android/vnode_on_event_listener.cc
./core/data_render/vnode/android/conversion.cc
./core/data_render/vnode/vnode_exec_env.cc
./core/data_render/vnode/vnode_render_manager.cc
./core/data_render/vnode/vnode_render_context.cc

./core/network/http_module.cc
)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/wson)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/wson)
Expand Down Expand Up @@ -153,6 +152,11 @@ if (ANDROID)
./android/wrap/wx_params.cpp
./android/base/jni_type.cpp

./core/data_render/vnode/android/vcomponent_lifecycle_listener.cc
./core/data_render/vnode/android/vnode_on_event_listener.cc
./core/data_render/vnode/android/conversion.cc
./core/network/android/default_request_handler.cc

./android/jsengine/multiprocess/WeexJSConnection.cpp
./android/jsengine/multiprocess/ExtendJSApi.cpp

Expand Down
1 change: 1 addition & 0 deletions weex_core/Source/android/jniprebuild/jni_files
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ com/taobao/weex/ui/component/list/template/jni/NativeRenderObjectUtils.java
com/taobao/weex/base/SystemMessageHandler.java
com/taobao/weex/bridge/WXBridge.java
com/taobao/weex/layout/ContentBoxMeasurement.java
com/taobao/weex/bridge/RequestHandler.java
4 changes: 3 additions & 1 deletion weex_core/Source/android/jniprebuild/jni_load.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "android/wrap/wx_js_object.h"
#include "android/wrap/wx_map.h"
#include "base/message_loop/message_pump_android.h"
#include "core/network/android/default_request_handler.h"

jint JNI_OnLoad(JavaVM *vm, void *reserved) {
base::android::InitVM(vm);
Expand All @@ -44,7 +45,8 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) {
WeexCore::WXJSObject::RegisterJNIUtils(env) &&
WeexCore::LogUtils::RegisterJNIUtils(env) &&
WeexCore::WXMap::RegisterJNIUtils(env) &&
WeexCore::HashSet::RegisterJNIUtils(env);
WeexCore::HashSet::RegisterJNIUtils(env) &&
weex::core::network::DefaultRequestHandler::RegisterJNIUtils(env);
if (result) {
WeexCore::SoUtils::Init(env);
WeexCore::WMLBridge::RegisterJNIUtils(env);
Expand Down
122 changes: 122 additions & 0 deletions weex_core/Source/android/jniprebuild/jniheader/RequestHandler_jni.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// 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.

// This file is autogenerated by
// weex/weex_core/Source/android/jniprebuild/jni_generator.py
// For
// com/taobao/weex/bridge/RequestHandler

#ifndef com_taobao_weex_bridge_RequestHandler_JNI
#define com_taobao_weex_bridge_RequestHandler_JNI

#include <jni.h>

//#include "base/android/jni_int_wrapper.h"

// Step 1: forward declarations.
namespace {
const char kRequestHandlerClassPath[] = "com/taobao/weex/bridge/RequestHandler";
// Leaking this jclass as we cannot use LazyInstance from some threads.
jclass g_RequestHandler_clazz = NULL;
#define RequestHandler_clazz(env) g_RequestHandler_clazz

} // namespace

static void InvokeOnSuccess(JNIEnv* env, jobject jcaller,
jlong callback,
jstring result);

static void InvokeOnFailed(JNIEnv* env, jobject jcaller,
jlong callback);

// Step 2: method stubs.

static intptr_t g_RequestHandler_create = 0;
static base::android::ScopedLocalJavaRef<jobject>
Java_RequestHandler_create(JNIEnv* env) {
/* Must call RegisterNativesImpl() */
//CHECK_CLAZZ(env, RequestHandler_clazz(env),
// RequestHandler_clazz(env), NULL);
jmethodID method_id =
base::android::GetMethod(
env, RequestHandler_clazz(env),
base::android::STATIC_METHOD,
"create",

"("
")"
"Lcom/taobao/weex/bridge/RequestHandler;",
&g_RequestHandler_create);

jobject ret =
env->CallStaticObjectMethod(RequestHandler_clazz(env),
method_id);
base::android::CheckException(env);
return base::android::ScopedLocalJavaRef<jobject>(env, ret);
}

static intptr_t g_RequestHandler_send = 0;
static void Java_RequestHandler_send(JNIEnv* env, jobject obj, jstring
instanceId,
jstring url,
jlong nativeCallback) {
/* Must call RegisterNativesImpl() */
//CHECK_CLAZZ(env, obj,
// RequestHandler_clazz(env));
jmethodID method_id =
base::android::GetMethod(
env, RequestHandler_clazz(env),
base::android::INSTANCE_METHOD,
"send",

"("
"Ljava/lang/String;"
"Ljava/lang/String;"
"J"
")"
"V",
&g_RequestHandler_send);

env->CallVoidMethod(obj,
method_id, instanceId, url, nativeCallback);
base::android::CheckException(env);

}

// Step 3: RegisterNatives.

static const JNINativeMethod kMethodsRequestHandler[] = {
{ "nativeInvokeOnSuccess",
"("
"J"
"Ljava/lang/String;"
")"
"V", reinterpret_cast<void*>(InvokeOnSuccess) },
{ "nativeInvokeOnFailed",
"("
"J"
")"
"V", reinterpret_cast<void*>(InvokeOnFailed) },
};

static bool RegisterNativesImpl(JNIEnv* env) {

g_RequestHandler_clazz = reinterpret_cast<jclass>(env->NewGlobalRef(
base::android::GetClass(env, kRequestHandlerClassPath).Get()));

const int kMethodsRequestHandlerSize =
sizeof(kMethodsRequestHandler)/sizeof(kMethodsRequestHandler[0]);

if (env->RegisterNatives(RequestHandler_clazz(env),
kMethodsRequestHandler,
kMethodsRequestHandlerSize) < 0) {
//jni_generator::HandleRegistrationError(
// env, RequestHandler_clazz(env), __FILE__);
return false;
}

return true;
}

#endif // com_taobao_weex_bridge_RequestHandler_JNI
21 changes: 17 additions & 4 deletions weex_core/Source/core/bridge/platform/core_side_in_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,16 +429,29 @@ int CoreSideInPlatform::CreateInstance(const char *instanceId, const char *func,
const char *render_strategy) {
// First check about DATA_RENDER mode
if (render_strategy != nullptr) {
std::function<void(const char *)> exec_js =
[instanceId = std::string(instanceId), func = std::string(func),
opts = std::string(opts), initData = std::string(initData),
extendsApi = std::string(extendsApi)](const char *result) {
WeexCoreManager::Instance()
->script_bridge()
->script_side()
->CreateInstance(instanceId.c_str(), func.c_str(), result,
opts.c_str(), initData.c_str(),
extendsApi.c_str());
};
if (strcmp(render_strategy, "DATA_RENDER") == 0) {
auto node_manager =
weex::core::data_render::VNodeRenderManager::GetInstance();
node_manager->CreatePage(script, instanceId, render_strategy, initData, nullptr);
weex::core::data_render::VNodeRenderManager::GetInstance();
node_manager->CreatePage(script, instanceId, render_strategy, initData,
exec_js);

return true;
} else if (strcmp(render_strategy, "DATA_RENDER_BINARY") == 0) {
auto node_manager =
weex::core::data_render::VNodeRenderManager::GetInstance();
node_manager->CreatePage(script, script_length, instanceId, render_strategy, initData);
weex::core::data_render::VNodeRenderManager::GetInstance();
node_manager->CreatePage(script, script_length, instanceId,
render_strategy, initData);

return true;
}
Expand Down
Loading

0 comments on commit ff165ef

Please sign in to comment.