File: privacy_sandbox_dialog_view.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (287 lines) | stat: -rw-r--r-- 11,442 bytes parent folder | download | duplicates (2)
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
278
279
280
281
282
283
284
285
286
287
// 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.

#include "chrome/browser/ui/views/privacy_sandbox/privacy_sandbox_dialog_view.h"

#include "base/memory/raw_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "chrome/browser/privacy_sandbox/notice/notice.mojom-forward.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service.h"
#include "chrome/browser/privacy_sandbox/privacy_sandbox_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/privacy_sandbox/privacy_sandbox_prompt.h"
#include "chrome/browser/ui/views/frame/app_menu_button.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#include "chrome/browser/ui/views/frame/toolbar_button_provider.h"
#include "chrome/browser/ui/views/privacy_sandbox/dialog_view_context.h"
#include "chrome/browser/ui/webui/privacy_sandbox/base_dialog_ui.h"
#include "chrome/browser/ui/webui/privacy_sandbox/privacy_sandbox_dialog_ui.h"
#include "chrome/common/webui_url_constants.h"
#include "components/constrained_window/constrained_window_views.h"
#include "components/privacy_sandbox/privacy_sandbox_features.h"
#include "components/web_modal/web_contents_modal_dialog_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_ui.h"
#include "net/base/url_util.h"
#include "third_party/blink/public/common/page/page_zoom.h"
#include "ui/base/metadata/metadata_impl_macros.h"
#include "ui/base/mojom/dialog_button.mojom.h"
#include "ui/base/mojom/ui_base_types.mojom-shared.h"
#include "ui/views/layout/fill_layout.h"

namespace {

using enum PrivacySandboxService::AdsDialogCallbackNoArgsEvents;
using privacy_sandbox::notice::mojom::PrivacySandboxNotice;
using views::WebView;

constexpr int kM1DialogWidth = 600;
constexpr int kDefaultDialogHeight = 494;

GURL GetDialogURL(PrivacySandboxService::PromptType prompt_type) {
  GURL base_url = GURL(chrome::kChromeUIPrivacySandboxDialogURL);
  GURL combined_dialog_url =
      base_url.Resolve(chrome::kChromeUIPrivacySandboxDialogCombinedPath);
  switch (prompt_type) {
    case PrivacySandboxService::PromptType::kM1Consent:
      return combined_dialog_url;
    case PrivacySandboxService::PromptType::kM1NoticeROW:
      return base_url.Resolve(chrome::kChromeUIPrivacySandboxDialogNoticePath);
    case PrivacySandboxService::PromptType::kM1NoticeEEA:
      return net::AppendQueryParameter(combined_dialog_url, "step", "notice");
    case PrivacySandboxService::PromptType::kM1NoticeRestricted:
      return base_url.Resolve(
          chrome::kChromeUIPrivacySandboxDialogNoticeRestrictedPath);
    case PrivacySandboxService::PromptType::kNone:
      NOTREACHED();
  }
}

class PrivacySandboxDialogDelegate : public views::DialogDelegate {
 public:
  explicit PrivacySandboxDialogDelegate(BrowserWindowInterface* browser)
      : browser_(browser) {
    RegisterWindowClosingCallback(
        base::BindOnce(&PrivacySandboxDialogDelegate::OnWindowClosing,
                       base::Unretained(this)));
  }

  bool OnCloseRequested(views::Widget::ClosedReason close_reason) override {
    // Only an unspecified close reason, which only occurs when the user has
    // actually made a choice is sufficient to close the consent. Reason for
    // closing the dialog (like dismissing notice dialog with escape) is handled
    // in WebUI.
    return close_reason == views::Widget::ClosedReason::kUnspecified;
  }

  void OnWindowClosing() {
    // Notice Framework doesn't have a browser to widget mapping.
    if (base::FeatureList::IsEnabled(
            privacy_sandbox::kPrivacySandboxNoticeFramework)) {
      return;
    }
    // TODO(crbug.com/408016824): To be deprecated once V2 is migrated to.
    if (auto* privacy_sandbox_service =
            PrivacySandboxServiceFactory::GetForProfile(
                browser_->GetProfile())) {
      privacy_sandbox_service->PromptClosedForBrowser(browser_);
    }
  }

