blob: 62e8ca598cb1c3c4389a90e20411a27247a0e4db [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
Sam Gotoc1294cab2019-07-15 22:36:015#include "content/browser/sms/sms_service.h"
Sam Goto9c87da7e2019-04-30 23:09:456
Sam Goto36d91172019-07-18 02:55:217#include <iterator>
8#include <queue>
Ayu Ishiid29455462019-10-18 23:23:199#include <string>
Sam Gotoc1294cab2019-07-15 22:36:0110#include <utility>
Sam Goto9c87da7e2019-04-30 23:09:4511
12#include "base/bind.h"
13#include "base/callback_helpers.h"
Jun Cai34b60532019-08-13 19:56:4014#include "base/logging.h"
Sam Goto36d91172019-07-18 02:55:2115#include "base/optional.h"
Ayu Ishii9d303da52019-07-26 15:50:4716#include "content/browser/sms/sms_metrics.h"
Sam Goto5cf068e82019-11-04 23:08:4417#include "content/public/browser/sms_fetcher.h"
Sam Gotoc108b7b2019-07-18 20:12:1518#include "content/public/browser/web_contents.h"
19#include "content/public/browser/web_contents_delegate.h"
Sam Goto36d91172019-07-18 02:55:2120
21using blink::mojom::SmsStatus;
Sam Goto9c87da7e2019-04-30 23:09:4522
23namespace content {
24
Miyoung Shin8ff3aa892019-08-31 03:01:3825SmsService::SmsService(
Sam Goto5cf068e82019-11-04 23:08:4426 SmsFetcher* fetcher,
Miyoung Shin8ff3aa892019-08-31 03:01:3827 const url::Origin& origin,
28 RenderFrameHost* host,
29 mojo::PendingReceiver<blink::mojom::SmsReceiver> receiver)
30 : FrameServiceBase(host, std::move(receiver)),
Sam Goto5cf068e82019-11-04 23:08:4431 fetcher_(fetcher),
Sam Goto36d91172019-07-18 02:55:2132 origin_(origin) {}
33
Miyoung Shin8ff3aa892019-08-31 03:01:3834SmsService::SmsService(
Sam Goto5cf068e82019-11-04 23:08:4435 SmsFetcher* fetcher,
Miyoung Shin8ff3aa892019-08-31 03:01:3836 RenderFrameHost* host,
37 mojo::PendingReceiver<blink::mojom::SmsReceiver> receiver)
Sam Goto5cf068e82019-11-04 23:08:4438 : SmsService(fetcher,
Sam Goto36d91172019-07-18 02:55:2139 host->GetLastCommittedOrigin(),
40 host,
Miyoung Shin8ff3aa892019-08-31 03:01:3841 std::move(receiver)) {}
Sam Goto9c87da7e2019-04-30 23:09:4542
Sam Gotoc1294cab2019-07-15 22:36:0143SmsService::~SmsService() {
Jun Cai2055a0d62019-07-24 17:11:0644 if (callback_)
45 Process(SmsStatus::kTimeout, base::nullopt);
Sam Goto9c87da7e2019-04-30 23:09:4546}
47
Sam Goto36d91172019-07-18 02:55:2148// static
Miyoung Shin8ff3aa892019-08-31 03:01:3849void SmsService::Create(
Sam Goto5cf068e82019-11-04 23:08:4450 SmsFetcher* fetcher,
Miyoung Shin8ff3aa892019-08-31 03:01:3851 RenderFrameHost* host,
52 mojo::PendingReceiver<blink::mojom::SmsReceiver> receiver) {
Sam Goto36d91172019-07-18 02:55:2153 DCHECK(host);
54
55 // SmsService owns itself. It will self-destruct when a mojo interface
56 // error occurs, the render frame host is deleted, or the render frame host
57 // navigates to a new document.
Sam Goto5cf068e82019-11-04 23:08:4458 new SmsService(fetcher, host, std::move(receiver));
Sam Goto9c87da7e2019-04-30 23:09:4559}
60
Ayu Ishiiac090532019-09-04 14:12:0661void SmsService::Receive(ReceiveCallback callback) {
Sam Goto9c87da7e2019-04-30 23:09:4562 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Jun Cai2055a0d62019-07-24 17:11:0663 if (callback_) {
Ayu Ishiid29455462019-10-18 23:23:1964 std::move(callback_).Run(SmsStatus::kCancelled, base::nullopt);
Sam Goto5cf068e82019-11-04 23:08:4465 fetcher_->Unsubscribe(origin_, this);
Sam Gotoc108b7b2019-07-18 20:12:1566 }
Sam Goto36d91172019-07-18 02:55:2167
Ayu Ishiiae662c22019-08-06 16:24:0968 start_time_ = base::TimeTicks::Now();
69
Jun Cai2055a0d62019-07-24 17:11:0670 callback_ = std::move(callback);
Sam Gotoc108b7b2019-07-18 20:12:1571
Sam Goto5cf068e82019-11-04 23:08:4472 fetcher_->Subscribe(origin_, this);
Sam Goto36d91172019-07-18 02:55:2173}
74
Sam Goto5cf068e82019-11-04 23:08:4475void SmsService::OnReceive(const std::string& one_time_code,
Sam Goto354bc1ad2019-08-30 00:10:4076 const std::string& sms) {
Sam Goto36d91172019-07-18 02:55:2177 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
Sam Goto36d91172019-07-18 02:55:2178
Jun Cai2055a0d62019-07-24 17:11:0679 DCHECK(!sms_);
Ayu Ishiiae662c22019-08-06 16:24:0980 DCHECK(!start_time_.is_null());
81
82 RecordSmsReceiveTime(base::TimeTicks::Now() - start_time_);
Sam Goto36d91172019-07-18 02:55:2183
Jun Cai2055a0d62019-07-24 17:11:0684 sms_ = sms;
Ayu Ishii9d303da52019-07-26 15:50:4785 receive_time_ = base::TimeTicks::Now();
Sam Goto4fedd242019-08-22 03:35:3286
Sam Goto354bc1ad2019-08-30 00:10:4087 OpenInfoBar(one_time_code);
Sam Goto354bc1ad2019-08-30 00:10:4088}
89
90void SmsService::OpenInfoBar(const std::string& one_time_code) {
Sam Goto4fedd242019-08-22 03:35:3291 WebContents* web_contents =
92 content::WebContents::FromRenderFrameHost(render_frame_host());
93
94 web_contents->GetDelegate()->CreateSmsPrompt(
Sam Goto354bc1ad2019-08-30 00:10:4095 render_frame_host(), origin_, one_time_code,
Sam Goto4fedd242019-08-22 03:35:3296 base::BindOnce(&SmsService::OnConfirm, weak_ptr_factory_.GetWeakPtr()),
97 base::BindOnce(&SmsService::OnCancel, weak_ptr_factory_.GetWeakPtr()));
Sam Goto36d91172019-07-18 02:55:2198}
99
Jun Cai2055a0d62019-07-24 17:11:06100void SmsService::Process(blink::mojom::SmsStatus status,
101 base::Optional<std::string> sms) {
Sam Goto36d91172019-07-18 02:55:21102 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
103
Jun Cai2055a0d62019-07-24 17:11:06104 DCHECK(callback_);
Sam Goto36d91172019-07-18 02:55:21105
Jun Cai2055a0d62019-07-24 17:11:06106 std::move(callback_).Run(status, sms);
Sam Goto36d91172019-07-18 02:55:21107
Sam Goto4fedd242019-08-22 03:35:32108 CleanUp();
Jun Cai2055a0d62019-07-24 17:11:06109}
110
Jun Cai7421705b2019-08-05 21:14:40111void SmsService::OnConfirm() {
Jun Cai2055a0d62019-07-24 17:11:06112 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
113
114 DCHECK(sms_);
Ayu Ishii9d303da52019-07-26 15:50:47115 DCHECK(!receive_time_.is_null());
116 RecordContinueOnSuccessTime(base::TimeTicks::Now() - receive_time_);
Jun Cai2055a0d62019-07-24 17:11:06117
118 Process(SmsStatus::kSuccess, sms_);
Sam Goto9c87da7e2019-04-30 23:09:45119}
120
Sam Gotoc108b7b2019-07-18 20:12:15121void SmsService::OnCancel() {
122 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
123
Ayu Ishii9d303da52019-07-26 15:50:47124 // Record only when SMS has already been received.
Sam Goto4fedd242019-08-22 03:35:32125 DCHECK(!receive_time_.is_null());
126 RecordCancelOnSuccessTime(base::TimeTicks::Now() - receive_time_);
Ayu Ishii9d303da52019-07-26 15:50:47127
Jun Cai2055a0d62019-07-24 17:11:06128 Process(SmsStatus::kCancelled, base::nullopt);
Sam Gotoc108b7b2019-07-18 20:12:15129}
130
Sam Goto4fedd242019-08-22 03:35:32131void SmsService::CleanUp() {
Jun Cai2055a0d62019-07-24 17:11:06132 callback_.Reset();
133 sms_.reset();
Ayu Ishiiae662c22019-08-06 16:24:09134 start_time_ = base::TimeTicks();
Ayu Ishii9d303da52019-07-26 15:50:47135 receive_time_ = base::TimeTicks();
Sam Goto5cf068e82019-11-04 23:08:44136 fetcher_->Unsubscribe(origin_, this);
Sam Gotoc108b7b2019-07-18 20:12:15137}
138
Sam Goto9c87da7e2019-04-30 23:09:45139} // namespace content