• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2020 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "package_version_map.h"
16 
17 #include <android-base/logging.h>
18 #include <android-base/properties.h>
19 
20 namespace iorap::binder {
21 
Create()22 std::shared_ptr<PackageVersionMap> PackageVersionMap::Create() {
23   std::shared_ptr<PackageManagerRemote> package_manager =
24       PackageManagerRemote::Create();
25   if (!package_manager) {
26     return std::make_shared<PackageVersionMap>();
27   }
28 
29   std::optional<VersionMap> map = package_manager->GetPackageVersionMap();
30   return std::make_shared<PackageVersionMap>(package_manager, map);
31 }
32 
UpdateAll()33 void PackageVersionMap::UpdateAll() {std::lock_guard<std::mutex> lock(mutex_);
34   size_t old_size = version_map_->size();
35   std::optional<VersionMap> new_version_map =
36       package_manager_->GetPackageVersionMap();
37   if (!new_version_map) {
38     LOG(DEBUG) << "Failed to get the latest version map";
39     return;
40   }
41   version_map_ = std::move(new_version_map);
42   LOG(DEBUG) << "Update for version is done. The size is from " << old_size
43              << " to " << version_map_->size();
44 }
45 
Update(std::string package_name,int64_t version)46 bool PackageVersionMap::Update(std::string package_name, int64_t version) {
47   std::lock_guard<std::mutex> lock(mutex_);
48   if (!version_map_) {
49     LOG(DEBUG) << "The version map doesn't exist. "
50                << "The package manager may be down.";
51     return false;
52   }
53 
54   VersionMap::iterator it = version_map_->find(package_name);
55   if (it == version_map_->end()) {
56     LOG(DEBUG) << "New installed package "
57                << package_name
58                << " with version "
59                << version;
60     (*version_map_)[package_name] = version;
61     return true;
62   }
63 
64   if (it->second != version) {
65     LOG(DEBUG) << "New version package "
66                << package_name
67                << " with version "
68                << version;
69     (*version_map_)[package_name] = version;
70     return true;
71   }
72 
73   LOG(DEBUG) << "Same version package "
74              << package_name
75              << " with version "
76              << version;
77   return false;
78 }
79 
Size()80 size_t PackageVersionMap::Size() {
81   if (!version_map_) {
82     LOG(DEBUG) << "The version map doesn't exist. "
83                << "The package manager may be down.";
84     return -1;
85   }
86   return version_map_->size();
87 }
88 
GetOrQueryPackageVersion(const std::string & package_name)89 std::optional<int64_t> PackageVersionMap::GetOrQueryPackageVersion(
90     const std::string& package_name) {
91   std::lock_guard<std::mutex> lock(mutex_);
92   if (!version_map_) {
93     LOG(DEBUG) << "The version map doesn't exist. "
94                << "The package manager may be down.";
95     return std::nullopt;
96   }
97 
98   VersionMap::iterator it = version_map_->find(package_name);
99   if (it == version_map_->end()) {
100     LOG(DEBUG) << "Cannot find version for: " << package_name
101                  << " in the hash table";
102     std::optional<int64_t> version =
103         package_manager_->GetPackageVersion(package_name);
104     if (version) {
105       LOG(VERBOSE) << "Find version for: " << package_name << " on the fly.";
106       (*version_map_)[package_name] = *version;
107       return *version;
108     } else {
109       LOG(ERROR) << "Cannot find version for: " << package_name
110                  << " on the fly.";
111       return -1;
112     }
113   }
114 
115   return it->second;
116 }
117 }  // namespace iorap::binder
118