File: auto_pip_setting_helper.cc

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; 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 (228 lines) | stat: -rw-r--r-- 9,498 bytes parent folder | download | duplicates (3)
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
// Copyright 2023 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/picture_in_picture/auto_pip_setting_helper.h"

#include "base/metrics/histogram_functions.h"
#include "chrome/browser/content_settings/host_content_settings_map_factory.h"
#include "chrome/browser/permissions/permission_decision_auto_blocker_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/content_settings/core/browser/host_content_settings_map.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/permissions/permission_decision_auto_blocker.h"
#include "content/public/browser/web_contents.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_recorder.h"

// static
std::unique_ptr<AutoPipSettingHelper>
AutoPipSettingHelper::CreateForWebContents(
    content::WebContents* web_contents,
    HostContentSettingsMap* settings_map,
    permissions::PermissionDecisionAutoBlockerBase* auto_blocker) {
  return std::make_unique<AutoPipSettingHelper>(
      web_contents->GetLastCommittedURL(), settings_map, auto_blocker);
}

AutoPipSettingHelper::AutoPipSettingHelper(
    const GURL& origin,
    HostContentSettingsMap* settings_map,
    permissions::PermissionDecisionAutoBlockerBase* auto_blocker)
    : origin_(origin),
      settings_map_(settings_map),
      auto_blocker_(auto_blocker) {}

AutoPipSettingHelper::~AutoPipSettingHelper() = default;

void AutoPipSettingHelper::OnUserClosedWindow(
    media::PictureInPictureEventsInfo::AutoPipReason auto_pip_reason,
    std::optional<ukm::SourceId> source_id) {
  if (ui_was_shown_but_not_acknowledged_) {
    RecordResult(PromptResult::kIgnored, auto_pip_reason, std::move(source_id));

    // Usually, this isn't needed, since any later pip window that reuses us
    // will be for the same site and will still be set to 'ASK'.  In that case,
    // we'll show the permission UI.  However, if the permission changes out
    // from under us somehow (e.g., the user sets it to allow via the permission
    // chip), then future windows might not show the prompt.  This ensures that
    // closing those windows, which were allowed, don't fiddle with the embargo.
    // It doesn't really matter, but for completeness we do it.
    ui_was_shown_but_not_acknowledged_ = false;

    if (auto_blocker_) {
      auto_blocker_->RecordDismissAndEmbargo(
          origin_, ContentSettingsType::AUTO_PICTURE_IN_PICTURE,
          /*dismissed_prompt_was_quiet=*/false);
    }
  }
}

ContentSetting AutoPipSettingHelper::GetEffectiveContentSetting() {
  auto setting = settings_map_->GetContentSetting(
      origin_, /*secondary_url=*/GURL(),
      ContentSettingsType::AUTO_PICTURE_IN_PICTURE);

  if (setting == CONTENT_SETTING_ASK && auto_blocker_) {
    if (auto_blocker_->IsEmbargoed(
            origin_, ContentSettingsType::AUTO_PICTURE_IN_PICTURE)) {
      return CONTENT_SETTING_BLOCK;
    }
  }

  return setting;
}

void AutoPipSettingHelper::UpdateContentSetting(ContentSetting new_setting) {
  content_settings::ContentSettingConstraints constraints;
  constraints.set_session_model(content_settings::mojom::SessionModel::DURABLE);

  settings_map_->SetContentSettingDefaultScope(
      origin_, /*secondary_url=*/GURL(),
      ContentSettingsType::AUTO_PICTURE_IN_PICTURE, new_setting, constraints);
}

AutoPipSettingHelper::ResultCb AutoPipSettingHelper::CreateResultCb(
    base::OnceClosure close_pip_cb,
    media::PictureInPictureEventsInfo::AutoPipReason auto_pip_reason,
    std::optional<ukm::SourceId> source_id) {
  weak_factory_.InvalidateWeakPtrs();
  return base::BindOnce(&AutoPipSettingHelper::OnUiResult,
                        weak_factory_.GetWeakPtr(), std::move(close_pip_cb),
                        auto_pip_reason, std::move(source_id));
}

std::unique_ptr<AutoPipSettingOverlayView>
AutoPipSettingHelper::CreateOverlayViewIfNeeded(
    base::OnceClosure close_pip_cb,
    media::PictureInPictureEventsInfo::AutoPipReason auto_pip_reason,
    std::optional<ukm::SourceId> source_id,
    views::View* anchor_view,
    views::BubbleBorder::Arrow arrow) {
  switch (GetEffectiveContentSetting()) {
    case CONTENT_SETTING_ASK:
      // If the user already said to allow once, then continue allowing.  It's
      // assumed that we're used for at most one visit to a site.
      if (already_selected_allow_once_) {
        RecordResult(PromptResult::kNotShownAllowedOnce, auto_pip_reason,
                     std::move(source_id));
        return nullptr;
      }
      // Create and return the UI to ask the user.
      ui_was_shown_but_not_acknowledged_ = true;
      return std::make_unique<AutoPipSettingOverlayView>(
          CreateResultCb(std::move(close_pip_cb), auto_pip_reason,
                         std::move(source_id)),
          origin_, anchor_view, arrow);
    case CONTENT_SETTING_ALLOW:
      // Nothing to do -- allow the auto pip to proceed.
      RecordResult(PromptResult::kNotShownAllowedOnEveryVisit, auto_pip_reason,
                   std::move(source_id));
      return nullptr;
    case CONTENT_SETTING_BLOCK:
      // Auto-pip is not allowed.  Close the window.
      RecordResult(PromptResult::kNotShownBlocked, auto_pip_reason,
                   std::move(source_id));
      std::move(close_pip_cb).Run();
      return nullptr;
    default:
      NOTREACHED() << " AutoPiP unknown effective content setting";
  }
}

