| 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 #include <stddef.h> | 8 #include <stddef.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #include "jni/FaviconHelper_jni.h" | 29 #include "jni/FaviconHelper_jni.h" |
| 30 #include "third_party/skia/include/core/SkBitmap.h" | 30 #include "third_party/skia/include/core/SkBitmap.h" |
| 31 #include "ui/gfx/android/java_bitmap.h" | 31 #include "ui/gfx/android/java_bitmap.h" |
| 32 #include "ui/gfx/codec/png_codec.h" | 32 #include "ui/gfx/codec/png_codec.h" |
| 33 #include "ui/gfx/color_analysis.h" | 33 #include "ui/gfx/color_analysis.h" |
| 34 #include "ui/gfx/color_utils.h" | 34 #include "ui/gfx/color_utils.h" |
| 35 #include "ui/gfx/image/image_skia.h" | 35 #include "ui/gfx/image/image_skia.h" |
| 36 #include "ui/gfx/image/image_skia_rep.h" | 36 #include "ui/gfx/image/image_skia_rep.h" |
| 37 | 37 |
| 38 using base::android::JavaParamRef; | 38 using base::android::JavaParamRef; |
| 39 using base::android::JavaRef; |
| 39 using base::android::ScopedJavaGlobalRef; | 40 using base::android::ScopedJavaGlobalRef; |
| 40 using base::android::ScopedJavaLocalRef; | 41 using base::android::ScopedJavaLocalRef; |
| 41 using base::android::AttachCurrentThread; | 42 using base::android::AttachCurrentThread; |
| 42 using base::android::ConvertJavaStringToUTF16; | 43 using base::android::ConvertJavaStringToUTF16; |
| 43 using base::android::ConvertJavaStringToUTF8; | 44 using base::android::ConvertJavaStringToUTF8; |
| 44 using base::android::ConvertUTF8ToJavaString; | 45 using base::android::ConvertUTF8ToJavaString; |
| 45 | 46 |
| 46 namespace { | 47 namespace { |
| 47 | 48 |
| 48 void OnLocalFaviconAvailable( | 49 void OnLocalFaviconAvailable( |
| 49 ScopedJavaGlobalRef<jobject>* j_favicon_image_callback, | 50 const JavaRef<jobject>& j_favicon_image_callback, |
| 50 const favicon_base::FaviconRawBitmapResult& result) { | 51 const favicon_base::FaviconRawBitmapResult& result) { |
| 51 JNIEnv* env = AttachCurrentThread(); | 52 JNIEnv* env = AttachCurrentThread(); |
| 52 | 53 |
| 53 // Convert favicon_image_result to java objects. | 54 // Convert favicon_image_result to java objects. |
| 54 ScopedJavaLocalRef<jstring> j_icon_url = | 55 ScopedJavaLocalRef<jstring> j_icon_url = |
| 55 ConvertUTF8ToJavaString(env, result.icon_url.spec()); | 56 ConvertUTF8ToJavaString(env, result.icon_url.spec()); |
| 56 ScopedJavaLocalRef<jobject> j_favicon_bitmap; | 57 ScopedJavaLocalRef<jobject> j_favicon_bitmap; |
| 57 if (result.is_valid()) { | 58 if (result.is_valid()) { |
| 58 SkBitmap favicon_bitmap; | 59 SkBitmap favicon_bitmap; |
| 59 gfx::PNGCodec::Decode(result.bitmap_data->front(), | 60 gfx::PNGCodec::Decode(result.bitmap_data->front(), |
| 60 result.bitmap_data->size(), | 61 result.bitmap_data->size(), |
| 61 &favicon_bitmap); | 62 &favicon_bitmap); |
| 62 if (!favicon_bitmap.isNull()) | 63 if (!favicon_bitmap.isNull()) |
| 63 j_favicon_bitmap = gfx::ConvertToJavaBitmap(&favicon_bitmap); | 64 j_favicon_bitmap = gfx::ConvertToJavaBitmap(&favicon_bitmap); |
| 64 } | 65 } |
| 65 | 66 |
| 66 // Call java side OnLocalFaviconAvailable method. | 67 // Call java side OnLocalFaviconAvailable method. |
| 67 Java_FaviconImageCallback_onFaviconAvailable( | 68 Java_FaviconImageCallback_onFaviconAvailable(env, j_favicon_image_callback, |
| 68 env, j_favicon_image_callback->obj(), j_favicon_bitmap, j_icon_url); | 69 j_favicon_bitmap, j_icon_url); |
| 69 } | 70 } |
| 70 | 71 |
| 71 size_t GetLargestSizeIndex(const std::vector<gfx::Size>& sizes) { | 72 size_t GetLargestSizeIndex(const std::vector<gfx::Size>& sizes) { |
| 72 DCHECK(!sizes.empty()); | 73 DCHECK(!sizes.empty()); |
| 73 size_t ret = 0; | 74 size_t ret = 0; |
| 74 for (size_t i = 1; i < sizes.size(); ++i) { | 75 for (size_t i = 1; i < sizes.size(); ++i) { |
| 75 if (sizes[ret].GetArea() < sizes[i].GetArea()) | 76 if (sizes[ret].GetArea() < sizes[i].GetArea()) |
| 76 ret = i; | 77 ret = i; |
| 77 } | 78 } |
| 78 return ret; | 79 return ret; |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 if (!profile) | 159 if (!profile) |
| 159 return false; | 160 return false; |
| 160 | 161 |
| 161 favicon::FaviconService* favicon_service = | 162 favicon::FaviconService* favicon_service = |
| 162 FaviconServiceFactory::GetForProfile(profile, | 163 FaviconServiceFactory::GetForProfile(profile, |
| 163 ServiceAccessType::EXPLICIT_ACCESS); | 164 ServiceAccessType::EXPLICIT_ACCESS); |
| 164 DCHECK(favicon_service); | 165 DCHECK(favicon_service); |
| 165 if (!favicon_service) | 166 if (!favicon_service) |
| 166 return false; | 167 return false; |
| 167 | 168 |
| 168 ScopedJavaGlobalRef<jobject>* j_scoped_favicon_callback = | 169 favicon_base::FaviconRawBitmapCallback callback_runner = |
| 169 new ScopedJavaGlobalRef<jobject>(); | 170 base::Bind(&OnLocalFaviconAvailable, |
| 170 j_scoped_favicon_callback->Reset(env, j_favicon_image_callback); | 171 ScopedJavaGlobalRef<jobject>(j_favicon_image_callback)); |
| 171 | |
| 172 favicon_base::FaviconRawBitmapCallback callback_runner = base::Bind( | |
| 173 &OnLocalFaviconAvailable, base::Owned(j_scoped_favicon_callback)); | |
| 174 | 172 |
| 175 favicon_service->GetRawFaviconForPageURL( | 173 favicon_service->GetRawFaviconForPageURL( |
| 176 GURL(ConvertJavaStringToUTF16(env, j_page_url)), | 174 GURL(ConvertJavaStringToUTF16(env, j_page_url)), |
| 177 static_cast<int>(j_icon_types), | 175 static_cast<int>(j_icon_types), |
| 178 static_cast<int>(j_desired_size_in_pixel), | 176 static_cast<int>(j_desired_size_in_pixel), |
| 179 callback_runner, | 177 callback_runner, |
| 180 cancelable_task_tracker_.get()); | 178 cancelable_task_tracker_.get()); |
| 181 | 179 |
| 182 return true; | 180 return true; |
| 183 } | 181 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 | 256 |
| 259 gfx::JavaBitmap bitmap_lock(bitmap); | 257 gfx::JavaBitmap bitmap_lock(bitmap); |
| 260 SkBitmap skbitmap = gfx::CreateSkBitmapFromJavaBitmap(bitmap_lock); | 258 SkBitmap skbitmap = gfx::CreateSkBitmapFromJavaBitmap(bitmap_lock); |
| 261 return color_utils::CalculateKMeanColorOfBitmap(skbitmap); | 259 return color_utils::CalculateKMeanColorOfBitmap(skbitmap); |
| 262 } | 260 } |
| 263 | 261 |
| 264 // static | 262 // static |
| 265 bool FaviconHelper::RegisterFaviconHelper(JNIEnv* env) { | 263 bool FaviconHelper::RegisterFaviconHelper(JNIEnv* env) { |
| 266 return RegisterNativesImpl(env); | 264 return RegisterNativesImpl(env); |
| 267 } | 265 } |
| OLD | NEW |