File: one_time_permission_provider.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,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 (390 lines) | stat: -rw-r--r-- 15,296 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// 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/content_settings/one_time_permission_provider.h"

#include <algorithm>
#include <memory>
#include <set>

#include "base/power_monitor/power_monitor.h"
#include "base/time/default_clock.h"
#include "base/time/time.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/browser_process_platform_part.h"
#include "chrome/browser/permissions/one_time_permissions_tracker.h"
#include "chrome/browser/permissions/one_time_permissions_tracker_factory.h"
#include "chrome/browser/permissions/one_time_permissions_tracker_observer.h"
#include "components/content_settings/core/browser/content_settings_registry.h"
#include "components/content_settings/core/browser/content_settings_rule.h"
#include "components/content_settings/core/browser/user_modifiable_provider.h"
#include "components/content_settings/core/common/content_settings.h"
#include "components/content_settings/core/common/content_settings_constraints.h"
#include "components/content_settings/core/common/content_settings_metadata.h"
#include "components/content_settings/core/common/content_settings_partition_key.h"
#include "components/content_settings/core/common/content_settings_utils.h"
#include "components/content_settings/core/common/features.h"
#include "components/permissions/permission_uma_util.h"
#include "components/permissions/permission_util.h"
#include "url/gurl.h"

OneTimePermissionProvider::OneTimePermissionProvider(
    OneTimePermissionsTracker* one_time_permissions_tracker)
    : one_time_permissions_tracker_(one_time_permissions_tracker),
      clock_(base::DefaultClock::GetInstance()) {
  one_time_permissions_tracker_->AddObserver(this);

  // The PowerMonitor is initialized in content_main_runner_impl.cc before the
  // main function for the browser process is run (which initializes the HCSM).
  // For this reason, the PowerMonitor is always initialized before the observer
  // is added here.
  base::PowerMonitor::GetInstance()->AddPowerSuspendObserver(this);
}

OneTimePermissionProvider::~OneTimePermissionProvider() {
  base::PowerMonitor::GetInstance()->RemovePowerSuspendObserver(this);
}

// TODO(b/307193732): handle the PartitionKey in all relevant methods, including
// when we call NotifyObservers().
std::unique_ptr<content_settings::RuleIterator>
OneTimePermissionProvider::GetRuleIterator(
    ContentSettingsType content_type,
    bool incognito,
    const content_settings::PartitionKey& partition_key) const {
  if (!permissions::PermissionUtil::DoesStoreTemporaryGrantsInHcsm(
          content_type)) {
    return nullptr;
  }
  return value_map_.GetRuleIterator(content_type);
}

std::unique_ptr<content_settings::Rule> OneTimePermissionProvider::GetRule(
    const GURL& primary_url,
    const GURL& secondary_url,
    ContentSettingsType content_type,
    bool off_the_record,
    const content_settings::PartitionKey& partition_key) const {
  if (!permissions::PermissionUtil::DoesStoreTemporaryGrantsInHcsm(
          content_type)) {
    return nullptr;
  }

  base::AutoLock auto_lock(value_map_.GetLock());
  return value_map_.GetRule(primary_url, secondary_url, content_type);
}

bool OneTimePermissionProvider::SetWebsiteSetting(
    const ContentSettingsPattern& primary_pattern,
    const ContentSettingsPattern& secondary_pattern,
    ContentSettingsType content_settings_type,
    base::Value&& value,
    const content_settings::ContentSettingConstraints& constraints,
    const content_settings::PartitionKey& partition_key) {
  if (!permissions::PermissionUtil::DoesStoreTemporaryGrantsInHcsm(
          content_settings_type)) {
    return false;
  }

  if (!content_settings::ContentSettingsRegistry::GetInstance()->Get(
          content_settings_type)) {
    // Object permissions cannot be mapped to a ContentSetting and thus cannot
    // be handled by this provider.
    return false;
  }

  auto content_setting = content_settings::ValueToContentSetting(value);

  // For transitions to block and resetting to default, clear the grant and let
  // the pref provider handle the permission as usual.
  if (content_setting == CONTENT_SETTING_DEFAULT ||
      content_setting == CONTENT_SETTING_BLOCK) {
    {
      base::AutoLock lock(value_map_.GetLock());
      auto* previous_one_time_grant = value_map_.GetValue(
          primary_pattern.ToRepresentativeUrl(),
          secondary_pattern.ToRepresentativeUrl(), content_settings_type);

      if (!previous_one_time_grant) {
        // If there was no grant, it means that this content setting is
        // already being handled by the pref provider.
        return false;
      }

      value_map_.DeleteValue(primary_pattern, secondary_pattern,
                             content_settings_type);
    }

    NotifyObservers(primary_pattern, secondary_pattern, content_settings_type,
                    nullptr);

    permissions::PermissionUmaUtil::RecordOneTimePermissionEvent(
        content_settings_type,
        permissions::OneTimePermissionEvent::REVOKED_MANUALLY);

    return false;
  }

  if (constraints.session_model() !=
      content_settings::mojom::SessionModel::ONE_TIME) {
    if (content_setting == CONTENT_SETTING_ALLOW) {
      // Transition from Allow once to Allow. Delete setting and let the pref
      // provider handle it.
      base::AutoLock lock(value_map_.GetLock());
      value_map_.DeleteValue(primary_pattern, secondary_pattern,
                             content_settings_type);
    }

    return false;
  }

  DCHECK_EQ(content_settings::ValueToContentSetting(value),
            CONTENT_SETTING_ALLOW);

  base::Time now = clock_->Now();
  content_settings::RuleMetaData metadata;
  metadata.set_session_model(content_settings::mojom::SessionModel::ONE_TIME);
  metadata.set_last_modified(now);
  if (content_settings::ShouldTypeExpireActively(content_settings_type) &&
      !constraints.lifetime().is_zero()) {
    metadata.SetExpirationAndLifetime(now + constraints.lifetime(),
                                      constraints.lifetime());
  }

  permissions::PermissionUmaUtil::RecordOneTimePermissionEvent(
      content_settings_type,
      permissions::OneTimePermissionEvent::GRANTED_ONE_TIME);

  {
    base::AutoLock lock(value_map_.GetLock());
    value_map_.SetValue(primary_pattern, secondary_pattern,
                        content_settings_type, std::move(value),
                        std::move(metadata));
  }

  NotifyObservers(primary_pattern, secondary_pattern, content_settings_type,
                  nullptr);

  // We need to handle transitions from Allow to Allow Once gracefully.
  // In that case we add the Allow Once setting in this provider, but also
  // have to clear the Allow setting in the pref provider. By returning false
  // here, we let the control flow trickle down to the pref provider.
  return false;
}

