Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1779)

Side by Side Diff: chrome/browser/android/favicon_helper.cc

Issue 1272883004: Popular sites on the NTP: Favicons! (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@enable_popular_sites
Patch Set: . Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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(ScopedJavaGlobalRef<jobject>* j_availability_callback,
98 Profile* profile,
99 const GURL& page_url,
100 int download_request_id,
101 int http_status_code,
102 const GURL& image_url,
103 const std::vector<SkBitmap>& bitmaps,
104 const std::vector<gfx::Size>& original_sizes) {
105 bool success = !bitmaps.empty();
106 if (success) {
107 gfx::Image image = gfx::Image(CreateFaviconImageSkia(bitmaps,
108 original_sizes,
109 gfx::kFaviconSize,
110 nullptr));
111 favicon_base::SetFaviconColorSpace(&image);
112 favicon::FaviconService* service = FaviconServiceFactory::GetForProfile(
113 profile, ServiceAccessType::IMPLICIT_ACCESS);
114 service->SetFavicons(page_url, image_url, favicon_base::FAVICON, image);
115 }
116
117 JNIEnv* env = AttachCurrentThread();
118 Java_FaviconAvailabilityCallback_onFaviconAvailabilityChecked(
119 env, j_availability_callback->obj(), success);
120 }
121
122 void OnFaviconImageResultAvailable(
123 ScopedJavaGlobalRef<jobject>* j_availability_callback,
124 Profile* profile,
125 content::WebContents* web_contents,
126 const GURL& page_url,
127 const GURL& favicon_url,
128 const favicon_base::FaviconImageResult& result) {
129 // If there already is a favicon, return immediately.
130 if (!result.image.IsEmpty()) {
131 JNIEnv* env = AttachCurrentThread();
132 Java_FaviconAvailabilityCallback_onFaviconAvailabilityChecked(
133 env, j_availability_callback->obj(), false);
134 return;
135 }
136
137 web_contents->DownloadImage(favicon_url, true, 0, false,
138 base::Bind(OnFaviconDownloaded,
139 j_availability_callback,
140 profile, page_url));
141 }
142
91 } // namespace 143 } // namespace
92 144
93 static jlong Init(JNIEnv* env, jclass clazz) { 145 static jlong Init(JNIEnv* env, jclass clazz) {
94 return reinterpret_cast<intptr_t>(new FaviconHelper()); 146 return reinterpret_cast<intptr_t>(new FaviconHelper());
95 } 147 }
96 148
97 FaviconHelper::FaviconHelper() { 149 FaviconHelper::FaviconHelper() {
98 cancelable_task_tracker_.reset(new base::CancelableTaskTracker()); 150 cancelable_task_tracker_.reset(new base::CancelableTaskTracker());
99 } 151 }
100 152
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 gfx::Image favicon_image = gfx::Image::CreateFrom1xPNGBytes(favicon_png); 255 gfx::Image favicon_image = gfx::Image::CreateFrom1xPNGBytes(favicon_png);
204 SkBitmap favicon_bitmap = favicon_image.AsBitmap(); 256 SkBitmap favicon_bitmap = favicon_image.AsBitmap();
205 257
206 ScopedJavaLocalRef<jobject> j_favicon_bitmap; 258 ScopedJavaLocalRef<jobject> j_favicon_bitmap;
207 if (favicon_bitmap.isNull()) 259 if (favicon_bitmap.isNull())
208 return ScopedJavaLocalRef<jobject>(); 260 return ScopedJavaLocalRef<jobject>();
209 261
210 return gfx::ConvertToJavaBitmap(&favicon_bitmap); 262 return gfx::ConvertToJavaBitmap(&favicon_bitmap);
211 } 263 }
212 264
265 void FaviconHelper::EnsureFaviconIsAvailable(
266 JNIEnv* env,
267 jobject obj,
268 jobject j_profile,
269 jobject j_web_contents,
270 jstring j_page_url,
271 jstring j_favicon_url,
272 jobject j_availability_callback) {
273 Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile);
274 DCHECK(profile);
275 content::WebContents* web_contents =
276 content::WebContents::FromJavaWebContents(j_web_contents);
277 DCHECK(web_contents);
278 GURL page_url = GURL(ConvertJavaStringToUTF8(env, j_page_url));
Bernhard Bauer 2015/08/11 09:37:25 You can directly initialize the variable (as oppos
Marc Treib 2015/08/11 10:23:33 I'm pretty sure that amounts to the exact same thi
279 GURL favicon_url = GURL(ConvertJavaStringToUTF8(env, j_favicon_url));
280
281 ScopedJavaGlobalRef<jobject>* j_scoped_callback =
Bernhard Bauer 2015/08/11 09:37:25 Where is this object destroyed?
Marc Treib 2015/08/11 10:23:33 It wasn't :) Now it's done in the Bind below via b
Bernhard Bauer 2015/08/11 10:45:06 Urr... except now it will be destroyed at the end
Marc Treib 2015/08/11 11:22:41 Durr. That was silly, sorry. (And good catch!) Now
282 new ScopedJavaGlobalRef<jobject>();
283 j_scoped_callback->Reset(env, j_availability_callback);
284
285 // TODO(treib): Optimize this by creating a FaviconService::HasFavicon method
286 // so that we don't have to actually get the image.
287 favicon_base::FaviconImageCallback callback_runner =
288 base::Bind(&OnFaviconImageResultAvailable, j_scoped_callback,
289 profile, web_contents, page_url, favicon_url);
290 favicon::FaviconService* service = FaviconServiceFactory::GetForProfile(
291 profile, ServiceAccessType::IMPLICIT_ACCESS);
292 service->GetFaviconImage(favicon_url, callback_runner,
293 cancelable_task_tracker_.get());
294 }
295
213 FaviconHelper::~FaviconHelper() {} 296 FaviconHelper::~FaviconHelper() {}
214 297
215 static jint GetDominantColorForBitmap(JNIEnv* env, 298 static jint GetDominantColorForBitmap(JNIEnv* env,
216 jclass clazz, 299 jclass clazz,
217 jobject bitmap) { 300 jobject bitmap) {
218 if (!bitmap) 301 if (!bitmap)
219 return 0; 302 return 0;
220 303
221 gfx::JavaBitmap bitmap_lock(bitmap); 304 gfx::JavaBitmap bitmap_lock(bitmap);
222 SkBitmap skbitmap = gfx::CreateSkBitmapFromJavaBitmap(bitmap_lock); 305 SkBitmap skbitmap = gfx::CreateSkBitmapFromJavaBitmap(bitmap_lock);
223 return color_utils::CalculateKMeanColorOfBitmap(skbitmap); 306 return color_utils::CalculateKMeanColorOfBitmap(skbitmap);
224 } 307 }
225 308
226 // static 309 // static
227 bool FaviconHelper::RegisterFaviconHelper(JNIEnv* env) { 310 bool FaviconHelper::RegisterFaviconHelper(JNIEnv* env) {
228 return RegisterNativesImpl(env); 311 return RegisterNativesImpl(env);
229 } 312 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698