1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef SERVICES_NETWORK_TRUST_TOKENS_HAS_TRUST_TOKENS_ANSWERER_H_
6 #define SERVICES_NETWORK_TRUST_TOKENS_HAS_TRUST_TOKENS_ANSWERER_H_
7 
8 #include <memory>
9 
10 #include "base/callback.h"
11 #include "services/network/public/mojom/trust_tokens.mojom.h"
12 #include "services/network/trust_tokens/pending_trust_token_store.h"
13 #include "services/network/trust_tokens/suitable_trust_token_origin.h"
14 #include "services/network/trust_tokens/trust_token_store.h"
15 #include "url/origin.h"
16 
17 namespace network {
18 
19 // HasTrustTokensAnswerer is a class bound to a top-level origin, able to answer
20 // queries about whether the user possesses trust tokens issued by any issuer
21 // origin associated with the top-level origin.
22 //
23 // When receiving a HasTrustTokens(issuer) call, the answerer attempts to
24 // associate |issuer| with the bound top-level origin; if this is not possible,
25 // it returns an error (see the HasTrustTokensAnswerer Mojo interface's comment
26 // for all possible error returns).
27 class HasTrustTokensAnswerer : public mojom::HasTrustTokensAnswerer {
28  public:
29   // Constructs a new answerer bound to the given top frame origin.
30   HasTrustTokensAnswerer(SuitableTrustTokenOrigin top_frame_origin,
31                          PendingTrustTokenStore* pending_trust_token_store);
32 
33   ~HasTrustTokensAnswerer() override;
34 
35   HasTrustTokensAnswerer(const HasTrustTokensAnswerer&) = delete;
36   HasTrustTokensAnswerer& operator=(const HasTrustTokensAnswerer&) = delete;
37 
38   // mojom::HasTrustTokensAnswerer:
39   void HasTrustTokens(const url::Origin& issuer,
40                       HasTrustTokensCallback callback) override;
41 
42  private:
43   // Continuation of HasTrustTokens: uses |trust_token_store| to answer a
44   // HasTrusttokens query against |issuer|.
45   //
46   // Requires that |issuer| is potentially trustworthy and HTTP or HTTPS.
47   void AnswerQueryWithStore(const SuitableTrustTokenOrigin& issuer,
48                             HasTrustTokensCallback callback,
49                             TrustTokenStore* trust_token_store);
50 
51   const SuitableTrustTokenOrigin top_frame_origin_;
52   PendingTrustTokenStore* pending_trust_token_store_;
53 
54   base::WeakPtrFactory<HasTrustTokensAnswerer> weak_factory_{this};
55 };
56 
57 }  // namespace network
58 
59 #endif  // SERVICES_NETWORK_TRUST_TOKENS_HAS_TRUST_TOKENS_ANSWERER_H_
60