bool OneTimePermissionProvider::UpdateLastUsedTime(
    const GURL& primary_url,
    const GURL& secondary_url,
    ContentSettingsType content_type,
    const base::Time time,
    const content_settings::PartitionKey& partition_key) {
  // Last used time is not tracked for one-time permissions.
  return false;
}

bool OneTimePermissionProvider::ResetLastVisitTime(
    const ContentSettingsPattern& primary_pattern,
    const ContentSettingsPattern& secondary_pattern,
    ContentSettingsType content_type,
    const content_settings::PartitionKey& partition_key) {
  // LastVisit time is not currently tracked for one-time permissions.
  return false;
}

bool OneTimePermissionProvider::UpdateLastVisitTime(
    const ContentSettingsPattern& primary_pattern,
    const ContentSettingsPattern& secondary_pattern,
    ContentSettingsType content_type,
    const content_settings::PartitionKey& partition_key) {
  // LastVisit time is not tracked for one-time permissions.
  return false;
}

std::optional<base::TimeDelta> OneTimePermissionProvider::RenewContentSetting(
    const GURL& primary_url,
    const GURL& secondary_url,
    ContentSettingsType type,
    std::optional<ContentSetting> setting_to_match,
    const content_settings::PartitionKey& partition_key) {
  // Setting renewal is not supported for one-time permissions.
  return std::nullopt;
}

void OneTimePermissionProvider::ClearAllContentSettingsRules(
    ContentSettingsType content_type,
    const content_settings::PartitionKey& partition_key) {
  if (permissions::PermissionUtil::DoesStoreTemporaryGrantsInHcsm(
          content_type)) {
    return;
  }
  base::AutoLock lock(value_map_.GetLock());
  value_map_.DeleteValues(content_type);
}

void OneTimePermissionProvider::ShutdownOnUIThread() {
  RemoveAllObservers();
}

void OneTimePermissionProvider::SetClockForTesting(const base::Clock* clock) {
  clock_ = clock;
}

void OneTimePermissionProvider::ExpireWebsiteSetting(
    const ContentSettingsPattern& primary_pattern,
    const ContentSettingsPattern& secondary_pattern,
    ContentSettingsType content_settings_type,
    const content_settings::PartitionKey& partition_key) {
  // Custom scope because NotifyObservers also requires value_map_'s exclusive
  // lock.
  {
    base::AutoLock lock(value_map_.GetLock());
    if (value_map_.GetValue(primary_pattern.ToRepresentativeUrl(),
                            secondary_pattern.ToRepresentativeUrl(),
                            content_settings_type) == nullptr) {
      return;
    }

    value_map_.DeleteValue(primary_pattern, secondary_pattern,
                           content_settings_type);
  }
  permissions::PermissionUmaUtil::RecordOneTimePermissionEvent(
      content_settings_type,
      permissions::OneTimePermissionEvent::EXPIRED_AFTER_MAXIMUM_LIFETIME);
  NotifyObservers(primary_pattern, secondary_pattern, content_settings_type,
                  /*partition_key=*/nullptr);
}

