Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / android / infobars / account_chooser_infobar.cc
blob15d1d3cd72819104bca13eba35a128dca681d537
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"
18 namespace {
20 void AddElementsToJavaCredentialArray(
21 JNIEnv* env,
22 ScopedJavaLocalRef<jobjectArray> java_credentials_array,
23 const std::vector<const autofill::PasswordForm*>& password_forms,
24 password_manager::CredentialType type,
25 int indexStart = 0) {
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());
32 index++;
36 class AvatarFetcherAndroid : public AccountAvatarFetcher {
37 public:
38 AvatarFetcherAndroid(
39 const GURL& url,
40 int index,
41 const base::android::ScopedJavaGlobalRef<jobject>& java_infobar);
43 private:
44 ~AvatarFetcherAndroid() override = default;
45 // chrome::BitmapFetcherDelegate:
46 void OnFetchComplete(const GURL& url, const SkBitmap* bitmap) override;
48 int index_;
49 base::android::ScopedJavaGlobalRef<jobject> java_infobar_;
51 DISALLOW_COPY_AND_ASSIGN(AvatarFetcherAndroid);
54 AvatarFetcherAndroid::AvatarFetcherAndroid(
55 const GURL& url,
56 int index,
57 const base::android::ScopedJavaGlobalRef<jobject>& java_infobar)
58 : AccountAvatarFetcher(url, base::WeakPtr<AccountAvatarFetcherDelegate>()),
59 index_(index),
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_,
69 java_bitmap.obj());
70 delete this;
73 void FetchAvatars(
74 const base::android::ScopedJavaGlobalRef<jobject>& java_infobar,
75 const std::vector<const autofill::PasswordForm*>& password_forms,
76 int index,
77 net::URLRequestContextGetter* request_context) {
78 for (auto password_form : password_forms) {
79 if (!password_form->avatar_url.is_valid())
80 continue;
81 // Fetcher deletes itself once fetching is finished.
82 auto fetcher = new AvatarFetcherAndroid(password_form->avatar_url, index,
83 java_infobar);
84 fetcher->Start(request_context);
85 ++index;
89 }; // namespace
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,
124 request_context);
125 FetchAvatars(
126 java_infobar_global, GetDelegate()->federated_credentials_forms(),
127 GetDelegate()->local_credentials_forms().size(), request_context);
128 return java_infobar;
131 void AccountChooserInfoBar::OnCredentialClicked(JNIEnv* env,
132 jobject obj,
133 jint credential_item,
134 jint credential_type) {
135 GetDelegate()->ChooseCredential(
136 credential_item,
137 static_cast<password_manager::CredentialType>(credential_type));
138 RemoveSelf();
141 void AccountChooserInfoBar::ProcessButton(int action,
142 const std::string& action_value) {
143 if (!owner())
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);
147 RemoveSelf();
150 AccountChooserInfoBarDelegateAndroid* AccountChooserInfoBar::GetDelegate() {
151 return static_cast<AccountChooserInfoBarDelegateAndroid*>(delegate());
154 bool RegisterAccountChooserInfoBar(JNIEnv* env) {
155 return RegisterNativesImpl(env);