Skip to content

Commit

Permalink
Merge pull request #296 from jiongxuan/feature_no_guard_srv
Browse files Browse the repository at this point in the history
Support "UI process" as a plug-in management process
  • Loading branch information
cundong authored Aug 13, 2017
2 parents d574eab + 2d07650 commit 0987524
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ class RepluginConfig {
/** 自定义进程的数量(除 UI 和 Persistent 进程) */
def countProcess = 3

/** 是否使用常驻进程? */
def persistentEnable = true

/** 常驻进程名称(也就是上面说的 Persistent 进程,开发者可自定义)*/
def persistentName = ':GuardService'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ public class RePluginHostConfig {
// 常驻进程名字
public static String PERSISTENT_NAME = "${config.persistentName}";
// 是否使用“常驻进程”(见PERSISTENT_NAME)作为插件的管理进程。若为False,则会使用默认进程
public static boolean PERSISTENT_ENABLE = ${config.persistentEnable};
// 背景透明的坑的数量(每种 launchMode 不同)
public static int ACTIVITY_PIT_COUNT_TS_STANDARD = ${config.countTranslucentStandard};
public static int ACTIVITY_PIT_COUNT_TS_SINGLE_TOP = ${config.countTranslucentSingleTop};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,31 +71,33 @@ class ComponentsGenerator {

/* 需要编译期动态修改进程名的组件*/

String pluginMgrProcessName = config.persistentEnable ? config.persistentName : applicationID

// 常驻进程Provider
provider(
"${name}":"com.qihoo360.replugin.component.process.ProcessPitProviderPersist",
"${authorities}":"${applicationID}.loader.p.main",
"${exp}":"false",
"${process}":"${config.persistentName}")
"${process}":"${pluginMgrProcessName}")

provider(
"${name}":"com.qihoo360.replugin.component.provider.PluginPitProviderPersist",
"${authorities}":"${applicationID}.Plugin.NP.PSP",
"${exp}":"false",
"${process}":"${config.persistentName}")
"${process}":"${pluginMgrProcessName}")

// ServiceManager 服务框架
provider(
"${name}":"com.qihoo360.mobilesafe.svcmanager.ServiceProvider",
"${authorities}":"${applicationID}.svcmanager",
"${exp}":"false",
"${multiprocess}":"false",
"${process}":"${config.persistentName}")
"${process}":"${pluginMgrProcessName}")

service(
"${name}":"com.qihoo360.replugin.component.service.server.PluginPitServiceGuard",
"${process}":"${config.persistentName}")
"${process}":"${pluginMgrProcessName}")

