Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / android / navigation_popup.cc
blob3ddf087bec212c2db5b04ad57eb3c1ade2bd551c
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.
5 #include "chrome/browser/ui/android/navigation_popup.h"
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_factory.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/profiles/profile_manager.h"
14 #include "chrome/common/url_constants.h"
15 #include "components/favicon/core/favicon_service.h"
16 #include "components/history/core/browser/history_types.h"
17 #include "jni/NavigationPopup_jni.h"
18 #include "third_party/skia/include/core/SkBitmap.h"
19 #include "ui/base/resource/resource_bundle.h"
20 #include "ui/gfx/android/java_bitmap.h"
21 #include "ui/gfx/image/image.h"
22 #include "ui/resources/grit/ui_resources.h"
23 #include "url/gurl.h"
25 using base::android::ConvertUTF8ToJavaString;
26 using base::android::ScopedJavaLocalRef;
28 NavigationPopup::NavigationPopup(JNIEnv* env, jobject obj)
29 : weak_jobject_(env, obj) {
32 NavigationPopup::~NavigationPopup() {
35 void NavigationPopup::Destroy(JNIEnv* env, jobject obj) {
36 delete this;
39 void NavigationPopup::FetchFaviconForUrl(JNIEnv* env,
40 jobject obj,
41 jstring jurl) {
42 Profile* profile = g_browser_process->profile_manager()->GetLastUsedProfile();
43 favicon::FaviconService* favicon_service =
44 FaviconServiceFactory::GetForProfile(profile,
45 ServiceAccessType::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->GetFaviconImageForPageURL(
52 url,
53 base::Bind(&NavigationPopup::OnFaviconDataAvailable,
54 base::Unretained(this),
55 url),
56 &cancelable_task_tracker_);
59 void NavigationPopup::OnFaviconDataAvailable(
60 GURL navigation_entry_url,
61 const favicon_base::FaviconImageResult& image_result) {
62 gfx::Image image(image_result.image);
63 if (image.IsEmpty()) {
64 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
65 image = rb.GetImageNamed(IDR_DEFAULT_FAVICON);
68 JNIEnv* env = base::android::AttachCurrentThread();
69 ScopedJavaLocalRef<jobject> obj(weak_jobject_.get(env));
70 if (!obj.obj())
71 return;
73 ScopedJavaLocalRef<jstring> jurl(
74 ConvertUTF8ToJavaString(env, navigation_entry_url.spec()));
76 Java_NavigationPopup_onFaviconUpdated(
77 env,
78 obj.obj(),
79 jurl.obj(),
80 gfx::ConvertToJavaBitmap(image.ToSkBitmap()).obj());
83 static ScopedJavaLocalRef<jstring> GetHistoryUrl(JNIEnv* env, jclass clazz) {
84 return ConvertUTF8ToJavaString(env, chrome::kChromeUIHistoryURL);
87 static jlong Init(JNIEnv* env, jobject obj) {
88 NavigationPopup* popup = new NavigationPopup(env, obj);
89 return reinterpret_cast<intptr_t>(popup);
92 // static
93 bool NavigationPopup::RegisterNavigationPopup(JNIEnv* env) {
94 return RegisterNativesImpl(env);