void AutoPipSettingHelper::OnAutoPipBlockedByPermission(
    media::PictureInPictureEventsInfo::AutoPipReason auto_pip_reason,
    std::optional<ukm::SourceId> source_id) {
  RecordResult(PromptResult::kNotShownBlocked, auto_pip_reason,
               std::move(source_id));
}

void AutoPipSettingHelper::OnAutoPipBlockedByIncognito(
    media::PictureInPictureEventsInfo::AutoPipReason auto_pip_reason) {
  RecordResult(PromptResult::kNotShownIncognito, auto_pip_reason, std::nullopt);
}

void AutoPipSettingHelper::OnUiResult(
    base::OnceClosure close_pip_cb,
    media::PictureInPictureEventsInfo::AutoPipReason auto_pip_reason,
    std::optional<ukm::SourceId> source_id,
    AutoPipSettingView::UiResult result) {
  // The UI was both shown and acknowledged, so we don't have to worry about it
  // being dismissed without being acted on for the permission embargo.
  ui_was_shown_but_not_acknowledged_ = false;
  switch (result) {
    case AutoPipSettingView::UiResult::kBlock:
      RecordResult(PromptResult::kBlock, auto_pip_reason, std::move(source_id));
      UpdateContentSetting(CONTENT_SETTING_BLOCK);
      // Also close the pip window.
      std::move(close_pip_cb).Run();
      break;
    case AutoPipSettingView::UiResult::kAllowOnEveryVisit:
      RecordResult(PromptResult::kAllowOnEveryVisit, auto_pip_reason,
                   std::move(source_id));
      UpdateContentSetting(CONTENT_SETTING_ALLOW);
      break;
    case AutoPipSettingView::UiResult::kAllowOnce:
      already_selected_allow_once_ = true;
      RecordResult(PromptResult::kAllowOnce, auto_pip_reason,
                   std::move(source_id));
      // Leave at 'ASK'.  Do not update the embargo, since the user allowed the
      // feature to continue.  If anything, this should vote for 'anti-embargo'.
      break;
  }
}

void AutoPipSettingHelper::RecordResult(
    PromptResult result,
    media::PictureInPictureEventsInfo::AutoPipReason auto_pip_reason,
    std::optional<ukm::SourceId> source_id) {
  base::UmaHistogramEnumeration("Media.AutoPictureInPicture.PromptResultV2",
                                result);
  switch (auto_pip_reason) {
    case media::PictureInPictureEventsInfo::AutoPipReason::kUnknown:
      break;
    case media::PictureInPictureEventsInfo::AutoPipReason::kVideoConferencing:
      base::UmaHistogramEnumeration(
          "Media.AutoPictureInPicture.EnterPictureInPicture.AutomaticReason."
          "VideoConferencing.PromptResultV2",
          result);
      RecordUkms(auto_pip_reason, source_id, result);
      break;
    case media::PictureInPictureEventsInfo::AutoPipReason::kMediaPlayback:
      base::UmaHistogramEnumeration(
          "Media.AutoPictureInPicture.EnterPictureInPicture.AutomaticReason."
          "MediaPlayback.PromptResultV2",
          result);
      RecordUkms(auto_pip_reason, source_id, result);
      break;
  }
}

void AutoPipSettingHelper::RecordUkms(
    media::PictureInPictureEventsInfo::AutoPipReason auto_pip_reason,
    std::optional<ukm::SourceId> source_id,
    PromptResult result) const {
  ukm::UkmRecorder* ukm_recorder = ukm::UkmRecorder::Get();
  if (!ukm_recorder || !source_id) {
    return;
  }

  switch (auto_pip_reason) {
    case media::PictureInPictureEventsInfo::AutoPipReason::kUnknown:
      break;
    case media::PictureInPictureEventsInfo::AutoPipReason::kVideoConferencing:
      ukm::builders::
          Media_AutoPictureInPicture_EnterPictureInPicture_AutomaticReason_PromptResultV2(
              source_id.value())
              .SetVideoConferencing(static_cast<uintmax_t>(result))
              .Record(ukm_recorder);
      break;
    case media::PictureInPictureEventsInfo::AutoPipReason::kMediaPlayback:
      ukm::builders::
          Media_AutoPictureInPicture_EnterPictureInPicture_AutomaticReason_PromptResultV2(
              source_id.value())
              .SetMediaPlayback(static_cast<uintmax_t>(result))
              .Record(ukm_recorder);
      break;
  }
}