blob: 35154d6a4e0a0bf56ff285c05cb73ba5308bb242 [file] [log] [blame]
Jae Hoon Kim38de3b12020-04-29 19:41:23 -07001//
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 Hassaniec7bc112020-10-29 16:47:58 -070017#include "update_engine/cros/excluder_chromeos.h"
Jae Hoon Kim38de3b12020-04-29 19:41:23 -070018
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 Hassaniec7bc112020-10-29 16:47:58 -070028#include "update_engine/common/system_state.h"
Jae Hoon Kim38de3b12020-04-29 19:41:23 -070029
30using std::string;
31using std::vector;
32
33namespace chromeos_update_engine {
34
Amin Hassani90e9f192020-11-18 14:20:56 -080035std::unique_ptr<ExcluderInterface> CreateExcluder() {
36 return std::make_unique<ExcluderChromeOS>();
Jae Hoon Kim38de3b12020-04-29 19:41:23 -070037}
38
Jae Hoon Kim38de3b12020-04-29 19:41:23 -070039bool ExcluderChromeOS::Exclude(const string& name) {
Amin Hassani90e9f192020-11-18 14:20:56 -080040 auto* prefs = SystemState::Get()->prefs();
41 auto key = prefs->CreateSubKey({kExclusionPrefsSubDir, name});
42 return prefs->SetString(key, "");
Jae Hoon Kim38de3b12020-04-29 19:41:23 -070043}
44
45bool ExcluderChromeOS::IsExcluded(const string& name) {
Amin Hassani90e9f192020-11-18 14:20:56 -080046 auto* prefs = SystemState::Get()->prefs();
47 auto key = prefs->CreateSubKey({kExclusionPrefsSubDir, name});
48 return prefs->Exists(key);
Jae Hoon Kim38de3b12020-04-29 19:41:23 -070049}
50
51bool ExcluderChromeOS::Reset() {
Amin Hassani90e9f192020-11-18 14:20:56 -080052 auto* prefs = SystemState::Get()->prefs();
Jae Hoon Kim38de3b12020-04-29 19:41:23 -070053 bool ret = true;
54 vector<string> keys;
Amin Hassani90e9f192020-11-18 14:20:56 -080055 if (!prefs->GetSubKeys(kExclusionPrefsSubDir, &keys))
Jae Hoon Kim38de3b12020-04-29 19:41:23 -070056 return false;
57 for (const auto& key : keys)
Amin Hassani90e9f192020-11-18 14:20:56 -080058 if (!(ret &= prefs->Delete(key)))
Jae Hoon Kim38de3b12020-04-29 19:41:23 -070059 LOG(ERROR) << "Failed to delete exclusion pref for " << key;
60 return ret;
61}
62
63} // namespace chromeos_update_engine