File: web_authentication_proxy_api.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 (185 lines) | stat: -rw-r--r-- 7,176 bytes parent folder | download | duplicates (4)
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
// 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/extensions/api/web_authentication_proxy/web_authentication_proxy_api.h"

#include "base/functional/bind.h"
#include "base/logging.h"
#include "chrome/browser/extensions/api/web_authentication_proxy/web_authentication_proxy_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/web_authentication_proxy.h"
#include "content/public/browser/browser_context.h"

namespace extensions {

BrowserContextKeyedAPIFactory<WebAuthenticationProxyAPI>*
WebAuthenticationProxyAPI::GetFactoryInstance() {
  static base::NoDestructor<
      BrowserContextKeyedAPIFactory<WebAuthenticationProxyAPI>>
      instance;
  return instance.get();
}

WebAuthenticationProxyAPI::WebAuthenticationProxyAPI(
    content::BrowserContext* context)
    : context_(context) {
  EventRouter::Get(context_)->RegisterObserver(
      this,
      api::web_authentication_proxy::OnRemoteSessionStateChange::kEventName);
}

WebAuthenticationProxyAPI::~WebAuthenticationProxyAPI() = default;

void WebAuthenticationProxyAPI::Shutdown() {
  EventRouter::Get(context_)->UnregisterObserver(this);
}

void WebAuthenticationProxyAPI::OnListenerAdded(
    const EventListenerInfo& details) {
  DCHECK_EQ(
      details.event_name,
      api::web_authentication_proxy::OnRemoteSessionStateChange::kEventName);
  // This may be called multiple times for the same extension, but we only need
  // to instantiate a notifier once.
  session_state_change_notifiers_.try_emplace(
      details.extension_id, EventRouter::Get(context_), details.extension_id);
}

void WebAuthenticationProxyAPI::OnListenerRemoved(
    const EventListenerInfo& details) {
  DCHECK_EQ(
      details.event_name,
      api::web_authentication_proxy::OnRemoteSessionStateChange::kEventName);
  if (EventRouter::Get(context_)->ExtensionHasEventListener(
          details.extension_id, api::web_authentication_proxy::
                                    OnRemoteSessionStateChange::kEventName)) {
    // This wasn't necessarily the last remaining listener for this extension.
    return;
  }
  auto it = session_state_change_notifiers_.find(details.extension_id);
  CHECK(it != session_state_change_notifiers_.end());
  session_state_change_notifiers_.erase(it);
}

WebAuthenticationProxyAttachFunction::WebAuthenticationProxyAttachFunction() =
    default;
WebAuthenticationProxyAttachFunction::~WebAuthenticationProxyAttachFunction() =
    default;

ExtensionFunction::ResponseAction WebAuthenticationProxyAttachFunction::Run() {
  DCHECK(extension());

  const bool success =
      WebAuthenticationProxyRegistrarFactory::GetForBrowserContext(
          browser_context())
          ->SetRequestProxy(Profile::FromBrowserContext(browser_context()),
                            extension());
  return RespondNow(success ? NoArguments()
                            : Error("Another extension is already attached"));
}

WebAuthenticationProxyDetachFunction::WebAuthenticationProxyDetachFunction() =
    default;
WebAuthenticationProxyDetachFunction::~WebAuthenticationProxyDetachFunction() =
    default;

ExtensionFunction::ResponseAction WebAuthenticationProxyDetachFunction::Run() {
  DCHECK(extension());

  WebAuthenticationProxyService* proxy_service =
      WebAuthenticationProxyService::GetIfProxyAttached(browser_context());
  if (!proxy_service || proxy_service->GetActiveRequestProxy() != extension()) {
    return RespondNow(NoArguments());
  }

  WebAuthenticationProxyRegistrar* proxy_registrar =
      WebAuthenticationProxyRegistrarFactory::GetForBrowserContext(
          browser_context());
  proxy_registrar->ClearRequestProxy(
      Profile::FromBrowserContext(browser_context()));
  return RespondNow(NoArguments());
}

WebAuthenticationProxyCompleteCreateRequestFunction::
    WebAuthenticationProxyCompleteCreateRequestFunction() = default;
WebAuthenticationProxyCompleteCreateRequestFunction::
    ~WebAuthenticationProxyCompleteCreateRequestFunction() = default;

void WebAuthenticationProxyCompleteCreateRequestFunction::DoRespond(
    std::optional<std::string> error) {
  Respond(error ? Error(std::move(*error)) : NoArguments());
}

ExtensionFunction::ResponseAction
WebAuthenticationProxyCompleteCreateRequestFunction::Run() {
  DCHECK(extension());
  auto params =
      api::web_authentication_proxy::CompleteCreateRequest::Params::Create(
          args());
  EXTENSION_FUNCTION_VALIDATE(params);
  WebAuthenticationProxyService* proxy_service =
      WebAuthenticationProxyService::GetIfProxyAttached(browser_context());
  if (!proxy_service || proxy_service->GetActiveRequestProxy() != extension()) {
    return RespondNow(Error("Invalid sender"));
  }
  proxy_service->CompleteCreateRequest(
      params->details,
      base::BindOnce(
          &WebAuthenticationProxyCompleteCreateRequestFunction::DoRespond,
          this));
  return did_respond() ? AlreadyResponded() : RespondLater();
}

WebAuthenticationProxyCompleteGetRequestFunction::
    WebAuthenticationProxyCompleteGetRequestFunction() = default;
WebAuthenticationProxyCompleteGetRequestFunction::
    ~WebAuthenticationProxyCompleteGetRequestFunction() = default;

void WebAuthenticationProxyCompleteGetRequestFunction::DoRespond(
    std::optional<std::string> error) {
  Respond(error ? Error(std::move(*error)) : NoArguments());
}

ExtensionFunction::ResponseAction
WebAuthenticationProxyCompleteGetRequestFunction::Run() {
  DCHECK(extension());
  auto params =
      api::web_authentication_proxy::CompleteGetRequest::Params::Create(args());
  EXTENSION_FUNCTION_VALIDATE(params);
  WebAuthenticationProxyService* proxy_service =
      WebAuthenticationProxyService::GetIfProxyAttached(browser_context());
  if (!proxy_service || proxy_service->GetActiveRequestProxy() != extension()) {
    return RespondNow(Error("Invalid sender"));
  }
  proxy_service->CompleteGetRequest(
      params->details,
      base::BindOnce(
          &WebAuthenticationProxyCompleteGetRequestFunction::DoRespond, this));
  return did_respond() ? AlreadyResponded() : RespondLater();
}

WebAuthenticationProxyCompleteIsUvpaaRequestFunction::
    WebAuthenticationProxyCompleteIsUvpaaRequestFunction() = default;
WebAuthenticationProxyCompleteIsUvpaaRequestFunction::
    ~WebAuthenticationProxyCompleteIsUvpaaRequestFunction() = default;

ExtensionFunction::ResponseAction
WebAuthenticationProxyCompleteIsUvpaaRequestFunction::Run() {
  DCHECK(extension());
  auto params =
      api::web_authentication_proxy::CompleteIsUvpaaRequest::Params::Create(
          args());
  EXTENSION_FUNCTION_VALIDATE(params);
  WebAuthenticationProxyService* proxy_service =
      WebAuthenticationProxyService::GetIfProxyAttached(browser_context());
  if (!proxy_service || proxy_service->GetActiveRequestProxy() != extension()) {
    return RespondNow(Error("Invalid sender"));
  }
  if (!proxy_service->CompleteIsUvpaaRequest(params->details)) {
    return RespondNow(Error("Invalid request id"));
  }
  return RespondNow(NoArguments());
}

}  // namespace extensions