Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / android / profiles / profile_downloader_android.cc
blob9ff6f4df439372f9a7134099ebb001f6ebfcb65e
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"
24 namespace {
26 // An account fetcher callback.
27 class AccountInfoRetriever : public ProfileDownloaderDelegate {
28 public:
29 AccountInfoRetriever(Profile* profile,
30 const std::string& account_id,
31 const std::string& email,
32 const int desired_image_side_pixels,
33 bool is_pre_signin)
34 : profile_(profile),
35 account_id_(account_id),
36 email_(email),
37 desired_image_side_pixels_(desired_image_side_pixels),
38 is_pre_signin_(is_pre_signin) {}
40 void Start() {
41 profile_image_downloader_.reset(new ProfileDownloader(this));
42 profile_image_downloader_->StartForAccount(account_id_);
45 private:
46 void Shutdown() {
47 profile_image_downloader_.reset();
48 delete this;
51 // ProfileDownloaderDelegate implementation:
52 bool NeedsProfilePicture() const override {
53 return desired_image_side_pixels_ > 0;
56 int GetDesiredImageSideLength() const override {
57 return desired_image_side_pixels_;
60 Profile* GetBrowserProfile() override {
61 return profile_;
64 std::string GetCachedPictureURL() const override {
65 return std::string();
68 bool IsPreSignin() const override {
69 return is_pre_signin_;
72 void OnProfileDownloadSuccess(
73 ProfileDownloader* downloader) override {
75 base::string16 full_name = downloader->GetProfileFullName();
76 base::string16 given_name = downloader->GetProfileGivenName();
77 SkBitmap bitmap = downloader->GetProfilePicture();
78 ScopedJavaLocalRef<jobject> jbitmap;
79 if (!bitmap.isNull() && bitmap.bytesPerPixel() != 0)
80 jbitmap = gfx::ConvertToJavaBitmap(&bitmap);
82 JNIEnv* env = base::android::AttachCurrentThread();
83 Java_ProfileDownloader_onProfileDownloadSuccess(
84 env,
85 base::android::ConvertUTF8ToJavaString(env, email_).obj(),
86 base::android::ConvertUTF16ToJavaString(env, full_name).obj(),
87 base::android::ConvertUTF16ToJavaString(env, given_name).obj(),
88 jbitmap.obj());
89 Shutdown();
92 void OnProfileDownloadFailure(
93 ProfileDownloader* downloader,
94 ProfileDownloaderDelegate::FailureReason reason) override {
95 LOG(ERROR) << "Failed to download the profile information: " << reason;
96 Shutdown();
99 // The profile image downloader instance.
100 scoped_ptr<ProfileDownloader> profile_image_downloader_;
102 // The browser profile associated with this download request.
103 Profile* profile_;
105 // The account ID and email address of account to be loaded.
106 const std::string account_id_;
107 const std::string email_;
109 // Desired side length of the profile image (in pixels).
110 const int desired_image_side_pixels_;
112 // True when the profile download is happening before the user has signed in,
113 // such as during first run when we can still get tokens and want to fetch
114 // the profile name and picture to display.
115 bool is_pre_signin_;
117 DISALLOW_COPY_AND_ASSIGN(AccountInfoRetriever);
120 } // namespace
122 // static
123 ScopedJavaLocalRef<jstring> GetCachedFullNameForPrimaryAccount(
124 JNIEnv* env,
125 const JavaParamRef<jclass>& clazz,
126 const JavaParamRef<jobject>& jprofile) {
127 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
128 ProfileInfoInterface& info =
129 g_browser_process->profile_manager()->GetProfileInfoCache();
130 const size_t index = info.GetIndexOfProfileWithPath(profile->GetPath());
132 base::string16 name;
133 if (index != std::string::npos)
134 name = info.GetGAIANameOfProfileAtIndex(index);
136 return base::android::ConvertUTF16ToJavaString(env, name);
139 // static
140 ScopedJavaLocalRef<jstring> GetCachedGivenNameForPrimaryAccount(
141 JNIEnv* env,
142 const JavaParamRef<jclass>& clazz,
143 const JavaParamRef<jobject>& jprofile) {
144 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
145 ProfileInfoInterface& info =
146 g_browser_process->profile_manager()->GetProfileInfoCache();
147 const size_t index = info.GetIndexOfProfileWithPath(profile->GetPath());
149 base::string16 name;
150 if (index != std::string::npos)
151 name = info.GetGAIAGivenNameOfProfileAtIndex(index);
153 return base::android::ConvertUTF16ToJavaString(env, name);
156 // static
157 ScopedJavaLocalRef<jobject> GetCachedAvatarForPrimaryAccount(
158 JNIEnv* env,
159 const JavaParamRef<jclass>& clazz,
160 const JavaParamRef<jobject>& jprofile) {
161 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
162 ProfileInfoInterface& info =
163 g_browser_process->profile_manager()->GetProfileInfoCache();
164 const size_t index = info.GetIndexOfProfileWithPath(profile->GetPath());
166 ScopedJavaLocalRef<jobject> jbitmap;
167 if (index != std::string::npos) {
168 gfx::Image avatar_image = info.GetAvatarIconOfProfileAtIndex(index);
169 if (!avatar_image.IsEmpty() &&
170 avatar_image.Width() > profiles::kAvatarIconWidth &&
171 avatar_image.Height() > profiles::kAvatarIconHeight &&
172 avatar_image.AsImageSkia().bitmap()) {
173 jbitmap = gfx::ConvertToJavaBitmap(avatar_image.AsImageSkia().bitmap());
177 return jbitmap;
180 // static
181 void StartFetchingAccountInfoFor(JNIEnv* env,
182 const JavaParamRef<jclass>& clazz,
183 const JavaParamRef<jobject>& jprofile,
184 const JavaParamRef<jstring>& jemail,
185 jint image_side_pixels,
186 jboolean is_pre_signin) {
187 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
188 const std::string email =
189 base::android::ConvertJavaStringToUTF8(env, jemail);
190 AccountTrackerService* account_tracker_service =
191 AccountTrackerServiceFactory::GetForProfile(profile);
193 AccountInfoRetriever* retriever = new AccountInfoRetriever(
194 profile,
195 account_tracker_service->FindAccountInfoByEmail(email).account_id, email,
196 image_side_pixels, is_pre_signin);
197 retriever->Start();
200 // static
201 bool RegisterProfileDownloader(JNIEnv* env) {
202 return RegisterNativesImpl(env);