Commit 3a492656 authored by Ovidio Henriquez's avatar Ovidio Henriquez Committed by Commit Bot

Restructure chooser object for site_settings

This change implements two new helper methods for site settings that
organize the chooser exceptions in a way that allows their corresponding
site exceptions to be grouped under the same exception. The structure of
the objects returned by GetChooserExceptionListFromProfile(), described
in jsdoc notation, is the following:

{
  chooserType: string,
  displayName: string,
  object: Object,
  sites: Array<!SiteException>
}

The objects in the sites array follow the same structure as the
non-chooser exceptions.

Design doc:
https://docs.google.com/document/d/1MPvsrWiVD_jAC8ELyk8njFpy6j1thfVU5aWT3TCWE8w

Bug: 854329
Change-Id: I92a654da2ef9d1d0f045df0a35eaf70a1940b44e
Reviewed-on: https://chromium-review.googlesource.com/c/1357605
Commit-Queue: Ovidio Henriquez <odejesush@chromium.org>
Reviewed-by: default avatarDan Beam <dbeam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#613280}
parent 3e525545
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include <algorithm> #include <algorithm>
#include <functional> #include <functional>
#include <set>
#include <string>
#include "base/feature_list.h" #include "base/feature_list.h"
#include "base/values.h" #include "base/values.h"
...@@ -31,7 +33,6 @@ namespace site_settings { ...@@ -31,7 +33,6 @@ namespace site_settings {
constexpr char kAppName[] = "appName"; constexpr char kAppName[] = "appName";
constexpr char kAppId[] = "appId"; constexpr char kAppId[] = "appId";
constexpr char kObject[] = "object";
constexpr char kObjectName[] = "objectName"; constexpr char kObjectName[] = "objectName";
namespace { namespace {
...@@ -669,4 +670,104 @@ void GetChooserExceptionsFromProfile(Profile* profile, ...@@ -669,4 +670,104 @@ void GetChooserExceptionsFromProfile(Profile* profile,
} }
} }
// Create a DictionaryValue* that will act as a data source for a single row
// in a chooser permission exceptions table. The chooser permission will contain
// a list of site exceptions that correspond to the exception.
std::unique_ptr<base::DictionaryValue> CreateChooserExceptionObject(
const std::string& display_name,
const base::Value& object,
const std::string& chooser_type,
const ChooserExceptionDetails& chooser_exception_details,
bool incognito) {
auto exception = std::make_unique<base::DictionaryValue>();
std::string setting_string =
content_settings::ContentSettingToString(CONTENT_SETTING_DEFAULT);
DCHECK(!setting_string.empty());
exception->SetString(kDisplayName, display_name);
exception->SetKey(kObject, object.Clone());
exception->SetString(kChooserType, chooser_type);
// Order the sites by the provider precedence order.
std::vector<std::unique_ptr<base::Value>>
all_provider_sites[HostContentSettingsMap::NUM_PROVIDER_TYPES];
for (const auto& details : chooser_exception_details) {
const GURL& requesting_origin = details.first.first;
const std::string& source = details.first.second;
auto& this_provider_sites =
all_provider_sites[HostContentSettingsMap::GetProviderTypeFromSource(
source)];
for (const GURL& embedding_origin : details.second) {
auto site = std::make_unique<base::DictionaryValue>();
site->SetString(kOrigin, requesting_origin.spec());
site->SetString(kDisplayName, requesting_origin.spec());
site->SetString(kEmbeddingOrigin, embedding_origin.is_empty()
? std::string()
: embedding_origin.spec());
site->SetString(kSetting, setting_string);
site->SetString(kSource, source);
site->SetBoolean(kIncognito, incognito);
this_provider_sites.push_back(std::move(site));
}
}
auto sites = std::make_unique<base::ListValue>();
for (auto& one_provider_sites : all_provider_sites) {
for (auto& site : one_provider_sites) {
sites->Append(std::move(site));
}
}
exception->SetList(kSites, std::move(sites));
return exception;
}
std::unique_ptr<base::ListValue> GetChooserExceptionListFromProfile(
Profile* profile,
bool incognito,
const ChooserTypeNameEntry& chooser_type) {
auto exceptions = std::make_unique<base::ListValue>();
if (incognito) {
if (!profile->HasOffTheRecordProfile())
return exceptions;
profile = profile->GetOffTheRecordProfile();
}
ChooserContextBase* chooser_context = chooser_type.get_context(profile);
std::vector<std::unique_ptr<ChooserContextBase::Object>> objects =
chooser_context->GetAllGrantedObjects();
AllChooserObjects all_chooser_objects;
for (const auto& object : objects) {
if (object->incognito == incognito) {
std::string name = chooser_context->GetObjectName(object->object);
auto& chooser_exception_details =
all_chooser_objects[std::make_pair(name, object->object.Clone())];
const auto requesting_origin_source_pair =
std::make_pair(object->requesting_origin, object->source);
auto& embedding_origin_set =
chooser_exception_details[requesting_origin_source_pair];
embedding_origin_set.insert(object->embedding_origin);
}
}
for (const auto& all_chooser_objects_entry : all_chooser_objects) {
const std::string& name = all_chooser_objects_entry.first.first;
const base::Value& object = all_chooser_objects_entry.first.second;
const ChooserExceptionDetails& chooser_exception_details =
all_chooser_objects_entry.second;
exceptions->Append(CreateChooserExceptionObject(
name, object, chooser_type.name, chooser_exception_details, incognito));
}
return exceptions;
}
} // namespace site_settings } // namespace site_settings
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <map> #include <map>
#include <memory> #include <memory>
#include <set>
#include <string> #include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
...@@ -40,12 +41,29 @@ typedef std::map<std::pair<ContentSettingsPattern, std::string>, ...@@ -40,12 +41,29 @@ typedef std::map<std::pair<ContentSettingsPattern, std::string>,
OnePatternSettings> OnePatternSettings>
AllPatternsSettings; AllPatternsSettings;
// TODO(https://crbug.com/854329): Once the Site Settings WebUI is capable of
// displaying the new chooser exception object format, remove the typedefs that
// are currently used for organizing the chooser exceptions.
// Maps from a primary URL pattern/source pair to a set of secondary URL
// patterns.
using ChooserExceptionDetails =
std::map<std::pair<GURL, std::string>, std::set<GURL>>;
// Maps from a chooser exception name/object pair to a ChooserExceptionDetails.
// This will group and sort the exceptions by the UI string and object for
// display.
using AllChooserObjects =
std::map<std::pair<std::string, base::Value>, ChooserExceptionDetails>;
constexpr char kChooserType[] = "chooserType";
constexpr char kDisplayName[] = "displayName"; constexpr char kDisplayName[] = "displayName";
constexpr char kEmbeddingOrigin[] = "embeddingOrigin"; constexpr char kEmbeddingOrigin[] = "embeddingOrigin";
constexpr char kIncognito[] = "incognito"; constexpr char kIncognito[] = "incognito";
constexpr char kObject[] = "object";
constexpr char kOrigin[] = "origin"; constexpr char kOrigin[] = "origin";
constexpr char kOriginForFavicon[] = "originForFavicon"; constexpr char kOriginForFavicon[] = "originForFavicon";
constexpr char kSetting[] = "setting"; constexpr char kSetting[] = "setting";
constexpr char kSites[] = "sites";
constexpr char kSource[] = "source"; constexpr char kSource[] = "source";
// Group types. // Group types.
...@@ -150,6 +168,31 @@ void GetChooserExceptionsFromProfile(Profile* profile, ...@@ -150,6 +168,31 @@ void GetChooserExceptionsFromProfile(Profile* profile,
const ChooserTypeNameEntry& chooser_type, const ChooserTypeNameEntry& chooser_type,
base::ListValue* exceptions); base::ListValue* exceptions);
// TODO(https://crbug.com/854329): Once the Site Settings WebUI is capable of
// displaying the new chooser exception object format, replace the existing
// chooser exception methods with these methods.
// Creates a chooser exception object for the object with |display_name|. The
// object contains the following properties
// * displayName: string,
// * object: Object,
// * chooserType: string,
// * sites: Array<SiteException>
// The structure of the SiteException objects is the same as the objects
// returned by GetExceptionForPage().
std::unique_ptr<base::DictionaryValue> CreateChooserExceptionObject(
const std::string& display_name,
const base::Value& object,
const std::string& chooser_type,
const ChooserExceptionDetails& chooser_exception_details,
bool incognito);
// Returns an array of chooser exception objects.
std::unique_ptr<base::ListValue> GetChooserExceptionListFromProfile(
Profile* profile,
bool incognito,
const ChooserTypeNameEntry& chooser_type);
} // namespace site_settings } // namespace site_settings
#endif // CHROME_BROWSER_UI_WEBUI_SITE_SETTINGS_HELPER_H_ #endif // CHROME_BROWSER_UI_WEBUI_SITE_SETTINGS_HELPER_H_
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