/* 透明坑 */
config.countTranslucentStandard.times {
activity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ static final void installHost(IPluginHost host) {
/**
* 非常驻进程调用,获取常驻进程的 IPluginHost
*/
static final void installHost() {
static final void connectToHostSvc() {
Context context = PMF.getApplicationContext();

//
Expand Down Expand Up @@ -352,6 +352,7 @@ public void binderDied() {
System.exit(1);
}

// 注册该进程信息到“插件管理进程”中
PMF.sPluginMgr.attach();
}

Expand All @@ -369,7 +370,7 @@ public static final IPluginHost getPluginHost() {
}
}
// 再次唤起常驻进程
installHost();
connectToHostSvc();
}
return sPluginHostRemote;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
import android.os.RemoteException;
import android.text.TextUtils;

import com.qihoo360.replugin.utils.CloseableUtils;
import com.qihoo360.loader2.sp.IPref;
import com.qihoo360.loader2.sp.PrefImpl;
import com.qihoo360.replugin.base.IPC;
import com.qihoo360.replugin.component.process.ProcessPitProviderBase;
import com.qihoo360.replugin.component.process.ProcessPitProviderPersist;
import com.qihoo360.replugin.helper.LogDebug;
import com.qihoo360.replugin.utils.CloseableUtils;

import java.util.Arrays;

Expand Down Expand Up @@ -127,7 +127,7 @@ public static final Uri stubPlugin(Uri uri, ContentValues values) {
//
PMF.sPluginMgr.mLocalCookie = cookie;
//
PluginProcessMain.installHost();
PluginProcessMain.connectToHostSvc();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.qihoo360.replugin.component.dummy.DummyService;
import com.qihoo360.replugin.component.process.PluginProcessHost;
import com.qihoo360.replugin.component.service.server.PluginPitService;
import com.qihoo360.replugin.helper.HostConfigHelper;
import com.qihoo360.replugin.helper.LogDebug;
import com.qihoo360.replugin.helper.LogRelease;
import com.qihoo360.replugin.model.PluginInfo;
Expand Down Expand Up @@ -228,19 +229,29 @@ static final void cleanIntentPluginParams(Intent intent) {
}

void init() {
if (IPC.isPersistentProcess()) {
mHostSvc = new PmHostSvc(mContext, this);
PluginProcessMain.installHost(mHostSvc);
PluginProcessMain.schedulePluginProcessLoop(PluginProcessMain.CHECK_STAGE1_DELAY);

if (HostConfigHelper.PERSISTENT_ENABLE) {
// (默认)“常驻进程”作为插件管理进程,则常驻进程作为Server,其余进程作为Client
if (IPC.isPersistentProcess()) {
// 初始化“Server”所做工作
initForServer();
} else {
// 连接到Server
initForClient();
}
} else {
PluginProcessMain.installHost();
}
// “UI进程”作为插件管理进程(唯一进程),则UI进程既可以作为Server也可以作为Client
if (IPC.isUIProcess()) {
// 1. 尝试初始化Server所做工作,
initForServer();

if (IPC.isPersistentProcess()) {
initForPersistent();
} else {
initForClient();
// 2. 注册该进程信息到“插件管理进程”中
// 注意:这里无需再做 initForClient,因为不需要再走一次Binder
PMF.sPluginMgr.attach();

} else {
// 其它进程?直接连接到Server即可
initForClient();
}
}

// 最新快照
Expand All @@ -258,11 +269,15 @@ void init() {
* Persistent(常驻)进程的初始化
*
*/
private final void initForPersistent() {
private final void initForServer() {
if (LOG) {
LogDebug.d(PLUGIN_TAG, "search plugins from file system");
}

mHostSvc = new PmHostSvc(mContext, this);
PluginProcessMain.installHost(mHostSvc);
PluginProcessMain.schedulePluginProcessLoop(PluginProcessMain.CHECK_STAGE1_DELAY);

// 兼容即将废弃的p-n方案 by Jiongxuan Zhang
mAll = new Builder.PxAll();
Builder.builder(mContext, mAll);
Expand All @@ -282,8 +297,6 @@ private final void initForPersistent() {
LogRelease.e(PLUGIN_TAG, "lst.p: " + e.getMessage(), e);
}
}


}

/**
Expand All @@ -295,6 +308,17 @@ private final void initForClient() {
LogDebug.d(PLUGIN_TAG, "list plugins from persistent process");
}

// 1. 先尝试连接
PluginProcessMain.connectToHostSvc();

// 2. 然后从常驻进程获取插件列表
refreshPluginsFromHostSvc();
}

/**
* 从HostSvc(插件管理所在进程)获取所有的插件信息
*/
private void refreshPluginsFromHostSvc() {
List<PluginInfo> plugins = null;
try {
plugins = PluginProcessMain.getPluginHost().listPlugins();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,30 +143,6 @@ public RePluginConfig setVerifySign(boolean verifySign) {
return this;
}

/**
* 是否开启"双进程"模式?
*
* @return 是否开启
*/
public boolean isPersistentEnable() {
return persistentEnable;
}

/**
* 设置是否允许开启"双进程"模式,开启后会极大的提升插件加载和获取的性能 <p>
* TODO 尚不支持单进程模式,在以后会开发
*
* @param persistentEnable 是否开启
* @return RePluginConfig自己。这样可以连环调用set方法
*/
public RePluginConfig setPersistentEnable(boolean persistentEnable) {
if (!checkAllowModify()) {
return this;
}
this.persistentEnable = persistentEnable;
return this;
}

/**
* 是否当插件没有指定类时,使用宿主的类? <p>
* 有关该开关的具体说明,请参见setUseHostClass方法
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import com.qihoo360.loader.utils.SysUtils;
import com.qihoo360.loader2.PluginProcessMain;
import com.qihoo360.replugin.RePlugin;
import com.qihoo360.replugin.helper.HostConfigHelper;
import com.qihoo360.replugin.helper.LogDebug;

Expand Down Expand Up @@ -58,13 +57,17 @@ public static void init(Context context) {
sPackageName = context.getApplicationInfo().packageName;

// 设置最终的常驻进程名
String cppn = HostConfigHelper.PERSISTENT_NAME;
if (!TextUtils.isEmpty(cppn)) {
if (cppn.startsWith(":")) {
sPersistentProcessName = sPackageName + cppn;
} else {
sPersistentProcessName = cppn;
if (HostConfigHelper.PERSISTENT_ENABLE) {
String cppn = HostConfigHelper.PERSISTENT_NAME;
if (!TextUtils.isEmpty(cppn)) {
if (cppn.startsWith(":")) {
sPersistentProcessName = sPackageName + cppn;
} else {
sPersistentProcessName = cppn;
}
}
} else {
sPersistentProcessName = sPackageName;
}

sIsUIProcess = sCurrentProcess.equals(sPackageName);
Expand Down Expand Up @@ -105,11 +108,7 @@ public static String getPersistentProcessName() {
* @return 插件处理逻辑所在进程名
*/
public static String getPluginHostProcessName() {
if (isPersistentEnable()) {
return getPersistentProcessName();
} else {
return getCurrentProcessName();
}
return sPersistentProcessName;
}

/**
Expand Down Expand Up @@ -148,7 +147,7 @@ public static boolean isPersistentProcess() {
* @return 是否支持?
*/
public static boolean isPersistentEnable() {
return RePlugin.getConfig().isPersistentEnable();
return HostConfigHelper.PERSISTENT_ENABLE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class HostConfigHelper {
// 注意:以下配置项必须和 replugin-host-gradle 插件中的配置相同
//------------------------------------------------------------

// 是否使用“常驻进程”(见PERSISTENT_NAME)作为插件的管理进程
public static boolean PERSISTENT_ENABLE = true;

// 常驻进程名
public static String PERSISTENT_NAME = ":GuardService";

Expand Down Expand Up @@ -76,6 +79,12 @@ public class HostConfigHelper {
// Ignore, Just use default value
}

try {
PERSISTENT_ENABLE = readField("PERSISTENT_ENABLE");
} catch (NoSuchFieldException e) {
// Ignore, Just use default value
}

try {
PERSISTENT_NAME = readField("PERSISTENT_NAME");
} catch (NoSuchFieldException e) {
Expand Down

0 comments on commit 0987524

Please sign in to comment.