void OneTimePermissionProvider::OnSuspend() {
  std::vector<ContentSettingEntry> patterns_to_delete;
  auto* registry = content_settings::ContentSettingsRegistry::GetInstance();

  for (const auto* info : *registry) {
    auto setting_type = info->website_settings_info()->type();
    if (permissions::PermissionUtil::DoesStoreTemporaryGrantsInHcsm(
            setting_type)) {
      std::unique_ptr<content_settings::RuleIterator> rule_iterator(
          value_map_.GetRuleIterator(setting_type));

      while (rule_iterator && rule_iterator->HasNext()) {
        auto rule = rule_iterator->Next();
        patterns_to_delete.emplace_back(setting_type, rule->primary_pattern,
                                        rule->secondary_pattern);
        permissions::PermissionUmaUtil::RecordOneTimePermissionEvent(
            setting_type,
            permissions::OneTimePermissionEvent::EXPIRED_ON_SUSPEND);
      }
    }
  }

  DeleteEntriesAndNotify(patterns_to_delete);
}

// All tabs with the given origin have either been closed or navigated away
// from. We remove all permissions associated with the origin.
void OneTimePermissionProvider::OnLastPageFromOriginClosed(
    const url::Origin& origin) {
  for (auto setting_type :
       content_settings::GetTypesWithTemporaryGrantsInHcsm()) {
    DeleteEntriesMatchingGURL(
        setting_type, origin.GetURL(),
        permissions::OneTimePermissionEvent::ALL_TABS_CLOSED_OR_DISCARDED);
  }
}

// All tabs with the given origin have either been in the background for a
// certain time or not used for a certain time. This situation currently only
// expires geolocation. We remove the geolocation permission associated with
// the origin.
void OneTimePermissionProvider::OnAllTabsInBackgroundTimerExpired(
    const url::Origin& origin,
    const OneTimePermissionsTrackerObserver::BackgroundExpiryType&
        expiry_type) {
  switch (expiry_type) {
    case BackgroundExpiryType::kTimeout:
      DeleteEntriesMatchingGURL(
          ContentSettingsType::GEOLOCATION, origin.GetURL(),
          permissions::OneTimePermissionEvent::EXPIRED_IN_BACKGROUND);
      return;
    case BackgroundExpiryType::kLongTimeout:
      return;
  }
}

// All tabs to the origin have not shown a tab indicator for video for a
// certain time and have been in the background. We remove the camera
// permission associated with the origin.
void OneTimePermissionProvider::OnCapturingVideoExpired(
    const url::Origin& origin) {
  DeleteEntriesMatchingGURL(
      ContentSettingsType::MEDIASTREAM_CAMERA, origin.GetURL(),
      permissions::OneTimePermissionEvent::EXPIRED_IN_BACKGROUND);
}

// All tabs to the origin have not shown a tab indicator for microphone access
// for a certain time and have been in the background. We remove the
// microphone permission associated with the origin.
void OneTimePermissionProvider::OnCapturingAudioExpired(
    const url::Origin& origin) {
  DeleteEntriesMatchingGURL(
      ContentSettingsType::MEDIASTREAM_MIC, origin.GetURL(),
      permissions::OneTimePermissionEvent::EXPIRED_IN_BACKGROUND);
}

void OneTimePermissionProvider::DeleteEntriesAndNotify(
    const std::vector<OneTimePermissionProvider::ContentSettingEntry>&
        entries_to_delete) {
  {
    base::AutoLock lock(value_map_.GetLock());
    for (const auto& pattern : entries_to_delete) {
      value_map_.DeleteValue(pattern.primary_pattern, pattern.secondary_pattern,
                             pattern.type);

      // In all the observable `OneTimePermissionsTrackerObserver` events
      // which are emitted from the tracker, the tracker knows for which
      // origins and content settings it can halt bookkeeping. However, the
      // tracker isn't aware of externally deleted content settings. To
      // prevent it from triggering observers for an already deleted content
      // setting, we need to inform it about the deletion here (and only
      // here).
      one_time_permissions_tracker_->CleanupStateForExpiredContentSetting(
          pattern.type, pattern.primary_pattern, pattern.secondary_pattern);
    }
  }

  for (const auto& pattern : entries_to_delete) {
    NotifyObservers(pattern.primary_pattern, pattern.secondary_pattern,
                    pattern.type, /*partition_key=*/nullptr);
  }
}

void OneTimePermissionProvider::DeleteEntriesMatchingGURL(
    ContentSettingsType content_setting_type,
    const GURL& origin_gurl,
    permissions::OneTimePermissionEvent trigger_event) {
  std::vector<ContentSettingEntry> patterns_to_delete;
  std::unique_ptr<content_settings::RuleIterator> rule_iterator(
      value_map_.GetRuleIterator(content_setting_type));

  while (rule_iterator && rule_iterator->HasNext()) {
    auto rule = rule_iterator->Next();
    if (rule->primary_pattern.Matches(origin_gurl) &&
        rule->secondary_pattern.Matches(origin_gurl)) {
      patterns_to_delete.emplace_back(
          content_setting_type, rule->primary_pattern, rule->secondary_pattern);
      permissions::PermissionUmaUtil::RecordOneTimePermissionEvent(
          content_setting_type, trigger_event);
    }
  }

  rule_iterator.reset();

  DeleteEntriesAndNotify(patterns_to_delete);
}

void OneTimePermissionProvider::OnShutdown() {
  if (one_time_permissions_tracker_) {
    one_time_permissions_tracker_->RemoveObserver(this);
    one_time_permissions_tracker_ = nullptr;
  }
}