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/android/autofill/credit_card_scanner_view_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "chrome/browser/ui/android/window_android_helper.h"
11 #include "chrome/browser/ui/autofill/credit_card_scanner_view_delegate.h"
12 #include "content/public/browser/android/content_view_core.h"
13 #include "jni/CreditCardScanner_jni.h"
14 #include "ui/android/view_android.h"
15 #include "ui/android/window_android.h"
20 bool CreditCardScannerView::CanShow() {
21 JNIEnv
* env
= base::android::AttachCurrentThread();
22 base::android::ScopedJavaGlobalRef
<jobject
> java_object(
23 Java_CreditCardScanner_create(
24 env
, 0, base::android::GetApplicationContext(), 0));
25 return Java_CreditCardScanner_canScan(env
, java_object
.obj());
29 scoped_ptr
<CreditCardScannerView
> CreditCardScannerView::Create(
30 const base::WeakPtr
<CreditCardScannerViewDelegate
>& delegate
,
31 content::WebContents
* web_contents
) {
32 return make_scoped_ptr(
33 new CreditCardScannerViewAndroid(delegate
, web_contents
));
37 bool CreditCardScannerViewAndroid::Register(JNIEnv
* env
) {
38 return RegisterNativesImpl(env
);
41 CreditCardScannerViewAndroid::CreditCardScannerViewAndroid(
42 const base::WeakPtr
<CreditCardScannerViewDelegate
>& delegate
,
43 content::WebContents
* web_contents
)
44 : delegate_(delegate
),
45 java_object_(Java_CreditCardScanner_create(
46 base::android::AttachCurrentThread(),
47 reinterpret_cast<intptr_t>(this),
48 base::android::GetApplicationContext(),
49 WindowAndroidHelper::FromWebContents(web_contents
)
50 ->GetWindowAndroid()->GetJavaObject().obj())) {}
52 CreditCardScannerViewAndroid::~CreditCardScannerViewAndroid() {}
54 void CreditCardScannerViewAndroid::ScanCancelled(JNIEnv
* env
, jobject object
) {
55 delegate_
->ScanCancelled();
58 void CreditCardScannerViewAndroid::ScanCompleted(JNIEnv
* env
,
61 jint expiration_month
,
62 jint expiration_year
) {
63 delegate_
->ScanCompleted(
64 base::android::ConvertJavaStringToUTF16(env
, card_number
),
65 static_cast<int>(expiration_month
), static_cast<int>(expiration_year
));
68 void CreditCardScannerViewAndroid::Show() {
69 JNIEnv
* env
= base::android::AttachCurrentThread();
70 Java_CreditCardScanner_scan(env
, java_object_
.obj());
73 } // namespace autofill