Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 1 | /* |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 2 | * Copyright (C) 2017 The Android Open Source Project |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 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 | |
| 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 | |
Ray Essick | 72a436b | 2018-06-14 15:08:13 -0700 | [diff] [blame^] | 29 | #include <future> |
| 30 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 31 | #include <media/IMediaAnalyticsService.h> |
| 32 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 33 | namespace android { |
| 34 | |
| 35 | class MediaAnalyticsService : public BnMediaAnalyticsService |
| 36 | { |
| 37 | |
| 38 | public: |
| 39 | |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 40 | // on this side, caller surrenders ownership |
| 41 | virtual int64_t submit(MediaAnalyticsItem *item, bool forcenew); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 42 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 43 | static void instantiate(); |
| 44 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 45 | |
| 46 | MediaAnalyticsService(); |
| 47 | virtual ~MediaAnalyticsService(); |
| 48 | |
Ray Essick | 72a436b | 2018-06-14 15:08:13 -0700 | [diff] [blame^] | 49 | bool processExpirations(); |
| 50 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 51 | private: |
| 52 | MediaAnalyticsItem::SessionID_t generateUniqueSessionID(); |
| 53 | |
| 54 | // statistics about our analytics |
| 55 | int64_t mItemsSubmitted; |
| 56 | int64_t mItemsFinalized; |
| 57 | int64_t mItemsDiscarded; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 58 | int64_t mItemsDiscardedExpire; |
| 59 | int64_t mItemsDiscardedCount; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 60 | MediaAnalyticsItem::SessionID_t mLastSessionID; |
| 61 | |
| 62 | // partitioned a bit so we don't over serialize |
| 63 | mutable Mutex mLock; |
| 64 | mutable Mutex mLock_ids; |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame] | 65 | mutable Mutex mLock_mappings; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 66 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 67 | // limit how many records we'll retain |
| 68 | // by count (in each queue (open, finalized)) |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 69 | int32_t mMaxRecords; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 70 | // by time (none older than this long agan |
| 71 | nsecs_t mMaxRecordAgeNs; |
Ray Essick | 72a436b | 2018-06-14 15:08:13 -0700 | [diff] [blame^] | 72 | // max to expire per expirations_l() invocation |
| 73 | int32_t mMaxRecordsExpiredAtOnce; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 74 | // |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 75 | // # of sets of summaries |
| 76 | int32_t mMaxRecordSets; |
| 77 | // nsecs until we start a new record set |
| 78 | nsecs_t mNewSetInterval; |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 79 | |
| 80 | // input validation after arrival from client |
Ray Essick | d38e174 | 2017-01-23 15:17:06 -0800 | [diff] [blame] | 81 | bool contentValid(MediaAnalyticsItem *item, bool isTrusted); |
Ray Essick | b5fac8e | 2016-12-12 11:33:56 -0800 | [diff] [blame] | 82 | bool rateLimited(MediaAnalyticsItem *); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 83 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 84 | // (oldest at front) so it prints nicely for dumpsys |
Ray Essick | 92d23b4 | 2018-01-29 12:10:30 -0800 | [diff] [blame] | 85 | List<MediaAnalyticsItem *> mItems; |
| 86 | void saveItem(MediaAnalyticsItem *); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 87 | |
Ray Essick | 72a436b | 2018-06-14 15:08:13 -0700 | [diff] [blame^] | 88 | bool expirations_l(MediaAnalyticsItem *); |
| 89 | std::future<bool> mExpireFuture; |
| 90 | |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 91 | // support for generating output |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 92 | int mDumpProto; |
Ray Essick | 583a23a | 2017-11-27 12:49:57 -0800 | [diff] [blame] | 93 | int mDumpProtoDefault; |
Ray Essick | 92d23b4 | 2018-01-29 12:10:30 -0800 | [diff] [blame] | 94 | String8 dumpQueue(); |
| 95 | String8 dumpQueue(nsecs_t, const char *only); |
Ray Essick | 2e9c63b | 2017-03-29 15:16:44 -0700 | [diff] [blame] | 96 | |
| 97 | void dumpHeaders(String8 &result, nsecs_t ts_since); |
| 98 | void dumpSummaries(String8 &result, nsecs_t ts_since, const char * only); |
| 99 | void dumpRecent(String8 &result, nsecs_t ts_since, const char * only); |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 100 | |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 101 | // mapping uids to package names |
| 102 | struct UidToPkgMap { |
| 103 | uid_t uid; |
Ray Essick | 783bd0d | 2018-01-11 11:10:35 -0800 | [diff] [blame] | 104 | std::string pkg; |
| 105 | std::string installer; |
Dianne Hackborn | 4e2eeff | 2017-11-27 14:01:29 -0800 | [diff] [blame] | 106 | int64_t versionCode; |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame] | 107 | nsecs_t expiration; |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Ray Essick | fa14956 | 2017-09-19 09:27:31 -0700 | [diff] [blame] | 110 | KeyedVector<uid_t,struct UidToPkgMap> mPkgMappings; |
| 111 | void setPkgInfo(MediaAnalyticsItem *item, uid_t uid, bool setName, bool setVersion); |
Ray Essick | f65f421 | 2017-08-31 11:41:19 -0700 | [diff] [blame] | 112 | |
Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | // ---------------------------------------------------------------------------- |
| 116 | |
| 117 | }; // namespace android |
| 118 | |
| 119 | #endif // ANDROID_MEDIAANALYTICSSERVICE_H |