base::Value modernisation for chrome importers
This change removes the use of deprecated methods from base::Value on the importer code. A single spot has been left with an annotation, as it relies on upstream changes.
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "base/files/file_util.h"
|
||||
@@ -24,7 +25,7 @@
|
||||
namespace {
|
||||
|
||||
#if BUILDFLAG(ENABLE_EXTENSIONS)
|
||||
absl::optional<base::Value> GetChromeExtensionsList(
|
||||
absl::optional<base::Value::Dict> GetChromeExtensionsList(
|
||||
const base::FilePath& secured_preference_path) {
|
||||
if (!base::PathExists(secured_preference_path))
|
||||
return absl::nullopt;
|
||||
@@ -33,9 +34,12 @@ absl::optional<base::Value> GetChromeExtensionsList(
|
||||
base::ReadFileToString(secured_preference_path, &secured_preference_content);
|
||||
absl::optional<base::Value> secured_preference =
|
||||
base::JSONReader::Read(secured_preference_content);
|
||||
if (auto* extensions = secured_preference->FindPath(
|
||||
DCHECK(secured_preference);
|
||||
DCHECK(secured_preference->is_dict());
|
||||
|
||||
if (auto* extensions = secured_preference->GetDict().FindDictByDottedPath(
|
||||
kChromeExtensionsListPath)) {
|
||||
return extensions->Clone();
|
||||
return std::move(*extensions);
|
||||
}
|
||||
return absl::nullopt;
|
||||
}
|
||||
@@ -82,8 +86,8 @@ void BraveExternalProcessImporterHost::LaunchExtensionsImport() {
|
||||
}
|
||||
|
||||
void BraveExternalProcessImporterHost::OnGetChromeExtensionsList(
|
||||
absl::optional<base::Value> extensions_list) {
|
||||
if (!extensions_list || !extensions_list->is_dict()) {
|
||||
absl::optional<base::Value::Dict> extensions_list) {
|
||||
if (!extensions_list) {
|
||||
ExternalProcessImporterHost::NotifyImportEnded();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -30,7 +30,8 @@ class BraveExternalProcessImporterHost : public ExternalProcessImporterHost {
|
||||
|
||||
#if BUILDFLAG(ENABLE_EXTENSIONS)
|
||||
void LaunchExtensionsImport();
|
||||
void OnGetChromeExtensionsList(absl::optional<base::Value> extensions_list);
|
||||
void OnGetChromeExtensionsList(
|
||||
absl::optional<base::Value::Dict> extensions_list);
|
||||
#endif
|
||||
|
||||
// Vends weak pointers for the importer to call us back.
|
||||
|
||||
@@ -40,7 +40,10 @@ bool HasImportableExtensions(const base::FilePath& secured_preference_path) {
|
||||
base::ReadFileToString(secured_preference_path, &secured_preference_content);
|
||||
absl::optional<base::Value> secured_preference =
|
||||
base::JSONReader::Read(secured_preference_content);
|
||||
if (auto* extensions = secured_preference->FindPath(
|
||||
DCHECK(secured_preference);
|
||||
DCHECK(secured_preference->is_dict());
|
||||
|
||||
if (auto* extensions = secured_preference->GetDict().FindDictByDottedPath(
|
||||
kChromeExtensionsListPath)) {
|
||||
auto extensions_list =
|
||||
GetImportableListFromChromeExtensionsList(*extensions);
|
||||
@@ -145,27 +148,27 @@ bool ChromeImporterCanImport(const base::FilePath& profile,
|
||||
|
||||
#if BUILDFLAG(ENABLE_EXTENSIONS)
|
||||
std::vector<std::string> GetImportableListFromChromeExtensionsList(
|
||||
const base::Value& extensions_list) {
|
||||
DCHECK(extensions_list.is_dict());
|
||||
|
||||
const base::Value::Dict& extensions_list) {
|
||||
std::vector<std::string> extensions;
|
||||
for (const auto item : extensions_list.DictItems()) {
|
||||
for (const auto [key, value] : extensions_list) {
|
||||
DCHECK(value.is_dict());
|
||||
const base::Value::Dict& dict = value.GetDict();
|
||||
// Only import if type is extension, it's came from webstore and it's not
|
||||
// installed by default.
|
||||
if (item.second.FindBoolKey("was_installed_by_default").value_or(true))
|
||||
continue;
|
||||
|
||||
if (!item.second.FindBoolKey("from_webstore").value_or(false))
|
||||
if (dict.FindBool("was_installed_by_default").value_or(true))
|
||||
continue;
|
||||
|
||||
if (auto* manifest_value = item.second.FindDictKey("manifest")) {
|
||||
if (!manifest_value->is_dict())
|
||||
continue;
|
||||
if (!dict.FindBool("from_webstore").value_or(false))
|
||||
continue;
|
||||
|
||||
const auto& manifest = base::Value::AsDictionaryValue(*manifest_value);
|
||||
if (Manifest::GetTypeFromManifestValue(manifest) ==
|
||||
if (auto* manifest_dict = dict.FindDict("manifest")) {
|
||||
// TODO(cdesouza): Whenever Manifest::GetTypeFromManifestValue gets
|
||||
// refactored upstream to take a base::Value::Dict reference, also
|
||||
// remove the cloning done here to convert back to value.
|
||||
if (Manifest::GetTypeFromManifestValue(base::Value::AsDictionaryValue(
|
||||
base::Value(manifest_dict->Clone()))) ==
|
||||
Manifest::TYPE_EXTENSION) {
|
||||
extensions.push_back(item.first);
|
||||
extensions.push_back(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,13 +32,12 @@ base::FilePath GetOperaUserDataFolder();
|
||||
base::FilePath GetOperaSnapUserDataFolder();
|
||||
#endif
|
||||
base::Value::List GetChromeSourceProfiles(const base::FilePath& local_state);
|
||||
base::ListValue* GetOperaSourceProfiles(const base::FilePath& user_data_folder);
|
||||
bool ChromeImporterCanImport(const base::FilePath& profile,
|
||||
uint16_t* services_supported);
|
||||
|
||||
#if BUILDFLAG(ENABLE_EXTENSIONS)
|
||||
std::vector<std::string> GetImportableListFromChromeExtensionsList(
|
||||
const base::Value& extension_list);
|
||||
const base::Value::Dict& extension_list);
|
||||
#endif
|
||||
|
||||
#endif // BRAVE_COMMON_IMPORTER_CHROME_IMPORTER_UTILS_H_
|
||||
|
||||
@@ -38,7 +38,11 @@ TEST(ChromeImporterUtilsTest, BasicTest) {
|
||||
base::ReadFileToString(secured_preference_path, &secured_preference_content);
|
||||
absl::optional<base::Value> secured_preference =
|
||||
base::JSONReader::Read(secured_preference_content);
|
||||
auto* extensions = secured_preference->FindPath(kChromeExtensionsListPath);
|
||||
ASSERT_TRUE(secured_preference);
|
||||
ASSERT_TRUE(secured_preference->is_dict());
|
||||
auto* extensions = secured_preference->GetDict().FindDictByDottedPath(
|
||||
kChromeExtensionsListPath);
|
||||
ASSERT_TRUE(extensions);
|
||||
auto extensions_list =
|
||||
GetImportableListFromChromeExtensionsList(*extensions);
|
||||
// Only 2 extensions installed from webstore are importing target extensions.
|
||||
|
||||
Reference in New Issue
Block a user