| 導(dǎo)語 本文描述了ContentProvider發(fā)布者和調(diào)用者這兩在Framework層是如何實(shí)現(xiàn)的。
作為Android的四大組件之一,ContentProvider作為進(jìn)程之間靜態(tài)數(shù)據(jù)傳遞的重要手段添坊,其在系統(tǒng)級別的應(yīng)用中起了重大的作用铅协。毫無疑問ContentProvider核心機(jī)制之一也是Binder剧浸,但和其它3大組件又有區(qū)別。因?yàn)镃ontentProvider涉及數(shù)據(jù)的增刪查改冶伞,當(dāng)數(shù)據(jù)量比較大的時(shí)候,繼續(xù)用Parcel做容器效率會比較低步氏,因此它還使用了匿名共享內(nèi)存的方式响禽。
但是有一個(gè)問題是,ContentProvider的提供者進(jìn)程不再存活時(shí)荚醒,其他進(jìn)程通過Provider讀一個(gè)非常簡單的數(shù)據(jù)時(shí)芋类,都需要先把提供者進(jìn)程啟動起來(除非指定multiprocess=true),這對用戶是相當(dāng)不友好的界阁。又因?yàn)槠涫情g接通過db進(jìn)行數(shù)據(jù)操作侯繁,所以效率也遠(yuǎn)不如直接操作db。因此在用戶app中泡躯,不是很建議經(jīng)常使用ContentProvider贮竟。不過對于系統(tǒng)級的app丽焊,它統(tǒng)一了數(shù)據(jù)操作的規(guī)范,利是遠(yuǎn)大于弊的咕别。
ContentProvider發(fā)布
當(dāng)進(jìn)程第一次啟動時(shí)候會調(diào)用handleBindApplication
if (!data.restrictedBackupMode) {
if (!ArrayUtils.isEmpty(data.providers)) {
installContentProviders(app, data.providers);
}
}
當(dāng)xml中有provider時(shí)技健,進(jìn)行provider的發(fā)布
final ArrayList<IActivityManager.ContentProviderHolder> results = new ArrayList<IActivityManager.ContentProviderHolder>();
for (ProviderInfo cpi : providers) {
IActivityManager.ContentProviderHolder cph = installProvider(context, null, cpi, false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
if (cph != null) {
cph.noReleaseNeeded = true;
results.add(cph);
}
}
try {
ActivityManagerNative.getDefault().publishContentProviders(
getApplicationThread(), results);
} catch (RemoteException ex) {
}
@installProvider(這個(gè)方法先簡單過一下,后面會繼續(xù)說)
final java.lang.ClassLoader cl = c.getClassLoader();
localProvider = (ContentProvider)cl.loadClass(info.name).newInstance();
provider = localProvider.getIContentProvider();
@installProviderAuthoritiesLocked
for (String auth : auths) {
final ProviderKey key = new ProviderKey(auth, userId);
final ProviderClientRecord existing = mProviderMap.get(key);
if (existing != null) {
} else {
mProviderMap.put(key, pcr);
}
}
這里兩步把ProviderInfo通過installProvider轉(zhuǎn)換成ContentProvider的Binder對象IContentProvider惰拱,并放于ContentProviderHolder中雌贱。并根據(jù)auth的不同,把發(fā)布進(jìn)程的ProviderClientRecord保存在一個(gè)叫mProviderMap的成員變量中弓颈,方便第二次調(diào)用同一個(gè)ContentProvider時(shí)帽芽,無需重新到AMS中去查詢。
AMS @publishContentProviders
final int N = providers.size();
for (int i = 0; i < N; i++) {
ContentProviderHolder src = providers.get(i);
...
ContentProviderRecord dst = r.pubProviders.get(src.info.name);
if (dst != null) {
ComponentName comp = new ComponentName(dst.info.packageName, dst.info.name);
mProviderMap.putProviderByClass(comp, dst);
String names[] = dst.info.authority.split(";");
for (int j = 0; j < names.length; j++) {
mProviderMap.putProviderByName(names[j], dst);
} int launchingCount = mLaunchingProviders.size();
int j;
boolean wasInLaunchingProviders = false;
for (j = 0; j < launchingCount; j++) {
if (mLaunchingProviders.get(j) == dst) {
mLaunchingProviders.remove(j);
wasInLaunchingProviders = true;
j--;
launchingCount--;
}
}
if (wasInLaunchingProviders) {
mHandler.removeMessages(CONTENT_PROVIDER_PUBLISH_TIMEOUT_MSG, r);
}
...
}
}
可以看到翔冀,AMS會遍歷所有ContentProviderHolder然后調(diào)用mProviderMap把信息保存起來导街,這塊接下來說。保存好之后纤子,先去看看之前是不是已經(jīng)有l(wèi)aunch過的搬瑰,如果已經(jīng)有l(wèi)aunch過的,不再重復(fù)launch控硼。再說說這個(gè)mProviderMap泽论,這個(gè)和ActivityThread中的mProviderMap不太一樣,這個(gè)是一個(gè)成員實(shí)例卡乾,非真正的map翼悴。
看看putProviderByClass和putProviderByName。
ProviderMap@putProviderByClass
if (record.singleton) {
mSingletonByClass.put(name, record);
} else {
final int userId = UserHandle.getUserId(record.appInfo.uid);
getProvidersByClass(userId).put(name, record);
}
ProviderMap@putProviderByName
if (record.singleton) {
mSingletonByName.put(name, record);
} else {
final int userId = UserHandle.getUserId(record.appInfo.uid);
getProvidersByName(userId).put(name, record);
}
可以看到幔妨,發(fā)布的Provider實(shí)際會根據(jù)class或authority存在不同的map中鹦赎。如果是單例,則分別存到相應(yīng)的mSingleton map中误堡,否則就根據(jù)userId存到相應(yīng)的map中古话。這樣發(fā)布的過程就完成了,其他進(jìn)程需要使用的時(shí)候?qū)贏MS按需讀取锁施。
ContentReslover跨進(jìn)程數(shù)據(jù)操作
當(dāng)我們跨進(jìn)程調(diào)用數(shù)據(jù)時(shí)候陪踩,會先調(diào)用獲取用戶進(jìn)程的
ContentResolver
context.getContentResolver().query(uri, ...);
public ContentResolver getContentResolver() {
return mContentResolver;
}
ContentResolver在每個(gè)進(jìn)程中都存在有且唯一的實(shí)例,其在ContextImpl構(gòu)造函數(shù)中就已經(jīng)初始化了悉抵,其初始化的實(shí)際對象ApplicationContentResolver肩狂。
mContentResolver = new ApplicationContentResolver(this, mainThread, user);
這個(gè)ContentResolver是活在調(diào)用者進(jìn)程中的,它是作為一個(gè)類似橋梁的作用姥饰。以插入為例:
ContentResolver@insert
IContentProvider provider = acquireProvider(url);
if (provider == null) {
throw new IllegalArgumentException("Unknown URL " + url);
}
try {
long startTime = SystemClock.uptimeMillis();
Uri createdRow = provider.insert(mPackageName, url, values);
... return createdRow;
} catch (RemoteException e) { return null;
} finally {
releaseProvider(provider);
}
問題就轉(zhuǎn)化成了婚温,拿到其他進(jìn)程的ContentProvider的Binder對象,有了binder對象就可以跨進(jìn)程調(diào)用其方法了媳否。ContentResolver@acquireProvider
if (!SCHEME_CONTENT.equals(uri.getScheme())) {
return null;
}
final String auth = uri.getAuthority();
if (auth != null) {
return acquireProvider(mContext, auth);
}
校驗(yàn)其URI栅螟,其scheme必須為content荆秦。ApplicationContentResolver@acquireProvider
protected IContentProvider acquireProvider(Context context, String auth) {
return mMainThread.acquireProvider(context,
ContentProvider.getAuthorityWithoutUserId(auth),
resolveUserIdFromAuthority(auth), true);
}
這里面有個(gè)特別的函數(shù)會傳遞一個(gè)true的參數(shù)給ActivityThread,這意味本次連接是stable的力图。那stable和非stable的區(qū)別是什么呢步绸?這么說吧:
Stable provider:若使用過程中,provider要是掛了吃媒,你的進(jìn)程也必掛瓤介。Unstable provider:若使用過程中,provider要是掛了赘那,你的進(jìn)程不會掛刑桑。但你會收到一個(gè)DeadObjectException的異常,可進(jìn)行容錯(cuò)處理募舟。繼續(xù)往下祠斧。ActivityThread@acquireProvider
final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
if (provider != null) {
return provider;
}
IActivityManager.ContentProviderHolder holder = null;
try {
holder = ActivityManagerNative.getDefault().getContentProvider(
getApplicationThread(), auth, userId, stable);
} catch (RemoteException ex) {
} if (holder == null) {
return null;
}
holder = installProvider(c, holder, holder.info, true /*noisy*/, holder.noReleaseNeeded, stable);
return holder.provider;
這里面分了三步,1拱礁、尋找自身進(jìn)程的緩存琢锋,有直接返回。 2呢灶、緩存沒有的話吴超,尋找AMS中的Provider。3鸯乃、InstallProvider鲸阻,又到了這個(gè)方法。怎么個(gè)install法缨睡?還是一會兒再說赘娄。@acquireExistingProvider (尋找自身緩存)
synchronized (mProviderMap) {
final ProviderKey key = new ProviderKey(auth, userId);
final ProviderClientRecord pr = mProviderMap.get(key);
if (pr == null) {
return null;
}
IContentProvider provider = pr.mProvider;
IBinder jBinder = provider.asBinder();
...
ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
if (prc != null) {
incProviderRefLocked(prc, stable);
} return provider;
這一步就是讀取我們發(fā)布時(shí)提到的mProviderMap中的緩存。當(dāng)provider記錄存在,且進(jìn)程存活的情況下,則在provider引用計(jì)數(shù)不為空時(shí)則繼續(xù)增加引用計(jì)數(shù)宏蛉。緩存不存在,則去AMS中找AMS@getContentProviderImpl
ContentProviderRecord cpr;
cpr = mProviderMap.getProviderByName(name, userId);
if (providerRunning){
if (r != null && cpr.canRunHere(r)) {
ContentProviderHolder holder = cpr.newHolder(null);
holder.provider = null;
return holder;
}
}
public boolean canRunHere(ProcessRecord app) {
return (info.multiprocess || info.processName.equals(app.processName))
&& uid == app.info.uid;
}
Provider是提供保護(hù)數(shù)據(jù)的接入訪問的性置。一般情況下拾并,不同進(jìn)程的訪問只能通過IPC來進(jìn)行,但那是有些情況是可以允許訪問者在自己的進(jìn)程中創(chuàng)建本地Provider來進(jìn)行訪問的鹏浅。
這種情況是在UID必須相同的前提下嗅义,要么同一進(jìn)程,要么provider設(shè)定了multiprocess為true
if (!providerRunning) {
cpi = AppGlobals.getPackageManager().resolveContentProvider(name,
STOCK_PM_FLAGS | PackageManager.GET_URI_PERMISSION_PATTERNS, userId);
...
ComponentName comp = new ComponentName(cpi.packageName, cpi.name);
cpr = mProviderMap.getProviderByClass(comp, userId);
if (r != null && cpr.canRunHere(r)) {
return cpr.newHolder(null);
}
ProcessRecord proc = getProcessRecordLocked(
cpi.processName, cpr.appInfo.uid, false);
if (proc != null && proc.thread != null) {
if (!proc.pubProviders.containsKey(cpi.name)) {
proc.pubProviders.put(cpi.name, cpr);
proc.thread.scheduleInstallProvider(cpi);
}
} else {
proc = startProcessLocked(cpi.processName,
cpr.appInfo, false, 0, "content provider",
new ComponentName(cpi.applicationInfo.packageName,
cpi.name), false, false, false);
}
}
}
mProviderMap.putProviderByName(name, cpr);
}
這塊步驟比較多隐砸,挑重點(diǎn)就是之碗,先從AMS的ProviderMap對象中獲取AMS緩存。獲得后如果Provider沒有l(wèi)aunch季希,則AMS通知其進(jìn)程install其provider褪那。如果進(jìn)程不存在幽纷,則新孵化一個(gè)進(jìn)程。@InstallProvider回到第三步中的installProvider
private IActivityManager.ContentProviderHolder installProvider(Context context,
IActivityManager.ContentProviderHolder holder, ProviderInfo info, boolean noisy, boolean noReleaseNeeded, boolean stable)
可以看到博敬,這個(gè)方法里面有6個(gè)參數(shù)友浸,其中包含ContentProviderHolder、ProviderInfo偏窝、noReleaseNeeded收恢,這幾個(gè)很重要的參數(shù)。
ContentProviderHolder:當(dāng)參數(shù)為空的時(shí)候祭往,說明緩存為空伦意,也就意味著是進(jìn)程啟動的時(shí)候調(diào)用發(fā)布provider。當(dāng)緩存不為空的時(shí)候硼补,還得做一些處理驮肉。ProviderInfo:包含Provider的一些信息,不能為空括勺。noReleaseNeeded:為true的時(shí)候Provider對于自身進(jìn)程來說或系統(tǒng)的Provider缆八,是永久install的,也就是不會被destory的疾捍。
ContentProvider localProvider = null;
IContentProvider provider;
if (holder == null || holder.provider == null) {
try {
final java.lang.ClassLoader cl = c.getClassLoader();
localProvider = (ContentProvider)cl.
loadClass(info.name).newInstance();
provider = localProvider.getIContentProvider();
if (provider == null) {
return null;
}
localProvider.attachInfo(c, info);
} catch (java.lang.Exception e) {
}
} else {
provider = holder.provider;
}
這部分在發(fā)布的時(shí)候已經(jīng)說了奈辰,緩存holder為null的時(shí)候,new一個(gè)實(shí)例乱豆。
IActivityManager.ContentProviderHolder retHolder;
synchronized (mProviderMap) {
IBinder jBinder = provider.asBinder();
if (localProvider != null) {
ComponentName cname = new ComponentName(info.packageName, info.name);
ProviderClientRecord pr = mLocalProvidersByName.get(cname);
if (pr != null) {
provider = pr.mProvider;
} else {
holder = new IActivityManager.ContentProviderHolder(info);
holder.provider = provider;
holder.noReleaseNeeded = true;
pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
mLocalProviders.put(jBinder, pr);
mLocalProvidersByName.put(cname, pr);
}
retHolder = pr.mHolder;
} else {
...
}
如果localProvider不等于null奖恰,則意味著是new一個(gè)實(shí)例的情況,這時(shí)候還是先去獲取緩存宛裕,沒有的話再真正地new一個(gè)ContentProviderHolder實(shí)例瑟啃,并把通過installProviderAuthoritiesLocked方法把相關(guān)信息存入mProviderMap中,這個(gè)就是對應(yīng)發(fā)布Provider提的那個(gè)方法揩尸。
IActivityManager.ContentProviderHolder retHolder;
synchronized (mProviderMap) {
...
} else {
ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
if (prc != null) {
if (!noReleaseNeeded) {
incProviderRefLocked(prc, stable);
try {
ActivityManagerNative.getDefault().removeContentProvider(
holder.connection, stable);
}
}
} else {
ProviderClientRecord client = installProviderAuthoritiesLocked(
provider, localProvider, holder);
if (noReleaseNeeded) {
prc = new ProviderRefCount(holder, client, 1000, 1000);
} else {
prc = stable
? new ProviderRefCount(holder, client, 1, 0)
: new ProviderRefCount(holder, client, 0, 1);
}
mProviderRefCountMap.put(jBinder, prc);
}
retHolder = prc.holder;
}
如果localProvider等于空蛹屿,也就意味著有holder緩存或者new時(shí)候出現(xiàn)的異常。那先從計(jì)數(shù)map中取緩存岩榆,如果緩存不為空(之前有過計(jì)數(shù)了)错负,這時(shí)候如果設(shè)置了noReleaseNeeded,那就說明不需要計(jì)數(shù)勇边。如果noReleaseNeeded為false犹撒,則把計(jì)數(shù)器數(shù)據(jù)轉(zhuǎn)移到一個(gè)新引用上,同時(shí)銷毀舊的粒褒。
如果緩存為空识颊,說明之前沒有計(jì)數(shù)過。那還是先通過installProviderAuthoritiesLocked把信息保存到mProviderMap中奕坟。這時(shí)候如果noReleaseNeeded為true祥款,把stable和非stable的數(shù)據(jù)都瞎設(shè)置了一個(gè)1000清笨,反正用不到。镰踏。函筋。否則就相應(yīng)的+1,并把計(jì)數(shù)器放入相應(yīng)的緩存中奠伪。最后再把holder返回跌帐。
再回到ContentResolver方法中,我們拿到了Provider的binder引用绊率,就可以執(zhí)行相應(yīng)的方法了
聲明:本文轉(zhuǎn)載自 2017-10-12 yixiongwang 騰訊