1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package androidx.room
17 
18 import android.app.Service
19 import android.content.Intent
20 import android.os.IBinder
21 import android.os.RemoteCallbackList
22 import android.os.RemoteException
23 import android.util.Log
24 import androidx.room.Room.LOG_TAG
25 
26 /**
27  * A [Service] for remote invalidation among multiple [InvalidationTracker] instances. This service
28  * runs in the main app process. All the instances of [InvalidationTracker] (potentially in other
29  * processes) has to connect to this service.
30  *
31  * The intent to launch it can be specified by
32  * [RoomDatabase.Builder.setMultiInstanceInvalidationServiceIntent], although the service is defined
33  * in the manifest by default so there should be no need to override it in a normal situation.
34  */
35 @ExperimentalRoomApi
36 class MultiInstanceInvalidationService : Service() {
37     internal var maxClientId = 0
38     internal val clientNames = mutableMapOf<Int, String>()
39 
40     internal val callbackList: RemoteCallbackList<IMultiInstanceInvalidationCallback> =
41         object : RemoteCallbackList<IMultiInstanceInvalidationCallback>() {
onCallbackDiednull42             override fun onCallbackDied(callback: IMultiInstanceInvalidationCallback, cookie: Any) {
43                 clientNames.remove(cookie as Int)
44             }
45         }
46 
47     private val binder: IMultiInstanceInvalidationService.Stub =
48         object : IMultiInstanceInvalidationService.Stub() {
49             // Assigns a client ID to the client.
registerCallbacknull50             override fun registerCallback(
51                 callback: IMultiInstanceInvalidationCallback,
52                 name: String?
53             ): Int {
54                 if (name == null) {
55                     return 0
56                 }
57                 synchronized(callbackList) {
58                     val clientId = ++maxClientId
59                     // Use the client ID as the RemoteCallbackList cookie.
60                     return if (callbackList.register(callback, clientId)) {
61                         clientNames[clientId] = name
62                         clientId
63                     } else {
64                         --maxClientId
65                         0
66                     }
67                 }
68             }
69 
70             // Explicitly removes the client.
71             // The client can die without calling this. In that case, callbackList
72             // .onCallbackDied() can take care of removal.
unregisterCallbacknull73             override fun unregisterCallback(
74                 callback: IMultiInstanceInvalidationCallback,
75                 clientId: Int
76             ) {
77                 synchronized(callbackList) {
78                     callbackList.unregister(callback)
79                     clientNames.remove(clientId)
80                 }
81             }
82 
83             // Broadcasts table invalidation to other instances of the same database file.
84             // The broadcast is not sent to the caller itself.
broadcastInvalidationnull85             override fun broadcastInvalidation(clientId: Int, tables: Array<out String>) {
86                 synchronized(callbackList) {
87                     val name = clientNames[clientId]
88                     if (name == null) {
89                         Log.w(LOG_TAG, "Remote invalidation client ID not registered")
90                         return
91                     }
92                     val count = callbackList.beginBroadcast()
93                     try {
94                         for (i in 0 until count) {
95                             val targetClientId = callbackList.getBroadcastCookie(i) as Int
96                             val targetName = clientNames[targetClientId]
97                             if (clientId == targetClientId || name != targetName) {
98                                 // Skip if this is the caller itself or broadcast is for another
99                                 // database.
100                                 continue
101                             }
102                             try {
103                                 callbackList.getBroadcastItem(i).onInvalidation(tables)
104                             } catch (e: RemoteException) {
105                                 Log.w(LOG_TAG, "Error invoking a remote callback", e)
106                             }
107                         }
108                     } finally {
109                         callbackList.finishBroadcast()
110                     }
111                 }
112             }
113         }
114 
onBindnull115     override fun onBind(intent: Intent): IBinder {
116         return binder
117     }
118 }
119