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
|
// Copyright 2013 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/ash/app_mode/kiosk_app_launch_error.h"
#include <optional>
#include <string>
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "base/values.h"
#include "chrome/browser/ash/app_mode/kiosk_chrome_app_manager.h"
#include "chrome/browser/browser_process.h"
#include "chrome/grit/generated_resources.h"
#include "chromeos/ash/components/login/auth/public/auth_failure.h"
#include "components/prefs/scoped_user_pref_update.h"
#include "ui/base/l10n/l10n_util.h"
namespace ash {
namespace {
// Key under "kiosk" dictionary to store the last launch error.
constexpr char kKeyLaunchError[] = "launch_error";
// Key under "kiosk" dictionary to store the last cryptohome error.
constexpr char kKeyCryptohomeFailure[] = "cryptohome_failure";
// Error from the last kiosk launch.
std::optional<KioskAppLaunchError::Error> s_last_error = std::nullopt;
} // namespace
const char kKioskLaunchErrorHistogram[] = "Kiosk.Launch.Error";
// static
std::string KioskAppLaunchError::GetErrorMessage(Error error) {
switch (error) {
case Error::kNone:
return std::string();
case Error::kHasPendingLaunch:
case Error::kNotKioskEnabled:
case Error::kUnableToRetrieveHash:
case Error::kPolicyLoadFailed:
case Error::kUserNotAllowlisted:
return l10n_util::GetStringUTF8(IDS_KIOSK_APP_FAILED_TO_LAUNCH);
case Error::kCryptohomedNotRunning:
case Error::kAlreadyMounted:
case Error::kUnableToMount:
case Error::kUnableToRemove:
return l10n_util::GetStringUTF8(IDS_KIOSK_APP_ERROR_UNABLE_TO_MOUNT);
case Error::kUnableToInstall:
return l10n_util::GetStringUTF8(IDS_KIOSK_APP_ERROR_UNABLE_TO_INSTALL);
case Error::kUserCancel:
return l10n_util::GetStringUTF8(IDS_KIOSK_APP_ERROR_USER_CANCEL);
case Error::kUnableToDownload:
return l10n_util::GetStringUTF8(IDS_KIOSK_APP_ERROR_UNABLE_TO_DOWNLOAD);
case Error::kUnableToLaunch:
return l10n_util::GetStringUTF8(IDS_KIOSK_APP_ERROR_UNABLE_TO_LAUNCH);
case Error::kExtensionsLoadTimeout:
return l10n_util::GetStringUTF8(
IDS_KIOSK_APP_ERROR_EXTENSIONS_LOAD_TIMEOUT);
case Error::kExtensionsPolicyInvalid:
return l10n_util::GetStringUTF8(
IDS_KIOSK_APP_ERROR_EXTENSIONS_POLICY_INVALID);
case Error::kChromeAppDeprecated:
return l10n_util::GetStringUTF8(
IDS_KIOSK_APP_ERROR_UNABLE_TO_LAUNCH_CHROME_APP);
}
NOTREACHED() << "Unknown kiosk app launch error, error="
<< static_cast<int>(error);
}
// static
void KioskAppLaunchError::Save(KioskAppLaunchError::Error error) {
s_last_error = error;
PrefService* local_state = g_browser_process->local_state();
{
ScopedDictPrefUpdate dict_update(
local_state, KioskChromeAppManager::kKioskDictionaryName);
dict_update->SetByDottedPath(kKeyLaunchError, static_cast<int>(error));
}
// Make sure that the kiosk launch error gets written to disk before the
// browser is killed.
local_state->CommitPendingWrite();
}
// static
void KioskAppLaunchError::SaveCryptohomeFailure(
const AuthFailure& auth_failure) {
PrefService* local_state = g_browser_process->local_state();
ScopedDictPrefUpdate dict_update(local_state,
KioskChromeAppManager::kKioskDictionaryName);
dict_update->SetByDottedPath(kKeyCryptohomeFailure, auth_failure.reason());
}
// static
KioskAppLaunchError::Error KioskAppLaunchError::Get() {
if (s_last_error) {
return *s_last_error;
}
s_last_error = Error::kNone;
PrefService* local_state = g_browser_process->local_state();
const base::Value::Dict& dict =
local_state->GetDict(KioskChromeAppManager::kKioskDictionaryName);
std::optional<int> error = dict.FindInt(kKeyLaunchError);
if (error.has_value()) {
s_last_error = static_cast<KioskAppLaunchError::Error>(error.value());
return *s_last_error;
}
return Error::kNone;
}
// static
void KioskAppLaunchError::RecordMetricAndClear() {
PrefService* local_state = g_browser_process->local_state();
ScopedDictPrefUpdate dict_update(local_state,
KioskChromeAppManager::kKioskDictionaryName);
std::optional<int> error = dict_update->FindInt(kKeyLaunchError);
if (error) {
base::UmaHistogramEnumeration(kKioskLaunchErrorHistogram,
static_cast<Error>(*error));
}
dict_update->Remove(kKeyLaunchError);
s_last_error = std::nullopt;
std::optional<int> cryptohome_failure =
dict_update->FindInt(kKeyCryptohomeFailure);
if (cryptohome_failure) {
UMA_HISTOGRAM_ENUMERATION(
"Kiosk.Launch.CryptohomeFailure",
static_cast<AuthFailure::FailureReason>(*cryptohome_failure),
AuthFailure::NUM_FAILURE_REASONS);
}
dict_update->Remove(kKeyCryptohomeFailure);
}
} // namespace ash
|