blob: 619edc7a2dd8c47ca3192de18cb44b70f8f3f0a9 [file] [log] [blame]
Sam Goto9c87da7e2019-04-30 23:09:451// Copyright 2019 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
Majid Valipourae11335e2020-10-14 04:09:245#ifndef CONTENT_BROWSER_SMS_WEBOTP_SERVICE_H_
6#define CONTENT_BROWSER_SMS_WEBOTP_SERVICE_H_
Sam Goto9c87da7e2019-04-30 23:09:457
Sam Goto9c87da7e2019-04-30 23:09:458#include <memory>
Ayu Ishii3a50e492019-11-26 01:49:309#include <string>
Sam Goto9c87da7e2019-04-30 23:09:4510
Sam Goto36d91172019-07-18 02:55:2111#include "base/callback_forward.h"
Keishi Hattori0e45c022021-11-27 09:25:5212#include "base/memory/raw_ptr.h"
Sam Goto9c87da7e2019-04-30 23:09:4513#include "base/sequence_checker.h"
Sam Goto36d91172019-07-18 02:55:2114#include "base/time/time.h"
Yi Gu4f8c0472020-12-10 17:45:2815#include "base/timer/timer.h"
Yi Gu56cab022020-10-20 14:11:3116#include "content/browser/sms/sms_parser.h"
Sam Goto5cf068e82019-11-04 23:08:4417#include "content/browser/sms/sms_queue.h"
Majid Valipourdcd50fa2020-07-20 16:59:4318#include "content/browser/sms/user_consent_handler.h"
Sam Goto9c87da7e2019-04-30 23:09:4519#include "content/common/content_export.h"
Daniel Cheng9fb887ff2021-10-01 20:27:3820#include "content/public/browser/document_service.h"
Miyoung Shin8ff3aa892019-08-31 03:01:3821#include "mojo/public/cpp/bindings/pending_receiver.h"
Majid Valipourae11335e2020-10-14 04:09:2422#include "third_party/blink/public/mojom/sms/webotp_service.mojom.h"
Sam Goto36d91172019-07-18 02:55:2123#include "url/origin.h"
Sam Goto9b201f92019-05-29 22:46:2024
Sam Goto9c87da7e2019-04-30 23:09:4525namespace content {
26
Sam Goto36d91172019-07-18 02:55:2127class RenderFrameHost;
Sam Goto5cf068e82019-11-04 23:08:4428class SmsFetcher;
Sam Goto36d91172019-07-18 02:55:2129
Majid Valipourae11335e2020-10-14 04:09:2430// WebOTPService handles mojo connections from the renderer, observing the
31// incoming SMS messages from an SmsFetcher. In practice, it is owned and
32// managed by a RenderFrameHost. It accomplishes that via subclassing
Daniel Cheng9fb887ff2021-10-01 20:27:3833// DocumentService, which observes the lifecycle of a RenderFrameHost and
Majid Valipourae11335e2020-10-14 04:09:2434// manages it own memory. Create() creates a self-managed instance of
35// WebOTPService and binds it to the request.
36class CONTENT_EXPORT WebOTPService
Daniel Cheng9fb887ff2021-10-01 20:27:3837 : public DocumentService<blink::mojom::WebOTPService>,
Sam Goto5cf068e82019-11-04 23:08:4438 public SmsFetcher::Subscriber {
Sam Goto9c87da7e2019-04-30 23:09:4539 public:
Ken Buchanan8c41e562020-10-28 23:08:4040 // Return value indicates success. Creation can fail if origin requirements
41 // are not met.
42 static bool Create(SmsFetcher*,
Sam Goto36d91172019-07-18 02:55:2143 RenderFrameHost*,
Majid Valipourae11335e2020-10-14 04:09:2444 mojo::PendingReceiver<blink::mojom::WebOTPService>);
danakjc70aec1f2022-07-07 15:48:1945 static WebOTPService& CreateForTesting(
46 SmsFetcher*,
47 const OriginList&,
48 RenderFrameHost&,
49 mojo::PendingReceiver<blink::mojom::WebOTPService>);
Peter Boström828b9022021-09-21 02:28:4350
51 WebOTPService(const WebOTPService&) = delete;
52 WebOTPService& operator=(const WebOTPService&) = delete;
53
Majid Valipourae11335e2020-10-14 04:09:2454 ~WebOTPService() override;
Sam Goto9c87da7e2019-04-30 23:09:4555
Yi Gu835847b2021-03-18 02:58:3656 using FailureType = SmsFetchFailureType;
Yi Gu9621ff92020-10-08 04:38:1057 using SmsParsingStatus = SmsParser::SmsParsingStatus;
Majid Valipour24a2a302020-11-18 00:37:0958 using UserConsent = SmsFetcher::UserConsent;
Yi Gu9621ff92020-10-08 04:38:1059
Daniel Chengeb5e8542022-04-20 17:32:2160 // content::DocumentService:
61 void WillBeDestroyed(DocumentServiceDestructionReason) override;
62
Majid Valipourae11335e2020-10-14 04:09:2463 // blink::mojom::WebOTPService:
Ayu Ishiiac090532019-09-04 14:12:0664 void Receive(ReceiveCallback) override;
Ayu Ishii3a50e492019-11-26 01:49:3065 void Abort() override;
Sam Goto9c87da7e2019-04-30 23:09:4566
Sam Goto5cf068e82019-11-04 23:08:4467 // content::SmsQueue::Subscriber
Yi Gu684eb2b2021-03-09 00:40:3768 void OnReceive(const OriginList&,
69 const std::string& one_time_code,
70 UserConsent) override;
Yi Gu9621ff92020-10-08 04:38:1071 void OnFailure(FailureType failure_type) override;
Sam Goto5cf068e82019-11-04 23:08:4472
Majid Valipourdcd50fa2020-07-20 16:59:4373 // Completes the in-flight sms otp code request. Invokes the receive callback,
74 // if one is available, with the provided status code and the existing one
75 // time code.
76 void CompleteRequest(blink::mojom::SmsStatus);
Majid Valipour24a2a302020-11-18 00:37:0977 void SetConsentHandlerForTesting(UserConsentHandler*);
Majid Valipourdcd50fa2020-07-20 16:59:4378
Yi Gu4f8c0472020-12-10 17:45:2879 // Rejects the pending request if it has not been resolved naturally yet.
80 void OnTimeout();
81
Yi Guab301942020-12-10 20:34:0382 void OnUserConsentComplete(UserConsentResult);
83
Sam Goto9c87da7e2019-04-30 23:09:4584 private:
danakjc70aec1f2022-07-07 15:48:1985 WebOTPService(SmsFetcher*,
86 const OriginList&,
87 RenderFrameHost&,
88 mojo::PendingReceiver<blink::mojom::WebOTPService>);
89
Sam Goto4fedd242019-08-22 03:35:3290 void CleanUp();
Majid Valipour24a2a302020-11-18 00:37:0991 UserConsentHandler* CreateConsentHandler(UserConsent);
92 UserConsentHandler* GetConsentHandler();
Yi Gu2968f982020-12-09 23:44:1593 void RecordMetrics(blink::mojom::SmsStatus);
Sam Gotoc108b7b2019-07-18 20:12:1594
Sam Goto5cf068e82019-11-04 23:08:4495 // |fetcher_| is safe because all instances of SmsFetcher are owned
96 // by the browser context, which transitively (through RenderFrameHost) owns
97 // and outlives this class.
Keishi Hattori0e45c022021-11-27 09:25:5298 raw_ptr<SmsFetcher> fetcher_;
Sam Gotoc108b7b2019-07-18 20:12:1599
Yi Gu5874e3cf2020-11-30 16:36:34100 const OriginList origin_list_;
Jun Cai2055a0d62019-07-24 17:11:06101 ReceiveCallback callback_;
Anton Bikineevf62d1bf2021-05-15 17:56:07102 absl::optional<std::string> one_time_code_;
Ayu Ishiiae662c22019-08-06 16:24:09103 base::TimeTicks start_time_;
Ayu Ishii9d303da52019-07-26 15:50:47104 base::TimeTicks receive_time_;
Yi Gu4f8c0472020-12-10 17:45:28105 // Timer to trigger timeout for any pending request. We (re)arm the timer
106 // every time we receive a new request.
107 base::DelayTimer timeout_timer_;
Yi Gu7bb3619e2021-07-09 14:04:51108 absl::optional<FailureType> delayed_rejection_reason_;
Sam Goto36d91172019-07-18 02:55:21109
Majid Valipour24a2a302020-11-18 00:37:09110 // The ptr is valid only when we are handling an incoming otp response.
111 std::unique_ptr<UserConsentHandler> consent_handler_;
112 // This is used to inject a mock consent handler for testing and it is owned
113 // by test code.
Keishi Hattori0e45c022021-11-27 09:25:52114 raw_ptr<UserConsentHandler> consent_handler_for_test_{nullptr};
Majid Valipour24a2a302020-11-18 00:37:09115
Sam Goto9c87da7e2019-04-30 23:09:45116 SEQUENCE_CHECKER(sequence_checker_);
Sam Goto4fedd242019-08-22 03:35:32117
Majid Valipourae11335e2020-10-14 04:09:24118 base::WeakPtrFactory<WebOTPService> weak_ptr_factory_{this};
Sam Goto9c87da7e2019-04-30 23:09:45119};
120
121} // namespace content
122
Majid Valipourae11335e2020-10-14 04:09:24123#endif // CONTENT_BROWSER_SMS_WEBOTP_SERVICE_H_