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
|
// Copyright 2020 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/chromeos/app_mode/kiosk_settings_navigation_throttle.h"
#include <memory>
#include <string>
#include <vector>
#include "base/check_is_test.h"
#include "base/no_destructor.h"
#include "base/strings/string_util.h"
#include "chrome/browser/app_mode/app_mode_utils.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_handle.h"
#include "content/public/browser/navigation_throttle.h"
#include "content/public/browser/web_contents.h"
namespace chromeos {
namespace {
// This list is used in tests to override `DefaultSettingsPages`.
const std::vector<KioskSettingsNavigationThrottle::SettingsPage>*
g_test_settings_pages = nullptr;
// `WebContents` that are marked with this UserData key should be restricted.
const void* const kRestrictedSettingsWindowKey = &kRestrictedSettingsWindowKey;
bool UrlMatchesSettingsPage(
const KioskSettingsNavigationThrottle::SettingsPage& page,
const std::string& url) {
return (page.allow_subpages &&
base::StartsWith(url,
page.url ? std::string(page.url) : std::string(),
base::CompareCase::SENSITIVE)) ||
(!page.allow_subpages && url == page.url);
}
} // namespace
// static
const std::vector<KioskSettingsNavigationThrottle::SettingsPage>&
KioskSettingsNavigationThrottle::DefaultSettingsPages() {
static base::NoDestructor<std::vector<SettingsPage>> settings_pages{
{SettingsPage{"chrome://os-settings/manageAccessibility", true},
SettingsPage{
"chrome-extension://mndnfokpggljbaajbnioimlmbfngpief/chromevox/"
"options/options.html",
false},
SettingsPage{"chrome-extension://klbcgckkldhdhonijdbnhhaiedfkllef/",
true},
SettingsPage{"chrome-extension://gjjabgpgjpampikjhjpfhneeoapjbjaf/",
true},
SettingsPage{"chrome-extension://dakbfdmgjiabojdgbiljlhgjbokobjpg/",
true}}};
return *settings_pages;
}
// static
bool KioskSettingsNavigationThrottle::IsSettingsPage(const std::string& url) {
auto& pages = g_test_settings_pages != nullptr ? *g_test_settings_pages
: DefaultSettingsPages();
for (auto& page : pages) {
if (UrlMatchesSettingsPage(page, url)) {
return true;
}
}
return false;
}
// static
void KioskSettingsNavigationThrottle::SetSettingPagesForTesting(
const std::vector<SettingsPage>* pages) {
CHECK_IS_TEST();
g_test_settings_pages = pages;
}
// static
void KioskSettingsNavigationThrottle::MaybeCreateAndAdd(
content::NavigationThrottleRegistry& registry) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
// Kiosk check.
if (!IsRunningInForcedAppMode()) {
return;
}
// If the web contents were previously marked as restricted, attach a throttle
// to it.
content::NavigationHandle& handle = registry.GetNavigationHandle();
if (handle.GetWebContents()->GetUserData(kRestrictedSettingsWindowKey)) {
registry.AddThrottle(
std::make_unique<KioskSettingsNavigationThrottle>(registry));
}
// Otherwise, check whether the navigated to url is a settings page, and if
// so, mark it.
if (IsSettingsPage(handle.GetURL().spec())) {
handle.GetWebContents()->SetUserData(
kRestrictedSettingsWindowKey,
std::make_unique<content::WebContents::Data>());
registry.AddThrottle(
std::make_unique<KioskSettingsNavigationThrottle>(registry));
}
}
KioskSettingsNavigationThrottle::KioskSettingsNavigationThrottle(
content::NavigationThrottleRegistry& registry)
: content::NavigationThrottle(registry) {}
KioskSettingsNavigationThrottle::ThrottleCheckResult
KioskSettingsNavigationThrottle::WillStartRequest() {
return WillStartOrRedirectRequest();
}
KioskSettingsNavigationThrottle::ThrottleCheckResult
KioskSettingsNavigationThrottle::WillRedirectRequest() {
return WillStartOrRedirectRequest();
}
const char* KioskSettingsNavigationThrottle::GetNameForLogging() {
return "KioskSettingsNavigationThrottle";
}
KioskSettingsNavigationThrottle::ThrottleCheckResult
KioskSettingsNavigationThrottle::WillStartOrRedirectRequest() {
return IsSettingsPage(navigation_handle()->GetURL().spec()) ? PROCEED
: CANCEL;
}
} // namespace chromeos
|