File: chromeos_smart_card_delegate.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 (158 lines) | stat: -rw-r--r-- 6,159 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
// 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/smart_card/chromeos_smart_card_delegate.h"

#include "base/check.h"
#include "base/check_deref.h"
#include "base/time/time.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/smart_card/get_smart_card_context_factory.h"
#include "chrome/browser/smart_card/smart_card_permission_context.h"
#include "chrome/browser/smart_card/smart_card_permission_context_factory.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "components/content_settings/browser/page_specific_content_settings.h"
#include "components/tabs/public/tab_interface.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "third_party/blink/public/common/features.h"

namespace {

// kSmartCardAllowedReconnectTime is amount of time after which a new connection
// to a smart card reader stops being deemed a reconnection (and isn't possible
// from the background).
BASE_FEATURE_PARAM(base::TimeDelta,
                   kSmartCardAllowedReconnectTime,
                   &blink::features::kSmartCard,
                   "allowed_reconnect_time",
                   base::Seconds(15));

// The check whether the window has focus is enough here, as this is for IWAs
// only, which appear in standalone windows dedicated to only them.
bool WindowHasFocus(content::RenderFrameHost& render_frame_host) {
  content::WebContents* tab_web_contents =
      content::WebContents::FromRenderFrameHost(&render_frame_host)
          ->GetOutermostWebContents();
  CHECK(tab_web_contents);
  // For the case when this is called in permission revoke handler called after
  // closing the last windows of the origin (and thus expiring the one-time
  // permission). Calling `tabs::TabInterface::GetFromContents` when the tab is
  // closing will crash.
  if (tab_web_contents->IsBeingDestroyed()) {
    return false;
  }

  return tabs::TabInterface::GetFromContents(tab_web_contents)
      ->GetBrowserWindowInterface()
      ->IsActive();
}

bool RecentlyHadConnection(content::RenderFrameHost& render_frame_host) {
  auto& pscs =
      CHECK_DEREF(content_settings::PageSpecificContentSettings::GetForFrame(
          &render_frame_host));
  return pscs.GetLastUsedTime(ContentSettingsType::SMART_CARD_GUARD) >=
             base::Time::Now() - kSmartCardAllowedReconnectTime.Get() ||
         pscs.IsInUse(ContentSettingsType::SMART_CARD_GUARD);
}

bool HasFocusOrRecentlyHadConnection(
    content::RenderFrameHost& render_frame_host) {
  return WindowHasFocus(render_frame_host) ||
         RecentlyHadConnection(render_frame_host);
}

}  // namespace

ChromeOsSmartCardDelegate::ChromeOsSmartCardDelegate() = default;
ChromeOsSmartCardDelegate::~ChromeOsSmartCardDelegate() = default;

mojo::PendingRemote<device::mojom::SmartCardContextFactory>
ChromeOsSmartCardDelegate::GetSmartCardContextFactory(
    content::BrowserContext& browser_context) {
  return ::GetSmartCardContextFactory(browser_context);
}

bool ChromeOsSmartCardDelegate::IsPermissionBlocked(
    content::RenderFrameHost& render_frame_host) {
  auto& profile = CHECK_DEREF(
      Profile::FromBrowserContext(render_frame_host.GetBrowserContext()));

  auto& permission_context =
      SmartCardPermissionContextFactory::GetForProfile(profile);

  const url::Origin& origin =
      render_frame_host.GetMainFrame()->GetLastCommittedOrigin();

  return !permission_context.CanRequestObjectPermission(origin) &&
         !permission_context.IsAllowlistedByPolicy(origin);
}

bool ChromeOsSmartCardDelegate::HasReaderPermission(
    content::RenderFrameHost& render_frame_host,
    const std::string& reader_name) {
  auto& profile = CHECK_DEREF(
      Profile::FromBrowserContext(render_frame_host.GetBrowserContext()));

  auto& permission_context =
      SmartCardPermissionContextFactory::GetForProfile(profile);

  return HasFocusOrRecentlyHadConnection(render_frame_host) &&
         permission_context.HasReaderPermission(render_frame_host, reader_name);
}

void ChromeOsSmartCardDelegate::RequestReaderPermission(
    content::RenderFrameHost& render_frame_host,
    const std::string& reader_name,
    RequestReaderPermissionCallback callback) {
  // Never allow requesting permissions from the background.
  if (!WindowHasFocus(render_frame_host)) {
    std::move(callback).Run(false);
    return;
  }

  auto& profile = CHECK_DEREF(
      Profile::FromBrowserContext(render_frame_host.GetBrowserContext()));

  auto& permission_context =
      SmartCardPermissionContextFactory::GetForProfile(profile);

  permission_context.RequestReaderPermisssion(render_frame_host, reader_name,
                                              std::move(callback));
}

void ChromeOsSmartCardDelegate::NotifyConnectionUsed(
    content::RenderFrameHost& render_frame_host) {
  CHECK_DEREF(content_settings::PageSpecificContentSettings::GetForFrame(
                  &render_frame_host))
      .OnDeviceUsed(
          content_settings::mojom::ContentSettingsType::SMART_CARD_GUARD);
}

void ChromeOsSmartCardDelegate::NotifyLastConnectionLost(
    content::RenderFrameHost& render_frame_host) {
  CHECK_DEREF(content_settings::PageSpecificContentSettings::GetForFrame(
                  &render_frame_host))
      .OnLastDeviceConnectionLost(
          content_settings::mojom::ContentSettingsType::SMART_CARD_GUARD);
}

void ChromeOsSmartCardDelegate::AddObserver(
    content::RenderFrameHost& render_frame_host,
    PermissionObserver* observer) {
  SmartCardPermissionContextFactory::GetForProfile(
      CHECK_DEREF(
          Profile::FromBrowserContext(render_frame_host.GetBrowserContext())))
      .AddObserver(observer);
}

void ChromeOsSmartCardDelegate::RemoveObserver(
    content::RenderFrameHost& render_frame_host,
    PermissionObserver* observer) {
  SmartCardPermissionContextFactory::GetForProfile(
      CHECK_DEREF(
          Profile::FromBrowserContext(render_frame_host.GetBrowserContext())))
      .RemoveObserver(observer);
}