File: web_app_launch_dialog.cc

package info (click to toggle)
chromium 139.0.7258.127-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,096 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (171 lines) | stat: -rw-r--r-- 6,706 bytes parent folder | download | duplicates (5)
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
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/auto_reset.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/apps/app_service/app_launch_params.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/profiles/profile.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/views/extensions/security_dialog_tracker.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/web_applications/web_app_dialogs.h"
#include "chrome/browser/web_applications/commands/launch_web_app_command.h"
#include "chrome/browser/web_applications/web_app_command_manager.h"
#include "chrome/browser/web_applications/web_app_constants.h"
#include "chrome/browser/web_applications/web_app_provider.h"
#include "chrome/browser/web_applications/web_app_ui_manager.h"
#include "chrome/common/chrome_features.h"
#include "chrome/grit/generated_resources.h"
#include "components/constrained_window/constrained_window_views.h"
#include "components/web_modal/web_contents_modal_dialog_manager.h"
#include "content/public/browser/web_contents.h"
#include "third_party/blink/public/mojom/web_install/web_install.mojom.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/views/bubble/bubble_dialog_model_host.h"
#include "ui/views/view.h"
#include "ui/views/view_class_properties.h"
#include "ui/views/view_utils.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_observer.h"
#include "web_app_modal_dialog_delegate.h"

namespace web_app {

DEFINE_ELEMENT_IDENTIFIER_VALUE(kWebInstallLaunchDialogAppName);
using WebAppBackgroundAppLaunchAcceptanceCallback =
    base::OnceCallback<void(bool accepted)>;

namespace {
bool g_auto_accept_launch_for_testing = false;
}  // namespace

class WebAppLaunchDialogDelegate : public WebAppModalDialogDelegate {
 public:
  WebAppLaunchDialogDelegate(
      content::WebContents* web_contents,
      WebAppBackgroundAppLaunchAcceptanceCallback callback,
      webapps::AppId app_id,
      Profile* profile)
      : WebAppModalDialogDelegate(web_contents),
        callback_(std::move(callback)),
        app_id_(app_id),
        profile_(profile) {}

  ~WebAppLaunchDialogDelegate() override = default;

  void OnAccept() {
    std::move(callback_).Run(/*accepted=*/true);

    WebAppProvider* provider = WebAppProvider::GetForWebApps(profile_);
    if (provider) {
      apps::AppLaunchParams params(
          app_id_, apps::LaunchContainer::kLaunchContainerWindow,
          WindowOpenDisposition::NEW_WINDOW,
          apps::LaunchSource::kFromWebInstallApi);

      provider->command_manager().ScheduleCommand(
          std::make_unique<LaunchWebAppCommand>(
              profile_, provider, std::move(params),
              LaunchWebAppWindowSetting::kUseLaunchParams, base::DoNothing()));
    }
  }

  void OnCancel() { std::move(callback_).Run(/*accepted=*/false); }

  void OnDestroyed() {
    if (!callback_.is_null()) {
      std::move(callback_).Run(/*accepted=*/false);
    }
  }

  void CloseDialogAsIgnored() override {
    if (!dialog_model() || !dialog_model()->host()) {
      return;
    }
    dialog_model()->host()->Close();
  }

  base::WeakPtr<WebAppLaunchDialogDelegate> AsWeakPtr() {
    return weak_ptr_factory_.GetWeakPtr();
  }

 private:
  WebAppBackgroundAppLaunchAcceptanceCallback callback_;
  webapps::AppId app_id_;
  raw_ptr<Profile> profile_;

  base::WeakPtrFactory<WebAppLaunchDialogDelegate> weak_ptr_factory_{this};
};

void ShowWebInstallAppLaunchDialog(
    content::WebContents* web_contents,
    const webapps::AppId& app_id,
    Profile* profile,
    std::string app_name,
    WebAppBackgroundAppLaunchAcceptanceCallback callback) {
  Browser* browser = chrome::FindBrowserWithTab(web_contents);
  if (!browser) {
    std::move(callback).Run(/*accepted=*/false);
    return;
  }

  // Do not show the dialog if it is already being shown.
  const web_modal::WebContentsModalDialogManager* manager =
      web_modal::WebContentsModalDialogManager::FromWebContents(web_contents);
  if (!manager || manager->IsDialogActive()) {
    std::move(callback).Run(/*accepted=*/false);
    return;
  }

  views::BubbleDialogDelegate* dialog_delegate = nullptr;

  auto delegate = std::make_unique<WebAppLaunchDialogDelegate>(
      web_contents, std::move(callback), app_id, profile);
  auto delegate_weak_ptr = delegate->AsWeakPtr();

  auto dialog_model =
      ui::DialogModel::Builder(std::move(delegate))
          .SetInternalName("WebInstallLaunchDialog")
          .SetTitle(l10n_util::GetStringUTF16(
              IDS_INTENT_PICKER_BUBBLE_VIEW_OPEN_WITH))
          // TODO(crbug.com/422940463): Show app icon in new launch dialog for
          // background document launches.
          // This paragraph is a placeholder for now.
          .AddParagraph(ui::DialogModelLabel(base::UTF8ToUTF16(app_name)))
          .AddOkButton(base::BindOnce(&WebAppLaunchDialogDelegate::OnAccept,
                                      delegate_weak_ptr),
                       ui::DialogModel::Button::Params().SetLabel(
                           l10n_util::GetStringUTF16(
                               IDS_INTENT_PICKER_BUBBLE_VIEW_OPEN)))
          .AddCancelButton(base::BindOnce(&WebAppLaunchDialogDelegate::OnCancel,
                                          delegate_weak_ptr))
          .SetCloseActionCallback(base::BindOnce(
              &WebAppLaunchDialogDelegate::OnCancel, delegate_weak_ptr))
          .SetDialogDestroyingCallback(base::BindOnce(
              &WebAppLaunchDialogDelegate::OnDestroyed, delegate_weak_ptr))
          .OverrideDefaultButton(ui::mojom::DialogButton::kCancel)
          .Build();
  auto dialog = views::BubbleDialogModelHost::CreateModal(
      std::move(dialog_model), ui::mojom::ModalType::kChild);

  dialog_delegate = dialog->AsBubbleDialogDelegate();
  views::Widget* launch_dialog_widget =
      constrained_window::ShowWebModalDialogViews(dialog.release(),
                                                  web_contents);
  delegate_weak_ptr->OnWidgetShownStartTracking(launch_dialog_widget);

  if (g_auto_accept_launch_for_testing) {
    dialog_delegate->AcceptDialog();
  }
}

base::AutoReset<bool> SetAutoAcceptWebInstallLaunchDialogForTesting() {
  return base::AutoReset<bool>(&g_auto_accept_launch_for_testing, true);
}

}  // namespace web_app