xref: /aosp_15_r20/frameworks/base/apex/blobstore/README.md (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
1*d57664e9SAndroid Build Coastguard Worker<!--
2*d57664e9SAndroid Build Coastguard Worker  Copyright (C) 2021 The Android Open Source Project
3*d57664e9SAndroid Build Coastguard Worker
4*d57664e9SAndroid Build Coastguard Worker  Licensed under the Apache License, Version 2.0 (the "License");
5*d57664e9SAndroid Build Coastguard Worker  you may not use this file except in compliance with the License.
6*d57664e9SAndroid Build Coastguard Worker  You may obtain a copy of the License at
7*d57664e9SAndroid Build Coastguard Worker
8*d57664e9SAndroid Build Coastguard Worker       http://www.apache.org/licenses/LICENSE-2.0
9*d57664e9SAndroid Build Coastguard Worker
10*d57664e9SAndroid Build Coastguard Worker  Unless required by applicable law or agreed to in writing, software
11*d57664e9SAndroid Build Coastguard Worker  distributed under the License is distributed on an "AS IS" BASIS,
12*d57664e9SAndroid Build Coastguard Worker  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*d57664e9SAndroid Build Coastguard Worker  See the License for the specific language governing permissions and
14*d57664e9SAndroid Build Coastguard Worker  limitations under the License
15*d57664e9SAndroid Build Coastguard Worker-->
16*d57664e9SAndroid Build Coastguard Worker
17*d57664e9SAndroid Build Coastguard Worker# BlobStore Manager
18*d57664e9SAndroid Build Coastguard Worker
19*d57664e9SAndroid Build Coastguard Worker## Introduction
20*d57664e9SAndroid Build Coastguard Worker* BlobStoreManager is a system service added in Android R release that facilitates sharing of
21*d57664e9SAndroid Build Coastguard Worker  data blobs among apps.
22*d57664e9SAndroid Build Coastguard Worker* Apps that would like to share data blobs with other apps can do so by contributing those
23*d57664e9SAndroid Build Coastguard Worker  data blobs with the System and can choose how they would like the System to share the data blobs
24*d57664e9SAndroid Build Coastguard Worker  with other apps.
25*d57664e9SAndroid Build Coastguard Worker* Apps can access data blobs shared by other apps from the System using checksum of the data blobs
26*d57664e9SAndroid Build Coastguard Worker  (plus some other data attributes. More details [below](#blob-handle)).
27*d57664e9SAndroid Build Coastguard Worker* The APIs provided by the BlobStoreManager are meant to reduce storage and network usage by
28*d57664e9SAndroid Build Coastguard Worker  reusing the data available on the device instead of downloading the same data again and having
29*d57664e9SAndroid Build Coastguard Worker  multiple copies of the same data on disk.
30*d57664e9SAndroid Build Coastguard Worker* It is not meant to provide access to the data which apps otherwise would not be able to access.
31*d57664e9SAndroid Build Coastguard Worker  In other words, if an app’s only means of obtaining access to certain data is through
32*d57664e9SAndroid Build Coastguard Worker  BlobStoreManager, then that use case is not really intended or supported.
33*d57664e9SAndroid Build Coastguard Worker* For example, if earlier an app was downloading certain shared data from a server, then by using
34*d57664e9SAndroid Build Coastguard Worker  BlobStoreManager, it can first check whether or not the data is already available on the device
35*d57664e9SAndroid Build Coastguard Worker  before downloading.
36*d57664e9SAndroid Build Coastguard Worker
37*d57664e9SAndroid Build Coastguard Worker## Concepts
38*d57664e9SAndroid Build Coastguard Worker### Blob handle
39*d57664e9SAndroid Build Coastguard WorkerBlob handle is the identifier of the data and it is what apps need to use for referring to the
40*d57664e9SAndroid Build Coastguard Workerdata blobs. Currently, this is made of following bits of information:
41*d57664e9SAndroid Build Coastguard Worker* SHA256 checksum of data
42*d57664e9SAndroid Build Coastguard Worker* Data label: A user readable string that indicates what the data blob is.
43*d57664e9SAndroid Build Coastguard Worker  This is meant to be used when surfacing a list of blobs to the user.
44*d57664e9SAndroid Build Coastguard Worker* Data expiry time: A timestamp after which the data blob should be considered invalid and not
45*d57664e9SAndroid Build Coastguard Worker  allowed to be accessed by any app.
46*d57664e9SAndroid Build Coastguard Worker* Data tag: An opaque string associated with the blob. System does not interpret this in any way or
47*d57664e9SAndroid Build Coastguard Worker  use it for any purposes other than when checking whether two Blob handle identifiers are referring
48*d57664e9SAndroid Build Coastguard Worker  to the same data blob. This is meant to be used by the apps, either for categorization for
49*d57664e9SAndroid Build Coastguard Worker  data blobs or for adding additional identifiers. For example, an app can add tags like
50*d57664e9SAndroid Build Coastguard Worker  *machine_learning* or *media* depending on the data blob if necessary.
51*d57664e9SAndroid Build Coastguard Worker
52*d57664e9SAndroid Build Coastguard WorkerWhen comparing two Blob handles, the System will compare all the pieces of information above and
53*d57664e9SAndroid Build Coastguard Workeronly when two Blob handles are equal, the data blobs corresponding to those identifiers are
54*d57664e9SAndroid Build Coastguard Workerconsidered equal.
55*d57664e9SAndroid Build Coastguard Worker
56*d57664e9SAndroid Build Coastguard Worker### Blob sharing session
57*d57664e9SAndroid Build Coastguard WorkerSession is a way to allow apps to contribute data over multiple time intervals. Each session is
58*d57664e9SAndroid Build Coastguard Workerassociated with a unique Identifier that is created and obtained by the apps by calling
59*d57664e9SAndroid Build Coastguard Worker[BlobStoreManager#createSession](https://developer.android.com/reference/android/app/blob/BlobStoreManager#createSession(android.app.blob.BlobHandle)).
60*d57664e9SAndroid Build Coastguard WorkerApps can save the Identifier associated with a session and use it to open and close it
61*d57664e9SAndroid Build Coastguard Workermultiple times for contributing the data. For example, if an app is downloading
62*d57664e9SAndroid Build Coastguard Workersome content over the network, it can start a Session and start contributing this data to the
63*d57664e9SAndroid Build Coastguard WorkerSystem immediately and if the network connection is lost for any reason, the app can close this
64*d57664e9SAndroid Build Coastguard Workersession. When the download resumes, the app can reopen the session and start contributing again.
65*d57664e9SAndroid Build Coastguard WorkerNote that once the entire data is contributed, the app has no reason to hold on to the Session Id.
66*d57664e9SAndroid Build Coastguard Worker
67*d57664e9SAndroid Build Coastguard Worker### Blob commit
68*d57664e9SAndroid Build Coastguard WorkerSince a data blob can be contributed in a session over multiple time intervals, an app closing a
69*d57664e9SAndroid Build Coastguard Workersession does not imply that the contribution is completed. So, *commit* is added as an explicit
70*d57664e9SAndroid Build Coastguard Workerevent / signal for the app to indicate that the contribution of the data blob is completed.
71*d57664e9SAndroid Build Coastguard WorkerAt this point, the System can verify the data blob does indeed correspond to the Blob handle used
72*d57664e9SAndroid Build Coastguard Workerby the app and prevent the app from making any further modifications to the data blob. Once the
73*d57664e9SAndroid Build Coastguard Workerdata blob is committed and verified by the System, it is available for other applications to access.
74*d57664e9SAndroid Build Coastguard Worker
75*d57664e9SAndroid Build Coastguard Worker### Access modes
76*d57664e9SAndroid Build Coastguard WorkerWhen an application contributes a data blob to the System, it can choose to specify how it would
77*d57664e9SAndroid Build Coastguard Workerlike the System to share this data blob with other applications. Access modes refer to the type of
78*d57664e9SAndroid Build Coastguard Workeraccess that apps specified when contributing a data blob. As of Android S release, there are
79*d57664e9SAndroid Build Coastguard Workerfour access modes:
80*d57664e9SAndroid Build Coastguard Worker* Allow specific packages: Apps can specify a specific set of applications that are allowed to
81*d57664e9SAndroid Build Coastguard Worker  access their data blob.
82*d57664e9SAndroid Build Coastguard Worker* Allow packages with the same signature: Apps can specify that only the applications that are
83*d57664e9SAndroid Build Coastguard Worker  signed with the same certificate as them can access their data blob.
84*d57664e9SAndroid Build Coastguard Worker* Allow public access: Apps can specify that any other app on the device can access their data blob.
85*d57664e9SAndroid Build Coastguard Worker* Allow private access: Apps can specify that no other app can access their data blob unless they
86*d57664e9SAndroid Build Coastguard Worker  happen to contribute the same data blob.
87*d57664e9SAndroid Build Coastguard Worker  * Note that in this case, two apps might download the same data blob and contribute to the System
88*d57664e9SAndroid Build Coastguard Worker    in which case we are not saving anything in terms of bandwidth usage, but we would still be
89*d57664e9SAndroid Build Coastguard Worker    saving disk usage since we would be keeping only one copy of data on disk.
90*d57664e9SAndroid Build Coastguard Worker
91*d57664e9SAndroid Build Coastguard Worker### Lease
92*d57664e9SAndroid Build Coastguard WorkerLeasing a blob is a way to specify that an application is interested in using a data blob
93*d57664e9SAndroid Build Coastguard Workerand would like the System to not delete this data blob. Applications can also access a blob
94*d57664e9SAndroid Build Coastguard Workerwithout holding a lease on it, in which case the System can choose to delete the data blob at any
95*d57664e9SAndroid Build Coastguard Workertime. So, if an application wants to make sure a data blob is available for access for a certain
96*d57664e9SAndroid Build Coastguard Workerperiod, it is recommended that the application acquire a lease on the data blob. Applications can
97*d57664e9SAndroid Build Coastguard Workereither specify upfront how long they would like to hold the lease for (which is called the lease
98*d57664e9SAndroid Build Coastguard Workerexpiry time), or they can acquire a lease without specifying a time period and release the lease
99*d57664e9SAndroid Build Coastguard Workerwhen they are done with the data blob.
100*d57664e9SAndroid Build Coastguard Worker
101*d57664e9SAndroid Build Coastguard Worker## Sharing data blobs across users
102*d57664e9SAndroid Build Coastguard WorkerBy default, data blobs are only accessible to applications in the user in which the data blob was
103*d57664e9SAndroid Build Coastguard Workercontributed, but if an application holds the permission
104*d57664e9SAndroid Build Coastguard Worker[ACCESS_BLOBS_ACROSS_USERS](https://developer.android.com/reference/android/Manifest.permission#ACCESS_BLOBS_ACROSS_USERS),
105*d57664e9SAndroid Build Coastguard Workerthen they are allowed to access blobs that are contributed by the applications in the other users.
106*d57664e9SAndroid Build Coastguard WorkerAs of Android S, this permission is only available to following set of applications:
107*d57664e9SAndroid Build Coastguard Worker* Apps signed with the platform certificate
108*d57664e9SAndroid Build Coastguard Worker* Privileged applications
109*d57664e9SAndroid Build Coastguard Worker* Applications holding the
110*d57664e9SAndroid Build Coastguard Worker  [ASSISTANT](https://developer.android.com/reference/android/app/role/RoleManager#ROLE_ASSISTANT)
111*d57664e9SAndroid Build Coastguard Worker  role
112*d57664e9SAndroid Build Coastguard Worker* Development applications
113*d57664e9SAndroid Build Coastguard Worker
114*d57664e9SAndroid Build Coastguard WorkerNote that the access modes that applications choose while committing the data blobs still apply
115*d57664e9SAndroid Build Coastguard Workerwhen these data blobs are accessed across users. So for example, if *appA* contributed a
116*d57664e9SAndroid Build Coastguard Workerdata blob in *user0* and specified to share this data blob with only a specific set of
117*d57664e9SAndroid Build Coastguard Workerapplications [*appB*, *appC*], then *appD* on *user10* will not be able to access this data blob
118*d57664e9SAndroid Build Coastguard Workereven if the app is granted the `ACCESS_BLOBS_ACROSS_USERS` permission.
119*d57664e9SAndroid Build Coastguard Worker
120*d57664e9SAndroid Build Coastguard WorkerWhen apps that are allowed to access blobs across users
121*d57664e9SAndroid Build Coastguard Worker(i.e. those holding the permission `ACCESS_BLOBS_ACROSS_USERS`) try to access a data blob,
122*d57664e9SAndroid Build Coastguard Workerthey can do so as if it is any other data blob. In other words, the applications don’t need to
123*d57664e9SAndroid Build Coastguard Workerknow where the data blob is contributed, because the System will automatically check and will
124*d57664e9SAndroid Build Coastguard Workerallow access if this data blob is available either on the user in which the calling application
125*d57664e9SAndroid Build Coastguard Workeris running in or other users.