1 // Copyright (c) 2012 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/ui/android/navigation_popup.h" 6 7 #include "base/android/jni_string.h" 8 #include "base/android/scoped_java_ref.h" 9 #include "base/bind.h" 10 #include "chrome/browser/browser_process.h" 11 #include "chrome/browser/favicon/favicon_service.h" 12 #include "chrome/browser/favicon/favicon_service_factory.h" 13 #include "chrome/browser/history/history_types.h" 14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile_manager.h" 16 #include "chrome/common/url_constants.h" 17 #include "grit/ui_resources.h" 18 #include "jni/NavigationPopup_jni.h" 19 #include "third_party/skia/include/core/SkBitmap.h" 20 #include "ui/base/resource/resource_bundle.h" 21 #include "ui/gfx/android/java_bitmap.h" 22 #include "ui/gfx/favicon_size.h" 23 #include "ui/gfx/image/image.h" 24 #include "url/gurl.h" 25 26 using base::android::ConvertUTF8ToJavaString; 27 using base::android::ScopedJavaLocalRef; 28 29 NavigationPopup::NavigationPopup(JNIEnv* env, jobject obj) 30 : weak_jobject_(env, obj) { 31 } 32 33 NavigationPopup::~NavigationPopup() { 34 } 35 36 void NavigationPopup::Destroy(JNIEnv* env, jobject obj) { 37 delete this; 38 } 39 40 void NavigationPopup::FetchFaviconForUrl(JNIEnv* env, 41 jobject obj, 42 jstring jurl) { 43 Profile* profile = g_browser_process->profile_manager()->GetLastUsedProfile(); 44 FaviconService* favicon_service = FaviconServiceFactory::GetForProfile( 45 profile, Profile::EXPLICIT_ACCESS); 46 if (!favicon_service) 47 return; 48 GURL url(base::android::ConvertJavaStringToUTF16(env, jurl)); 49 // TODO(tedchoc): Request higher favicons based on screen density instead of 50 // hardcoding kFaviconSize. 51 favicon_service->GetFaviconImageForURL( 52 FaviconService::FaviconForURLParams(profile, 53 url, 54 chrome::FAVICON, 55 gfx::kFaviconSize), 56 base::Bind(&NavigationPopup::OnFaviconDataAvailable, 57 base::Unretained(this), 58 url), 59 &cancelable_task_tracker_); 60 } 61 62 void NavigationPopup::OnFaviconDataAvailable( 63 GURL navigation_entry_url, 64 const chrome::FaviconImageResult& image_result) { 65 gfx::Image image(image_result.image); 66 if (image.IsEmpty()) { 67 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 68 image = rb.GetImageNamed(IDR_DEFAULT_FAVICON); 69 } 70 71 JNIEnv* env = base::android::AttachCurrentThread(); 72 ScopedJavaLocalRef<jobject> obj(weak_jobject_.get(env)); 73 if (!obj.obj()) 74 return; 75 76 ScopedJavaLocalRef<jstring> jurl( 77 ConvertUTF8ToJavaString(env, navigation_entry_url.spec())); 78 79 Java_NavigationPopup_onFaviconUpdated( 80 env, 81 obj.obj(), 82 jurl.obj(), 83 gfx::ConvertToJavaBitmap(image.ToSkBitmap()).obj()); 84 } 85 86 static jstring GetHistoryUrl(JNIEnv* env, jclass clazz) { 87 return ConvertUTF8ToJavaString(env, chrome::kChromeUIHistoryURL).Release(); 88 } 89 90 static jint Init(JNIEnv* env, jobject obj) { 91 NavigationPopup* popup = new NavigationPopup(env, obj); 92 return reinterpret_cast<jint>(popup); 93 } 94 95 // static 96 bool NavigationPopup::RegisterNavigationPopup(JNIEnv* env) { 97 return RegisterNativesImpl(env); 98 } 99