 private:
  raw_ptr<BrowserWindowInterface> browser_;
};

}  // namespace

// static
void PrivacySandboxDialog::Show(Browser* browser,
                                PrivacySandboxService::PromptType prompt_type) {
  auto delegate = std::make_unique<PrivacySandboxDialogDelegate>(browser);
  delegate->SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));
  delegate->SetModalType(ui::mojom::ModalType::kWindow);
  delegate->SetShowCloseButton(false);
  delegate->SetOwnedByWidget(views::WidgetDelegate::OwnedByWidgetPassKey());

  auto dialog_view = PrivacySandboxDialogView::CreateDialogViewForPromptType(
      browser, prompt_type);
  delegate->SetContentsView(std::move(dialog_view));

  auto* widget = constrained_window::CreateBrowserModalDialogViews(
      std::move(delegate), browser->window()->GetNativeWindow());

  if (auto* privacy_sandbox_service =
          PrivacySandboxServiceFactory::GetForProfile(browser->profile())) {
    privacy_sandbox_service->PromptOpenedForBrowser(browser, widget);
  }
}

// static
void PrivacySandboxDialog::Show(BrowserWindowInterface* browser,
                                PrivacySandboxNotice notice) {
  auto delegate = std::make_unique<PrivacySandboxDialogDelegate>(browser);
  delegate->SetButtons(static_cast<int>(ui::mojom::DialogButton::kNone));
  delegate->SetModalType(ui::mojom::ModalType::kWindow);
  delegate->SetShowCloseButton(false);
  delegate->SetOwnedByWidget(views::WidgetDelegate::OwnedByWidgetPassKey());

  auto dialog_view =
      PrivacySandboxDialogView::CreateDialogViewForPrivacySandboxNotice(browser,
                                                                        notice);
  delegate->SetContentsView(std::move(dialog_view));

  constrained_window::CreateBrowserModalDialogViews(
      std::move(delegate), browser->GetTabStripModel()
                               ->GetActiveWebContents()
                               ->GetTopLevelNativeWindow());
}

PrivacySandboxDialogView::PrivacySandboxDialogView(
    BrowserWindowInterface* browser)
    : web_view_(AddChildView(std::make_unique<WebView>(browser->GetProfile()))),
      browser_(browser) {
  // Attach DialogViewContext to this WebContents. This identifies the
  // WebContents as originating from the dialog and provides WebUI controllers
  // with access to this view (as the BaseDialogUIDelegate) for native dialog
  // operations.
  privacy_sandbox::DialogViewContext::CreateForWebContents(
      web_view_->GetWebContents(), *this);
  // Override the default zoom level for the Privacy Sandbox dialog. Its size
  // should align with native UI elements, rather than web content.
  auto* web_contents = web_view_->GetWebContents();
  auto* rfh = web_contents->GetPrimaryMainFrame();
  auto* zoom_map = content::HostZoomMap::GetForWebContents(web_contents);
  zoom_map->SetTemporaryZoomLevel(rfh->GetGlobalId(),
                                  blink::ZoomFactorToZoomLevel(1.0f));

  const int max_width = browser->GetWebContentsModalDialogHostForWindow()
                            ->GetMaximumDialogSize()
                            .width();
  const int width =
      views::LayoutProvider::Get()->GetSnappedDialogWidth(kM1DialogWidth);
  web_view_->SetPreferredSize(
      gfx::Size(std::min(width, max_width), kDefaultDialogHeight));
}

// static
std::unique_ptr<PrivacySandboxDialogView>
PrivacySandboxDialogView::CreateDialogViewForPromptType(
    BrowserWindowInterface* browser,
    PrivacySandboxService::PromptType prompt_type) {
  CHECK_NE(PrivacySandboxService::PromptType::kNone, prompt_type);

  // Sets content view.
  // Using `new` to access a non-public constructor.
  auto dialog_view = base::WrapUnique(new PrivacySandboxDialogView(browser));
  dialog_view->InitializeDialogUIForPromptType(prompt_type);
  return dialog_view;
}

