blob: b3c902a4964354350ca30dfd13bc69d1671fa021 [file] [log] [blame]
Ray Essick3938dc62016-11-01 08:56:56 -07001/*
Ray Essick2e9c63b2017-03-29 15:16:44 -07002 * Copyright (C) 2017 The Android Open Source Project
Ray Essick3938dc62016-11-01 08:56:56 -07003 *
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
18#ifndef ANDROID_MEDIAANALYTICSSERVICE_H
19#define ANDROID_MEDIAANALYTICSSERVICE_H
20
21#include <arpa/inet.h>
22
23#include <utils/threads.h>
24#include <utils/Errors.h>
25#include <utils/KeyedVector.h>
26#include <utils/String8.h>
27#include <utils/List.h>
28
29#include <media/IMediaAnalyticsService.h>
30
Ray Essick3938dc62016-11-01 08:56:56 -070031namespace android {
32
33class MediaAnalyticsService : public BnMediaAnalyticsService
34{
35
36 public:
37
Ray Essickb5fac8e2016-12-12 11:33:56 -080038 // on this side, caller surrenders ownership
39 virtual int64_t submit(MediaAnalyticsItem *item, bool forcenew);
Ray Essick3938dc62016-11-01 08:56:56 -070040
Ray Essick3938dc62016-11-01 08:56:56 -070041 static void instantiate();
42 virtual status_t dump(int fd, const Vector<String16>& args);
43
44 MediaAnalyticsService();
45 virtual ~MediaAnalyticsService();
46
47 private:
48 MediaAnalyticsItem::SessionID_t generateUniqueSessionID();
49
50 // statistics about our analytics
51 int64_t mItemsSubmitted;
52 int64_t mItemsFinalized;
53 int64_t mItemsDiscarded;
Ray Essickf65f4212017-08-31 11:41:19 -070054 int64_t mItemsDiscardedExpire;
55 int64_t mItemsDiscardedCount;
Ray Essick3938dc62016-11-01 08:56:56 -070056 MediaAnalyticsItem::SessionID_t mLastSessionID;
57
58 // partitioned a bit so we don't over serialize
59 mutable Mutex mLock;
60 mutable Mutex mLock_ids;
Ray Essickfa149562017-09-19 09:27:31 -070061 mutable Mutex mLock_mappings;
Ray Essick3938dc62016-11-01 08:56:56 -070062
Ray Essickf65f4212017-08-31 11:41:19 -070063 // limit how many records we'll retain
64 // by count (in each queue (open, finalized))
Ray Essick3938dc62016-11-01 08:56:56 -070065 int32_t mMaxRecords;
Ray Essickf65f4212017-08-31 11:41:19 -070066 // by time (none older than this long agan
67 nsecs_t mMaxRecordAgeNs;
68 //
Ray Essick2e9c63b2017-03-29 15:16:44 -070069 // # of sets of summaries
70 int32_t mMaxRecordSets;
71 // nsecs until we start a new record set
72 nsecs_t mNewSetInterval;
Ray Essick3938dc62016-11-01 08:56:56 -070073
74 // input validation after arrival from client
Ray Essickd38e1742017-01-23 15:17:06 -080075 bool contentValid(MediaAnalyticsItem *item, bool isTrusted);
Ray Essickb5fac8e2016-12-12 11:33:56 -080076 bool rateLimited(MediaAnalyticsItem *);
Ray Essick3938dc62016-11-01 08:56:56 -070077
Ray Essick3938dc62016-11-01 08:56:56 -070078 // (oldest at front) so it prints nicely for dumpsys
Ray Essick92d23b42018-01-29 12:10:30 -080079 List<MediaAnalyticsItem *> mItems;
80 void saveItem(MediaAnalyticsItem *);
Ray Essick3938dc62016-11-01 08:56:56 -070081
Ray Essick2e9c63b2017-03-29 15:16:44 -070082 // support for generating output
Ray Essickf65f4212017-08-31 11:41:19 -070083 int mDumpProto;
Ray Essick583a23a2017-11-27 12:49:57 -080084 int mDumpProtoDefault;
Ray Essick92d23b42018-01-29 12:10:30 -080085 String8 dumpQueue();
86 String8 dumpQueue(nsecs_t, const char *only);
Ray Essick2e9c63b2017-03-29 15:16:44 -070087
88 void dumpHeaders(String8 &result, nsecs_t ts_since);
89 void dumpSummaries(String8 &result, nsecs_t ts_since, const char * only);
90 void dumpRecent(String8 &result, nsecs_t ts_since, const char * only);
Ray Essick3938dc62016-11-01 08:56:56 -070091
Ray Essickf65f4212017-08-31 11:41:19 -070092 // mapping uids to package names
93 struct UidToPkgMap {
94 uid_t uid;
Ray Essick783bd0d2018-01-11 11:10:35 -080095 std::string pkg;
96 std::string installer;
Dianne Hackborn4e2eeff2017-11-27 14:01:29 -080097 int64_t versionCode;
Ray Essickfa149562017-09-19 09:27:31 -070098 nsecs_t expiration;
Ray Essickf65f4212017-08-31 11:41:19 -070099 };
100
Ray Essickfa149562017-09-19 09:27:31 -0700101 KeyedVector<uid_t,struct UidToPkgMap> mPkgMappings;
102 void setPkgInfo(MediaAnalyticsItem *item, uid_t uid, bool setName, bool setVersion);
Ray Essickf65f4212017-08-31 11:41:19 -0700103
Ray Essick3938dc62016-11-01 08:56:56 -0700104};
105
106// ----------------------------------------------------------------------------
107
108}; // namespace android
109
110#endif // ANDROID_MEDIAANALYTICSSERVICE_H