1 // Copyright 2014 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.
5 #include "chrome/browser/ui/autofill/credit_card_scanner_controller.h"
7 #include "base/callback.h"
8 #include "base/logging.h"
9 #include "base/macros.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/strings/string16.h"
13 #include "base/time/time.h"
14 #include "chrome/browser/ui/autofill/credit_card_scanner_view.h"
15 #include "chrome/browser/ui/autofill/credit_card_scanner_view_delegate.h"
16 #include "components/autofill/core/browser/autofill_metrics.h"
22 // Controller for the credit card scanner UI. The controller deletes itself
23 // after the view is dismissed.
24 class Controller
: public CreditCardScannerViewDelegate
,
25 public base::SupportsWeakPtr
<Controller
> {
27 Controller(content::WebContents
* web_contents
,
28 const AutofillClient::CreditCardScanCallback
& callback
)
29 : view_(CreditCardScannerView::Create(AsWeakPtr(), web_contents
)),
34 // Shows the UI to scan the credit card.
36 show_time_
= base::TimeTicks::Now();
41 ~Controller() override
{}
43 // CreditCardScannerViewDelegate implementation.
44 void ScanCancelled() override
{
45 AutofillMetrics::LogScanCreditCardCompleted(
46 base::TimeTicks::Now() - show_time_
, false);
50 // CreditCardScannerViewDelegate implementation.
51 void ScanCompleted(const base::string16
& card_number
,
53 int expiration_year
) override
{
54 AutofillMetrics::LogScanCreditCardCompleted(
55 base::TimeTicks::Now() - show_time_
, true);
56 callback_
.Run(card_number
, expiration_month
, expiration_year
);
60 // The view for the credit card scanner.
61 scoped_ptr
<CreditCardScannerView
> view_
;
63 // The callback to be invoked when scanning completes successfully.
64 AutofillClient::CreditCardScanCallback callback_
;
66 // The time when the UI was shown.
67 base::TimeTicks show_time_
;
69 DISALLOW_COPY_AND_ASSIGN(Controller
);
75 bool CreditCardScannerController::HasCreditCardScanFeature() {
76 static const bool kCanShow
= CreditCardScannerView::CanShow();
81 void CreditCardScannerController::ScanCreditCard(
82 content::WebContents
* web_contents
,
83 const AutofillClient::CreditCardScanCallback
& callback
) {
84 (new Controller(web_contents
, callback
))->Show();
87 } // namespace autofill