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
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_UI_VIEWS_WEB_APPS_WEB_APP_INSTALL_DIALOG_DELEGATE_H_
#define CHROME_BROWSER_UI_VIEWS_WEB_APPS_WEB_APP_INSTALL_DIALOG_DELEGATE_H_
#include <memory>
#include <string>
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "chrome/browser/picture_in_picture/picture_in_picture_occlusion_observer.h"
#include "chrome/browser/picture_in_picture/scoped_picture_in_picture_occlusion_observation.h"
#include "chrome/browser/ui/web_applications/web_app_dialogs.h"
#include "chrome/browser/web_applications/web_app_install_info.h"
#include "content/public/browser/web_contents_observer.h"
#include "ui/base/interaction/element_identifier.h"
#include "ui/base/interaction/element_tracker.h"
#include "ui/base/models/dialog_model.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_observer.h"
class PrefService;
namespace content {
class Page;
class WebContents;
} // namespace content
namespace feature_engagement {
class Tracker;
}
namespace webapps {
class MlInstallOperationTracker;
} // namespace webapps
namespace views {
class Widget;
} // namespace views
namespace gfx {
class Rect;
} // namespace gfx
namespace web_app {
enum InstallDialogType { kSimple, kDetailed, kDiy, kMaxValue = kDiy };
inline constexpr int kIconSize = 32;
// When pre-populating the name field (using the web app title) we
// should try to make some effort to not suggest things we know work extra
// poorly when used as filenames in the OS. This is especially problematic when
// creating apps for pages that have no title, because then the URL of the
// page will be used as a suggestion, and (if accepted by the user) the shortcut
// name will look really weird. For example, MacOS will convert a colon (:) to a
// forward-slash (/), and Windows will convert the colons to spaces. MacOS even
// goes a step further and collapses multiple consecutive forward-slashes in
// localized names into a single forward-slash. That means, using 'https://foo'
// as an example, an app with a display name of 'https/foo' is created on
// MacOS and 'https foo' on Windows. By stripping away the schema, we will be
// greatly reducing the frequency of apps having weird names. Note: This
// does not affect user's ability to use URLs as an app name (which would
// result in a weird filename), it only restricts what we suggest as titles.
std::u16string NormalizeSuggestedAppTitle(const std::u16string& title);
// For some browser windows that are smaller in size, the install dialog's
// current size is smaller than the preferred size, leading to important
// security information being occluded. This function performs the comparison
// between the sizes and prevents that from happening.
// This serves as a stop-gap fix for crbug.com/384962294.
// TODO(crbug.com/346974105): Remove once tab modal dialogs can be sized
// irrespective of the size of the browser window triggering it.
bool IsWidgetCurrentSizeSmallerThanPreferredSize(views::Widget* widget);
class WebAppInstallDialogDelegate : public ui::DialogModelDelegate,
public content::WebContentsObserver,
public PictureInPictureOcclusionObserver,
public views::WidgetObserver {
public:
DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kDiyAppsDialogOkButtonId);
DECLARE_CLASS_ELEMENT_IDENTIFIER_VALUE(kPwaInstallDialogInstallButton);
DECLARE_CLASS_CUSTOM_ELEMENT_EVENT_TYPE(kInstalledPWAEventId);
WebAppInstallDialogDelegate(
content::WebContents* web_contents,
std::unique_ptr<WebAppInstallInfo> install_info,
std::unique_ptr<webapps::MlInstallOperationTracker> install_tracker,
AppInstallationAcceptanceCallback callback,
PwaInProductHelpState iph_state,
PrefService* prefs,
feature_engagement::Tracker* tracker,
InstallDialogType dialog_type);
~WebAppInstallDialogDelegate() override;
// Once the install dialog is shown, start tracking the widget for:
// 1. Observing it to prevent picture in picture occlusion.
// 2. Observing it for size changes so that it can be closed if needed.
// 3. Tracking it as a security dialog so that extension popups do not appear
// over it.
void OnWidgetShownStartTracking(views::Widget* install_dialog_widget);
void OnAccept();
void OnCancel();
void OnClose();
// This is called when the dialog has been either accepted, cancelled, closed
// or destroyed without an user-action.
void OnDestroyed();
// Takes care of enabling or disabling the dialog model's OK button for DIY
// apps based on changes in the text field, and also keeps track of the text
// field's contents.
void OnTextFieldChangedMaybeUpdateButton(
const std::u16string& text_field_contents);
base::WeakPtr<WebAppInstallDialogDelegate> AsWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
// content::WebContentsObserver overrides:
void OnVisibilityChanged(content::Visibility visibility) override;
void WebContentsDestroyed() override;
void PrimaryPageChanged(content::Page& page) override;
// PictureInPictureOcclusionObserver overrides:
void OnOcclusionStateChanged(bool occluded) override;
// views::WidgetObserver overrides:
void OnWidgetBoundsChanged(views::Widget* widget,
const gfx::Rect& new_bounds) override;
void OnWidgetDestroyed(views::Widget* widget) override;
void CloseDialogAsIgnored();
private:
void MeasureIphOnDialogClose();
void MeasureAcceptUserActionsForInstallDialog();
void MeasureCancelUserActionsForInstallDialog();
raw_ptr<content::WebContents> web_contents_;
std::unique_ptr<WebAppInstallInfo> install_info_;
std::unique_ptr<webapps::MlInstallOperationTracker> install_tracker_;
AppInstallationAcceptanceCallback callback_;
PwaInProductHelpState iph_state_;
raw_ptr<PrefService> prefs_;
raw_ptr<feature_engagement::Tracker> tracker_;
InstallDialogType dialog_type_;
std::u16string text_field_contents_;
bool received_user_response_ = false;
ScopedPictureInPictureOcclusionObservation occlusion_observation_{this};
base::ScopedObservation<views::Widget, views::WidgetObserver>
widget_observation_{this};
base::WeakPtrFactory<WebAppInstallDialogDelegate> weak_ptr_factory_{this};
};
} // namespace web_app
#endif // CHROME_BROWSER_UI_VIEWS_WEB_APPS_WEB_APP_INSTALL_DIALOG_DELEGATE_H_
|