blob: 5bb458a8b1d8e53ad5e7d8ea8e91630d12012a48 [file] [log] [blame]
Bookatzc6977972018-01-16 16:55:05 -08001/*
2 * Copyright (C) 2018 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#pragma once
18
19#include <android/os/IStatsCompanionService.h>
20#include <utils/RefBase.h>
21
22#include "config/ConfigKey.h"
23#include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" // subscription
24#include "frameworks/base/cmds/statsd/src/stats_log.pb.h" // DimensionsValue
25#include "android/os/StatsDimensionsValue.h"
26#include "HashableDimensionKey.h"
27
28#include <mutex>
29#include <unordered_map>
30
31namespace android {
32namespace os {
33namespace statsd {
34
35// Reports information to subscribers.
36// Single instance shared across the process. All methods are thread safe.
37class SubscriberReporter {
38public:
39 /** Get (singleton) instance of SubscriberReporter. */
40 static SubscriberReporter& getInstance() {
41 static SubscriberReporter subscriberReporter;
42 return subscriberReporter;
43 }
44
45 ~SubscriberReporter(){};
46 SubscriberReporter(SubscriberReporter const&) = delete;
47 void operator=(SubscriberReporter const&) = delete;
48
49 /**
50 * Tells SubscriberReporter what IStatsCompanionService to use.
51 * May be nullptr, but SubscriberReporter will not send broadcasts for any calls
52 * to alertBroadcastSubscriber that occur while nullptr.
53 */
54 void setStatsCompanionService(sp<IStatsCompanionService> statsCompanionService) {
55 std::lock_guard<std::mutex> lock(mLock);
56 sp<IStatsCompanionService> tmpForLock = mStatsCompanionService;
57 mStatsCompanionService = statsCompanionService;
58 }
59
60 /**
61 * Stores the given intentSender, associating it with the given (configKey, subscriberId) pair.
62 * intentSender must be convertible into an IntentSender (in Java) using IntentSender(IBinder).
63 */
64 void setBroadcastSubscriber(const ConfigKey& configKey,
65 int64_t subscriberId,
66 const sp<android::IBinder>& intentSender);
67
68 /**
69 * Erases any intentSender information from the given (configKey, subscriberId) pair.
70 */
71 void unsetBroadcastSubscriber(const ConfigKey& configKey, int64_t subscriberId);
72
73 /** Remove all information stored by SubscriberReporter about the given config. */
74 void removeConfig(const ConfigKey& configKey);
75
76 /**
77 * Sends a broadcast via the intentSender previously stored for the
78 * given (configKey, subscriberId) pair by setBroadcastSubscriber.
79 * Information about the subscriber, as well as information extracted from the dimKey, is sent.
80 */
81 void alertBroadcastSubscriber(const ConfigKey& configKey,
82 const Subscription& subscription,
83 const HashableDimensionKey& dimKey) const;
84
85private:
86 SubscriberReporter() {};
87
88 mutable std::mutex mLock;
89
90 /** Binder interface for communicating with StatsCompanionService. */
91 sp<IStatsCompanionService> mStatsCompanionService = nullptr;
92
93 /** Maps <ConfigKey, SubscriberId> -> IBinder (which represents an IIntentSender). */
94 std::unordered_map<ConfigKey,
95 std::unordered_map<int64_t, sp<android::IBinder>>> mIntentMap;
96
97 /**
98 * Sends a broadcast via the given intentSender (using mStatsCompanionService), along
99 * with the information in the other parameters.
100 */
101 void sendBroadcastLocked(const sp<android::IBinder>& intentSender,
102 const ConfigKey& configKey,
103 const Subscription& subscription,
104 const HashableDimensionKey& dimKey) const;
105
106 /** Converts a stats_log.proto DimensionsValue to a StatsDimensionsValue. */
107 static StatsDimensionsValue protoToStatsDimensionsValue(
108 const DimensionsValue& protoDimsVal);
109
110 /** Converts a HashableDimensionKey to a StatsDimensionsValue. */
111 static StatsDimensionsValue protoToStatsDimensionsValue(
112 const HashableDimensionKey& dimKey);
113};
114
115} // namespace statsd
116} // namespace os
117} // namespace android