Ray Essick | 3938dc6 | 2016-11-01 08:56:56 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
| 32 | namespace android { |
| 33 | |
| 34 | class MediaAnalyticsService : public BnMediaAnalyticsService |
| 35 | { |
| 36 | |
| 37 | public: |
| 38 | |
| 39 | virtual int64_t submit(sp<MediaAnalyticsItem> item, bool forcenew); |
| 40 | |
| 41 | virtual List<sp<MediaAnalyticsItem>> |
| 42 | *getMediaAnalyticsItemList(bool finished, int64_t ts); |
| 43 | virtual List<sp<MediaAnalyticsItem>> |
| 44 | *getMediaAnalyticsItemList(bool finished, int64_t ts, MediaAnalyticsItem::Key key); |
| 45 | |
| 46 | |
| 47 | static void instantiate(); |
| 48 | virtual status_t dump(int fd, const Vector<String16>& args); |
| 49 | |
| 50 | MediaAnalyticsService(); |
| 51 | virtual ~MediaAnalyticsService(); |
| 52 | |
| 53 | private: |
| 54 | MediaAnalyticsItem::SessionID_t generateUniqueSessionID(); |
| 55 | |
| 56 | // statistics about our analytics |
| 57 | int64_t mItemsSubmitted; |
| 58 | int64_t mItemsFinalized; |
| 59 | int64_t mItemsDiscarded; |
| 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; |
| 65 | |
| 66 | // the most we hold in memory |
| 67 | // up to this many in each queue (open, finalized) |
| 68 | int32_t mMaxRecords; |
| 69 | |
| 70 | // input validation after arrival from client |
| 71 | bool contentValid(sp<MediaAnalyticsItem>); |
| 72 | bool rateLimited(sp<MediaAnalyticsItem>); |
| 73 | |
| 74 | // the ones that are still open |
| 75 | // (newest at front) since we keep looking for them |
| 76 | List<sp<MediaAnalyticsItem>> *mOpen; |
| 77 | // the ones we've finalized |
| 78 | // (oldest at front) so it prints nicely for dumpsys |
| 79 | List<sp<MediaAnalyticsItem>> *mFinalized; |
| 80 | // searching within these queues: queue, key |
| 81 | sp<MediaAnalyticsItem> findItem(List<sp<MediaAnalyticsItem>> *, |
| 82 | sp<MediaAnalyticsItem>, bool removeit); |
| 83 | |
| 84 | void saveItem(sp<MediaAnalyticsItem>); |
| 85 | void saveItem(List<sp<MediaAnalyticsItem>>*, sp<MediaAnalyticsItem>, int); |
| 86 | void deleteItem(List<sp<MediaAnalyticsItem>>*, sp<MediaAnalyticsItem>); |
| 87 | |
| 88 | String8 dumpQueue(List<sp<MediaAnalyticsItem>> *); |
| 89 | |
| 90 | }; |
| 91 | |
| 92 | // ---------------------------------------------------------------------------- |
| 93 | |
| 94 | }; // namespace android |
| 95 | |
| 96 | #endif // ANDROID_MEDIAANALYTICSSERVICE_H |