// static
std::unique_ptr<PrivacySandboxDialogView>
PrivacySandboxDialogView::CreateDialogViewForPrivacySandboxNotice(
    BrowserWindowInterface* browser,
    PrivacySandboxNotice notice) {
  // Sets content view
  // Using `new` to access a non-public constructor.
  auto dialog_view = base::WrapUnique(new PrivacySandboxDialogView(browser));
  dialog_view->InitializeDialogUIForPrivacySandboxNotice(notice);
  return dialog_view;
}

void PrivacySandboxDialogView::InitializeDialogUIForPromptType(
    PrivacySandboxService::PromptType prompt_type) {
  web_view_->LoadInitialURL(GetDialogURL(prompt_type));
  PrivacySandboxDialogUI* web_ui = web_view_->GetWebContents()
                                       ->GetWebUI()
                                       ->GetController()
                                       ->GetAs<PrivacySandboxDialogUI>();
  DCHECK(web_ui);
  // Unretained is fine because this outlives the inner web UI.
  web_ui->Initialize(
      browser_->GetProfile(),
      base::BindRepeating(&PrivacySandboxDialogView::AdsDialogNoArgsCallback,
                          base::Unretained(this)),
      base::BindOnce(&PrivacySandboxDialogView::ResizeNativeView,
                     base::Unretained(this)),

      prompt_type);

  SetUseDefaultFillLayout(true);
}

void PrivacySandboxDialogView::InitializeDialogUIForPrivacySandboxNotice(
    PrivacySandboxNotice notice) {
  notice_ = notice;
  web_view_->LoadInitialURL(GURL(chrome::kChromeUIPrivacySandboxBaseDialogURL));
  SetUseDefaultFillLayout(true);
}

void PrivacySandboxDialogView::AdsDialogNoArgsCallback(
    PrivacySandboxService::AdsDialogCallbackNoArgsEvents event) {
  switch (event) {
    case kShowDialog:
      ShowNativeView();
      break;
    case kCloseDialog:
      CloseNativeView();
      break;
    case kOpenAdsPrivacySettings:
      OpenPrivacySandboxSettings();
      break;
    case kOpenMeasurementSettings:
      OpenPrivacySandboxAdMeasurementSettings();
      break;
  }
}

void PrivacySandboxDialogView::CloseNativeView() {
  GetWidget()->Close();
}

void PrivacySandboxDialogView::ResizeNativeView(int height) {
  const int max_height = browser_->GetWebContentsModalDialogHostForWindow()
                             ->GetMaximumDialogSize()
                             .height();
  const int target_height = std::min(height, max_height);
  web_view_->SetPreferredSize(
      gfx::Size(web_view_->GetPreferredSize().width(), target_height));
  constrained_window::UpdateWebContentsModalDialogPosition(
      GetWidget(), browser_->GetWebContentsModalDialogHostForWindow());
}

PrivacySandboxNotice PrivacySandboxDialogView::GetPrivacySandboxNotice() {
  return notice_;
}

void PrivacySandboxDialogView::ShowNativeView() {
  GetWidget()->Show();
  web_view_->RequestFocus();
}

void PrivacySandboxDialogView::OpenPrivacySandboxSettings() {
  DCHECK(browser_);
  chrome::ShowPrivacySandboxSettings(browser_->GetBrowserForMigrationOnly());
}

void PrivacySandboxDialogView::OpenPrivacySandboxAdMeasurementSettings() {
  CHECK(browser_);
  chrome::ShowPrivacySandboxAdMeasurementSettings(
      browser_->GetBrowserForMigrationOnly());
}

content::WebContents* PrivacySandboxDialogView::GetWebContentsForTesting() {
  CHECK(web_view_);
  return web_view_->GetWebContents();
}

void PrivacySandboxDialogView::SetPrivacySandboxNotice(
    privacy_sandbox::notice::mojom::PrivacySandboxNotice notice) {
  notice_ = notice;
}

BEGIN_METADATA(PrivacySandboxDialogView)
END_METADATA