美文网首页
Android10.0 ContentProvider源码分析

Android10.0 ContentProvider源码分析

作者: 竖起大拇指 | 来源:发表于2020-08-26 13:58 被阅读0次

    1.ContentProvider

    ContentProvider为不同的应用程序之间的数据共享提供了统一的访问接口,它需要和ContentResolver配合使用,ContentProvider负责提供数据,ContentResolver负责获取数据。在应用程序启动的时候,ContentProvider就会被初始化注册到ActivityManagerService,然后其他应用通过uri向服务端获取ContentProvider所对应的Transport对象。Transport对象本质上是一个Binder,有了Binder这个中间人对象,我们就可以调用远程的ContentProvider所提供的方法了。

    2.注册ContentProvider

    <provider
                android:name=".provider.MyContentProvider"
                android:authorities="com.test.provider"
                android:process=":remote"
                android:enabled="true"
                android:exported="true"/>
    

    通过name指定ContentProvider所在的路径,authorities作为ContentProvider唯一的身份标识,外界也就是通过它来访问ContentProvider的,process为ContentProvider指定所在的进程,exported属性表示是否允许外部应用访问,默认为true。

    3.ContentProvider的注册过程

    注册首先是从ActivityThread的main方法开始的,然后直接到ContentProvider的onCreate方法被回调,此时就标记整个注册过程完成。下面我们来看下ActivityThread的main方法:

     //ActivityThread.java
       public static void main(String[] args) {
            Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain");
    
            // Install selective syscall interception
            AndroidOs.install();
    
            // CloseGuard defaults to true and can be quite spammy.  We
            // disable it here, but selectively enable it later (via
            // StrictMode) on debug builds, but using DropBox, not logs.
            CloseGuard.setEnabled(false);
    
            Environment.initForCurrentUser();
    
            // Make sure TrustedCertificateStore looks in the right place for CA certificates
            final File configDir = Environment.getUserConfigDirectory(UserHandle.myUserId());
            TrustedCertificateStore.setDefaultUserDirectory(configDir);
    
            Process.setArgV0("<pre-initialized>");
    
            Looper.prepareMainLooper();
    
            // Find the value for {@link #PROC_START_SEQ_IDENT} if provided on the command line.
            // It will be in the format "seq=114"
            long startSeq = 0;
            if (args != null) {
                for (int i = args.length - 1; i >= 0; --i) {
                    if (args[i] != null && args[i].startsWith(PROC_START_SEQ_IDENT)) {
                        startSeq = Long.parseLong(
                                args[i].substring(PROC_START_SEQ_IDENT.length()));
                    }
                }
            }
            //注释1
            ActivityThread thread = new ActivityThread();
              //注释2
            thread.attach(false, startSeq);
    
            if (sMainThreadHandler == null) {
                //注释3
                sMainThreadHandler = thread.getHandler();
            }
    
            if (false) {
                Looper.myLooper().setMessageLogging(new
                        LogPrinter(Log.DEBUG, "ActivityThread"));
            }
    
            // End of event ActivityThreadMain.
            Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
            Looper.loop();
    
            throw new RuntimeException("Main thread loop unexpectedly exited");
        }
    

    我们知道在我们应用程序的进程被创建完毕之后,紧接着就会执行ActivityThread的main方法初始化应用,在main方法里主要做了这么几个工作,首先注释1处创建了一个ActivityThread对象,然后注释2处挂载了应用程序,紧接着在注释3处创建主线程的消息管理器。应用程序的ContentProvider就是在ActivityThread的注释2处的attach方法所初始化的。该方法的实现如下:

      //ActivityThread.java
       @UnsupportedAppUsage
        private void attach(boolean system, long startSeq) {
            sCurrentActivityThread = this;
            mSystemThread = system;
            if (!system) {
                android.ddm.DdmHandleAppName.setAppName("<pre-initialized>",
                                                        UserHandle.myUserId());
                RuntimeInit.setApplicationObject(mAppThread.asBinder());
                final IActivityManager mgr = ActivityManager.getService();
                try {
                  //注释1
                    mgr.attachApplication(mAppThread, startSeq);
                } catch (RemoteException ex) {
                    throw ex.rethrowFromSystemServer();
                }
                // Watch for getting close to heap limit.
                BinderInternal.addGcWatcher(new Runnable() {
                    @Override public void run() {
                        if (!mSomeActivitiesChanged) {
                            return;
                        }
                        Runtime runtime = Runtime.getRuntime();
                        long dalvikMax = runtime.maxMemory();
                        long dalvikUsed = runtime.totalMemory() - runtime.freeMemory();
                        if (dalvikUsed > ((3*dalvikMax)/4)) {
                            if (DEBUG_MEMORY_TRIM) Slog.d(TAG, "Dalvik max=" + (dalvikMax/1024)
                                    + " total=" + (runtime.totalMemory()/1024)
                                    + " used=" + (dalvikUsed/1024));
                            mSomeActivitiesChanged = false;
                            try {
                                ActivityTaskManager.getService().releaseSomeActivities(mAppThread);
                            } catch (RemoteException e) {
                                throw e.rethrowFromSystemServer();
                            }
                        }
                    }
                });
            } else {
                // Don't set application object here -- if the system crashes,
                // we can't display an alert, we just want to die die die.
                android.ddm.DdmHandleAppName.setAppName("system_process",
                        UserHandle.myUserId());
                try {
                    mInstrumentation = new Instrumentation();
                    mInstrumentation.basicInit(this);
                    ContextImpl context = ContextImpl.createAppContext(
                            this, getSystemContext().mPackageInfo);
                    mInitialApplication = context.mPackageInfo.makeApplication(true, null);
                    mInitialApplication.onCreate();
                } catch (Exception e) {
                    throw new RuntimeException(
                            "Unable to instantiate Application():" + e.toString(), e);
                }
            }
    
            ViewRootImpl.ConfigChangedCallback configChangedCallback
                    = (Configuration globalConfig) -> {
                synchronized (mResourcesManager) {
                    // We need to apply this change to the resources immediately, because upon returning
                    // the view hierarchy will be informed about it.
                    if (mResourcesManager.applyConfigurationToResourcesLocked(globalConfig,
                            null /* compat */)) {
                        updateLocaleListFromAppContext(mInitialApplication.getApplicationContext(),
                                mResourcesManager.getConfiguration().getLocales());
    
                        // This actually changed the resources! Tell everyone about it.
                        if (mPendingConfiguration == null
                                || mPendingConfiguration.isOtherSeqNewer(globalConfig)) {
                            mPendingConfiguration = globalConfig;
                            sendMessage(H.CONFIGURATION_CHANGED, globalConfig);
                        }
                    }
                }
            };
            ViewRootImpl.addConfigCallback(configChangedCallback);
        }
    

    注释1处它调用了ActivityManager.getService()对象的attachApplication方法,下面我们看下ActivityManagerService的attachApplication方法:

      //ActivityManagerService.java
       @Override
        public final void attachApplication(IApplicationThread thread, long startSeq) {
            synchronized (this) {
                int callingPid = Binder.getCallingPid();
                final int callingUid = Binder.getCallingUid();
                final long origId = Binder.clearCallingIdentity();
              
              //注释1
                attachApplicationLocked(thread, callingPid, callingUid, startSeq);
                Binder.restoreCallingIdentity(origId);
            }
        }
    
    
    @GuardedBy("this")
        private final boolean attachApplicationLocked(IApplicationThread thread,
                int pid, int callingUid, long startSeq) {
    
            // Find the application record that is being attached...  either via
            // the pid if we are running in multiple processes, or just pull the
            // next app record if we are emulating process with anonymous threads.
            ProcessRecord app;
            long startTime = SystemClock.uptimeMillis();
            long bindApplicationTimeMillis;
       ..................省略.................
    
    
    checkTime(startTime, "attachApplicationLocked: immediately before bindApplication");
                bindApplicationTimeMillis = SystemClock.elapsedRealtime();
                mAtmInternal.preBindApplication(app.getWindowProcessController());
                final ActiveInstrumentation instr2 = app.getActiveInstrumentation();
                if (app.isolatedEntryPoint != null) {
                    // This is an isolated process which should just call an entry point instead of
                    // being bound to an application.
                    thread.runIsolatedEntryPoint(app.isolatedEntryPoint, app.isolatedEntryPointArgs);
                } else if (instr2 != null) {
                    //注释2
                    thread.bindApplication(processName, appInfo, providers,
                            instr2.mClass,
                            profilerInfo, instr2.mArguments,
                            instr2.mWatcher,
                            instr2.mUiAutomationConnection, testMode,
                            mBinderTransactionTrackingEnabled, enableTrackAllocation,
                            isRestrictedBackupMode || !normalMode, app.isPersistent(),
                            new Configuration(app.getWindowProcessController().getConfiguration()),
                            app.compat, getCommonServicesLocked(app.isolated),
                            mCoreSettingsObserver.getCoreSettingsLocked(),
                            buildSerial, autofillOptions, contentCaptureOptions);
                } else {
                    thread.bindApplication(processName, appInfo, providers, null, profilerInfo,
                            null, null, null, testMode,
                            mBinderTransactionTrackingEnabled, enableTrackAllocation,
                            isRestrictedBackupMode || !normalMode, app.isPersistent(),
                            new Configuration(app.getWindowProcessController().getConfiguration()),
                            app.compat, getCommonServicesLocked(app.isolated),
                            mCoreSettingsObserver.getCoreSettingsLocked(),
                            buildSerial, autofillOptions, contentCaptureOptions);
                }
                if (profilerInfo != null) {
                    profilerInfo.closeFd();
                    profilerInfo = null;
                }
    
                // Make app active after binding application or client may be running requests (e.g
                // starting activities) before it is ready.
                app.makeActive(thread, mProcessStats);
                checkTime(startTime, "attachApplicationLocked: immediately after bindApplication");
                mProcessList.updateLruProcessLocked(app, false, null);
                checkTime(startTime, "attachApplicationLocked: after updateLruProcessLocked");
                app.lastRequestedGc = app.lastLowMemory = SystemClock.uptimeMillis();
            } catch (Exception e) {
                // todo: Yikes!  What should we do?  For now we will try to
                // start another process, but that could easily get us in
                // an infinite loop of restarting processes...
                Slog.wtf(TAG, "Exception thrown during bind of " + app, e);
    
                app.resetPackageList(mProcessStats);
                app.unlinkDeathRecipient();
                mProcessList.startProcessLocked(app, new HostingRecord("bind-fail", processName));
                return false;
            }
    
          ...............省略......................
    }
    
    

    我们直接看注释2,此处调用了ApplicationThread的bindApplication方法,该方法的实现如下:

    private class ApplicationThread extends IApplicationThread.Stub {
    
    public final void bindApplication(String processName, ApplicationInfo appInfo,
                    List<ProviderInfo> providers, ComponentName instrumentationName,
                    ProfilerInfo profilerInfo, Bundle instrumentationArgs,
                    IInstrumentationWatcher instrumentationWatcher,
                    IUiAutomationConnection instrumentationUiConnection, int debugMode,
                    boolean enableBinderTracking, boolean trackAllocation,
                    boolean isRestrictedBackupMode, boolean persistent, Configuration config,
                    CompatibilityInfo compatInfo, Map services, Bundle coreSettings,
                    String buildSerial, AutofillOptions autofillOptions,
                    ContentCaptureOptions contentCaptureOptions) {
                if (services != null) {
                    if (false) {
                        // Test code to make sure the app could see the passed-in services.
                        for (Object oname : services.keySet()) {
                            if (services.get(oname) == null) {
                                continue; // AM just passed in a null service.
                            }
                            String name = (String) oname;
    
                            // See b/79378449 about the following exemption.
                            switch (name) {
                                case "package":
                                case Context.WINDOW_SERVICE:
                                    continue;
                            }
    
                            if (ServiceManager.getService(name) == null) {
                                Log.wtf(TAG, "Service " + name + " should be accessible by this app");
                            }
                        }
                    }
    
                    // Setup the service cache in the ServiceManager
                    ServiceManager.initServiceCache(services);
                }
    
                setCoreSettings(coreSettings);
    
                AppBindData data = new AppBindData();
                data.processName = processName;
                data.appInfo = appInfo;
                data.providers = providers;
                data.instrumentationName = instrumentationName;
                data.instrumentationArgs = instrumentationArgs;
                data.instrumentationWatcher = instrumentationWatcher;
                data.instrumentationUiAutomationConnection = instrumentationUiConnection;
                data.debugMode = debugMode;
                data.enableBinderTracking = enableBinderTracking;
                data.trackAllocation = trackAllocation;
                data.restrictedBackupMode = isRestrictedBackupMode;
                data.persistent = persistent;
                data.config = config;
                data.compatInfo = compatInfo;
                data.initProfilerInfo = profilerInfo;
                data.buildSerial = buildSerial;
                data.autofillOptions = autofillOptions;
                data.contentCaptureOptions = contentCaptureOptions;
                sendMessage(H.BIND_APPLICATION, data);
            }
    }
    

    此时构建了一个AppBindData并把它传递给了ActivityThread的消息管理器Handler,收到消息后会调用ActivityThread的handleBindApplicaiton方法,该方法的具体实现如下:

       //ActivityThread.java
     @UnsupportedAppUsage
        private void handleBindApplication(AppBindData data) {
         ................省略....................
    // block processing ordered broadcasts, but later processing would
            // probably end up doing the same disk access.
            Application app;
            final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskWrites();
            final StrictMode.ThreadPolicy writesAllowedPolicy = StrictMode.getThreadPolicy();
            try {
                // If the app is being launched for full backup or restore, bring it up in
                // a restricted environment with the base application class.
                //注释1
                app = data.info.makeApplication(data.restrictedBackupMode, null);
    
                // Propagate autofill compat state
                app.setAutofillOptions(data.autofillOptions);
    
                // Propagate Content Capture options
                app.setContentCaptureOptions(data.contentCaptureOptions);
    
                mInitialApplication = app;
    
                // don't bring up providers in restricted mode; they may depend on the
                // app's custom Application class
                if (!data.restrictedBackupMode) {
                    if (!ArrayUtils.isEmpty(data.providers)) {
                          //注释2
                        installContentProviders(app, data.providers);
                    }
                }
    
                // Do this after providers, since instrumentation tests generally start their
                // test thread at this point, and we don't want that racing.
                try {
                    mInstrumentation.onCreate(data.instrumentationArgs);
                }
                catch (Exception e) {
                    throw new RuntimeException(
                        "Exception thrown in onCreate() of "
                        + data.instrumentationName + ": " + e.toString(), e);
                }
                try {
                    //注释3
                    mInstrumentation.callApplicationOnCreate(app);
                } catch (Exception e) {
                    if (!mInstrumentation.onException(app, e)) {
                        throw new RuntimeException(
                          "Unable to create application " + app.getClass().getName()
                          + ": " + e.toString(), e);
                    }
                }
            } finally {
                // If the app targets < O-MR1, or doesn't change the thread policy
                // during startup, clobber the policy to maintain behavior of b/36951662
                if (data.appInfo.targetSdkVersion < Build.VERSION_CODES.O_MR1
                        || StrictMode.getThreadPolicy().equals(writesAllowedPolicy)) {
                    StrictMode.setThreadPolicy(savedPolicy);
                }
            }
          .... 省略
    
    }
    

    我们只看关键部分,注释1处Application创建了,在注释2处我们终于找到了初始化ContentProvider的真正方法installContentProviders,在注释3处Application的onCreate被回调了,我们有没有发现ContentProvider是先于Application初始化的。,下面我们主要看ContentProvider初始化的核心方法installContentProviders,该方法的具体实现如下:

        //ActivityThread.java
    
     @UnsupportedAppUsage
        private void installContentProviders(
                Context context, List<ProviderInfo> providers) {
            final ArrayList<ContentProviderHolder> results = new ArrayList<>();
    
            for (ProviderInfo cpi : providers) {
                if (DEBUG_PROVIDER) {
                    StringBuilder buf = new StringBuilder(128);
                    buf.append("Pub ");
                    buf.append(cpi.authority);
                    buf.append(": ");
                    buf.append(cpi.name);
                    Log.i(TAG, buf.toString());
                }
                //注释1
                ContentProviderHolder cph = installProvider(context, null, cpi,
                        false /*noisy*/, true /*noReleaseNeeded*/, true /*stable*/);
                if (cph != null) {
                    cph.noReleaseNeeded = true;
                    results.add(cph);
                }
            }
    
            try {
                //注释2
                ActivityManager.getService().publishContentProviders(
                    getApplicationThread(), results);
            } catch (RemoteException ex) {
                throw ex.rethrowFromSystemServer();
            }
        }
    

    installContentProviders方法的实现不是很复杂,就做了一件事就是初始化了一个ContentProviderHolder集合,并把它传递给了服务端,ContentProviderHolder具体是什么,我们看下注释1处的installProvider方法,

     //ActivityThread.java
      
     @UnsupportedAppUsage
        private ContentProviderHolder installProvider(Context context,
                ContentProviderHolder holder, ProviderInfo info,
                boolean noisy, boolean noReleaseNeeded, boolean stable) {
            ContentProvider localProvider = null;
            IContentProvider provider;
            if (holder == null || holder.provider == null) {
                if (DEBUG_PROVIDER || noisy) {
                    Slog.d(TAG, "Loading provider " + info.authority + ": "
                            + info.name);
                }
                Context c = null;
                ApplicationInfo ai = info.applicationInfo;
                if (context.getPackageName().equals(ai.packageName)) {
                    c = context;
                } else if (mInitialApplication != null &&
                        mInitialApplication.getPackageName().equals(ai.packageName)) {
                    c = mInitialApplication;
                } else {
                    try {
                        c = context.createPackageContext(ai.packageName,
                                Context.CONTEXT_INCLUDE_CODE);
                    } catch (PackageManager.NameNotFoundException e) {
                        // Ignore
                    }
                }
                if (c == null) {
                    Slog.w(TAG, "Unable to get context for package " +
                          ai.packageName +
                          " while loading content provider " +
                          info.name);
                    return null;
                }
    
                if (info.splitName != null) {
                    try {
                        c = c.createContextForSplit(info.splitName);
                    } catch (NameNotFoundException e) {
                        throw new RuntimeException(e);
                    }
                }
    
                try {
                    final java.lang.ClassLoader cl = c.getClassLoader();
                    LoadedApk packageInfo = peekPackageInfo(ai.packageName, true);
                    if (packageInfo == null) {
                        // System startup case.
                        packageInfo = getSystemContext().mPackageInfo;
                    }
                    //注释1
                    localProvider = packageInfo.getAppFactory()
                            .instantiateProvider(cl, info.name);
                    //注释2
                    provider = localProvider.getIContentProvider();
                    if (provider == null) {
                        Slog.e(TAG, "Failed to instantiate class " +
                              info.name + " from sourceDir " +
                              info.applicationInfo.sourceDir);
                        return null;
                    }
                    if (DEBUG_PROVIDER) Slog.v(
                        TAG, "Instantiating local provider " + info.name);
                    // XXX Need to create the correct context for this provider.
                    //注释3
                    localProvider.attachInfo(c, info);
                } catch (java.lang.Exception e) {
                    if (!mInstrumentation.onException(null, e)) {
                        throw new RuntimeException(
                                "Unable to get provider " + info.name
                                + ": " + e.toString(), e);
                    }
                    return null;
                }
            } else {
                provider = holder.provider;
                if (DEBUG_PROVIDER) Slog.v(TAG, "Installing external provider " + info.authority + ": "
                        + info.name);
            }
    
            ContentProviderHolder retHolder;
    
            synchronized (mProviderMap) {
                if (DEBUG_PROVIDER) Slog.v(TAG, "Checking to add " + provider
                        + " / " + info.name);
                IBinder jBinder = provider.asBinder();
                if (localProvider != null) {
                    ComponentName cname = new ComponentName(info.packageName, info.name);
                    ProviderClientRecord pr = mLocalProvidersByName.get(cname);
                    if (pr != null) {
                        if (DEBUG_PROVIDER) {
                            Slog.v(TAG, "installProvider: lost the race, "
                                    + "using existing local provider");
                        }
                        provider = pr.mProvider;
                    } else {
                            //注释4
                        holder = new ContentProviderHolder(info);
                          //注释5
                        holder.provider = provider;
                        holder.noReleaseNeeded = true;
                        pr = installProviderAuthoritiesLocked(provider, localProvider, holder);
                        mLocalProviders.put(jBinder, pr);
                        mLocalProvidersByName.put(cname, pr);
                    }
                    retHolder = pr.mHolder;
                } else {
                    ProviderRefCount prc = mProviderRefCountMap.get(jBinder);
                    if (prc != null) {
                        if (DEBUG_PROVIDER) {
                            Slog.v(TAG, "installProvider: lost the race, updating ref count");
                        }
                        // We need to transfer our new reference to the existing
                        // ref count, releasing the old one...  but only if
                        // release is needed (that is, it is not running in the
                        // system process).
                        if (!noReleaseNeeded) {
                            incProviderRefLocked(prc, stable);
                            try {
                                ActivityManager.getService().removeContentProvider(
                                        holder.connection, stable);
                            } catch (RemoteException e) {
                                //do nothing content provider object is dead any way
                            }
                        }
                    } 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;
                }
            }
            return retHolder;
        }
    

    在注释1处调用了packageInfo.getAppFactory()对象的instantiateProvider方法,我们来看下该方法的具体实现:

      //AppComponentFactory.java
    
     public @NonNull ContentProvider instantiateProvider(@NonNull ClassLoader cl,
                @NonNull String className)
                throws InstantiationException, IllegalAccessException, ClassNotFoundException {
            return (ContentProvider) cl.loadClass(className).newInstance();
        }
    

    在instantiateProvider方法里ContentProvider终于通过反射实例化了。

    在注释2处调用了ContentProvider的getIContentProvider方法,我们看看具体实现:

      //ContentProvider.java
    
    private Transport mTransport = new Transport();
    
      /**
         * Returns the Binder object for this provider.
         *
         * @return the Binder object for this provider
         * @hide
         */
        @UnsupportedAppUsage
        public IContentProvider getIContentProvider() {
            return mTransport;
        }
    
    

    返回了一个Transport对象,我们来看下它的具体实现:

     /**
         * Binder object that deals with remoting.
         *
         * @hide
         */
        class Transport extends ContentProviderNative {
    }
    
    abstract public class ContentProviderNative extends Binder implements IContentProvider {
    }
    

    通过继承关系我们发现Transport继承了ContentProviderNative,而ContentProviderNative又继承了Binder并实现了IContentProvider接口,所以注释2处的provider的实现就是Transport,而它就是一个Binder,这个binder会在注释5处被赋值给ContentProviderHolder对象的provider属性,installProvider方法最后返回了持有ContentProvider的Binder对象的ContentProviderHolder。
    然后我们在看注释3处调用了ContentProvider.attachInfo方法,该方法具体实现如下:

        //ContentProvider.java
    
    /**
         * After being instantiated, this is called to tell the content provider
         * about itself.
         *
         * @param context The context this provider is running in
         * @param info Registered information about this content provider
         */
        public void attachInfo(Context context, ProviderInfo info) {
            attachInfo(context, info, false);
        }
    
        private void attachInfo(Context context, ProviderInfo info, boolean testing) {
            mNoPerms = testing;
            mCallingPackage = new ThreadLocal<>();
    
            /*
             * Only allow it to be set once, so after the content service gives
             * this to us clients can't change it.
             */
            if (mContext == null) {
                mContext = context;
                if (context != null && mTransport != null) {
                    mTransport.mAppOpsManager = (AppOpsManager) context.getSystemService(
                            Context.APP_OPS_SERVICE);
                }
                mMyUid = Process.myUid();
                if (info != null) {
                    setReadPermission(info.readPermission);
                    setWritePermission(info.writePermission);
                    setPathPermissions(info.pathPermissions);
                    mExported = info.exported;
                    mSingleUser = (info.flags & ProviderInfo.FLAG_SINGLE_USER) != 0;
                    setAuthorities(info.authority);
                }
    
                //注释1
                ContentProvider.this.onCreate();
            }
        }
    

    看注释1,我们终于发现我们的ContentProvider的onCreate方法被调用了,意味着ContentProvider的启动已经完成。

    现在我们再看installContentProviders方法的注释2处调用了ActivityManager.getService()对象的publishContentProviders方法,并把客户端生成的ContentProviderHolder对象传递给了服务端。我们在看ActivityManagerService对象的publishContentProviders方法的具体实现:

       //ActivityManagerService.java
    
      public final void publishContentProviders(IApplicationThread caller,
                List<ContentProviderHolder> providers) {
            if (providers == null) {
                return;
            }
    
            enforceNotIsolatedCaller("publishContentProviders");
            synchronized (this) {
                final ProcessRecord r = getRecordForAppLocked(caller);
                if (DEBUG_MU) Slog.v(TAG_MU, "ProcessRecord uid = " + r.uid);
                if (r == null) {
                    throw new SecurityException(
                            "Unable to find app for caller " + caller
                          + " (pid=" + Binder.getCallingPid()
                          + ") when publishing content providers");
                }
    
                final long origId = Binder.clearCallingIdentity();
    
                final int N = providers.size();
                for (int i = 0; i < N; i++) {
                    ContentProviderHolder src = providers.get(i);
                    if (src == null || src.info == null || src.provider == null) {
                        continue;
                    }
                    ContentProviderRecord dst = r.pubProviders.get(src.info.name);
                    if (DEBUG_MU) Slog.v(TAG_MU, "ContentProviderRecord uid = " + dst.uid);
                    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);
                        }
                        // Make sure the package is associated with the process.
                        // XXX We shouldn't need to do this, since we have added the package
                        // when we generated the providers in generateApplicationProvidersLocked().
                        // But for some reason in some cases we get here with the package no longer
                        // added...  for now just patch it in to make things happy.
                        r.addPackage(dst.info.applicationInfo.packageName,
                                dst.info.applicationInfo.longVersionCode, mProcessStats);
                        synchronized (dst) {
                            dst.provider = src.provider;
                            dst.setProcess(r);
                            dst.notifyAll();
                        }
                        updateOomAdjLocked(r, true, OomAdjuster.OOM_ADJ_REASON_GET_PROVIDER);
                        maybeUpdateProviderUsageStatsLocked(r, src.info.packageName,
                                src.info.authority);
                    }
                }
    
                Binder.restoreCallingIdentity(origId);
            }
        }
    

    这个方法的主要作用就是把客户但传递过来的ContentProviderHolder对象保存到了ContentProviderRecord对象中。而通过上面的分析我们知道ContentProviderHolder里面存储的就是ContentProvider的Binder对象,所以其他应用通过uri所查找的就是ActivityManagerService里面的ContentProvider的Binder,拿到了Binder对象就可以远程调用ContentProvder里面的方法了。好了,截止目前ContentProvider的注册流程我们就分析完了,接下来我们看看ContentResolver方法的调用流程。

    ContentResolver的调用过程

    我们先了解下ContentResolver类:

    public abstract class ContentResolver implements ContentInterface {}
    

    它是个抽象类,实现了ContentInterface接口,ContentInterface接口中定义了insert,delete,update,query方法,我们看下这个接口定义:

      //ContentInterface.java
    public interface ContentInterface {
        public @Nullable Cursor query(@NonNull Uri uri, @Nullable String[] projection,
                @Nullable Bundle queryArgs, @Nullable CancellationSignal cancellationSignal)
                throws RemoteException;
    
        public @Nullable String getType(@NonNull Uri uri) throws RemoteException;
    
        public @Nullable String[] getStreamTypes(@NonNull Uri uri, @NonNull String mimeTypeFilter)
                throws RemoteException;
    
        public @Nullable Uri canonicalize(@NonNull Uri uri) throws RemoteException;
    
        public @Nullable Uri uncanonicalize(@NonNull Uri uri) throws RemoteException;
    
        public boolean refresh(@NonNull Uri uri, @Nullable Bundle args,
                @Nullable CancellationSignal cancellationSignal) throws RemoteException;
    
        public @Nullable Uri insert(@NonNull Uri uri, @Nullable ContentValues initialValues)
                throws RemoteException;
    
        public int bulkInsert(@NonNull Uri uri, @NonNull ContentValues[] initialValues)
                throws RemoteException;
    
        public int delete(@NonNull Uri uri, @Nullable String selection,
                @Nullable String[] selectionArgs) throws RemoteException;
    
        public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection,
                @Nullable String[] selectionArgs) throws RemoteException;
    
        public @Nullable ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode,
                @Nullable CancellationSignal signal) throws RemoteException, FileNotFoundException;
    
        public @Nullable AssetFileDescriptor openAssetFile(@NonNull Uri uri, @NonNull String mode,
                @Nullable CancellationSignal signal) throws RemoteException, FileNotFoundException;
    
        public @Nullable AssetFileDescriptor openTypedAssetFile(@NonNull Uri uri,
                @NonNull String mimeTypeFilter, @Nullable Bundle opts,
                @Nullable CancellationSignal signal) throws RemoteException, FileNotFoundException;
    
        public @NonNull ContentProviderResult[] applyBatch(@NonNull String authority,
                @NonNull ArrayList<ContentProviderOperation> operations)
                throws RemoteException, OperationApplicationException;
    
        public @Nullable Bundle call(@NonNull String authority, @NonNull String method,
                @Nullable String arg, @Nullable Bundle extras) throws RemoteException;
    }
    

    我们一般获取ContentResolver都是通过getContentResolver方法,方法实现如下:

      //ContextWrapper.java
    
      @Override
        public ContentResolver getContentResolver() {
            return mBase.getContentResolver();
        }
    

    mBase就是ContextImpl,getContentResolver方法如下所示:

        //ContextImpl.java
    
       @UnsupportedAppUsage
        private final ApplicationContentResolver mContentResolver;
    
     private ContextImpl(@Nullable ContextImpl container, @NonNull ActivityThread mainThread,
                @NonNull LoadedApk packageInfo, @Nullable String splitName,
                @Nullable IBinder activityToken, @Nullable UserHandle user, int flags,
                @Nullable ClassLoader classLoader, @Nullable String overrideOpPackageName) {
            mOuterContext = this;
    
            // If creator didn't specify which storage to use, use the default
            // location for application.
            if ((flags & (Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE
                    | Context.CONTEXT_DEVICE_PROTECTED_STORAGE)) == 0) {
                final File dataDir = packageInfo.getDataDirFile();
                if (Objects.equals(dataDir, packageInfo.getCredentialProtectedDataDirFile())) {
                    flags |= Context.CONTEXT_CREDENTIAL_PROTECTED_STORAGE;
                } else if (Objects.equals(dataDir, packageInfo.getDeviceProtectedDataDirFile())) {
                    flags |= Context.CONTEXT_DEVICE_PROTECTED_STORAGE;
                }
            }
    
            mMainThread = mainThread;
            mActivityToken = activityToken;
            mFlags = flags;
    
            if (user == null) {
                user = Process.myUserHandle();
            }
            mUser = user;
    
            mPackageInfo = packageInfo;
            mSplitName = splitName;
            mClassLoader = classLoader;
            mResourcesManager = ResourcesManager.getInstance();
    
            String opPackageName;
    
            if (container != null) {
                mBasePackageName = container.mBasePackageName;
                opPackageName = container.mOpPackageName;
                setResources(container.mResources);
                mDisplay = container.mDisplay;
            } else {
                mBasePackageName = packageInfo.mPackageName;
                ApplicationInfo ainfo = packageInfo.getApplicationInfo();
                if (ainfo.uid == Process.SYSTEM_UID && ainfo.uid != Process.myUid()) {
                    // Special case: system components allow themselves to be loaded in to other
                    // processes.  For purposes of app ops, we must then consider the context as
                    // belonging to the package of this process, not the system itself, otherwise
                    // the package+uid verifications in app ops will fail.
                    opPackageName = ActivityThread.currentPackageName();
                } else {
                    opPackageName = mBasePackageName;
                }
            }
    
            mOpPackageName = overrideOpPackageName != null ? overrideOpPackageName : opPackageName;
            //注释1
            mContentResolver = new ApplicationContentResolver(this, mainThread);
        }
    
     @Override
        public ContentResolver getContentResolver() {
            return mContentResolver;
        }
    
    
    

    mContentResolver就是一个ApplicationContentResolver,我们看看它的具体实现:

     private static final class ApplicationContentResolver extends ContentResolver {
            @UnsupportedAppUsage
            private final ActivityThread mMainThread;
    
            public ApplicationContentResolver(Context context, ActivityThread mainThread) {
                super(context);
                mMainThread = Preconditions.checkNotNull(mainThread);
            }
    
            @Override
            @UnsupportedAppUsage
            protected IContentProvider acquireProvider(Context context, String auth) {
                return mMainThread.acquireProvider(context,
                        ContentProvider.getAuthorityWithoutUserId(auth),
                        resolveUserIdFromAuthority(auth), true);
            }
    
            @Override
            protected IContentProvider acquireExistingProvider(Context context, String auth) {
                return mMainThread.acquireExistingProvider(context,
                        ContentProvider.getAuthorityWithoutUserId(auth),
                        resolveUserIdFromAuthority(auth), true);
            }
    
            @Override
            public boolean releaseProvider(IContentProvider provider) {
                return mMainThread.releaseProvider(provider, true);
            }
    
            @Override
            protected IContentProvider acquireUnstableProvider(Context c, String auth) {
                return mMainThread.acquireProvider(c,
                        ContentProvider.getAuthorityWithoutUserId(auth),
                        resolveUserIdFromAuthority(auth), false);
            }
    
            @Override
            public boolean releaseUnstableProvider(IContentProvider icp) {
                return mMainThread.releaseProvider(icp, false);
            }
    
            @Override
            public void unstableProviderDied(IContentProvider icp) {
                mMainThread.handleUnstableProviderDied(icp.asBinder(), true);
            }
    
            @Override
            public void appNotRespondingViaProvider(IContentProvider icp) {
                mMainThread.appNotRespondingViaProvider(icp.asBinder());
            }
    
            /** @hide */
            protected int resolveUserIdFromAuthority(String auth) {
                return ContentProvider.getUserIdFromAuthority(auth, getUserId());
            }
        }
    

    ApplicationContentResolver是ContentResolver的一个具体实现类,它位于ContextImpl的内部。所以调用getContentResolver().query等方法,其实就是调用了ApplicationContentResolver对像的query方法,该方法的具体实现:

      //ContentResolver.java
    
     @Override
        public final @Nullable Cursor query(final @RequiresPermission.Read @NonNull Uri uri,
                @Nullable String[] projection, @Nullable Bundle queryArgs,
                @Nullable CancellationSignal cancellationSignal) {
            Preconditions.checkNotNull(uri, "uri");
    
            try {
                if (mWrapped != null) {
                    return mWrapped.query(uri, projection, queryArgs, cancellationSignal);
                }
            } catch (RemoteException e) {
                return null;
            }
            
            //注释1
            IContentProvider unstableProvider = acquireUnstableProvider(uri);
            if (unstableProvider == null) {
                return null;
            }
            IContentProvider stableProvider = null;
            Cursor qCursor = null;
            try {
                long startTime = SystemClock.uptimeMillis();
    
                ICancellationSignal remoteCancellationSignal = null;
                if (cancellationSignal != null) {
                    cancellationSignal.throwIfCanceled();
                    remoteCancellationSignal = unstableProvider.createCancellationSignal();
                    cancellationSignal.setRemote(remoteCancellationSignal);
                }
                try {
                    qCursor = unstableProvider.query(mPackageName, uri, projection,
                            queryArgs, remoteCancellationSignal);
                } catch (DeadObjectException e) {
                    // The remote process has died...  but we only hold an unstable
                    // reference though, so we might recover!!!  Let's try!!!!
                    // This is exciting!!1!!1!!!!1
                    unstableProviderDied(unstableProvider);
                    stableProvider = acquireProvider(uri);
                    if (stableProvider == null) {
                        return null;
                    }
                    qCursor = stableProvider.query(
                            mPackageName, uri, projection, queryArgs, remoteCancellationSignal);
                }
                if (qCursor == null) {
                    return null;
                }
    
                // Force query execution.  Might fail and throw a runtime exception here.
                qCursor.getCount();
                long durationMillis = SystemClock.uptimeMillis() - startTime;
                maybeLogQueryToEventLog(durationMillis, uri, projection, queryArgs);
    
                // Wrap the cursor object into CursorWrapperInner object.
                final IContentProvider provider = (stableProvider != null) ? stableProvider
                        : acquireProvider(uri);
                final CursorWrapperInner wrapper = new CursorWrapperInner(qCursor, provider);
                stableProvider = null;
                qCursor = null;
                return wrapper;
            } catch (RemoteException e) {
                // Arbitrary and not worth documenting, as Activity
                // Manager will kill this process shortly anyway.
                return null;
            } finally {
                if (qCursor != null) {
                    qCursor.close();
                }
                if (cancellationSignal != null) {
                    cancellationSignal.setRemote(null);
                }
                if (unstableProvider != null) {
                    releaseUnstableProvider(unstableProvider);
                }
                if (stableProvider != null) {
                    releaseProvider(stableProvider);
                }
            }
        }
    
    

    我们继续分析注释1的acquireUnstableProvider方法:

    //ContentResolver.java
    
     /**
         * Returns the content provider for the given content URI.
         *
         * @param uri The URI to a content provider
         * @return The ContentProvider for the given URI, or null if no content provider is found.
         * @hide
         */
        public final IContentProvider acquireUnstableProvider(Uri uri) {
            if (!SCHEME_CONTENT.equals(uri.getScheme())) {
                return null;
            }
            String auth = uri.getAuthority();
            if (auth != null) {
                return acquireUnstableProvider(mContext, uri.getAuthority());
            }
            return null;
        }
    
    
        @Override
            protected IContentProvider acquireUnstableProvider(Context c, String auth) {
                return mMainThread.acquireProvider(c,
                        ContentProvider.getAuthorityWithoutUserId(auth),
                        resolveUserIdFromAuthority(auth), false);
            }
    

    ContentResolver最终会调用ActivityThread的acquireProvider方法,该方法实现如下所示:

    //ActivityThread.java
    
    @UnsupportedAppUsage
        public final IContentProvider acquireProvider(
                Context c, String auth, int userId, boolean stable) {
        //注释1    
       final IContentProvider provider = acquireExistingProvider(c, auth, userId, stable);
            if (provider != null) {
                return provider;
            }
    
            // There is a possible race here.  Another thread may try to acquire
            // the same provider at the same time.  When this happens, we want to ensure
            // that the first one wins.
            // Note that we cannot hold the lock while acquiring and installing the
            // provider since it might take a long time to run and it could also potentially
            // be re-entrant in the case where the provider is in the same process.
            ContentProviderHolder holder = null;
            try {
                synchronized (getGetProviderLock(auth, userId)) {
        //注释2            
        holder = ActivityManager.getService().getContentProvider(
                            getApplicationThread(), c.getOpPackageName(), auth, userId, stable);
                }
            } catch (RemoteException ex) {
                throw ex.rethrowFromSystemServer();
            }
            if (holder == null) {
                Slog.e(TAG, "Failed to find provider info for " + auth);
                return null;
            }
    
            // Install provider will increment the reference count for us, and break
            // any ties in the race.
            holder = installProvider(c, holder, holder.info,
                    true /*noisy*/, holder.noReleaseNeeded, stable);
            return holder.provider;
        }
    

    在注释1处如果能查询到直接返回。如果查询不到会在服务端去查询ContentProvider注册的时候所在服务端ActivityManagerService中所保存的ContentProviderHolder,而最终返回IContentProvider就是ContentProvider的对象的Transport对象,最终调用的是Transport对象的query方法,该方法具体实现如下:

    //ContentProvider.java
    
     /**
         * Binder object that deals with remoting.
         *
         * @hide
         */
        class Transport extends ContentProviderNative {
     volatile AppOpsManager mAppOpsManager = null;
            volatile int mReadOp = AppOpsManager.OP_NONE;
            volatile int mWriteOp = AppOpsManager.OP_NONE;
    
          //注释1
            volatile ContentInterface mInterface = ContentProvider.this;
    
            ContentProvider getContentProvider() {
                return ContentProvider.this;
            }
    
            @Override
            public String getProviderName() {
                return getContentProvider().getClass().getName();
            }
    
            @Override
            public Cursor query(String callingPkg, Uri uri, @Nullable String[] projection,
                    @Nullable Bundle queryArgs, @Nullable ICancellationSignal cancellationSignal) {
                uri = validateIncomingUri(uri);
                uri = maybeGetUriWithoutUserId(uri);
                if (enforceReadPermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) {
                    // The caller has no access to the data, so return an empty cursor with
                    // the columns in the requested order. The caller may ask for an invalid
                    // column and we would not catch that but this is not a problem in practice.
                    // We do not call ContentProvider#query with a modified where clause since
                    // the implementation is not guaranteed to be backed by a SQL database, hence
                    // it may not handle properly the tautology where clause we would have created.
                    if (projection != null) {
                        return new MatrixCursor(projection, 0);
                    }
    
                    // Null projection means all columns but we have no idea which they are.
                    // However, the caller may be expecting to access them my index. Hence,
                    // we have to execute the query as if allowed to get a cursor with the
                    // columns. We then use the column names to return an empty cursor.
                    Cursor cursor;
                    final String original = setCallingPackage(callingPkg);
                    try {
                      //注释2
                        cursor = mInterface.query(
                                uri, projection, queryArgs,
                                CancellationSignal.fromTransport(cancellationSignal));
                    } catch (RemoteException e) {
                        throw e.rethrowAsRuntimeException();
                    } finally {
                        setCallingPackage(original);
                    }
                    if (cursor == null) {
                        return null;
                    }
    
                    // Return an empty cursor for all columns.
                    return new MatrixCursor(cursor.getColumnNames(), 0);
                }
                Trace.traceBegin(TRACE_TAG_DATABASE, "query");
                final String original = setCallingPackage(callingPkg);
                try {
                    return mInterface.query(
                            uri, projection, queryArgs,
                            CancellationSignal.fromTransport(cancellationSignal));
                } catch (RemoteException e) {
                    throw e.rethrowAsRuntimeException();
                } finally {
                    setCallingPackage(original);
                    Trace.traceEnd(TRACE_TAG_DATABASE);
                }
            }
    
            @Override
            public String getType(Uri uri) {
                // getCallingPackage() isn't available in getType(), as the javadoc states.
                uri = validateIncomingUri(uri);
                uri = maybeGetUriWithoutUserId(uri);
                Trace.traceBegin(TRACE_TAG_DATABASE, "getType");
                try {
                    return mInterface.getType(uri);
                } catch (RemoteException e) {
                    throw e.rethrowAsRuntimeException();
                } finally {
                    Trace.traceEnd(TRACE_TAG_DATABASE);
                }
            }
    
            @Override
            public Uri insert(String callingPkg, Uri uri, ContentValues initialValues) {
                uri = validateIncomingUri(uri);
                int userId = getUserIdFromUri(uri);
                uri = maybeGetUriWithoutUserId(uri);
                if (enforceWritePermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) {
                    final String original = setCallingPackage(callingPkg);
                    try {
                        return rejectInsert(uri, initialValues);
                    } finally {
                        setCallingPackage(original);
                    }
                }
                Trace.traceBegin(TRACE_TAG_DATABASE, "insert");
                final String original = setCallingPackage(callingPkg);
                try {
                    return maybeAddUserId(mInterface.insert(uri, initialValues), userId);
                } catch (RemoteException e) {
                    throw e.rethrowAsRuntimeException();
                } finally {
                    setCallingPackage(original);
                    Trace.traceEnd(TRACE_TAG_DATABASE);
                }
            }
     @Override
            public int delete(String callingPkg, Uri uri, String selection, String[] selectionArgs) {
                uri = validateIncomingUri(uri);
                uri = maybeGetUriWithoutUserId(uri);
                if (enforceWritePermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) {
                    return 0;
                }
                Trace.traceBegin(TRACE_TAG_DATABASE, "delete");
                final String original = setCallingPackage(callingPkg);
                try {
                    return mInterface.delete(uri, selection, selectionArgs);
                } catch (RemoteException e) {
                    throw e.rethrowAsRuntimeException();
                } finally {
                    setCallingPackage(original);
                    Trace.traceEnd(TRACE_TAG_DATABASE);
                }
            }
    
            @Override
            public int update(String callingPkg, Uri uri, ContentValues values, String selection,
                    String[] selectionArgs) {
                uri = validateIncomingUri(uri);
                uri = maybeGetUriWithoutUserId(uri);
                if (enforceWritePermission(callingPkg, uri, null) != AppOpsManager.MODE_ALLOWED) {
                    return 0;
                }
                Trace.traceBegin(TRACE_TAG_DATABASE, "update");
                final String original = setCallingPackage(callingPkg);
                try {
                    return mInterface.update(uri, values, selection, selectionArgs);
                } catch (RemoteException e) {
                    throw e.rethrowAsRuntimeException();
                } finally {
                    setCallingPackage(original);
                    Trace.traceEnd(TRACE_TAG_DATABASE);
                }
            }
    
    }
    

    在Transport的query方法的注释2处会调用mInterface对象的query方法,而mInterface就是我们注释1处的ContentProvider对象,此时ContentProvider对象的query方法就执行了,这就意味了ConentResolver的调用过程就彻底讲完了。

    相关文章

      网友评论

          本文标题:Android10.0 ContentProvider源码分析

          本文链接:https://www.haomeiwen.com/subject/pddxsktx.html