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
|
// Copyright 2024 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/shortcuts/create_desktop_shortcut_delegate.h"
#include <string>
#include "base/metrics/user_metrics.h"
#include "base/metrics/user_metrics_action.h"
#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/picture_in_picture/picture_in_picture_window_manager.h"
#include "chrome/browser/picture_in_picture/scoped_picture_in_picture_occlusion_observation.h"
#include "chrome/browser/profiles/profile_attributes_entry.h"
#include "chrome/browser/profiles/profile_attributes_storage.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/views/controls/site_icon_text_and_origin_view.h"
#include "chrome/browser/ui/views/shortcuts/create_desktop_shortcut.h"
#include "content/public/browser/page.h"
#include "content/public/browser/visibility.h"
#include "content/public/browser/web_contents.h"
namespace shortcuts {
std::u16string AppendProfileNameToTitleIfNeeded(Profile* profile,
std::u16string old_title) {
ProfileManager* profile_manager = g_browser_process->profile_manager();
if (!profile_manager || profile_manager->GetNumberOfProfiles() <= 1) {
return old_title;
}
ProfileAttributesStorage& storage =
profile_manager->GetProfileAttributesStorage();
ProfileAttributesEntry* entry =
storage.GetProfileAttributesWithPath(profile->GetPath());
if (!entry) {
return old_title;
}
base::TrimWhitespace(old_title, base::TRIM_ALL, &old_title);
return base::StrCat({old_title, u" (", entry->GetName(), u")"});
}
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(CreateDesktopShortcutDelegate,
kCreateShortcutDialogOkButtonId);
DEFINE_CLASS_ELEMENT_IDENTIFIER_VALUE(CreateDesktopShortcutDelegate,
kCreateShortcutDialogTitleFieldId);
CreateDesktopShortcutDelegate::CreateDesktopShortcutDelegate(
content::WebContents* web_contents,
CreateShortcutDialogCallback final_callback)
: content::WebContentsObserver(web_contents),
final_callback_(std::move(final_callback)) {}
CreateDesktopShortcutDelegate::~CreateDesktopShortcutDelegate() = default;
void CreateDesktopShortcutDelegate::StartObservingForPictureInPictureOcclusion(
views::Widget* dialog_widget) {
occlusion_observation_.Observe(dialog_widget);
}
void CreateDesktopShortcutDelegate::OnAccept() {
if (final_callback_) {
base::RecordAction(
base::UserMetricsAction("CreateDesktopShortcutDialogAccepted"));
CHECK(!text_field_data_.empty());
std::move(final_callback_).Run(text_field_data_);
}
}
void CreateDesktopShortcutDelegate::OnClose() {
if (final_callback_) {
base::RecordAction(
base::UserMetricsAction("CreateDesktopShortcutDialogCancelled"));
std::move(final_callback_).Run(std::nullopt);
}
}
void CreateDesktopShortcutDelegate::OnTitleUpdated(
const std::u16string& trimmed_text_field_data) {
text_field_data_ = trimmed_text_field_data;
ui::DialogModel::Button* ok_button =
dialog_model()->GetButtonByUniqueId(kCreateShortcutDialogOkButtonId);
CHECK(ok_button);
dialog_model()->SetButtonEnabled(ok_button,
/*enabled=*/!text_field_data_.empty());
}
void CreateDesktopShortcutDelegate::OnVisibilityChanged(
content::Visibility visibility) {
if (visibility == content::Visibility::HIDDEN) {
CloseDialogAsIgnored();
}
}
void CreateDesktopShortcutDelegate::WebContentsDestroyed() {
CloseDialogAsIgnored();
}
void CreateDesktopShortcutDelegate::PrimaryPageChanged(content::Page& page) {
CloseDialogAsIgnored();
}
void CreateDesktopShortcutDelegate::OnOcclusionStateChanged(bool occluded) {
// If a picture-in-picture window is occluding the dialog, force it to close
// to prevent spoofing.
if (occluded) {
PictureInPictureWindowManager::GetInstance()->ExitPictureInPicture();
}
}
void CreateDesktopShortcutDelegate::CloseDialogAsIgnored() {
if (dialog_model() && dialog_model()->host()) {
dialog_model()->host()->Close();
}
}
} // namespace shortcuts
|