Home | History | Annotate | Download | only in mediaanalytics
      1 /*
      2  * Copyright (C) 2017 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 
     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 
     31 namespace android {
     32 
     33 class MediaAnalyticsService : public BnMediaAnalyticsService
     34 {
     35 
     36  public:
     37 
     38     // on this side, caller surrenders ownership
     39     virtual int64_t submit(MediaAnalyticsItem *item, bool forcenew);
     40 
     41     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;
     54     int64_t mItemsDiscardedExpire;
     55     int64_t mItemsDiscardedCount;
     56     MediaAnalyticsItem::SessionID_t mLastSessionID;
     57 
     58     // partitioned a bit so we don't over serialize
     59     mutable Mutex           mLock;
     60     mutable Mutex           mLock_ids;
     61     mutable Mutex           mLock_mappings;
     62 
     63     // limit how many records we'll retain
     64     // by count (in each queue (open, finalized))
     65     int32_t mMaxRecords;
     66     // by time (none older than this long agan
     67     nsecs_t mMaxRecordAgeNs;
     68     //
     69     // # of sets of summaries
     70     int32_t mMaxRecordSets;
     71     // nsecs until we start a new record set
     72     nsecs_t mNewSetInterval;
     73 
     74     // input validation after arrival from client
     75     bool contentValid(MediaAnalyticsItem *item, bool isTrusted);
     76     bool rateLimited(MediaAnalyticsItem *);
     77 
     78     // (oldest at front) so it prints nicely for dumpsys
     79     List<MediaAnalyticsItem *> mItems;
     80     void saveItem(MediaAnalyticsItem *);
     81 
     82     // support for generating output
     83     int mDumpProto;
     84     int mDumpProtoDefault;
     85     String8 dumpQueue();
     86     String8 dumpQueue(nsecs_t, const char *only);
     87 
     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);
     91 
     92     // mapping uids to package names
     93     struct UidToPkgMap {
     94         uid_t uid;
     95         std::string pkg;
     96         std::string installer;
     97         int64_t versionCode;
     98         nsecs_t expiration;
     99     };
    100 
    101     KeyedVector<uid_t,struct UidToPkgMap>  mPkgMappings;
    102     void setPkgInfo(MediaAnalyticsItem *item, uid_t uid, bool setName, bool setVersion);
    103 
    104 };
    105 
    106 // ----------------------------------------------------------------------------
    107 
    108 }; // namespace android
    109 
    110 #endif // ANDROID_MEDIAANALYTICSSERVICE_H
    111