Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / android / profiles / profile_downloader_android.cc
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.
4
5 #include "chrome/browser/android/profiles/profile_downloader_android.h"
6
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"
23
24 namespace {
25
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       : profile_(profile),
34         account_id_(account_id),
35         email_(email),
36         desired_image_side_pixels_(desired_image_side_pixels) {}
37
38   void Start() {
39     profile_image_downloader_.reset(new ProfileDownloader(this));
40     profile_image_downloader_->StartForAccount(account_id_);
41   }
42
43  private:
44   void Shutdown() {
45     profile_image_downloader_.reset();
46     delete this;
47   }
48
49   // ProfileDownloaderDelegate implementation:
50   bool NeedsProfilePicture() const override {
51     return desired_image_side_pixels_ > 0;
52   }
53
54   int GetDesiredImageSideLength() const override {
55     return desired_image_side_pixels_;
56   }
57
58   Profile* GetBrowserProfile() override {
59     return profile_;
60   }
61
62   std::string GetCachedPictureURL() const override {
63     return std::string();
64   }
65
66   void OnProfileDownloadSuccess(
67       ProfileDownloader* downloader) override {
68
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);
75
76     JNIEnv* env = base::android::AttachCurrentThread();
77     Java_ProfileDownloader_onProfileDownloadSuccess(
78         env,
79         base::android::ConvertUTF8ToJavaString(env, email_).obj(),
80         base::android::ConvertUTF16ToJavaString(env, full_name).obj(),
81         base::android::ConvertUTF16ToJavaString(env, given_name).obj(),
82         jbitmap.obj());
83     Shutdown();
84   }
85
86   void OnProfileDownloadFailure(
87       ProfileDownloader* downloader,
88       ProfileDownloaderDelegate::FailureReason reason) override {
89     LOG(ERROR) << "Failed to download the profile information: " << reason;
90     Shutdown();
91   }
92
93   // The profile image downloader instance.
94   scoped_ptr<ProfileDownloader> profile_image_downloader_;
95
96   // The browser profile associated with this download request.
97   Profile* profile_;
98
99   // The account ID and email address of account to be loaded.
100   const std::string account_id_;
101   const std::string email_;
102
103   // Desired side length of the profile image (in pixels).
104   const int desired_image_side_pixels_;
105
106   DISALLOW_COPY_AND_ASSIGN(AccountInfoRetriever);
107 };
108
109 }  // namespace
110
111 // static
112 jstring GetCachedFullNameForPrimaryAccount(JNIEnv* env,
113                                            jclass clazz,
114                                            jobject jprofile) {
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());
119
120   base::string16 name;
121   if (index != std::string::npos)
122     name = info.GetGAIANameOfProfileAtIndex(index);
123
124   return base::android::ConvertUTF16ToJavaString(env, name).Release();
125 }
126
127 // static
128 jstring GetCachedGivenNameForPrimaryAccount(JNIEnv* env,
129                                             jclass clazz,
130                                             jobject jprofile) {
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());
135
136   base::string16 name;
137   if (index != std::string::npos)
138     name = info.GetGAIAGivenNameOfProfileAtIndex(index);
139
140   return base::android::ConvertUTF16ToJavaString(env, name).Release();
141 }
142
143 // static
144 jobject GetCachedAvatarForPrimaryAccount(JNIEnv* env,
145                                          jclass clazz,
146                                          jobject jprofile) {
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());
151
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());
160     }
161   }
162
163   return jbitmap.Release();
164 }
165
166 // static
167 void StartFetchingAccountInfoFor(
168     JNIEnv* env,
169     jclass clazz,
170     jobject jprofile,
171     jstring jemail,
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,
184           image_side_pixels);
185   retriever->Start();
186 }
187
188 // static
189 bool RegisterProfileDownloader(JNIEnv* env) {
190   return RegisterNativesImpl(env);
191 }