Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / autofill / credit_card_scanner_controller.cc
blob55ed37bb4bcb0698c5482b01a10635b74b7977e5
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"
18 namespace autofill {
20 namespace {
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> {
26 public:
27 Controller(content::WebContents* web_contents,
28 const AutofillClient::CreditCardScanCallback& callback)
29 : view_(CreditCardScannerView::Create(AsWeakPtr(), web_contents)),
30 callback_(callback) {
31 DCHECK(view_);
34 // Shows the UI to scan the credit card.
35 void Show() {
36 show_time_ = base::TimeTicks::Now();
37 view_->Show();
40 private:
41 ~Controller() override {}
43 // CreditCardScannerViewDelegate implementation.
44 void ScanCancelled() override {
45 AutofillMetrics::LogScanCreditCardCompleted(
46 base::TimeTicks::Now() - show_time_, false);
47 delete this;
50 // CreditCardScannerViewDelegate implementation.
51 void ScanCompleted(const base::string16& card_number,
52 int expiration_month,
53 int expiration_year) override {
54 AutofillMetrics::LogScanCreditCardCompleted(
55 base::TimeTicks::Now() - show_time_, true);
56 callback_.Run(card_number, expiration_month, expiration_year);
57 delete this;
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);
72 } // namespace
74 // static
75 bool CreditCardScannerController::HasCreditCardScanFeature() {
76 static const bool kCanShow = CreditCardScannerView::CanShow();
77 return kCanShow;
80 // static
81 void CreditCardScannerController::ScanCreditCard(
82 content::WebContents* web_contents,
83 const AutofillClient::CreditCardScanCallback& callback) {
84 (new Controller(web_contents, callback))->Show();
87 } // namespace autofill