Update broken references to image assets
[chromium-blink-merge.git] / chrome / browser / android / profiles / profile_downloader_android.cc
blob9560c8688455da5656f959884ac730a084c5de74
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 jstring GetCachedFullNameForPrimaryAccount(JNIEnv* env,
124 jclass clazz,
125 jobject jprofile) {
126 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
127 ProfileInfoInterface& info =
128 g_browser_process->profile_manager()->GetProfileInfoCache();
129 const size_t index = info.GetIndexOfProfileWithPath(profile->GetPath());
131 base::string16 name;
132 if (index != std::string::npos)
133 name = info.GetGAIANameOfProfileAtIndex(index);
135 return base::android::ConvertUTF16ToJavaString(env, name).Release();
138 // static
139 jstring GetCachedGivenNameForPrimaryAccount(JNIEnv* env,
140 jclass clazz,
141 jobject jprofile) {
142 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
143 ProfileInfoInterface& info =
144 g_browser_process->profile_manager()->GetProfileInfoCache();
145 const size_t index = info.GetIndexOfProfileWithPath(profile->GetPath());
147 base::string16 name;
148 if (index != std::string::npos)
149 name = info.GetGAIAGivenNameOfProfileAtIndex(index);
151 return base::android::ConvertUTF16ToJavaString(env, name).Release();
154 // static
155 jobject GetCachedAvatarForPrimaryAccount(JNIEnv* env,
156 jclass clazz,
157 jobject jprofile) {
158 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
159 ProfileInfoInterface& info =
160 g_browser_process->profile_manager()->GetProfileInfoCache();
161 const size_t index = info.GetIndexOfProfileWithPath(profile->GetPath());
163 ScopedJavaLocalRef<jobject> jbitmap;
164 if (index != std::string::npos) {
165 gfx::Image avatar_image = info.GetAvatarIconOfProfileAtIndex(index);
166 if (!avatar_image.IsEmpty() &&
167 avatar_image.Width() > profiles::kAvatarIconWidth &&
168 avatar_image.Height() > profiles::kAvatarIconHeight &&
169 avatar_image.AsImageSkia().bitmap()) {
170 jbitmap = gfx::ConvertToJavaBitmap(avatar_image.AsImageSkia().bitmap());
174 return jbitmap.Release();
177 // static
178 void StartFetchingAccountInfoFor(
179 JNIEnv* env,
180 jclass clazz,
181 jobject jprofile,
182 jstring jemail,
183 jint image_side_pixels,
184 jboolean is_pre_signin) {
185 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
186 const std::string email =
187 base::android::ConvertJavaStringToUTF8(env, jemail);
188 // TODO(rogerta): the java code will need to pass in the gaia-id
189 // of the account instead of the email when chrome uses gaia-id as key.
190 DCHECK_EQ(AccountTrackerService::MIGRATION_NOT_STARTED,
191 AccountTrackerServiceFactory::GetForProfile(profile)->
192 GetMigrationState());
193 AccountInfoRetriever* retriever =
194 new AccountInfoRetriever(
195 profile, gaia::CanonicalizeEmail(gaia::SanitizeEmail(email)), email,
196 image_side_pixels, is_pre_signin);
197 retriever->Start();
200 // static
201 bool RegisterProfileDownloader(JNIEnv* env) {
202 return RegisterNativesImpl(env);