Commit a842c622 authored by Maggie Cai's avatar Maggie Cai Committed by Commit Bot

[IntentHandling] Convert preferred apps list to value format.

This CL adds the converter to convert the preferred apps list to
base::value format so that it can be store into disk in json format
easily.

BUG=853604

Change-Id: Ie1b104523015696c74a1bee3a343feefacc75744
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2139852Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Commit-Queue: Maggie Cai <mxcai@chromium.org>
Cr-Commit-Position: refs/heads/master@{#759156}
parent bca5e41e
...@@ -72,6 +72,8 @@ source_set("intents") { ...@@ -72,6 +72,8 @@ source_set("intents") {
source_set("preferred_apps") { source_set("preferred_apps") {
sources = [ sources = [
"preferred_apps_converter.cc",
"preferred_apps_converter.h",
"preferred_apps_list.cc", "preferred_apps_list.cc",
"preferred_apps_list.h", "preferred_apps_list.h",
] ]
...@@ -95,6 +97,7 @@ source_set("unit_tests") { ...@@ -95,6 +97,7 @@ source_set("unit_tests") {
"intent_test_util.cc", "intent_test_util.cc",
"intent_test_util.h", "intent_test_util.h",
"intent_util_unittest.cc", "intent_util_unittest.cc",
"preferred_apps_converter_unittest.cc",
"preferred_apps_list_unittest.cc", "preferred_apps_list_unittest.cc",
] ]
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include "chrome/services/app_service/public/cpp/preferred_apps_converter.h"
#include "chrome/services/app_service/public/mojom/types.mojom.h"
namespace {
base::Value ConvertConditionValueToValue(
const apps::mojom::ConditionValuePtr& condition_value) {
base::Value condition_value_dict(base::Value::Type::DICTIONARY);
condition_value_dict.SetStringKey(apps::kValueKey, condition_value->value);
condition_value_dict.SetIntKey(apps::kMatchTypeKey,
static_cast<int>(condition_value->match_type));
return condition_value_dict;
}
base::Value ConvertConditionToValue(
const apps::mojom::ConditionPtr& condition) {
base::Value condition_dict(base::Value::Type::DICTIONARY);
condition_dict.SetIntKey(apps::kConditionTypeKey,
static_cast<int>(condition->condition_type));
base::Value condition_values_list(base::Value::Type::LIST);
for (auto& condition_value : condition->condition_values) {
condition_values_list.Append(ConvertConditionValueToValue(condition_value));
}
condition_dict.SetKey(apps::kConditionValuesKey,
std::move(condition_values_list));
return condition_dict;
}
base::Value ConvertIntentFilterToValue(
const apps::mojom::IntentFilterPtr& intent_filter) {
base::Value intent_filter_value(base::Value::Type::LIST);
for (auto& condition : intent_filter->conditions) {
intent_filter_value.Append(ConvertConditionToValue(condition));
}
return intent_filter_value;
}
} // namespace
namespace apps {
const char kConditionTypeKey[] = "condition_type";
const char kConditionValuesKey[] = "condition_values";
const char kValueKey[] = "value";
const char kMatchTypeKey[] = "match_type";
const char kAppIdKey[] = "app_id";
const char kIntentFilterKey[] = "intent_filter";
base::Value ConvertPreferredAppsToValue(
const PreferredAppsList::PreferredApps& preferred_apps) {
base::Value preferred_apps_value(base::Value::Type::LIST);
for (auto& preferred_app : preferred_apps) {
base::Value preferred_app_dict(base::Value::Type::DICTIONARY);
preferred_app_dict.SetKey(
kIntentFilterKey,
ConvertIntentFilterToValue(preferred_app->intent_filter));
preferred_app_dict.SetStringKey(kAppIdKey, preferred_app->app_id);
preferred_apps_value.Append(std::move(preferred_app_dict));
}
return preferred_apps_value;
}
} // namespace apps
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_SERVICES_APP_SERVICE_PUBLIC_CPP_PREFERRED_APPS_CONVERTER_H_
#define CHROME_SERVICES_APP_SERVICE_PUBLIC_CPP_PREFERRED_APPS_CONVERTER_H_
#include "base/values.h"
#include "chrome/services/app_service/public/cpp/preferred_apps_list.h"
namespace apps {
extern const char kConditionTypeKey[];
extern const char kConditionValuesKey[];
extern const char kValueKey[];
extern const char kMatchTypeKey[];
extern const char kAppIdKey[];
extern const char kIntentFilterKey[];
// Convert the PreferredAppsList struct to base::Value to write to JSON file.
// e.g. for preferred app with |app_id| "abcdefg", and |intent_filter| for url
// https://www.google.com/abc.
// The converted base::Value format will be:
//[ {"app_id": "abcdefg",
// "intent_filter": [ {
// "condition_type": 0,
// "condition_values": [ {
// "match_type": 0,
// "value": "https"
// } ]
// }, {
// "condition_type": 1,
// "condition_values": [ {
// "match_type": 0,
// "value": "www.google.com"
// } ]
// }, {
// "condition_type": 2,
// "condition_values": [ {
// "match_type": 2,
// "value": "/abc"
// } ]
// } ]
// } ]
base::Value ConvertPreferredAppsToValue(
const PreferredAppsList::PreferredApps& preferred_apps);
} // namespace apps
#endif // CHROME_SERVICES_APP_SERVICE_PUBLIC_CPP_PREFERRED_APPS_CONVERTER_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/services/app_service/public/cpp/preferred_apps_converter.h"
#include "base/json/json_reader.h"
#include "chrome/services/app_service/public/cpp/intent_filter_util.h"
#include "chrome/services/app_service/public/cpp/intent_test_util.h"
#include "chrome/services/app_service/public/cpp/intent_util.h"
#include "chrome/services/app_service/public/cpp/preferred_apps_list.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
const char kAppId1[] = "abcdefg";
} // namespace
class PreferredAppsConverterTest : public testing::Test {
protected:
apps::PreferredAppsList preferred_apps_;
};
// Test one simple entry with simple filter.
TEST_F(PreferredAppsConverterTest, ConvertSimpleEntry) {
GURL filter_url = GURL("https://www.google.com/abc");
auto intent_filter = apps_util::CreateIntentFilterForUrlScope(filter_url);
preferred_apps_.AddPreferredApp(kAppId1, intent_filter);
auto converted_value =
apps::ConvertPreferredAppsToValue(preferred_apps_.GetReference());
// Check that each entry is correct.
ASSERT_EQ(1u, converted_value.GetList().size());
auto& entry = converted_value.GetList()[0];
EXPECT_EQ(kAppId1, *entry.FindStringKey(apps::kAppIdKey));
auto* converted_intent_filter = entry.FindKey(apps::kIntentFilterKey);
ASSERT_EQ(intent_filter->conditions.size(),
converted_intent_filter->GetList().size());
for (size_t i = 0; i < intent_filter->conditions.size(); i++) {
auto& condition = intent_filter->conditions[i];
auto& converted_condition = converted_intent_filter->GetList()[i];
auto& condition_values = condition->condition_values;
auto converted_condition_values =
converted_condition.FindKey(apps::kConditionValuesKey)->GetList();
EXPECT_EQ(static_cast<int>(condition->condition_type),
converted_condition.FindIntKey(apps::kConditionTypeKey));
ASSERT_EQ(1u, converted_condition_values.size());
EXPECT_EQ(condition_values[0]->value,
*converted_condition_values[0].FindStringKey(apps::kValueKey));
EXPECT_EQ(static_cast<int>(condition_values[0]->match_type),
converted_condition_values[0].FindIntKey(apps::kMatchTypeKey));
}
}
// Test one simple entry with json string.
TEST_F(PreferredAppsConverterTest, ConvertSimpleEntryJson) {
GURL filter_url = GURL("https://www.google.com/abc");
auto intent_filter = apps_util::CreateIntentFilterForUrlScope(filter_url);
preferred_apps_.AddPreferredApp(kAppId1, intent_filter);
auto converted_value =
apps::ConvertPreferredAppsToValue(preferred_apps_.GetReference());
const char expected_output_string[] =
"[ {\"app_id\": \"abcdefg\","
" \"intent_filter\": [ {"
" \"condition_type\": 0,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"https\""
" } ]"
" }, {"
" \"condition_type\": 1,"
" \"condition_values\": [ {"
" \"match_type\": 0,"
" \"value\": \"www.google.com\""
" } ]"
" }, {"
" \"condition_type\": 2,"
" \"condition_values\": [ {"
" \"match_type\": 2,"
" \"value\": \"/abc\""
" } ]"
" } ]"
"} ]";
base::Optional<base::Value> expected_output =
base::JSONReader::Read(expected_output_string);
ASSERT_TRUE(expected_output);
EXPECT_EQ(expected_output.value(), converted_value);
}
...@@ -155,4 +155,8 @@ bool PreferredAppsList::IsInitialized() { ...@@ -155,4 +155,8 @@ bool PreferredAppsList::IsInitialized() {
return initialized_; return initialized_;
} }
const PreferredAppsList::PreferredApps& PreferredAppsList::GetReference() {
return preferred_apps_;
}
} // namespace apps } // namespace apps
...@@ -77,6 +77,8 @@ class PreferredAppsList { ...@@ -77,6 +77,8 @@ class PreferredAppsList {
bool IsInitialized(); bool IsInitialized();
const PreferredApps& GetReference();
private: private:
PreferredApps preferred_apps_; PreferredApps preferred_apps_;
bool initialized_ = false; bool initialized_ = false;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment