blob: d83c3a42668573a979c52f3725f9b3767c3a36cb [file] [log] [blame]
David Chende701692017-10-05 13:16:02 -07001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, versionCode 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
Joe Onorato9fc9edf2017-10-15 20:08:52 -070017#include "Log.h"
18
19#include "packages/UidMap.h"
20
David Chende701692017-10-05 13:16:02 -070021#include <utils/Errors.h>
22
23using namespace android;
24
25namespace android {
26namespace os {
27namespace statsd {
28
29bool UidMap::hasApp(int uid, const string& packageName) const {
30 lock_guard<mutex> lock(mMutex);
31
32 auto range = mMap.equal_range(uid);
33 for (auto it = range.first; it != range.second; ++it) {
34 if (it->second.packageName == packageName) {
35 return true;
36 }
37 }
38 return false;
39}
40
41int UidMap::getAppVersion(int uid, const string& packageName) const {
42 lock_guard<mutex> lock(mMutex);
43
44 auto range = mMap.equal_range(uid);
45 for (auto it = range.first; it != range.second; ++it) {
46 if (it->second.packageName == packageName) {
47 return it->second.versionCode;
48 }
49 }
50 return 0;
51}
52
Joe Onorato9fc9edf2017-10-15 20:08:52 -070053void UidMap::updateMap(const vector<int32_t>& uid, const vector<int32_t>& versionCode,
54 const vector<String16>& packageName) {
David Chend6896892017-10-25 11:49:03 -070055 updateMap(time(nullptr) * 1000000000, uid, versionCode, packageName);
56}
57
58void UidMap::updateMap(const int64_t& timestamp, const vector<int32_t>& uid,
59 const vector<int32_t>& versionCode, const vector<String16>& packageName) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -070060 lock_guard<mutex> lock(mMutex); // Exclusively lock for updates.
David Chende701692017-10-05 13:16:02 -070061
62 mMap.clear();
David Chend6896892017-10-25 11:49:03 -070063 for (size_t j = 0; j < uid.size(); j++) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -070064 mMap.insert(make_pair(uid[j],
65 AppData(string(String8(packageName[j]).string()), versionCode[j])));
David Chende701692017-10-05 13:16:02 -070066 }
67
David Chend6896892017-10-25 11:49:03 -070068 auto snapshot = mOutput.add_snapshots();
69 snapshot->set_timestamp_nanos(timestamp);
70 for (size_t j = 0; j < uid.size(); j++) {
71 auto t = snapshot->add_package_info();
72 t->set_name(string(String8(packageName[j]).string()));
73 t->set_version(int(versionCode[j]));
74 t->set_uid(uid[j]);
David Chende701692017-10-05 13:16:02 -070075 }
76}
77
Joe Onorato9fc9edf2017-10-15 20:08:52 -070078void UidMap::updateApp(const String16& app_16, const int32_t& uid, const int32_t& versionCode) {
David Chend6896892017-10-25 11:49:03 -070079 updateApp(time(nullptr) * 1000000000, app_16, uid, versionCode);
80}
81
82void UidMap::updateApp(const int64_t& timestamp, const String16& app_16, const int32_t& uid,
83 const int32_t& versionCode) {
David Chende701692017-10-05 13:16:02 -070084 lock_guard<mutex> lock(mMutex);
85
86 string app = string(String8(app_16).string());
87
88 // Notify any interested producers that this app has updated
89 for (auto it : mSubscribers) {
90 it->notifyAppUpgrade(app, uid, versionCode);
91 }
92
93 auto log = mOutput.add_changes();
94 log->set_deletion(false);
David Chend6896892017-10-25 11:49:03 -070095 log->set_timestamp_nanos(timestamp);
David Chende701692017-10-05 13:16:02 -070096 log->set_app(app);
97 log->set_uid(uid);
98 log->set_version(versionCode);
99
100 auto range = mMap.equal_range(int(uid));
101 for (auto it = range.first; it != range.second; ++it) {
102 if (it->second.packageName == app) {
103 it->second.versionCode = int(versionCode);
104 return;
105 }
106 ALOGD("updateApp failed to find the app %s with uid %i to update", app.c_str(), uid);
107 return;
108 }
109
110 // Otherwise, we need to add an app at this uid.
111 mMap.insert(make_pair(uid, AppData(app, int(versionCode))));
112}
113
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700114void UidMap::removeApp(const String16& app_16, const int32_t& uid) {
David Chend6896892017-10-25 11:49:03 -0700115 removeApp(time(nullptr) * 1000000000, app_16, uid);
116}
117void UidMap::removeApp(const int64_t& timestamp, const String16& app_16, const int32_t& uid) {
David Chende701692017-10-05 13:16:02 -0700118 lock_guard<mutex> lock(mMutex);
119
120 string app = string(String8(app_16).string());
121
David Chend6896892017-10-25 11:49:03 -0700122 for (auto it : mSubscribers) {
123 it->notifyAppRemoved(app, uid);
124 }
125
David Chende701692017-10-05 13:16:02 -0700126 auto log = mOutput.add_changes();
127 log->set_deletion(true);
David Chend6896892017-10-25 11:49:03 -0700128 log->set_timestamp_nanos(timestamp);
David Chende701692017-10-05 13:16:02 -0700129 log->set_app(app);
130 log->set_uid(uid);
131
132 auto range = mMap.equal_range(int(uid));
133 for (auto it = range.first; it != range.second; ++it) {
134 if (it->second.packageName == app) {
135 mMap.erase(it);
136 return;
137 }
138 }
139 ALOGD("removeApp failed to find the app %s with uid %i to remove", app.c_str(), uid);
140 return;
141}
142
143void UidMap::addListener(sp<PackageInfoListener> producer) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700144 lock_guard<mutex> lock(mMutex); // Lock for updates
David Chende701692017-10-05 13:16:02 -0700145 mSubscribers.insert(producer);
146}
147
148void UidMap::removeListener(sp<PackageInfoListener> producer) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700149 lock_guard<mutex> lock(mMutex); // Lock for updates
David Chende701692017-10-05 13:16:02 -0700150 mSubscribers.erase(producer);
151}
152
David Chend6896892017-10-25 11:49:03 -0700153void UidMap::clearOutput() {
David Chende701692017-10-05 13:16:02 -0700154 mOutput.Clear();
155
156 // Re-initialize the initial state for the outputs. This results in extra data being uploaded
David Chend6896892017-10-25 11:49:03 -0700157 // but helps ensure we can re-construct the UID->app name, versionCode mapping in server.
158 auto snapshot = mOutput.add_snapshots();
David Chende701692017-10-05 13:16:02 -0700159 for (auto it : mMap) {
David Chend6896892017-10-25 11:49:03 -0700160 auto t = snapshot->add_package_info();
161 t->set_name(it.second.packageName);
David Chende701692017-10-05 13:16:02 -0700162 t->set_version(it.second.versionCode);
163 t->set_uid(it.first);
164 }
David Chend6896892017-10-25 11:49:03 -0700165}
David Chende701692017-10-05 13:16:02 -0700166
David Chend6896892017-10-25 11:49:03 -0700167int64_t UidMap::getMinimumTimestampNs() {
168 int64_t m = 0;
169 for (auto it : mLastUpdatePerConfigKey) {
170 if (m == 0) {
171 m = it.second;
172 } else if (it.second < m) {
173 m = it.second;
174 }
175 }
176 return m;
177}
178
179UidMapping UidMap::getOutput(const ConfigKey& key) {
180 return getOutput(time(nullptr) * 1000000000, key);
181}
182
183UidMapping UidMap::getOutput(const int64_t& timestamp, const ConfigKey& key) {
184 lock_guard<mutex> lock(mMutex); // Lock for updates
185
186 auto ret = UidMapping(mOutput); // Copy that will be returned.
187 int64_t prevMin = getMinimumTimestampNs();
188 mLastUpdatePerConfigKey[key] = timestamp;
189 int64_t newMin = getMinimumTimestampNs();
190
191 if (newMin > prevMin) {
192 int64_t cutoff_nanos = newMin;
193 auto snapshots = mOutput.mutable_snapshots();
194 auto it_snapshots = snapshots->cbegin();
195 while (it_snapshots != snapshots->cend()) {
196 if (it_snapshots->timestamp_nanos() < cutoff_nanos) {
197 // it_snapshots now points to the following element.
198 it_snapshots = snapshots->erase(it_snapshots);
199 } else {
200 ++it_snapshots;
201 }
202 }
203 auto deltas = mOutput.mutable_changes();
204 auto it_deltas = deltas->cbegin();
205 while (it_deltas != deltas->cend()) {
206 if (it_deltas->timestamp_nanos() < cutoff_nanos) {
207 // it_deltas now points to the following element.
208 it_deltas = deltas->erase(it_deltas);
209 } else {
210 ++it_deltas;
211 }
212 }
213 }
David Chende701692017-10-05 13:16:02 -0700214 return ret;
215}
216
217void UidMap::printUidMap(FILE* out) {
218 lock_guard<mutex> lock(mMutex);
219
220 for (auto it : mMap) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700221 fprintf(out, "%s, v%d (%i)\n", it.second.packageName.c_str(), it.second.versionCode,
222 it.first);
David Chende701692017-10-05 13:16:02 -0700223 }
224}
225
David Chend6896892017-10-25 11:49:03 -0700226void UidMap::OnConfigUpdated(const ConfigKey& key) {
227 mLastUpdatePerConfigKey[key] = -1;
228}
229
230void UidMap::OnConfigRemoved(const ConfigKey& key) {
231 mLastUpdatePerConfigKey.erase(key);
232}
233
David Chende701692017-10-05 13:16:02 -0700234} // namespace statsd
235} // namespace os
236} // namespace android