summaryrefslogtreecommitdiffstats
path: root/chrome/browser/android/profiles/profile_downloader_android.cc
blob: b49a808d1f704fc90c6ac0ae83171b69c07b2f63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/android/profiles/profile_downloader_android.h"

#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile_android.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/profiles/profile_downloader.h"
#include "chrome/browser/profiles/profile_downloader_delegate.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "jni/ProfileDownloader_jni.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/android/java_bitmap.h"
#include "ui/gfx/image/image_skia.h"
#include "ui/gfx/screen.h"

namespace {

// An account fetcher callback.
class AccountInfoRetriever : public ProfileDownloaderDelegate {
 public:
  explicit AccountInfoRetriever(Profile* profile,
                                const std::string& account_id,
                                const int desired_image_side_pixels)
      : profile_(profile),
        account_id_(account_id),
        desired_image_side_pixels_(desired_image_side_pixels) {}

  void Start() {
    profile_image_downloader_.reset(new ProfileDownloader(this));
    profile_image_downloader_->StartForAccount(account_id_);
  }

  void Shutdown() {
    profile_image_downloader_.reset();
    delete this;
  }

  // ProfileDownloaderDelegate implementation:
  virtual bool NeedsProfilePicture() const OVERRIDE {
    return desired_image_side_pixels_ > 0;
  }

  virtual int GetDesiredImageSideLength() const OVERRIDE {
    return desired_image_side_pixels_;
  }

  virtual Profile* GetBrowserProfile() OVERRIDE {
    return profile_;
  }

  virtual std::string GetCachedPictureURL() const OVERRIDE {
    return std::string();
  }

  virtual void OnProfileDownloadSuccess(
      ProfileDownloader* downloader) OVERRIDE {
    ProfileDownloaderAndroid::OnProfileDownloadSuccess(
        account_id_,
        downloader->GetProfileFullName(),
        downloader->GetProfilePicture());
    Shutdown();
  }

  virtual void OnProfileDownloadFailure(
      ProfileDownloader* downloader,
      ProfileDownloaderDelegate::FailureReason reason) OVERRIDE {
    LOG(ERROR) << "Failed to download the profile information: " << reason;
    Shutdown();
  }

  // The profile image downloader instance.
  scoped_ptr<ProfileDownloader> profile_image_downloader_;

  // The browser profile associated with this download request.
  Profile* profile_;

  // The account ID (email address) to be loaded.
  const std::string account_id_;

  // Desired side length of the profile image (in pixels).
  const int desired_image_side_pixels_;

  DISALLOW_COPY_AND_ASSIGN(AccountInfoRetriever);
};

}  // namespace

// static
void ProfileDownloaderAndroid::OnProfileDownloadSuccess(
    const std::string& account_id,
    const base::string16& full_name,
    const SkBitmap& bitmap) {
  JNIEnv* env = base::android::AttachCurrentThread();
  ScopedJavaLocalRef<jobject> jbitmap;
  if (!bitmap.isNull() && bitmap.bytesPerPixel() != 0)
    jbitmap = gfx::ConvertToJavaBitmap(&bitmap);
  Java_ProfileDownloader_onProfileDownloadSuccess(
      env,
      base::android::ConvertUTF8ToJavaString(env, account_id).obj(),
      base::android::ConvertUTF16ToJavaString(env, full_name).obj(),
      jbitmap.obj());
}

// static
jstring GetCachedNameForPrimaryAccount(JNIEnv* env,
                                       jclass clazz,
                                       jobject jprofile) {
  Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
  ProfileInfoInterface& info =
      g_browser_process->profile_manager()->GetProfileInfoCache();
  const size_t index = info.GetIndexOfProfileWithPath(profile->GetPath());

  base::string16 name;

  if (index != std::string::npos &&
      !info.ProfileIsUsingDefaultNameAtIndex(index)) {
    name = info.GetGAIANameOfProfileAtIndex(index);
    if (name.empty())
      name = info.GetNameOfProfileAtIndex(index);
  }

  return base::android::ConvertUTF16ToJavaString(env, name).Release();
}

// static
jobject GetCachedAvatarForPrimaryAccount(JNIEnv* env,
                                         jclass clazz,
                                         jobject jprofile) {
  Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
  ProfileInfoInterface& info =
      g_browser_process->profile_manager()->GetProfileInfoCache();
  const size_t index = info.GetIndexOfProfileWithPath(profile->GetPath());

  ScopedJavaLocalRef<jobject> jbitmap;
  if (index != std::string::npos) {
    gfx::Image avatar_image = info.GetAvatarIconOfProfileAtIndex(index);
    if (!avatar_image.IsEmpty() &&
        avatar_image.Width() > profiles::kAvatarIconWidth &&
        avatar_image.Height() > profiles::kAvatarIconHeight &&
        avatar_image.AsImageSkia().bitmap()) {
      jbitmap = gfx::ConvertToJavaBitmap(avatar_image.AsImageSkia().bitmap());
    }
  }

  return jbitmap.Release();
}

// static
void StartFetchingAccountInfoFor(
    JNIEnv* env,
    jclass clazz,
    jobject jprofile,
    jstring jaccount_id,
    jint image_side_pixels) {
  Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile);
  const std::string account_id =
      base::android::ConvertJavaStringToUTF8(env, jaccount_id);
  AccountInfoRetriever* retriever =
      new AccountInfoRetriever(profile, account_id, image_side_pixels);
  retriever->Start();
}

// static
bool ProfileDownloaderAndroid::Register(JNIEnv* env) {
  return RegisterNativesImpl(env);
}