File: permission_blocked_message_delegate_android.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 (244 lines) | stat: -rw-r--r-- 8,892 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
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
// Copyright 2021 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/permissions/permission_blocked_message_delegate_android.h"

#include "chrome/browser/android/android_theme_resources.h"
#include "chrome/browser/android/resource_mapper.h"
#include "chrome/browser/permissions/quiet_notification_permission_ui_config.h"
#include "chrome/browser/permissions/quiet_notification_permission_ui_state.h"
#include "chrome/browser/permissions/quiet_permission_prompt_model_android.h"
#include "chrome/grit/generated_resources.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/messages/android/message_dispatcher_bridge.h"
#include "components/permissions/android/permission_prompt/permission_prompt_android.h"
#include "components/permissions/permission_request.h"
#include "components/permissions/permission_request_manager.h"
#include "components/permissions/permission_ui_selector.h"
#include "components/permissions/permission_util.h"
#include "components/strings/grit/components_strings.h"
#include "components/url_formatter/elide_url.h"
#include "content/public/browser/web_contents.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/strings/grit/ui_strings.h"

PermissionBlockedMessageDelegate::PermissionBlockedMessageDelegate(
    content::WebContents* web_contents,
    std::unique_ptr<Delegate> delegate)
    : content::WebContentsObserver(web_contents),
      web_contents_(web_contents),
      delegate_(std::move(delegate)) {
  message_ = std::make_unique<messages::MessageWrapper>(
      messages::MessageIdentifier::PERMISSION_BLOCKED,
      base::BindOnce(
          &PermissionBlockedMessageDelegate::HandlePrimaryActionClick,
          base::Unretained(this)),
      base::BindOnce(&PermissionBlockedMessageDelegate::HandleDismissCallback,
                     base::Unretained(this)));
  const ContentSettingsType content_setting_type =
      delegate_->GetContentSettingsType();
  int title = 0;
  int icon = 0;
  switch (content_setting_type) {
    case ContentSettingsType::NOTIFICATIONS:
      title = IDS_NOTIFICATION_QUIET_PERMISSION_INFOBAR_TITLE;
      icon = IDR_ANDROID_INFOBAR_NOTIFICATIONS_OFF;
      break;
    case ContentSettingsType::GEOLOCATION:
      title = IDS_LOCATION_QUIET_PERMISSION_MESSAGE_UI_TITLE;
      icon = IDR_ANDROID_MESSAGE_LOCATION_OFF;
      break;
    default:
      NOTREACHED();
  }
  message_->SetTitle(l10n_util::GetStringUTF16(title));

  // IDS_OK: notification will still be blocked if primary button is clicked.
  message_->SetPrimaryButtonText(l10n_util::GetStringUTF16(IDS_OK));
  message_->SetIconResourceId(ResourceMapper::MapToJavaDrawableId(icon));
  message_->SetSecondaryIconResourceId(
      ResourceMapper::MapToJavaDrawableId(IDR_ANDROID_MESSAGE_SETTINGS));

  message_->SetSecondaryActionCallback(
      base::BindRepeating(&PermissionBlockedMessageDelegate::HandleManageClick,
                          base::Unretained(this)));
  messages::MessageDispatcherBridge::Get()->EnqueueMessage(
      message_.get(), web_contents_, messages::MessageScopeType::NAVIGATION,
      messages::MessagePriority::kNormal);
}

PermissionBlockedMessageDelegate::~PermissionBlockedMessageDelegate() {
  DismissInternal();
}

void PermissionBlockedMessageDelegate::OnContinueBlocking() {
  has_interacted_with_dialog_ = true;
  dialog_controller_.reset();
  delegate_->Deny();
}

void PermissionBlockedMessageDelegate::OnAllowForThisSite() {
  has_interacted_with_dialog_ = true;
  dialog_controller_.reset();
  delegate_->Accept();
}

void PermissionBlockedMessageDelegate::OnLearnMoreClicked() {
  should_reshow_dialog_on_focus_ = true;
  dialog_controller_->DismissDialog();
  delegate_->SetLearnMoreClicked();
  web_contents_->OpenURL(
      content::OpenURLParams(GetNotificationBlockedLearnMoreUrl(),
                             content::Referrer(),
                             WindowOpenDisposition::NEW_FOREGROUND_TAB,
                             ui::PAGE_TRANSITION_LINK, false),
      /*navigation_handle_callback=*/{});
}

void PermissionBlockedMessageDelegate::OnOpenedSettings() {
  should_reshow_dialog_on_focus_ = true;
  delegate_->SetManageClicked();
}

