| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/favicon_helper.h" | 5 #include "chrome/browser/android/favicon_helper.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 | 8 |
| 9 #include <vector> |
| 10 |
| 9 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 12 #include "base/android/jni_array.h" |
| 11 #include "base/android/jni_string.h" | 13 #include "base/android/jni_string.h" |
| 12 #include "base/android/scoped_java_ref.h" | 14 #include "base/android/scoped_java_ref.h" |
| 13 #include "base/bind.h" | 15 #include "base/bind.h" |
| 14 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
| 16 #include "chrome/browser/favicon/favicon_service_factory.h" | 18 #include "chrome/browser/favicon/favicon_service_factory.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/profiles/profile_android.h" | 20 #include "chrome/browser/profiles/profile_android.h" |
| 19 #include "chrome/browser/sync/profile_sync_service.h" | 21 #include "chrome/browser/sync/profile_sync_service.h" |
| 20 #include "chrome/browser/sync/profile_sync_service_factory.h" | 22 #include "chrome/browser/sync/profile_sync_service_factory.h" |
| 21 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
| 22 #include "components/favicon/core/favicon_service.h" | 23 #include "components/favicon/core/favicon_service.h" |
| 24 #include "components/favicon_base/favicon_util.h" |
| 25 #include "components/favicon_base/select_favicon_frames.h" |
| 23 #include "components/sync_driver/open_tabs_ui_delegate.h" | 26 #include "components/sync_driver/open_tabs_ui_delegate.h" |
| 27 #include "content/public/browser/web_contents.h" |
| 24 #include "jni/FaviconHelper_jni.h" | 28 #include "jni/FaviconHelper_jni.h" |
| 25 #include "third_party/skia/include/core/SkBitmap.h" | 29 #include "third_party/skia/include/core/SkBitmap.h" |
| 26 #include "ui/gfx/android/java_bitmap.h" | 30 #include "ui/gfx/android/java_bitmap.h" |
| 27 #include "ui/gfx/codec/png_codec.h" | 31 #include "ui/gfx/codec/png_codec.h" |
| 28 #include "ui/gfx/color_analysis.h" | 32 #include "ui/gfx/color_analysis.h" |
| 29 #include "ui/gfx/color_utils.h" | 33 #include "ui/gfx/color_utils.h" |
| 34 #include "ui/gfx/favicon_size.h" |
| 35 #include "ui/gfx/image/image_skia.h" |
| 30 | 36 |
| 31 using base::android::ScopedJavaGlobalRef; | 37 using base::android::ScopedJavaGlobalRef; |
| 32 using base::android::ScopedJavaLocalRef; | 38 using base::android::ScopedJavaLocalRef; |
| 33 using base::android::AttachCurrentThread; | 39 using base::android::AttachCurrentThread; |
| 34 using base::android::ConvertJavaStringToUTF16; | 40 using base::android::ConvertJavaStringToUTF16; |
| 35 using base::android::ConvertJavaStringToUTF8; | 41 using base::android::ConvertJavaStringToUTF8; |
| 36 using base::android::ConvertUTF8ToJavaString; | 42 using base::android::ConvertUTF8ToJavaString; |
| 37 | 43 |
| 38 namespace { | 44 namespace { |
| 39 | 45 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 if (!favicon_bitmap.isNull()) | 87 if (!favicon_bitmap.isNull()) |
| 82 j_favicon_bitmap = gfx::ConvertToJavaBitmap(&favicon_bitmap); | 88 j_favicon_bitmap = gfx::ConvertToJavaBitmap(&favicon_bitmap); |
| 83 | 89 |
| 84 // Call java side OnLocalFaviconAvailable method. | 90 // Call java side OnLocalFaviconAvailable method. |
| 85 Java_FaviconImageCallback_onFaviconAvailable(env, | 91 Java_FaviconImageCallback_onFaviconAvailable(env, |
| 86 j_favicon_image_callback->obj(), | 92 j_favicon_image_callback->obj(), |
| 87 j_favicon_bitmap.obj(), | 93 j_favicon_bitmap.obj(), |
| 88 j_icon_url.obj()); | 94 j_icon_url.obj()); |
| 89 } | 95 } |
| 90 | 96 |
| 97 void OnFaviconDownloaded( |
| 98 const ScopedJavaGlobalRef<jobject>& j_availability_callback, |
| 99 Profile* profile, |
| 100 const GURL& page_url, |
| 101 int download_request_id, |
| 102 int http_status_code, |
| 103 const GURL& image_url, |
| 104 const std::vector<SkBitmap>& bitmaps, |
| 105 const std::vector<gfx::Size>& original_sizes) { |
| 106 bool success = !bitmaps.empty(); |
| 107 if (success) { |
| 108 gfx::Image image = gfx::Image(CreateFaviconImageSkia(bitmaps, |
| 109 original_sizes, |
| 110 gfx::kFaviconSize, |
| 111 nullptr)); |
| 112 favicon_base::SetFaviconColorSpace(&image); |
| 113 favicon::FaviconService* service = FaviconServiceFactory::GetForProfile( |
| 114 profile, ServiceAccessType::IMPLICIT_ACCESS); |
| 115 service->SetFavicons(page_url, image_url, favicon_base::FAVICON, image); |
| 116 } |
| 117 |
| 118 JNIEnv* env = AttachCurrentThread(); |
| 119 Java_FaviconAvailabilityCallback_onFaviconAvailabilityChecked( |
| 120 env, j_availability_callback.obj(), success); |
| 121 } |
| 122 |
| 123 void OnFaviconImageResultAvailable( |
| 124 const ScopedJavaGlobalRef<jobject>& j_availability_callback, |
| 125 Profile* profile, |
| 126 content::WebContents* web_contents, |
| 127 const GURL& page_url, |
| 128 const GURL& favicon_url, |
| 129 const favicon_base::FaviconImageResult& result) { |
| 130 // If there already is a favicon, return immediately. |
| 131 if (!result.image.IsEmpty()) { |
| 132 JNIEnv* env = AttachCurrentThread(); |
| 133 Java_FaviconAvailabilityCallback_onFaviconAvailabilityChecked( |
| 134 env, j_availability_callback.obj(), false); |
| 135 return; |
| 136 } |
| 137 |
| 138 web_contents->DownloadImage(favicon_url, true, 0, false, |
| 139 base::Bind(OnFaviconDownloaded, |
| 140 j_availability_callback, |
| 141 profile, page_url)); |
| 142 } |
| 143 |
| 91 } // namespace | 144 } // namespace |
| 92 | 145 |
| 93 static jlong Init(JNIEnv* env, jclass clazz) { | 146 static jlong Init(JNIEnv* env, jclass clazz) { |
| 94 return reinterpret_cast<intptr_t>(new FaviconHelper()); | 147 return reinterpret_cast<intptr_t>(new FaviconHelper()); |
| 95 } | 148 } |
| 96 | 149 |
| 97 FaviconHelper::FaviconHelper() { | 150 FaviconHelper::FaviconHelper() { |
| 98 cancelable_task_tracker_.reset(new base::CancelableTaskTracker()); | 151 cancelable_task_tracker_.reset(new base::CancelableTaskTracker()); |
| 99 } | 152 } |
| 100 | 153 |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 gfx::Image favicon_image = gfx::Image::CreateFrom1xPNGBytes(favicon_png); | 256 gfx::Image favicon_image = gfx::Image::CreateFrom1xPNGBytes(favicon_png); |
| 204 SkBitmap favicon_bitmap = favicon_image.AsBitmap(); | 257 SkBitmap favicon_bitmap = favicon_image.AsBitmap(); |
| 205 | 258 |
| 206 ScopedJavaLocalRef<jobject> j_favicon_bitmap; | 259 ScopedJavaLocalRef<jobject> j_favicon_bitmap; |
| 207 if (favicon_bitmap.isNull()) | 260 if (favicon_bitmap.isNull()) |
| 208 return ScopedJavaLocalRef<jobject>(); | 261 return ScopedJavaLocalRef<jobject>(); |
| 209 | 262 |
| 210 return gfx::ConvertToJavaBitmap(&favicon_bitmap); | 263 return gfx::ConvertToJavaBitmap(&favicon_bitmap); |
| 211 } | 264 } |
| 212 | 265 |
| 266 void FaviconHelper::EnsureFaviconIsAvailable( |
| 267 JNIEnv* env, |
| 268 jobject obj, |
| 269 jobject j_profile, |
| 270 jobject j_web_contents, |
| 271 jstring j_page_url, |
| 272 jstring j_favicon_url, |
| 273 jobject j_availability_callback) { |
| 274 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile); |
| 275 DCHECK(profile); |
| 276 content::WebContents* web_contents = |
| 277 content::WebContents::FromJavaWebContents(j_web_contents); |
| 278 DCHECK(web_contents); |
| 279 GURL page_url(ConvertJavaStringToUTF8(env, j_page_url)); |
| 280 GURL favicon_url(ConvertJavaStringToUTF8(env, j_favicon_url)); |
| 281 |
| 282 // TODO(treib): Optimize this by creating a FaviconService::HasFavicon method |
| 283 // so that we don't have to actually get the image. |
| 284 ScopedJavaGlobalRef<jobject> j_scoped_callback(env, j_availability_callback); |
| 285 favicon_base::FaviconImageCallback callback_runner = |
| 286 base::Bind(&OnFaviconImageResultAvailable, j_scoped_callback, |
| 287 profile, web_contents, page_url, favicon_url); |
| 288 favicon::FaviconService* service = FaviconServiceFactory::GetForProfile( |
| 289 profile, ServiceAccessType::IMPLICIT_ACCESS); |
| 290 service->GetFaviconImage(favicon_url, callback_runner, |
| 291 cancelable_task_tracker_.get()); |
| 292 } |
| 293 |
| 213 FaviconHelper::~FaviconHelper() {} | 294 FaviconHelper::~FaviconHelper() {} |
| 214 | 295 |
| 215 static jint GetDominantColorForBitmap(JNIEnv* env, | 296 static jint GetDominantColorForBitmap(JNIEnv* env, |
| 216 jclass clazz, | 297 jclass clazz, |
| 217 jobject bitmap) { | 298 jobject bitmap) { |
| 218 if (!bitmap) | 299 if (!bitmap) |
| 219 return 0; | 300 return 0; |
| 220 | 301 |
| 221 gfx::JavaBitmap bitmap_lock(bitmap); | 302 gfx::JavaBitmap bitmap_lock(bitmap); |
| 222 SkBitmap skbitmap = gfx::CreateSkBitmapFromJavaBitmap(bitmap_lock); | 303 SkBitmap skbitmap = gfx::CreateSkBitmapFromJavaBitmap(bitmap_lock); |
| 223 return color_utils::CalculateKMeanColorOfBitmap(skbitmap); | 304 return color_utils::CalculateKMeanColorOfBitmap(skbitmap); |
| 224 } | 305 } |
| 225 | 306 |
| 226 // static | 307 // static |
| 227 bool FaviconHelper::RegisterFaviconHelper(JNIEnv* env) { | 308 bool FaviconHelper::RegisterFaviconHelper(JNIEnv* env) { |
| 228 return RegisterNativesImpl(env); | 309 return RegisterNativesImpl(env); |
| 229 } | 310 } |
| OLD | NEW |