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
|
// 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/ui/views/borealis/borealis_disallowed_dialog.h"
#include <memory>
#include <string>
#include "ash/constants/ash_features.h"
#include "ash/public/cpp/new_window_delegate.h"
#include "ash/public/cpp/shelf_types.h"
#include "ash/public/cpp/window_properties.h"
#include "base/functional/callback_forward.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ash/borealis/borealis_features.h"
#include "chrome/browser/ash/borealis/borealis_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/ash/system_web_apps/system_web_app_ui_utils.h"
#include "chrome/browser/ui/views/chrome_typography.h"
#include "chrome/common/webui_url_constants.h"
#include "chrome/grit/generated_resources.h"
#include "components/policy/policy_constants.h"
#include "components/strings/grit/components_strings.h"
#include "content/public/browser/browser_thread.h"
#include "ui/aura/window.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/base/ui_base_types.h"
#include "ui/strings/grit/ui_strings.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/link.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/layout_provider.h"
#include "ui/views/style/typography.h"
#include "ui/views/view.h"
#include "ui/views/window/dialog_delegate.h"
namespace views::borealis {
namespace {
using MaybeAction = std::optional<std::pair<std::u16string, base::OnceClosure>>;
// Views uses tricks like this to ensure singleton-ness of dialogs.
static Widget* g_instance_ = nullptr;
class BehaviourProvider {
public:
virtual ~BehaviourProvider() = default;
virtual std::u16string GetMessage() const = 0;
// Get a label and callback for the "call to action" button, if present.
virtual MaybeAction GetAction() const { return std::nullopt; }
virtual std::vector<std::pair<std::u16string, GURL>> GetLinks() const {
return {};
}
};
} // namespace
class BorealisDisallowedDialog : public DialogDelegate {
public:
BorealisDisallowedDialog(std::unique_ptr<BehaviourProvider> behaviour,
int title_id) {
DCHECK(!g_instance_);
SetTitle(IDS_BOREALIS_INSTALLER_APP_NAME);
set_internal_name("BorealisDisallowedDialog");
MaybeAction second_action = behaviour->GetAction();
if (second_action.has_value()) {
SetButtons(static_cast<int>(ui::mojom::DialogButton::kOk) |
static_cast<int>(ui::mojom::DialogButton::kCancel));
SetButtonLabel(ui::mojom::DialogButton::kCancel,
l10n_util::GetStringUTF16(IDS_CLOSE));
SetButtonLabel(ui::mojom::DialogButton::kOk,
std::move(second_action.value().first));
SetAcceptCallback(std::move(second_action.value().second));
} else {
SetButtons(static_cast<int>(ui::mojom::DialogButton::kOk));
SetButtonLabel(ui::mojom::DialogButton::kOk,
l10n_util::GetStringUTF16(IDS_CLOSE));
}
InitializeView(*behaviour, title_id);
SetModalType(ui::mojom::ModalType::kSystem);
SetOwnedByWidget(OwnedByWidgetPassKey());
SetShowCloseButton(false);
set_fixed_width(views::LayoutProvider::Get()->GetDistanceMetric(
views::DISTANCE_MODAL_DIALOG_PREFERRED_WIDTH));
}
~BorealisDisallowedDialog() override {
DCHECK(g_instance_);
g_instance_ = nullptr;
}
bool ShouldShowWindowTitle() const override { return false; }
private:
void InitializeView(const BehaviourProvider& behaviour, int title_id) {
auto view = std::make_unique<views::View>();
views::LayoutProvider* provider = views::LayoutProvider::Get();
view->SetLayoutManager(std::make_unique<views::BoxLayout>(
views::BoxLayout::Orientation::kVertical,
provider->GetInsetsMetric(views::InsetsMetric::INSETS_DIALOG),
provider->GetDistanceMetric(views::DISTANCE_RELATED_CONTROL_VERTICAL)));
views::Label* title_label = new views::Label(
l10n_util::GetStringUTF16(title_id), CONTEXT_IPH_BUBBLE_TITLE,
views::style::STYLE_EMPHASIZED);
title_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
title_label->SetMultiLine(true);
view->AddChildViewRaw(title_label);
views::Label* message_label = new views::Label(behaviour.GetMessage());
message_label->SetMultiLine(true);
message_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
view->AddChildViewRaw(message_label);
for (const std::pair<std::u16string, GURL>& link : behaviour.GetLinks()) {
views::Link* link_label =
view->AddChildView(std::make_unique<views::Link>(link.first));
link_label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
link_label->SetCallback(base::BindRepeating(
[](GURL url) {
ash::NewWindowDelegate::GetPrimary()->OpenUrl(
url, ash::NewWindowDelegate::OpenUrlFrom::kUserInteraction,
ash::NewWindowDelegate::Disposition::kNewForegroundTab);
},
link.second));
}
SetContentsView(std::move(view));
}
};
namespace {
using AllowStatus = ::borealis::BorealisFeatures::AllowStatus;
class DisallowedHardware : public BehaviourProvider {
public:
std::u16string GetMessage() const override {
return l10n_util::GetStringUTF16(IDS_BOREALIS_DISALLOWED_HARDWARE);
}
std::vector<std::pair<std::u16string, GURL>> GetLinks() const override {
return {{l10n_util::GetStringUTF16(IDS_LEARN_MORE),
GURL("https://support.google.com/"
"chromebook?p=steam_on_chromebook")}};
}
};
class DisallowedFailure : public BehaviourProvider {
public:
std::u16string GetMessage() const override {
return l10n_util::GetStringUTF16(IDS_BOREALIS_DISALLOWED_FAILED);
}
};
class DisallowedIrregular : public BehaviourProvider {
std::u16string GetMessage() const override {
return l10n_util::GetStringUTF16(IDS_BOREALIS_DISALLOWED_IRREGULAR);
}
};
class DisallowedPrimary : public BehaviourProvider {
std::u16string GetMessage() const override {
return l10n_util::GetStringUTF16(IDS_BOREALIS_DISALLOWED_PRIMARY);
}
std::vector<std::pair<std::u16string, GURL>> GetLinks() const override {
// TODO(b/256699588): Replace this with a p-link.
return {{l10n_util::GetStringUTF16(IDS_LEARN_MORE),
GURL("https://support.google.com/chromebook/answer/6088201")}};
}
};
class DisallowedChild : public BehaviourProvider {
std::u16string GetMessage() const override {
return l10n_util::GetStringUTF16(IDS_BOREALIS_DISALLOWED_CHILD);
}
std::vector<std::pair<std::u16string, GURL>> GetLinks() const override {
return {{l10n_util::GetStringUTF16(IDS_LEARN_MORE),
GURL("https://support.google.com/"
"chromebook?p=steam_on_chromebook")}};
}
};
class DisallowedPolicy : public BehaviourProvider {
std::u16string GetMessage() const override {
return l10n_util::GetStringUTF16(IDS_BOREALIS_DISALLOWED_ADMIN);
}
std::vector<std::pair<std::u16string, GURL>> GetLinks() const override {
// As of 2023q2 directly-linking to chromeenterprise is common, so we shall
// do it too.
return {
{base::UTF8ToUTF16(policy::key::kUserBorealisAllowed),
GURL("https://chromeenterprise.google/policies/#UserBorealisAllowed")},
{base::UTF8ToUTF16(policy::key::kVirtualMachinesAllowed),
GURL("https://chromeenterprise.google/policies/"
"#VirtualMachinesAllowed")},
};
}
};
class DisallowedFlag : public BehaviourProvider {
std::u16string GetMessage() const override {
return l10n_util::GetStringUTF16(IDS_BOREALIS_DISALLOWED_FLAG);
}
MaybeAction GetAction() const override {
return {{l10n_util::GetStringUTF16(IDS_BOREALIS_DISALLOWED_FLAG_BUTTON),
base::BindOnce([]() {
ash::SystemAppLaunchParams params;
params.url = GURL{std::string(chrome::kChromeUIOsFlagsAppURL) +
"#borealis-enabled"};
ash::LaunchSystemWebAppAsync(
ProfileManager::GetPrimaryUserProfile(),
ash::SystemWebAppType::OS_FLAGS, params);
})}};
}
};
std::unique_ptr<BehaviourProvider> StatusBehaviour(AllowStatus status) {
switch (status) {
case AllowStatus::kAllowed:
DCHECK(false);
// Unreachable in practice. Show "failed" message just in case.
return std::make_unique<DisallowedFailure>();
case AllowStatus::kFeatureDisabled:
case AllowStatus::kInsufficientHardware:
return std::make_unique<DisallowedHardware>();
case AllowStatus::kFailedToDetermine:
return std::make_unique<DisallowedFailure>();
case AllowStatus::kBlockedOnIrregularProfile:
return std::make_unique<DisallowedIrregular>();
case AllowStatus::kBlockedOnNonPrimaryProfile:
return std::make_unique<DisallowedPrimary>();
case AllowStatus::kBlockedOnChildAccount:
return std::make_unique<DisallowedChild>();
case AllowStatus::kVmPolicyBlocked:
case AllowStatus::kUserPrefBlocked:
return std::make_unique<DisallowedPolicy>();
case AllowStatus::kBlockedByFlag:
return std::make_unique<DisallowedFlag>();
}
}
void ShowDisallowedDialog(AllowStatus status, int title_id) {
DCHECK(status != AllowStatus::kAllowed);
// TODO(b/248938308): Closing and reopening the dialog this way is not
// desirable. When we move to webui we should just re-show the current dialog.
if (g_instance_) {
g_instance_->CloseNow();
}
auto delegate = std::make_unique<BorealisDisallowedDialog>(
StatusBehaviour(status), title_id);
g_instance_ = views::DialogDelegate::CreateDialogWidget(std::move(delegate),
nullptr, nullptr);
g_instance_->Show();
}
} // namespace
void ShowInstallerDisallowedDialog(AllowStatus status) {
ShowDisallowedDialog(status, IDS_BOREALIS_DISALLOWED_TITLE);
}
void ShowLauncherDisallowedDialog(AllowStatus status) {
ShowDisallowedDialog(status, IDS_BOREALIS_LAUNCH_ERROR_TITLE);
}
} // namespace views::borealis
|