File: per_device_provisioning_permission.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 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 (167 lines) | stat: -rw-r--r-- 6,331 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
// Copyright 2019 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/media/android/cdm/per_device_provisioning_permission.h"

#include <utility>

#include "base/functional/callback.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "base/time/time.h"
#include "chrome/browser/android/android_theme_resources.h"
#include "components/content_settings/core/common/content_settings_types.h"
#include "components/permissions/permission_decision.h"
#include "components/permissions/permission_request.h"
#include "components/permissions/permission_request_data.h"
#include "components/permissions/permission_request_manager.h"
#include "components/permissions/request_type.h"
#include "components/permissions/resolvers/content_setting_permission_resolver.h"
#include "components/strings/grit/components_strings.h"
#include "components/url_formatter/elide_url.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "media/base/android/media_drm_bridge.h"
#include "ui/base/l10n/l10n_util.h"
#include "url/origin.h"

namespace {

// Only keep track of the last response for a short period of time.
constexpr base::TimeDelta kLastRequestDelta = base::Minutes(15);

// Keep track of the last response. This is only kept in memory, so once Chrome
// quits it is forgotten.
class LastResponse {
 public:
  // If |origin| matches the previously saved |origin_| and this request is
  // before |expiry_time|, return true indicating that the previous value should
  // be used, and update |allowed| with the previous response. If the origin
  // doesn't match or the previous response was too long ago, return false.
  bool Matches(const url::Origin& origin, bool* allowed) {
    if (!origin_.IsSameOriginWith(origin) || base::Time::Now() > expiry_time_)
      return false;

    *allowed = allowed_;
    return true;
  }

  // Updates this object with the latest |origin| and |response|.
  void Update(const url::Origin& origin, bool response) {
    origin_ = origin;
    expiry_time_ = base::Time::Now() + kLastRequestDelta;
    allowed_ = response;
  }

 private:
  url::Origin origin_;
  base::Time expiry_time_;
  bool allowed_ = false;
};

// Returns an object containing the last response. We only keep track of one
// response (the latest). This is done for simplicity, as it is unlikely that
// there will different origins getting to this path at the same time. Requests
// could be from different |render_frame_host| objects, but this matches what
// normal permission requests do when the decision is persisted in user's
// profile.
LastResponse& GetLastResponse() {
  static base::NoDestructor<LastResponse> s_last_response;
  return *s_last_response;
}

// A permissions::PermissionRequest to allow MediaDrmBridge to use per-device
// provisioning.
class PerDeviceProvisioningPermissionRequest final
    : public permissions::PermissionRequest {
 public:
  PerDeviceProvisioningPermissionRequest(
      const url::Origin& origin,
      base::OnceCallback<void(bool)> callback)
      : PermissionRequest(
            std::make_unique<permissions::PermissionRequestData>(
                std::make_unique<permissions::ContentSettingPermissionResolver>(
                    ContentSettingsType::PROTECTED_MEDIA_IDENTIFIER),
                /*user_gesture=*/false,
                origin.GetURL()),
            base::BindRepeating(
                &PerDeviceProvisioningPermissionRequest::PermissionDecided,
                base::Unretained(this)),
            base::BindOnce(
                &PerDeviceProvisioningPermissionRequest::RequestFinished,
                base::Unretained(this))),
        origin_(origin),
        callback_(std::move(callback)) {}

  PerDeviceProvisioningPermissionRequest(
      const PerDeviceProvisioningPermissionRequest&) = delete;
  PerDeviceProvisioningPermissionRequest& operator=(
      const PerDeviceProvisioningPermissionRequest&) = delete;

  void PermissionDecided(
      PermissionDecision decision,
      bool is_final_decision,
      const permissions::PermissionRequestData& request_data) {
    DCHECK(decision != PermissionDecision::kAllowThisTime);
    DCHECK(!is_final_decision);
    const bool granted = decision == PermissionDecision::kAllow;
    UpdateLastResponse(granted);
    std::move(callback_).Run(granted);
  }

  void RequestFinished() {
    // The |callback_| may not have run if the prompt was ignored, e.g. the tab
    // was closed while the prompt was displayed. Don't save this result as the
    // last response since it wasn't really a user action.
    if (callback_)
      std::move(callback_).Run(false);
  }

 private:
  void UpdateLastResponse(bool allowed) {
    GetLastResponse().Update(origin_, allowed);
  }

  const url::Origin origin_;
  base::OnceCallback<void(bool)> callback_;
};

}  // namespace

void RequestPerDeviceProvisioningPermission(
    content::RenderFrameHost* render_frame_host,
    base::OnceCallback<void(bool)> callback) {
  DVLOG(1) << __func__;
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  DCHECK(render_frame_host);
  DCHECK(callback);

  // Return the previous response if it was for the same origin.
  bool last_response = false;
  if (GetLastResponse().Matches(render_frame_host->GetLastCommittedOrigin(),
                                &last_response)) {
    DVLOG(1) << "Using previous response: " << last_response;
    std::move(callback).Run(last_response);
    return;
  }

  auto* web_contents =
      content::WebContents::FromRenderFrameHost(render_frame_host);
  DCHECK(web_contents) << "WebContents not available.";

  auto* permission_request_manager =
      permissions::PermissionRequestManager::FromWebContents(web_contents);
  if (!permission_request_manager) {
    std::move(callback).Run(false);
    return;
  }

  // The created PerDeviceProvisioningPermissionRequest deletes itself once
  // complete. See PerDeviceProvisioningPermissionRequest::DeleteRequest().
  permission_request_manager->AddRequest(
      render_frame_host,
      std::make_unique<PerDeviceProvisioningPermissionRequest>(
          render_frame_host->GetLastCommittedOrigin(), std::move(callback)));
}