blob: f4621ee15dbe7be63e4b6ed71d0f14d9bde32aeb [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) {
55 lock_guard<mutex> lock(mMutex); // Exclusively lock for updates.
David Chende701692017-10-05 13:16:02 -070056
57 mMap.clear();
Joe Onorato9fc9edf2017-10-15 20:08:52 -070058 for (unsigned long j = 0; j < uid.size(); j++) {
59 mMap.insert(make_pair(uid[j],
60 AppData(string(String8(packageName[j]).string()), versionCode[j])));
David Chende701692017-10-05 13:16:02 -070061 }
62
Joe Onorato9fc9edf2017-10-15 20:08:52 -070063 if (mOutput.initial_size() == 0) { // Provide the initial states in the mOutput proto
64 for (unsigned long j = 0; j < uid.size(); j++) {
David Chende701692017-10-05 13:16:02 -070065 auto t = mOutput.add_initial();
66 t->set_app(string(String8(packageName[j]).string()));
67 t->set_version(int(versionCode[j]));
68 t->set_uid(uid[j]);
69 }
70 }
71}
72
Joe Onorato9fc9edf2017-10-15 20:08:52 -070073void UidMap::updateApp(const String16& app_16, const int32_t& uid, const int32_t& versionCode) {
David Chende701692017-10-05 13:16:02 -070074 lock_guard<mutex> lock(mMutex);
75
76 string app = string(String8(app_16).string());
77
78 // Notify any interested producers that this app has updated
79 for (auto it : mSubscribers) {
80 it->notifyAppUpgrade(app, uid, versionCode);
81 }
82
83 auto log = mOutput.add_changes();
84 log->set_deletion(false);
Joe Onorato9fc9edf2017-10-15 20:08:52 -070085 // log.timestamp = TODO: choose how timestamps are computed
David Chende701692017-10-05 13:16:02 -070086 log->set_app(app);
87 log->set_uid(uid);
88 log->set_version(versionCode);
89
90 auto range = mMap.equal_range(int(uid));
91 for (auto it = range.first; it != range.second; ++it) {
92 if (it->second.packageName == app) {
93 it->second.versionCode = int(versionCode);
94 return;
95 }
96 ALOGD("updateApp failed to find the app %s with uid %i to update", app.c_str(), uid);
97 return;
98 }
99
100 // Otherwise, we need to add an app at this uid.
101 mMap.insert(make_pair(uid, AppData(app, int(versionCode))));
102}
103
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700104void UidMap::removeApp(const String16& app_16, const int32_t& uid) {
David Chende701692017-10-05 13:16:02 -0700105 lock_guard<mutex> lock(mMutex);
106
107 string app = string(String8(app_16).string());
108
109 auto log = mOutput.add_changes();
110 log->set_deletion(true);
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700111 // log.timestamp = TODO: choose how timestamps are computed
David Chende701692017-10-05 13:16:02 -0700112 log->set_app(app);
113 log->set_uid(uid);
114
115 auto range = mMap.equal_range(int(uid));
116 for (auto it = range.first; it != range.second; ++it) {
117 if (it->second.packageName == app) {
118 mMap.erase(it);
119 return;
120 }
121 }
122 ALOGD("removeApp failed to find the app %s with uid %i to remove", app.c_str(), uid);
123 return;
124}
125
126void UidMap::addListener(sp<PackageInfoListener> producer) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700127 lock_guard<mutex> lock(mMutex); // Lock for updates
David Chende701692017-10-05 13:16:02 -0700128 mSubscribers.insert(producer);
129}
130
131void UidMap::removeListener(sp<PackageInfoListener> producer) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700132 lock_guard<mutex> lock(mMutex); // Lock for updates
David Chende701692017-10-05 13:16:02 -0700133 mSubscribers.erase(producer);
134}
135
136UidMapping UidMap::getAndClearOutput() {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700137 lock_guard<mutex> lock(mMutex); // Lock for updates
David Chende701692017-10-05 13:16:02 -0700138
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700139 auto ret = UidMapping(mOutput); // Copy that will be returned.
David Chende701692017-10-05 13:16:02 -0700140 mOutput.Clear();
141
142 // Re-initialize the initial state for the outputs. This results in extra data being uploaded
143 // but helps ensure we can't re-construct the UID->app name, versionCode mapping in server.
144 for (auto it : mMap) {
145 auto t = mOutput.add_initial();
146 t->set_app(it.second.packageName);
147 t->set_version(it.second.versionCode);
148 t->set_uid(it.first);
149 }
150
151 return ret;
152}
153
154void UidMap::printUidMap(FILE* out) {
155 lock_guard<mutex> lock(mMutex);
156
157 for (auto it : mMap) {
Joe Onorato9fc9edf2017-10-15 20:08:52 -0700158 fprintf(out, "%s, v%d (%i)\n", it.second.packageName.c_str(), it.second.versionCode,
159 it.first);
David Chende701692017-10-05 13:16:02 -0700160 }
161}
162
David Chende701692017-10-05 13:16:02 -0700163} // namespace statsd
164} // namespace os
165} // namespace android