Andy Hung | c9b6f8b | 2021-07-08 10:17:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 "MediaMetricsService" // not ValidateId |
| 19 | #include <utils/Log.h> |
| 20 | |
| 21 | #include "ValidateId.h" |
| 22 | |
| 23 | namespace android::mediametrics { |
| 24 | |
| 25 | std::string ValidateId::dump() const |
| 26 | { |
| 27 | std::stringstream ss; |
| 28 | ss << "Entries:" << mIdSet.size() << " InvalidIds:" << mInvalidIds << "\n"; |
| 29 | ss << mIdSet.dump(10); |
| 30 | return ss.str(); |
| 31 | } |
| 32 | |
| 33 | void ValidateId::registerId(const std::string& id) |
| 34 | { |
| 35 | if (id.empty()) return; |
| 36 | if (!mediametrics::stringutils::isLogSessionId(id.c_str())) { |
| 37 | ALOGW("%s: rejecting malformed id %s", __func__, id.c_str()); |
| 38 | return; |
| 39 | } |
| 40 | ALOGV("%s: registering %s", __func__, id.c_str()); |
| 41 | mIdSet.add(id); |
| 42 | } |
| 43 | |
| 44 | const std::string& ValidateId::validateId(const std::string& id) |
| 45 | { |
| 46 | static const std::string empty{}; |
| 47 | if (id.empty()) return empty; |
| 48 | |
| 49 | // reject because the id is malformed |
| 50 | if (!mediametrics::stringutils::isLogSessionId(id.c_str())) { |
| 51 | ALOGW("%s: rejecting malformed id %s", __func__, id.c_str()); |
| 52 | ++mInvalidIds; |
| 53 | return empty; |
| 54 | } |
| 55 | |
| 56 | // reject because the id is unregistered |
| 57 | if (!mIdSet.check(id)) { |
| 58 | ALOGW("%s: rejecting unregistered id %s", __func__, id.c_str()); |
| 59 | ++mInvalidIds; |
| 60 | return empty; |
| 61 | } |
| 62 | return id; |
| 63 | } |
| 64 | |
| 65 | } // namespace android::mediametrics |