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//#define LOG_NDEBUG 0
18#define LOG_TAG "PluginMetricsReporting"
19
20#include <media/PluginMetricsReporting.h>
21
22#include <inttypes.h>
23
24#include <media/MediaAnalyticsItem.h>
25#include <utils/Log.h>
26
27
28namespace android {
29
30namespace {
31
32constexpr char kSerializedMetricsField[] = "serialized_metrics";
33
34status_t reportVendorMetrics(const std::string& metrics,
35                             const String8& name,
36                             const String8& appPackageName) {
37    MediaAnalyticsItem analyticsItem(name.c_str());
38    analyticsItem.generateSessionID();
39
40    std::string app_package_name(appPackageName.c_str(), appPackageName.size());
41    analyticsItem.setPkgName(app_package_name);
42    if (metrics.size() > 0) {
43        analyticsItem.setCString(kSerializedMetricsField, metrics.c_str());
44    }
45
46    if (!analyticsItem.selfrecord()) {
47      ALOGE("selfrecord() returned false. sessioId %" PRId64, analyticsItem.getSessionID());
48    }
49
50    return OK;
51}
52
53String8 sanitize(const String8& input) {
54    // Filters the input string down to just alphanumeric characters.
55    String8 output;
56    for (size_t i = 0; i < input.size(); ++i) {
57        char candidate = input[i];
58        if ((candidate >= 'a' && candidate <= 'z') ||
59                (candidate >= 'A' && candidate <= 'Z') ||
60                (candidate >= '0' && candidate <= '9')) {
61            output.append(&candidate, 1);
62        }
63    }
64    return output;
65}
66
67}  // namespace
68
69status_t reportDrmPluginMetrics(const std::string& b64EncodedMetrics,
70                                const String8& vendor,
71                                const String8& description,
72                                const String8& appPackageName) {
73
74    String8 name = String8::format("drm.vendor.%s.%s",
75                                   sanitize(vendor).c_str(),
76                                   sanitize(description).c_str());
77
78    return reportVendorMetrics(b64EncodedMetrics, name, appPackageName);
79}
80
81}  // namespace android
82