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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
|
// Copyright 2015 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_browser_session.h"
#include <errno.h>
#include <signal.h>
#include <optional>
#include <string>
#include "base/functional/bind.h"
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/memory/raw_ptr.h"
#include "base/task/single_thread_task_runner.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/app_mode/kiosk_browser_window_handler.h"
#include "chrome/browser/chromeos/app_mode/kiosk_metrics_service.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/pref_names.h"
#include "chromeos/dbus/power/power_manager_client.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_child_process_host_iterator.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/child_process_data.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/process_type.h"
#include "content/public/common/webplugininfo.h"
#include "extensions/browser/app_window/app_window.h"
#include "extensions/browser/app_window/app_window_registry.h"
#include "extensions/browser/guest_view/web_view/web_view_guest.h"
#include "ppapi/buildflags/buildflags.h"
#if BUILDFLAG(ENABLE_PLUGINS)
#include "chrome/browser/chromeos/app_mode/kiosk_session_plugin_handler.h"
#include "chrome/browser/chromeos/app_mode/kiosk_session_plugin_handler_delegate.h"
#include "content/public/browser/plugin_service.h"
#endif
using extensions::AppWindow;
using extensions::AppWindowRegistry;
namespace chromeos {
namespace {
#if BUILDFLAG(ENABLE_PLUGINS)
bool IsPepperPlugin(const base::FilePath& plugin_path) {
content::WebPluginInfo plugin_info;
return content::PluginService::GetInstance()->GetPluginInfoByPath(
plugin_path, &plugin_info) &&
plugin_info.is_pepper_plugin();
}
#endif
void RebootDevice() {
chromeos::PowerManagerClient::Get()->RequestRestart(
power_manager::REQUEST_RESTART_OTHER, "kiosk app session");
}
// Sends a SIGFPE signal to plugin subprocesses that matches `child_ids`
// to trigger a dump.
void DumpPluginProcess(const std::set<int>& child_ids) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
bool dump_requested = false;
content::BrowserChildProcessHostIterator iter(
content::PROCESS_TYPE_PPAPI_PLUGIN);
while (!iter.Done()) {
const content::ChildProcessData& data = iter.GetData();
if (child_ids.count(data.id) == 1) {
// Send a signal to dump the plugin process.
if (kill(data.GetProcess().Handle(), SIGFPE) == 0) {
dump_requested = true;
} else {
PLOG(WARNING) << "Failed to send SIGFPE to plugin process"
<< ", pid=" << data.GetProcess().Pid()
<< ", type=" << data.process_type
<< ", name=" << data.name;
}
}
++iter;
}
// Wait a bit to let dump finish (if requested) before rebooting the device.
const int kDumpWaitSeconds = 10;
content::GetUIThreadTaskRunner({})->PostDelayedTask(
FROM_HERE, base::BindOnce(&RebootDevice),
base::Seconds(dump_requested ? kDumpWaitSeconds : 0));
}
} // namespace
class KioskBrowserSession::AppWindowHandler
: public AppWindowRegistry::Observer {
public:
explicit AppWindowHandler(KioskBrowserSession* session)
: kiosk_browser_session_(session) {}
AppWindowHandler(const AppWindowHandler&) = delete;
AppWindowHandler& operator=(const AppWindowHandler&) = delete;
~AppWindowHandler() override = default;
void Init(Profile* profile, const std::string& app_id) {
DCHECK(!window_registry_);
window_registry_ = AppWindowRegistry::Get(profile);
if (window_registry_) {
window_registry_->AddObserver(this);
}
app_id_ = app_id;
}
private:
// extensions::AppWindowRegistry::Observer overrides:
void OnAppWindowAdded(AppWindow* app_window) override {
if (app_window->extension_id() != app_id_) {
return;
}
kiosk_browser_session_->OnAppWindowAdded(app_window);
app_window_created_ = true;
}
void OnAppWindowRemoved(AppWindow* app_window) override {
if (!app_window_created_ ||
!window_registry_->GetAppWindowsForApp(app_id_).empty()) {
return;
}
LOG(WARNING) << "Last app window removed, ending kiosk session.";
kiosk_browser_session_->Shutdown();
window_registry_->RemoveObserver(this);
}
const raw_ptr<KioskBrowserSession> kiosk_browser_session_;
raw_ptr<AppWindowRegistry> window_registry_ = nullptr;
std::string app_id_;
bool app_window_created_ = false;
};
#if BUILDFLAG(ENABLE_PLUGINS)
class KioskBrowserSession::PluginHandlerDelegateImpl
: public KioskSessionPluginHandlerDelegate {
public:
explicit PluginHandlerDelegateImpl(KioskBrowserSession* owner)
: owner_(owner) {}
PluginHandlerDelegateImpl(const PluginHandlerDelegateImpl&) = delete;
PluginHandlerDelegateImpl& operator=(const PluginHandlerDelegateImpl&) =
delete;
~PluginHandlerDelegateImpl() override = default;
// KioskSessionPluginHandlerDelegate:
bool ShouldHandlePlugin(const base::FilePath& plugin_path) const override {
// Note that BrowserChildProcessHostIterator in DumpPluginProcess also needs
// to be updated when adding more plugin types here.
return IsPepperPlugin(plugin_path);
}
void OnPluginCrashed(const base::FilePath& plugin_path) override {
if (owner_->is_shutting_down()) {
return;
}
owner_->metrics_service_->RecordKioskSessionPluginCrashed();
owner_->is_shutting_down_ = true;
LOG(ERROR) << "Reboot due to plugin crash, path=" << plugin_path.value();
RebootDevice();
}
void OnPluginHung(const std::set<int>& hung_plugins) override {
if (owner_->is_shutting_down()) {
return;
}
owner_->metrics_service_->RecordKioskSessionPluginHung();
owner_->is_shutting_down_ = true;
LOG(ERROR) << "Plugin hung detected. Dump and reboot.";
DumpPluginProcess(hung_plugins);
}
private:
const raw_ptr<KioskBrowserSession> owner_;
};
#endif
KioskBrowserSession::KioskBrowserSession(Profile* profile)
: KioskBrowserSession(profile,
base::BindOnce(chrome::AttemptUserExit),
g_browser_process->local_state()) {}
KioskBrowserSession::KioskBrowserSession(Profile* profile,
base::OnceClosure attempt_user_exit,
PrefService* local_state)
: KioskBrowserSession(profile,
std::move(attempt_user_exit),
std::make_unique<KioskMetricsService>(local_state)) {}
KioskBrowserSession::~KioskBrowserSession() {
if (!is_shutting_down()) {
metrics_service_->RecordKioskSessionStopped();
}
}
// static
std::unique_ptr<KioskBrowserSession> KioskBrowserSession::CreateForTesting(
Profile* profile,
base::OnceClosure attempt_user_exit,
PrefService* local_state,
const std::vector<std::string>& crash_dirs) {
return base::WrapUnique(new KioskBrowserSession(
profile, std::move(attempt_user_exit),
KioskMetricsService::CreateForTesting(local_state, crash_dirs)));
}
void KioskBrowserSession::RegisterLocalStatePrefs(
PrefRegistrySimple* registry) {
registry->RegisterDictionaryPref(prefs::kKioskMetrics);
}
void KioskBrowserSession::RegisterProfilePrefs(
user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterBooleanPref(prefs::kNewWindowsInKioskAllowed, false);
registry->RegisterBooleanPref(prefs::kKioskTroubleshootingToolsEnabled,
false);
registry->RegisterListPref(prefs::kKioskBrowserPermissionsAllowedForOrigins,
PrefRegistrySimple::NO_REGISTRATION_FLAGS);
registry->RegisterBooleanPref(prefs::kKioskWebAppOfflineEnabled, true);
registry->RegisterBooleanPref(prefs::kKioskChromeAppsForceAllowed, false);
registry->RegisterBooleanPref(prefs::kKioskApplicationLogCollectionEnabled,
false);
}
void KioskBrowserSession::InitForChromeAppKiosk(const std::string& app_id) {
app_window_handler_ = std::make_unique<AppWindowHandler>(this);
app_window_handler_->Init(profile(), app_id);
CreateBrowserWindowHandler(std::nullopt);
#if BUILDFLAG(ENABLE_PLUGINS)
plugin_handler_ = std::make_unique<KioskSessionPluginHandler>(
plugin_handler_delegate_.get());
#endif
metrics_service_->RecordKioskSessionStarted();
}
void KioskBrowserSession::InitForWebKiosk(
const std::optional<std::string>& web_app_name) {
CreateBrowserWindowHandler(web_app_name);
metrics_service_->RecordKioskSessionWebStarted();
}
void KioskBrowserSession::InitForIwaKiosk(
const std::optional<std::string>& app_name) {
CreateBrowserWindowHandler(app_name);
metrics_service_->RecordKioskSessionIwaStarted();
}
void KioskBrowserSession::SetOnHandleBrowserCallbackForTesting(
base::RepeatingCallback<void(bool is_closing)> callback) {
on_handle_browser_callback_ = std::move(callback);
}
KioskSessionPluginHandlerDelegate*
KioskBrowserSession::GetPluginHandlerDelegateForTesting() {
return plugin_handler_delegate_.get();
}
KioskBrowserSession::KioskBrowserSession(
Profile* profile,
base::OnceClosure attempt_user_exit,
std::unique_ptr<KioskMetricsService> metrics_service)
: profile_(profile),
#if BUILDFLAG(ENABLE_PLUGINS)
plugin_handler_delegate_(
std::make_unique<PluginHandlerDelegateImpl>(this)),
#endif
attempt_user_exit_(std::move(attempt_user_exit)),
metrics_service_(std::move(metrics_service)) {
}
void KioskBrowserSession::CreateBrowserWindowHandler(
const std::optional<std::string>& web_app_name) {
browser_window_handler_ = std::make_unique<KioskBrowserWindowHandler>(
profile(), web_app_name,
base::BindRepeating(&KioskBrowserSession::OnHandledNewBrowserWindow,
weak_ptr_factory_.GetWeakPtr()),
base::BindOnce(&KioskBrowserSession::Shutdown,
weak_ptr_factory_.GetWeakPtr()));
}
void KioskBrowserSession::OnHandledNewBrowserWindow(bool is_closing) {
if (on_handle_browser_callback_) {
on_handle_browser_callback_.Run(is_closing);
}
}
void KioskBrowserSession::OnAppWindowAdded(AppWindow* app_window) {
if (is_shutting_down()) {
return;
}
#if BUILDFLAG(ENABLE_PLUGINS)
plugin_handler_->Observe(app_window->web_contents());
#endif
}
void KioskBrowserSession::OnGuestAdded(
content::WebContents* guest_web_contents) {
// Bail if the session is shutting down.
if (is_shutting_down()) {
return;
}
// Bail if the guest is not a WebViewGuest.
if (!extensions::WebViewGuest::FromWebContents(guest_web_contents)) {
return;
}
#if BUILDFLAG(ENABLE_PLUGINS)
// Plugin handler is initialized only for Chrome app Kiosks.
if (plugin_handler_) {
plugin_handler_->Observe(guest_web_contents);
}
#endif
}
void KioskBrowserSession::Shutdown() {
if (is_shutting_down()) {
return;
}
is_shutting_down_ = true;
metrics_service_->RecordKioskSessionStopped();
std::move(attempt_user_exit_).Run();
}
Browser* KioskBrowserSession::GetSettingsBrowserForTesting() {
if (browser_window_handler_) {
return browser_window_handler_->GetSettingsBrowserForTesting(); // IN-TEST
}
return nullptr;
}
} // namespace chromeos
|