1 // Copyright 2015 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/infobars/account_chooser_infobar.h"
7 #include "base/android/scoped_java_ref.h"
8 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher.h"
9 #include "chrome/browser/infobars/infobar_service.h"
10 #include "chrome/browser/password_manager/account_chooser_infobar_delegate_android.h"
11 #include "chrome/browser/password_manager/credential_android.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/passwords/account_avatar_fetcher.h"
14 #include "components/password_manager/core/common/credential_manager_types.h"
15 #include "jni/AccountChooserInfoBar_jni.h"
16 #include "ui/gfx/android/java_bitmap.h"
20 void AddElementsToJavaCredentialArray(
22 ScopedJavaLocalRef
<jobjectArray
> java_credentials_array
,
23 const std::vector
<const autofill::PasswordForm
*>& password_forms
,
24 password_manager::CredentialType type
,
26 int index
= indexStart
;
27 for (auto password_form
: password_forms
) {
28 ScopedJavaLocalRef
<jobject
> java_credential
= CreateNativeCredential(
29 env
, *password_form
, index
- indexStart
, static_cast<int>(type
));
30 env
->SetObjectArrayElement(java_credentials_array
.obj(), index
,
31 java_credential
.obj());
36 class AvatarFetcherAndroid
: public AccountAvatarFetcher
{
41 const base::android::ScopedJavaGlobalRef
<jobject
>& java_infobar
);
44 ~AvatarFetcherAndroid() override
= default;
45 // chrome::BitmapFetcherDelegate:
46 void OnFetchComplete(const GURL
& url
, const SkBitmap
* bitmap
) override
;
49 base::android::ScopedJavaGlobalRef
<jobject
> java_infobar_
;
51 DISALLOW_COPY_AND_ASSIGN(AvatarFetcherAndroid
);
54 AvatarFetcherAndroid::AvatarFetcherAndroid(
57 const base::android::ScopedJavaGlobalRef
<jobject
>& java_infobar
)
58 : AccountAvatarFetcher(url
, base::WeakPtr
<AccountAvatarFetcherDelegate
>()),
60 java_infobar_(java_infobar
) {
63 void AvatarFetcherAndroid::OnFetchComplete(const GURL
& url
,
64 const SkBitmap
* bitmap
) {
65 base::android::ScopedJavaLocalRef
<jobject
> java_bitmap
=
66 gfx::ConvertToJavaBitmap(bitmap
);
67 Java_AccountChooserInfoBar_imageFetchComplete(
68 base::android::AttachCurrentThread(), java_infobar_
.obj(), index_
,
74 const base::android::ScopedJavaGlobalRef
<jobject
>& java_infobar
,
75 const std::vector
<const autofill::PasswordForm
*>& password_forms
,
77 net::URLRequestContextGetter
* request_context
) {
78 for (auto password_form
: password_forms
) {
79 if (!password_form
->avatar_url
.is_valid())
81 // Fetcher deletes itself once fetching is finished.
82 auto fetcher
= new AvatarFetcherAndroid(password_form
->avatar_url
, index
,
84 fetcher
->Start(request_context
);
91 AccountChooserInfoBar::AccountChooserInfoBar(
92 scoped_ptr
<AccountChooserInfoBarDelegateAndroid
> delegate
)
93 : InfoBarAndroid(delegate
.Pass()) {
96 AccountChooserInfoBar::~AccountChooserInfoBar() {
99 base::android::ScopedJavaLocalRef
<jobject
>
100 AccountChooserInfoBar::CreateRenderInfoBar(JNIEnv
* env
) {
101 size_t credential_array_size
=
102 GetDelegate()->local_credentials_forms().size() +
103 GetDelegate()->federated_credentials_forms().size();
104 ScopedJavaLocalRef
<jobjectArray
> java_credentials_array
=
105 CreateNativeCredentialArray(env
, credential_array_size
);
106 AddElementsToJavaCredentialArray(
107 env
, java_credentials_array
, GetDelegate()->local_credentials_forms(),
108 password_manager::CredentialType::CREDENTIAL_TYPE_LOCAL
);
109 AddElementsToJavaCredentialArray(
110 env
, java_credentials_array
, GetDelegate()->federated_credentials_forms(),
111 password_manager::CredentialType::CREDENTIAL_TYPE_FEDERATED
,
112 GetDelegate()->local_credentials_forms().size());
113 base::android::ScopedJavaGlobalRef
<jobject
> java_infobar_global
;
114 java_infobar_global
.Reset(Java_AccountChooserInfoBar_show(
115 env
, reinterpret_cast<intptr_t>(this), GetEnumeratedIconId(),
116 java_credentials_array
.obj()));
117 base::android::ScopedJavaLocalRef
<jobject
> java_infobar(java_infobar_global
);
118 content::WebContents
* web_contents
=
119 InfoBarService::WebContentsFromInfoBar(this);
120 net::URLRequestContextGetter
* request_context
=
121 Profile::FromBrowserContext(web_contents
->GetBrowserContext())
122 ->GetRequestContext();
123 FetchAvatars(java_infobar_global
, GetDelegate()->local_credentials_forms(), 0,
126 java_infobar_global
, GetDelegate()->federated_credentials_forms(),
127 GetDelegate()->local_credentials_forms().size(), request_context
);
131 void AccountChooserInfoBar::OnCredentialClicked(JNIEnv
* env
,
133 jint credential_item
,
134 jint credential_type
) {
135 GetDelegate()->ChooseCredential(
137 static_cast<password_manager::CredentialType
>(credential_type
));
141 void AccountChooserInfoBar::ProcessButton(int action
,
142 const std::string
& action_value
) {
144 return; // We're closing; don't call anything, it might access the owner.
145 GetDelegate()->ChooseCredential(
146 -1, password_manager::CredentialType::CREDENTIAL_TYPE_EMPTY
);
150 AccountChooserInfoBarDelegateAndroid
* AccountChooserInfoBar::GetDelegate() {
151 return static_cast<AccountChooserInfoBarDelegateAndroid
*>(delegate());
154 bool RegisterAccountChooserInfoBar(JNIEnv
* env
) {
155 return RegisterNativesImpl(env
);