void PermissionBlockedMessageDelegate::OnDialogDismissed() {
  if (!dialog_controller_) {
    // Dismissed by clicking on dialog buttons.
    return;
  }
  if (should_reshow_dialog_on_focus_) {
    // When the dialog has been dismissed due to the user clicking on
    // 'Learn more', do not clean up the dialog instance as the dialog
    // will be restored when the user navigates back to the original tab.
    return;
  }
  dialog_controller_.reset();
  // If |has_interacted_with_dialog_| is true, |Allow| or |Deny| should be
  // recorded instead.
  if (!has_interacted_with_dialog_) {
    // call Closing destroys the current object.
    delegate_->Closing();
  }
}

ContentSettingsType PermissionBlockedMessageDelegate::GetContentSettingsType() {
  return delegate_->GetContentSettingsType();
}

void PermissionBlockedMessageDelegate::OnWebContentsFocused(
    content::RenderWidgetHost* render_widget_host) {
  if (should_reshow_dialog_on_focus_ && dialog_controller_) {
    // This will be true only if the user has been redirected to
    // a new tab by clicking on 'Learn more' on the dialog.
    // Upon returning to the original tab from the redirected tab,
    // the dialog will be restored.
    should_reshow_dialog_on_focus_ = false;
    // If the page is navigated to another url, |this| will be destroyed
    // by the PermissionRequestManager, thereby causing message to be
    // dismissed and dialog_controller to dismiss the dialog.
    dialog_controller_->ShowDialog(*delegate_->ReasonForUsingQuietUi());
    // TODO(crbug.com/40818674): add browser tests to test if
    // webcontents have been navigated to another page in the meantime.
  }
}

void PermissionBlockedMessageDelegate::HandlePrimaryActionClick() {
  DCHECK(delegate_->ShouldUseQuietUI());
  delegate_->Deny();
}

void PermissionBlockedMessageDelegate::HandleManageClick() {
  DCHECK(!dialog_controller_);
  dialog_controller_ =
      std::make_unique<PermissionBlockedDialogController>(this, web_contents_);
  dialog_controller_->ShowDialog(*delegate_->ReasonForUsingQuietUi());
  messages::MessageDispatcherBridge::Get()->DismissMessage(
      message_.get(), messages::DismissReason::SECONDARY_ACTION);
}

void PermissionBlockedMessageDelegate::HandleDismissCallback(
    messages::DismissReason reason) {
  message_.reset();

  // When message is dismissed by secondary action, |permission_prompt_| should
  // be reset when the dialog is dismissed.
  if (reason == messages::DismissReason::SECONDARY_ACTION) {
    return;
  }

  dialog_controller_.reset();

  if (reason == messages::DismissReason::GESTURE) {
    delegate_->Closing();
  }
  // Other un-tracked actions will be recorded as "Ignored" by
  // |permission_prompt_|.
}

void PermissionBlockedMessageDelegate::DismissInternal() {
  if (message_) {
    messages::MessageDispatcherBridge::Get()->DismissMessage(
        message_.get(), messages::DismissReason::UNKNOWN);
  }
}

void PermissionBlockedMessageDelegate::Delegate::Accept() {
  if (!permission_prompt_) {
    return;
  }
  permission_prompt_->Accept();
}

void PermissionBlockedMessageDelegate::Delegate::Deny() {
  if (!permission_prompt_) {
    return;
  }
  permission_prompt_->Deny();
}

void PermissionBlockedMessageDelegate::Delegate::Closing() {
  if (!permission_prompt_) {
    return;
  }
  permission_prompt_->Closing();
}

void PermissionBlockedMessageDelegate::Delegate::SetManageClicked() {
  if (!permission_prompt_) {
    return;
  }
  permission_prompt_->SetManageClicked();
}

void PermissionBlockedMessageDelegate::Delegate::SetLearnMoreClicked() {
  if (!permission_prompt_) {
    return;
  }
  permission_prompt_->SetLearnMoreClicked();
}

bool PermissionBlockedMessageDelegate::Delegate::ShouldUseQuietUI() {
  return permission_prompt_->ShouldCurrentRequestUseQuietUI();
}

std::optional<permissions::PermissionUiSelector::QuietUiReason>
PermissionBlockedMessageDelegate::Delegate::ReasonForUsingQuietUi() {
  return permission_prompt_->ReasonForUsingQuietUi();
}

ContentSettingsType
PermissionBlockedMessageDelegate::Delegate::GetContentSettingsType() {
  // QuietUI is only supported for notifications and geolocation so there will
  // be only one ContentSettingsType in the queue.
  return permission_prompt_->GetContentSettingType(0);
}
PermissionBlockedMessageDelegate::Delegate::~Delegate() {
  Closing();
}

PermissionBlockedMessageDelegate::Delegate::Delegate() = default;

PermissionBlockedMessageDelegate::Delegate::Delegate(
    const base::WeakPtr<permissions::PermissionPromptAndroid>&
        permission_prompt)
    : permission_prompt_(permission_prompt) {}