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/android/profiles/profile_downloader_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "chrome/browser/browser_process.h"
10 #include "chrome/browser/profiles/profile_android.h"
11 #include "chrome/browser/profiles/profile_avatar_icon_util.h"
12 #include "chrome/browser/profiles/profile_downloader.h"
13 #include "chrome/browser/profiles/profile_downloader_delegate.h"
14 #include "chrome/browser/profiles/profile_manager.h"
15 #include "chrome/browser/signin/account_tracker_service_factory.h"
16 #include "components/signin/core/browser/account_tracker_service.h"
17 #include "google_apis/gaia/gaia_auth_util.h"
18 #include "jni/ProfileDownloader_jni.h"
19 #include "third_party/skia/include/core/SkBitmap.h"
20 #include "ui/gfx/android/java_bitmap.h"
21 #include "ui/gfx/image/image_skia.h"
22 #include "ui/gfx/screen.h"
26 // An account fetcher callback.
27 class AccountInfoRetriever
: public ProfileDownloaderDelegate
{
29 AccountInfoRetriever(Profile
* profile
,
30 const std::string
& account_id
,
31 const std::string
& email
,
32 const int desired_image_side_pixels
)
34 account_id_(account_id
),
36 desired_image_side_pixels_(desired_image_side_pixels
) {}
39 profile_image_downloader_
.reset(new ProfileDownloader(this));
40 profile_image_downloader_
->StartForAccount(account_id_
);
45 profile_image_downloader_
.reset();
49 // ProfileDownloaderDelegate implementation:
50 bool NeedsProfilePicture() const override
{
51 return desired_image_side_pixels_
> 0;
54 int GetDesiredImageSideLength() const override
{
55 return desired_image_side_pixels_
;
58 Profile
* GetBrowserProfile() override
{
62 std::string
GetCachedPictureURL() const override
{
66 void OnProfileDownloadSuccess(
67 ProfileDownloader
* downloader
) override
{
69 base::string16 full_name
= downloader
->GetProfileFullName();
70 base::string16 given_name
= downloader
->GetProfileGivenName();
71 SkBitmap bitmap
= downloader
->GetProfilePicture();
72 ScopedJavaLocalRef
<jobject
> jbitmap
;
73 if (!bitmap
.isNull() && bitmap
.bytesPerPixel() != 0)
74 jbitmap
= gfx::ConvertToJavaBitmap(&bitmap
);
76 JNIEnv
* env
= base::android::AttachCurrentThread();
77 Java_ProfileDownloader_onProfileDownloadSuccess(
79 base::android::ConvertUTF8ToJavaString(env
, email_
).obj(),
80 base::android::ConvertUTF16ToJavaString(env
, full_name
).obj(),
81 base::android::ConvertUTF16ToJavaString(env
, given_name
).obj(),
86 void OnProfileDownloadFailure(
87 ProfileDownloader
* downloader
,
88 ProfileDownloaderDelegate::FailureReason reason
) override
{
89 LOG(ERROR
) << "Failed to download the profile information: " << reason
;
93 // The profile image downloader instance.
94 scoped_ptr
<ProfileDownloader
> profile_image_downloader_
;
96 // The browser profile associated with this download request.
99 // The account ID and email address of account to be loaded.
100 const std::string account_id_
;
101 const std::string email_
;
103 // Desired side length of the profile image (in pixels).
104 const int desired_image_side_pixels_
;
106 DISALLOW_COPY_AND_ASSIGN(AccountInfoRetriever
);
112 jstring
GetCachedFullNameForPrimaryAccount(JNIEnv
* env
,
115 Profile
* profile
= ProfileAndroid::FromProfileAndroid(jprofile
);
116 ProfileInfoInterface
& info
=
117 g_browser_process
->profile_manager()->GetProfileInfoCache();
118 const size_t index
= info
.GetIndexOfProfileWithPath(profile
->GetPath());
121 if (index
!= std::string::npos
)
122 name
= info
.GetGAIANameOfProfileAtIndex(index
);
124 return base::android::ConvertUTF16ToJavaString(env
, name
).Release();
128 jstring
GetCachedGivenNameForPrimaryAccount(JNIEnv
* env
,
131 Profile
* profile
= ProfileAndroid::FromProfileAndroid(jprofile
);
132 ProfileInfoInterface
& info
=
133 g_browser_process
->profile_manager()->GetProfileInfoCache();
134 const size_t index
= info
.GetIndexOfProfileWithPath(profile
->GetPath());
137 if (index
!= std::string::npos
)
138 name
= info
.GetGAIAGivenNameOfProfileAtIndex(index
);
140 return base::android::ConvertUTF16ToJavaString(env
, name
).Release();
144 jobject
GetCachedAvatarForPrimaryAccount(JNIEnv
* env
,
147 Profile
* profile
= ProfileAndroid::FromProfileAndroid(jprofile
);
148 ProfileInfoInterface
& info
=
149 g_browser_process
->profile_manager()->GetProfileInfoCache();
150 const size_t index
= info
.GetIndexOfProfileWithPath(profile
->GetPath());
152 ScopedJavaLocalRef
<jobject
> jbitmap
;
153 if (index
!= std::string::npos
) {
154 gfx::Image avatar_image
= info
.GetAvatarIconOfProfileAtIndex(index
);
155 if (!avatar_image
.IsEmpty() &&
156 avatar_image
.Width() > profiles::kAvatarIconWidth
&&
157 avatar_image
.Height() > profiles::kAvatarIconHeight
&&
158 avatar_image
.AsImageSkia().bitmap()) {
159 jbitmap
= gfx::ConvertToJavaBitmap(avatar_image
.AsImageSkia().bitmap());
163 return jbitmap
.Release();
167 void StartFetchingAccountInfoFor(
172 jint image_side_pixels
) {
173 Profile
* profile
= ProfileAndroid::FromProfileAndroid(jprofile
);
174 const std::string email
=
175 base::android::ConvertJavaStringToUTF8(env
, jemail
);
176 // TODO(rogerta): the java code will need to pass in the gaia-id
177 // of the account instead of the email when chrome uses gaia-id as key.
178 DCHECK_EQ(AccountTrackerService::MIGRATION_NOT_STARTED
,
179 AccountTrackerServiceFactory::GetForProfile(profile
)->
180 GetMigrationState());
181 AccountInfoRetriever
* retriever
=
182 new AccountInfoRetriever(
183 profile
, gaia::CanonicalizeEmail(gaia::SanitizeEmail(email
)), email
,
189 bool RegisterProfileDownloader(JNIEnv
* env
) {
190 return RegisterNativesImpl(env
);