Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 17 | package com.android.providers.settings; |
| 18 | |
| 19 | import android.os.Bundle; |
| 20 | import android.os.UserManager; |
| 21 | import android.provider.Settings; |
| 22 | import android.util.MemoryIntArray; |
| 23 | import android.util.Slog; |
| 24 | import android.util.SparseIntArray; |
| 25 | import com.android.internal.annotations.GuardedBy; |
| 26 | |
| 27 | import java.io.IOException; |
| 28 | |
| 29 | /** |
| 30 | * This class tracks changes for global/secure/system tables on a |
| 31 | * per user basis and updates a shared memory region which client |
| 32 | * processes can read to determine if their local caches are stale, |
| 33 | */ |
| 34 | final class GenerationRegistry { |
Svet Ganov | 9d723d3 | 2016-08-27 11:05:56 -0700 | [diff] [blame] | 35 | private static final String LOG_TAG = "GenerationRegistry"; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 36 | |
| 37 | private static final boolean DEBUG = false; |
| 38 | |
| 39 | private final Object mLock; |
| 40 | |
| 41 | @GuardedBy("mLock") |
| 42 | private final SparseIntArray mKeyToIndexMap = new SparseIntArray(); |
| 43 | |
| 44 | @GuardedBy("mLock") |
Svetoslav Ganov | 04df738 | 2016-05-10 18:55:47 -0700 | [diff] [blame] | 45 | private MemoryIntArray mBackingStore; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 46 | |
| 47 | public GenerationRegistry(Object lock) { |
| 48 | mLock = lock; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | public void incrementGeneration(int key) { |
| 52 | synchronized (mLock) { |
Svetoslav Ganov | 04df738 | 2016-05-10 18:55:47 -0700 | [diff] [blame] | 53 | MemoryIntArray backingStore = getBackingStoreLocked(); |
| 54 | if (backingStore != null) { |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 55 | try { |
Svetoslav Ganov | 04df738 | 2016-05-10 18:55:47 -0700 | [diff] [blame] | 56 | final int index = getKeyIndexLocked(key, mKeyToIndexMap, backingStore); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 57 | if (index >= 0) { |
Svetoslav Ganov | 04df738 | 2016-05-10 18:55:47 -0700 | [diff] [blame] | 58 | final int generation = backingStore.get(index) + 1; |
| 59 | backingStore.set(index, generation); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 60 | } |
| 61 | } catch (IOException e) { |
| 62 | Slog.e(LOG_TAG, "Error updating generation id", e); |
Svetoslav Ganov | 04df738 | 2016-05-10 18:55:47 -0700 | [diff] [blame] | 63 | destroyBackingStore(); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | public void addGenerationData(Bundle bundle, int key) { |
| 70 | synchronized (mLock) { |
Svetoslav Ganov | 04df738 | 2016-05-10 18:55:47 -0700 | [diff] [blame] | 71 | MemoryIntArray backingStore = getBackingStoreLocked(); |
| 72 | try { |
| 73 | if (backingStore != null) { |
| 74 | final int index = getKeyIndexLocked(key, mKeyToIndexMap, backingStore); |
| 75 | if (index >= 0) { |
| 76 | bundle.putParcelable(Settings.CALL_METHOD_TRACK_GENERATION_KEY, |
| 77 | backingStore); |
| 78 | bundle.putInt(Settings.CALL_METHOD_GENERATION_INDEX_KEY, index); |
Svetoslav Ganov | 8c35dcc | 2016-06-17 15:27:43 -0700 | [diff] [blame] | 79 | bundle.putInt(Settings.CALL_METHOD_GENERATION_KEY, |
| 80 | backingStore.get(index)); |
Svetoslav Ganov | 04df738 | 2016-05-10 18:55:47 -0700 | [diff] [blame] | 81 | if (DEBUG) { |
| 82 | Slog.i(LOG_TAG, "Exported index:" + index + " for key:" |
| 83 | + SettingsProvider.keyToString(key)); |
| 84 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 85 | } |
| 86 | } |
Svetoslav Ganov | 04df738 | 2016-05-10 18:55:47 -0700 | [diff] [blame] | 87 | } catch (IOException e) { |
| 88 | Slog.e(LOG_TAG, "Error adding generation data", e); |
| 89 | destroyBackingStore(); |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public void onUserRemoved(int userId) { |
| 95 | synchronized (mLock) { |
| 96 | MemoryIntArray backingStore = getBackingStoreLocked(); |
| 97 | if (backingStore != null && mKeyToIndexMap.size() > 0) { |
| 98 | try { |
| 99 | final int secureKey = SettingsProvider.makeKey( |
| 100 | SettingsProvider.SETTINGS_TYPE_SECURE, userId); |
| 101 | resetSlotForKeyLocked(secureKey, mKeyToIndexMap, backingStore); |
| 102 | |
| 103 | final int systemKey = SettingsProvider.makeKey( |
| 104 | SettingsProvider.SETTINGS_TYPE_SYSTEM, userId); |
| 105 | resetSlotForKeyLocked(systemKey, mKeyToIndexMap, backingStore); |
| 106 | } catch (IOException e) { |
| 107 | Slog.e(LOG_TAG, "Error cleaning up for user", e); |
| 108 | destroyBackingStore(); |
| 109 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
Andreas Gampe | b5889307 | 2018-09-05 16:52:31 -0700 | [diff] [blame] | 114 | @GuardedBy("mLock") |
Svetoslav Ganov | 04df738 | 2016-05-10 18:55:47 -0700 | [diff] [blame] | 115 | private MemoryIntArray getBackingStoreLocked() { |
| 116 | if (mBackingStore == null) { |
| 117 | // One for the global table, two for system and secure tables for a |
| 118 | // managed profile (managed profile is not included in the max user |
| 119 | // count), ten for partially deleted users if users are quickly removed, |
| 120 | // and twice max user count for system and secure. |
| 121 | final int size = 1 + 2 + 10 + 2 * UserManager.getMaxSupportedUsers(); |
| 122 | try { |
Svetoslav Ganov | 74c9983 | 2016-12-05 20:07:20 -0800 | [diff] [blame] | 123 | mBackingStore = new MemoryIntArray(size); |
Svet Ganov | 9d723d3 | 2016-08-27 11:05:56 -0700 | [diff] [blame] | 124 | if (DEBUG) { |
| 125 | Slog.e(LOG_TAG, "Created backing store " + mBackingStore); |
| 126 | } |
Svetoslav Ganov | 04df738 | 2016-05-10 18:55:47 -0700 | [diff] [blame] | 127 | } catch (IOException e) { |
| 128 | Slog.e(LOG_TAG, "Error creating generation tracker", e); |
| 129 | } |
| 130 | } |
| 131 | return mBackingStore; |
| 132 | } |
| 133 | |
| 134 | private void destroyBackingStore() { |
| 135 | if (mBackingStore != null) { |
| 136 | try { |
| 137 | mBackingStore.close(); |
Svet Ganov | 9d723d3 | 2016-08-27 11:05:56 -0700 | [diff] [blame] | 138 | if (DEBUG) { |
| 139 | Slog.e(LOG_TAG, "Destroyed backing store " + mBackingStore); |
| 140 | } |
Svetoslav Ganov | 04df738 | 2016-05-10 18:55:47 -0700 | [diff] [blame] | 141 | } catch (IOException e) { |
| 142 | Slog.e(LOG_TAG, "Cannot close generation memory array", e); |
| 143 | } |
| 144 | mBackingStore = null; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | private static void resetSlotForKeyLocked(int key, SparseIntArray keyToIndexMap, |
| 149 | MemoryIntArray backingStore) throws IOException { |
| 150 | final int index = keyToIndexMap.get(key, -1); |
| 151 | if (index >= 0) { |
| 152 | keyToIndexMap.delete(key); |
| 153 | backingStore.set(index, 0); |
| 154 | if (DEBUG) { |
| 155 | Slog.i(LOG_TAG, "Freed index:" + index + " for key:" |
| 156 | + SettingsProvider.keyToString(key)); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | private static int getKeyIndexLocked(int key, SparseIntArray keyToIndexMap, |
| 162 | MemoryIntArray backingStore) throws IOException { |
| 163 | int index = keyToIndexMap.get(key, -1); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 164 | if (index < 0) { |
Svetoslav Ganov | 04df738 | 2016-05-10 18:55:47 -0700 | [diff] [blame] | 165 | index = findNextEmptyIndex(backingStore); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 166 | if (index >= 0) { |
Svetoslav Ganov | 04df738 | 2016-05-10 18:55:47 -0700 | [diff] [blame] | 167 | backingStore.set(index, 1); |
| 168 | keyToIndexMap.append(key, index); |
| 169 | if (DEBUG) { |
| 170 | Slog.i(LOG_TAG, "Allocated index:" + index + " for key:" |
| 171 | + SettingsProvider.keyToString(key)); |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 172 | } |
| 173 | } else { |
| 174 | Slog.e(LOG_TAG, "Could not allocate generation index"); |
| 175 | } |
| 176 | } |
| 177 | return index; |
| 178 | } |
| 179 | |
Svetoslav Ganov | 04df738 | 2016-05-10 18:55:47 -0700 | [diff] [blame] | 180 | private static int findNextEmptyIndex(MemoryIntArray backingStore) throws IOException { |
| 181 | final int size = backingStore.size(); |
| 182 | for (int i = 0; i < size; i++) { |
| 183 | if (backingStore.get(i) == 0) { |
| 184 | return i; |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 185 | } |
| 186 | } |
Svet Ganov | 53a441c | 2016-04-19 19:38:00 -0700 | [diff] [blame] | 187 | return -1; |
| 188 | } |
| 189 | } |