Jae Hoon Kim | 38de3b1 | 2020-04-29 19:41:23 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2020 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 | |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #include "update_engine/cros/excluder_chromeos.h" |
Jae Hoon Kim | 38de3b1 | 2020-04-29 19:41:23 -0700 | [diff] [blame] | 18 | |
| 19 | #include <memory> |
| 20 | #include <vector> |
| 21 | |
| 22 | #include <base/logging.h> |
| 23 | #include <base/strings/string_piece.h> |
| 24 | #include <base/strings/string_number_conversions.h> |
| 25 | #include <base/strings/string_split.h> |
| 26 | |
| 27 | #include "update_engine/common/constants.h" |
Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 28 | #include "update_engine/common/system_state.h" |
Jae Hoon Kim | 38de3b1 | 2020-04-29 19:41:23 -0700 | [diff] [blame] | 29 | |
| 30 | using std::string; |
| 31 | using std::vector; |
| 32 | |
| 33 | namespace chromeos_update_engine { |
| 34 | |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 35 | std::unique_ptr<ExcluderInterface> CreateExcluder() { |
| 36 | return std::make_unique<ExcluderChromeOS>(); |
Jae Hoon Kim | 38de3b1 | 2020-04-29 19:41:23 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Jae Hoon Kim | 38de3b1 | 2020-04-29 19:41:23 -0700 | [diff] [blame] | 39 | bool ExcluderChromeOS::Exclude(const string& name) { |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 40 | auto* prefs = SystemState::Get()->prefs(); |
| 41 | auto key = prefs->CreateSubKey({kExclusionPrefsSubDir, name}); |
| 42 | return prefs->SetString(key, ""); |
Jae Hoon Kim | 38de3b1 | 2020-04-29 19:41:23 -0700 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | bool ExcluderChromeOS::IsExcluded(const string& name) { |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 46 | auto* prefs = SystemState::Get()->prefs(); |
| 47 | auto key = prefs->CreateSubKey({kExclusionPrefsSubDir, name}); |
| 48 | return prefs->Exists(key); |
Jae Hoon Kim | 38de3b1 | 2020-04-29 19:41:23 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | bool ExcluderChromeOS::Reset() { |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 52 | auto* prefs = SystemState::Get()->prefs(); |
Jae Hoon Kim | 38de3b1 | 2020-04-29 19:41:23 -0700 | [diff] [blame] | 53 | bool ret = true; |
| 54 | vector<string> keys; |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 55 | if (!prefs->GetSubKeys(kExclusionPrefsSubDir, &keys)) |
Jae Hoon Kim | 38de3b1 | 2020-04-29 19:41:23 -0700 | [diff] [blame] | 56 | return false; |
| 57 | for (const auto& key : keys) |
Amin Hassani | 90e9f19 | 2020-11-18 14:20:56 -0800 | [diff] [blame] | 58 | if (!(ret &= prefs->Delete(key))) |
Jae Hoon Kim | 38de3b1 | 2020-04-29 19:41:23 -0700 | [diff] [blame] | 59 | LOG(ERROR) << "Failed to delete exclusion pref for " << key; |
| 60 | return ret; |
| 61 | } |
| 62 | |
| 63 | } // namespace chromeos_update_engine |