1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/supervised_user/chromeos/supervised_user_web_content_handler_impl.h"
#include "base/functional/bind.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_key.h"
#include "chrome/browser/supervised_user/chromeos/chromeos_utils.h"
#include "chrome/browser/supervised_user/chromeos/supervised_user_favicon_request_handler.h"
#include "chrome/browser/supervised_user/supervised_user_browser_utils.h"
#include "chrome/browser/supervised_user/supervised_user_settings_service_factory.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chromeos/crosapi/mojom/parent_access.mojom.h"
#include "components/favicon/core/large_icon_service.h"
#include "components/supervised_user/core/browser/supervised_user_settings_service.h"
#include "components/supervised_user/core/browser/supervised_user_utils.h"
#include "components/supervised_user/core/common/features.h"
#include "components/supervised_user/core/common/supervised_user_constants.h"
#include "content/public/browser/web_contents.h"
namespace {
supervised_user::LocalApprovalResult ChromeOSResultToLocalApprovalResult(
crosapi::mojom::ParentAccessResult::Tag result) {
switch (result) {
case crosapi::mojom::ParentAccessResult::Tag::kApproved:
return supervised_user::LocalApprovalResult::kApproved;
case crosapi::mojom::ParentAccessResult::Tag::kDeclined:
return supervised_user::LocalApprovalResult::kDeclined;
case crosapi::mojom::ParentAccessResult::Tag::kCanceled:
return supervised_user::LocalApprovalResult::kCanceled;
case crosapi::mojom::ParentAccessResult::Tag::kError:
return supervised_user::LocalApprovalResult::kError;
case crosapi::mojom::ParentAccessResult::Tag::kDisabled:
// Disabled is not a possible result for Local Web Approvals.
NOTREACHED();
}
}
void HandleChromeOSErrorResult(
crosapi::mojom::ParentAccessErrorResult::Type type) {
switch (type) {
case crosapi::mojom::ParentAccessErrorResult::Type::kNotAChildUser:
// Fatal debug error because this can only occur due to a programming
// error.
DLOG(FATAL) << "ParentAccess UI invoked by non-child user";
return;
case crosapi::mojom::ParentAccessErrorResult::Type::kAlreadyVisible:
// Fatal debug error because this can only occur due to a programming
// error.
DLOG(FATAL) << "ParentAccess UI invoked while instance already visible";
return;
case crosapi::mojom::ParentAccessErrorResult::Type::kUnknown:
LOG(ERROR) << "Unknown error in ParentAccess UI";
return;
case crosapi::mojom::ParentAccessErrorResult::Type::kNone:
NOTREACHED();
}
}
} // namespace
SupervisedUserWebContentHandlerImpl::SupervisedUserWebContentHandlerImpl(
content::WebContents* web_contents,
const GURL& url,
favicon::LargeIconService& large_icon_service,
content::FrameTreeNodeId frame_id,
int64_t interstitial_navigation_id)
: ChromeSupervisedUserWebContentHandlerBase(web_contents,
frame_id,
interstitial_navigation_id),
favicon_handler_(std::make_unique<SupervisedUserFaviconRequestHandler>(
url.GetWithEmptyPath(),
&large_icon_service)),
profile_(
*Profile::FromBrowserContext(web_contents->GetBrowserContext())) {
CHECK(web_contents_);
if (supervised_user::IsLocalWebApprovalsEnabled()) {
// Prefetch the favicon which will be rendered as part of the web approvals
// ParentAccessDialog. Pass in DoNothing() for the favicon fetched callback
// because if the favicon is by the time the user triggers the opening of
// the ParentAccessDialog, we show the default favicon.
favicon_handler_->StartFaviconFetch(base::DoNothing());
}
}
SupervisedUserWebContentHandlerImpl::~SupervisedUserWebContentHandlerImpl() =
default;
void SupervisedUserWebContentHandlerImpl::RequestLocalApproval(
const GURL& url,
const std::u16string& child_display_name,
const supervised_user::UrlFormatter& url_formatter,
const supervised_user::FilteringBehaviorReason& filtering_behavior_reason,
ApprovalRequestInitiatedCallback callback) {
CHECK(web_contents_);
supervised_user::SupervisedUserSettingsService* settings_service =
SupervisedUserSettingsServiceFactory::GetForKey(
Profile::FromBrowserContext(web_contents_->GetBrowserContext())
->GetProfileKey());
crosapi::mojom::ParentAccess* parent_access =
supervised_user::GetParentAccessApi();
CHECK(parent_access);
GURL target_url = url_formatter.FormatUrl(url);
// TODO(b/322484529): Standardize the url formatting for local approvals
// across platforms.
parent_access->GetWebsiteParentApproval(
target_url.GetWithEmptyPath(), child_display_name,
// Receiver does not need multi-resolution image. Pass single-resolution
// ImageSkia for compatibility with mojo interface.
gfx::ImageSkia::CreateFrom1xBitmap(
favicon_handler_->GetFaviconOrFallback()),
base::BindOnce(
&SupervisedUserWebContentHandlerImpl::OnLocalApprovalRequestCompleted,
weak_ptr_factory_.GetWeakPtr(), std::ref(*settings_service),
target_url, base::TimeTicks::Now()));
std::move(callback).Run(true);
}
void SupervisedUserWebContentHandlerImpl::OnLocalApprovalRequestCompleted(
supervised_user::SupervisedUserSettingsService& settings_service,
const GURL& url,
base::TimeTicks start_time,
crosapi::mojom::ParentAccessResultPtr result) {
WebContentHandler::OnLocalApprovalRequestCompleted(
settings_service, url, start_time,
ChromeOSResultToLocalApprovalResult(result->which()),
/*local_approval_error_type=*/std::nullopt);
if (result->is_error()) {
HandleChromeOSErrorResult(result->get_error()->type);
}
}
|