Commit 1de9c499 authored by Mugdha Lakhani's avatar Mugdha Lakhani Committed by Commit Bot

[WebLayer] Part 2: Refactor PageInfo.

Currently, PageInfo embeds function pointers in a ChooserUIInfo struct
that map from a Profile to the appropriate ChooserContextBase instance
that is associated with the Profile. However, this organization blocks
componentization of PageInfo.

This CL instead has PageInfo obtain the ChooserContextBase instance to
use for a given ChooserUiInfo via the delegate; the Chrome delegate
impl has access to the Profile and can key off the ContentSettingsType
param in the ChooserUiInfo object to determine the appropriate
ChooserContextBase instance to fetch. External clients of the prior
ChooserUIInfo functionality all have access to a PageInfo instance,
hence, they are ported to this model as well via PageInfo exposing the
necessary API publicly.

Bug: 1052375
Change-Id: Ic6750e5504fb7b2ea23dc6b3cff64542d44b6084
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2111171
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Reviewed-by: default avatarColin Blundell <blundell@chromium.org>
Cr-Commit-Position: refs/heads/master@{#752490}
parent 07086961
......@@ -171,9 +171,9 @@ void PageInfoControllerAndroid::SetPermissionInfo(
}
for (const auto& chosen_object : chosen_object_info_list) {
base::string16 object_title = PageInfoUI::ChosenObjectToUIString(
*chosen_object,
Profile::FromBrowserContext(web_contents_->GetBrowserContext()));
base::string16 object_title =
presenter_->GetChooserContextFromUIInfo(chosen_object->ui_info)
->GetObjectDisplayName(chosen_object->chooser_object->value);
Java_PageInfoController_addPermissionSection(
env, controller_jobject_, ConvertUTF16ToJavaString(env, object_title),
......
......@@ -5,10 +5,15 @@
#include "chrome/browser/ui/page_info/chrome_page_info_delegate.h"
#include "build/build_config.h"
#include "chrome/browser/bluetooth/bluetooth_chooser_context.h"
#include "chrome/browser/bluetooth/bluetooth_chooser_context_factory.h"
#include "chrome/browser/content_settings/tab_specific_content_settings.h"
#include "chrome/browser/permissions/permission_manager_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/safe_browsing/chrome_password_protection_service.h"
#include "chrome/browser/usb/usb_chooser_context.h"
#include "chrome/browser/usb/usb_chooser_context_factory.h"
#include "components/permissions/chooser_context_base.h"
#include "components/permissions/permission_manager.h"
#include "components/permissions/permission_result.h"
#include "content/public/browser/web_contents.h"
......@@ -17,6 +22,11 @@
#include "chrome/browser/safe_browsing/chrome_password_protection_service.h"
#endif
#if !defined(OS_ANDROID)
#include "chrome/browser/serial/serial_chooser_context.h"
#include "chrome/browser/serial/serial_chooser_context_factory.h"
#endif
ChromePageInfoDelegate::ChromePageInfoDelegate(
content::WebContents* web_contents)
: web_contents_(web_contents) {}
......@@ -25,6 +35,26 @@ Profile* ChromePageInfoDelegate::GetProfile() const {
return Profile::FromBrowserContext(web_contents_->GetBrowserContext());
}
permissions::ChooserContextBase* ChromePageInfoDelegate::GetChooserContext(
ContentSettingsType type) {
switch (type) {
case ContentSettingsType::USB_CHOOSER_DATA:
return UsbChooserContextFactory::GetForProfile(GetProfile());
case ContentSettingsType::BLUETOOTH_CHOOSER_DATA:
return BluetoothChooserContextFactory::GetForProfile(GetProfile());
case ContentSettingsType::SERIAL_CHOOSER_DATA:
#if !defined(OS_ANDROID)
return SerialChooserContextFactory::GetForProfile(GetProfile());
#else
NOTREACHED();
return nullptr;
#endif
default:
NOTREACHED();
return nullptr;
}
}
bool ChromePageInfoDelegate::HasContentSettingChangedViaPageInfo(
ContentSettingsType type) {
return tab_specific_content_settings()->HasContentSettingChangedViaPageInfo(
......
......@@ -9,12 +9,13 @@
#include "chrome/browser/content_settings/local_shared_objects_container.h"
#include "chrome/browser/content_settings/tab_specific_content_settings.h"
#include "chrome/browser/ui/page_info/page_info_delegate.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_user_data.h"
#include "url/gurl.h"
namespace content {
class WebContents;
} // namespace content
namespace permissions {
class ChooserContextBase;
}
namespace safe_browsing {
class PasswordProtectionService;
......@@ -29,6 +30,8 @@ class ChromePageInfoDelegate : public PageInfoDelegate {
~ChromePageInfoDelegate() override = default;
// PageInfoDelegate implementation
permissions::ChooserContextBase* GetChooserContext(
ContentSettingsType type) override;
bool HasContentSettingChangedViaPageInfo(ContentSettingsType type) override;
int GetFirstPartyAllowedCookiesCount(const GURL& site_url) override;
int GetFirstPartyBlockedCookiesCount(const GURL& site_url) override;
......
......@@ -23,8 +23,6 @@
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/bluetooth/bluetooth_chooser_context.h"
#include "chrome/browser/bluetooth/bluetooth_chooser_context_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browsing_data/browsing_data_cookie_helper.h"
#include "chrome/browser/browsing_data/browsing_data_database_helper.h"
......@@ -39,8 +37,6 @@
#include "chrome/browser/ssl/stateful_ssl_host_state_delegate_factory.h"
#include "chrome/browser/ui/page_info/page_info_delegate.h"
#include "chrome/browser/ui/page_info/page_info_ui.h"
#include "chrome/browser/usb/usb_chooser_context.h"
#include "chrome/browser/usb/usb_chooser_context_factory.h"
#include "chrome/browser/vr/vr_tab_helper.h"
#include "chrome/common/chrome_features.h"
#include "chrome/common/chrome_switches.h"
......@@ -92,8 +88,6 @@
#endif
#if !defined(OS_ANDROID)
#include "chrome/browser/serial/serial_chooser_context.h"
#include "chrome/browser/serial/serial_chooser_context_factory.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/exclusive_access/exclusive_access_manager.h"
......@@ -301,39 +295,21 @@ base::string16 GetSimpleSiteName(const GURL& url) {
url, url_formatter::SchemeDisplay::OMIT_HTTP_AND_HTTPS);
}
permissions::ChooserContextBase* GetUsbChooserContext(Profile* profile) {
return UsbChooserContextFactory::GetForProfile(profile);
}
#if !defined(OS_ANDROID)
permissions::ChooserContextBase* GetSerialChooserContext(Profile* profile) {
return SerialChooserContextFactory::GetForProfile(profile);
}
#endif
permissions::ChooserContextBase* GetBluetoothChooserContext(Profile* profile) {
if (base::FeatureList::IsEnabled(
features::kWebBluetoothNewPermissionsBackend)) {
return BluetoothChooserContextFactory::GetForProfile(profile);
}
return nullptr;
}
// The list of chooser types that need to display entries in the Website
// Settings UI. THE ORDER OF THESE ITEMS IS IMPORTANT. To propose changing it,
// email security-dev@chromium.org.
const PageInfo::ChooserUIInfo kChooserUIInfo[] = {
{ContentSettingsType::USB_CHOOSER_DATA, &GetUsbChooserContext,
{ContentSettingsType::USB_CHOOSER_DATA,
IDS_PAGE_INFO_USB_DEVICE_SECONDARY_LABEL,
IDS_PAGE_INFO_USB_DEVICE_ALLOWED_BY_POLICY_LABEL,
IDS_PAGE_INFO_DELETE_USB_DEVICE},
#if !defined(OS_ANDROID)
{ContentSettingsType::SERIAL_CHOOSER_DATA, &GetSerialChooserContext,
{ContentSettingsType::SERIAL_CHOOSER_DATA,
IDS_PAGE_INFO_SERIAL_PORT_SECONDARY_LABEL,
/*allowed_by_policy_description_string_id=*/-1,
IDS_PAGE_INFO_DELETE_SERIAL_PORT},
#endif
{ContentSettingsType::BLUETOOTH_CHOOSER_DATA, &GetBluetoothChooserContext,
{ContentSettingsType::BLUETOOTH_CHOOSER_DATA,
IDS_PAGE_INFO_BLUETOOTH_DEVICE_SECONDARY_LABEL,
/*allowed_by_policy_description_string_id=*/-1,
IDS_PAGE_INFO_DELETE_BLUETOOTH_DEVICE},
......@@ -594,7 +570,8 @@ void PageInfo::OnSitePermissionChanged(ContentSettingsType type,
void PageInfo::OnSiteChosenObjectDeleted(const ChooserUIInfo& ui_info,
const base::Value& object) {
// TODO(reillyg): Create metrics for revocations. crbug.com/556845
permissions::ChooserContextBase* context = ui_info.get_context(profile_);
permissions::ChooserContextBase* context =
delegate_->GetChooserContext(ui_info.content_settings_type);
const auto origin = url::Origin::Create(site_url_);
context->RevokeObjectPermission(origin, origin, object);
show_info_bar_ = true;
......@@ -668,6 +645,11 @@ void PageInfo::OnWhitelistPasswordReuseButtonPressed(
#endif
}
permissions::ChooserContextBase* PageInfo::GetChooserContextFromUIInfo(
const ChooserUIInfo& ui_info) const {
return delegate_->GetChooserContext(ui_info.content_settings_type);
}
void PageInfo::ComputeUIInputs(
const GURL& url,
security_state::SecurityLevel security_level,
......@@ -1006,7 +988,8 @@ void PageInfo::PresentSitePermissions() {
const auto origin = url::Origin::Create(site_url_);
for (const ChooserUIInfo& ui_info : kChooserUIInfo) {
permissions::ChooserContextBase* context = ui_info.get_context(profile_);
permissions::ChooserContextBase* context =
delegate_->GetChooserContext(ui_info.content_settings_type);
if (!context)
continue;
auto chosen_objects = context->GetGrantedObjects(origin, origin);
......
......@@ -137,7 +137,6 @@ class PageInfo : public content::WebContentsObserver {
struct ChooserUIInfo {
ContentSettingsType content_settings_type;
permissions::ChooserContextBase* (*get_context)(Profile*);
int description_string_id;
int allowed_by_policy_description_string_id;
int delete_tooltip_string_id;
......@@ -196,6 +195,15 @@ class PageInfo : public content::WebContentsObserver {
void OnWhitelistPasswordReuseButtonPressed(
content::WebContents* web_contents);
// Return the object name of the ChooserContextBase corresponding to the
// content settings type, |type|. Returns an empty string for content settings
// for which there's no ChooserContextBase.
base::string16 GetChooserContextObjectName(ContentSettingsType type,
const base::Value& object);
permissions::ChooserContextBase* GetChooserContextFromUIInfo(
const ChooserUIInfo& ui_info) const;
// Accessors.
const SiteConnectionStatus& site_connection_status() const {
return site_connection_status_;
......
......@@ -6,6 +6,8 @@
#define CHROME_BROWSER_UI_PAGE_INFO_PAGE_INFO_DELEGATE_H_
#include "base/strings/string16.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/permissions/permission_result.h"
#include "components/safe_browsing/buildflags.h"
......@@ -16,10 +18,20 @@ class PasswordProtectionService;
} // namespace safe_browsing
// PageInfoDelegate allows an embedder to customize PageInfo logic.
namespace permissions {
class ChooserContextBase;
}
class PageInfoDelegate {
public:
virtual ~PageInfoDelegate() = default;
// Return the |ChooserContextBase| corresponding to the content settings
// type, |type|. Returns a nullptr for content settings for which there's no
// ChooserContextBase.
virtual permissions::ChooserContextBase* GetChooserContext(
ContentSettingsType type) = 0;
// Whether the content setting of type |type| has changed via Page Info UI.
virtual bool HasContentSettingChangedViaPageInfo(
ContentSettingsType type) = 0;
......
......@@ -12,6 +12,7 @@
#include "base/stl_util.h"
#include "base/strings/utf_string_conversions.h"
#include "build/build_config.h"
#include "chrome/browser/ui/page_info/page_info_delegate.h"
#include "chrome/browser/ui/page_info/page_info_ui_delegate.h"
#include "chrome/grit/generated_resources.h"
#include "components/permissions/permission_manager.h"
......@@ -486,16 +487,6 @@ SkColor PageInfoUI::GetSecondaryTextColor() {
return SK_ColorGRAY;
}
// static
base::string16 PageInfoUI::ChosenObjectToUIString(
const ChosenObjectInfo& object,
Profile* profile) {
permissions::ChooserContextBase* context =
object.ui_info.get_context(profile);
DCHECK(context);
return context->GetObjectDisplayName(object.chooser_object->value);
}
#if defined(OS_ANDROID)
// static
int PageInfoUI::GetIdentityIconID(PageInfo::SiteIdentityStatus status) {
......
......@@ -195,10 +195,6 @@ class PageInfoUI {
// Returns the color to use for the permission decision reason strings.
static SkColor GetSecondaryTextColor();
// Returns the UI string describing the given object |info|.
static base::string16 ChosenObjectToUIString(const ChosenObjectInfo& info,
Profile* profile);
#if defined(OS_ANDROID)
// Returns the identity icon ID for the given identity |status|.
static int GetIdentityIconID(PageInfo::SiteIdentityStatus status);
......
......@@ -7,6 +7,7 @@
#include <memory>
#include <utility>
#include "chrome/browser/ui/page_info/page_info_delegate.h"
#include "chrome/browser/ui/views/chrome_layout_provider.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/browser/ui/views/page_info/chosen_object_view_observer.h"
......@@ -24,7 +25,7 @@
ChosenObjectView::ChosenObjectView(
std::unique_ptr<PageInfoUI::ChosenObjectInfo> info,
Profile* profile)
base::string16 display_name)
: info_(std::move(info)) {
// |ChosenObjectView| layout (fills parent):
// *------------------------------------*
......@@ -69,9 +70,8 @@ ChosenObjectView::ChosenObjectView(
icon_ = layout->AddView(std::make_unique<views::ImageView>());
// Create the label that displays the chosen object name.
auto label = std::make_unique<views::Label>(
PageInfoUI::ChosenObjectToUIString(*info_, profile),
CONTEXT_BODY_TEXT_LARGE);
auto label =
std::make_unique<views::Label>(display_name, CONTEXT_BODY_TEXT_LARGE);
icon_->SetImage(
PageInfoUI::GetChosenObjectIcon(*info_, false, label->GetEnabledColor()));
layout->AddView(std::move(label));
......
......@@ -6,6 +6,7 @@
#define CHROME_BROWSER_UI_VIEWS_PAGE_INFO_CHOSEN_OBJECT_VIEW_H_
#include "base/macros.h"
#include "base/strings/string16.h"
#include "chrome/browser/ui/page_info/page_info_ui.h"
#include "ui/views/controls/button/button.h"
#include "ui/views/view.h"
......@@ -16,15 +17,14 @@ class ImageView;
} // namespace views
class ChosenObjectViewObserver;
class Profile;
// A ChosenObjectView is a row in the Page Info bubble that shows an individual
// object (e.g. a Bluetooth device, a USB device) that the current site has
// access to.
class ChosenObjectView : public views::View, public views::ButtonListener {
public:
ChosenObjectView(std::unique_ptr<PageInfoUI::ChosenObjectInfo> info,
Profile* profile);
explicit ChosenObjectView(std::unique_ptr<PageInfoUI::ChosenObjectInfo> info,
base::string16 display_name);
~ChosenObjectView() override;
void AddObserver(ChosenObjectViewObserver* observer);
......
......@@ -28,7 +28,6 @@
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/page_info/chrome_page_info_delegate.h"
#include "chrome/browser/ui/page_info/page_info.h"
#include "chrome/browser/ui/page_info/page_info_delegate.h"
#include "chrome/browser/ui/page_info/page_info_dialog.h"
#include "chrome/browser/ui/ui_features.h"
#include "chrome/browser/ui/view_ids.h"
......@@ -729,8 +728,10 @@ void PageInfoBubbleView::SetPermissionInfo(
layout->StartRow(1.0, kChosenObjectSectionId,
min_height_for_permission_rows + list_item_padding);
// The view takes ownership of the object info.
auto object_view =
std::make_unique<ChosenObjectView>(std::move(object), profile_);
auto object_view = std::make_unique<ChosenObjectView>(
std::move(object),
presenter_->GetChooserContextFromUIInfo(object->ui_info)
->GetObjectDisplayName(object->chooser_object->value));
object_view->AddObserver(this);
layout->AddView(std::move(object_view));
}
......
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