OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/android/tab/thumbnail_tab_helper_android.h" |
| 6 |
| 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" |
| 9 #include "base/memory/ref_counted.h" |
| 10 #include "chrome/browser/history/top_sites_factory.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/profiles/profile_android.h" |
| 13 #include "chrome/browser/thumbnails/thumbnail_service.h" |
| 14 #include "chrome/browser/thumbnails/thumbnail_service_factory.h" |
| 15 #include "chrome/browser/thumbnails/thumbnail_tab_helper.h" |
| 16 #include "components/history/core/browser/top_sites.h" |
| 17 #include "content/public/browser/web_contents.h" |
| 18 #include "jni/ThumbnailTabHelper_jni.h" |
| 19 #include "third_party/skia/include/core/SkBitmap.h" |
| 20 #include "ui/gfx/android/java_bitmap.h" |
| 21 #include "ui/gfx/color_utils.h" |
| 22 #include "ui/gfx/image/image_skia.h" |
| 23 #include "url/gurl.h" |
| 24 |
| 25 // static |
| 26 bool RegisterThumbnailTabHelperAndroid(JNIEnv* env) { |
| 27 return RegisterNativesImpl(env); |
| 28 } |
| 29 |
| 30 static void InitThumbnailHelper(JNIEnv* env, |
| 31 jclass clazz, |
| 32 jobject jweb_contents) { |
| 33 content::WebContents* web_contents = |
| 34 content::WebContents::FromJavaWebContents(jweb_contents); |
| 35 DCHECK(web_contents); |
| 36 |
| 37 // Don't allow ThumbnailTabHelper to take thumbnail snapshots. |
| 38 // Currently the process is driven from Tab, but long term will |
| 39 // move into a Android specific tab helper. |
| 40 // Bug: crbug.com/157431 |
| 41 ThumbnailTabHelper* thumbnail_tab_helper = |
| 42 ThumbnailTabHelper::FromWebContents(web_contents); |
| 43 if (thumbnail_tab_helper) |
| 44 thumbnail_tab_helper->set_enabled(false); |
| 45 } |
| 46 |
| 47 static jboolean ShouldUpdateThumbnail( |
| 48 JNIEnv* env, jclass clazz, jobject jprofile, jstring jurl) { |
| 49 Profile* profile = ProfileAndroid::FromProfileAndroid(jprofile); |
| 50 |
| 51 GURL url(base::android::ConvertJavaStringToUTF8(env, jurl)); |
| 52 scoped_refptr<thumbnails::ThumbnailService> thumbnail_service = |
| 53 ThumbnailServiceFactory::GetForProfile(profile); |
| 54 return (thumbnail_service.get() != NULL && |
| 55 thumbnail_service->ShouldAcquirePageThumbnail(url)); |
| 56 } |
| 57 |
| 58 static void UpdateThumbnail(JNIEnv* env, |
| 59 jclass clazz, |
| 60 jobject jweb_contents, |
| 61 jobject bitmap, |
| 62 jboolean jat_top) { |
| 63 content::WebContents* web_contents = |
| 64 content::WebContents::FromJavaWebContents(jweb_contents); |
| 65 DCHECK(web_contents); |
| 66 |
| 67 Profile* profile = Profile::FromBrowserContext( |
| 68 web_contents->GetBrowserContext()); |
| 69 |
| 70 gfx::JavaBitmap bitmap_lock(bitmap); |
| 71 SkBitmap sk_bitmap; |
| 72 gfx::Size size = bitmap_lock.size(); |
| 73 SkColorType color_type = kN32_SkColorType; |
| 74 sk_bitmap.setInfo( |
| 75 SkImageInfo::Make(size.width(), size.height(), |
| 76 color_type, kPremul_SkAlphaType), 0); |
| 77 sk_bitmap.setPixels(bitmap_lock.pixels()); |
| 78 |
| 79 // TODO(nileshagrawal): Refactor this. |
| 80 // We were using some non-public methods from ThumbnailTabHelper. We need to |
| 81 // either add android specific logic to ThumbnailTabHelper or create our own |
| 82 // helper which is driven by the java app (will need to pull out some logic |
| 83 // from ThumbnailTabHelper to a common class). |
| 84 scoped_refptr<history::TopSites> ts = TopSitesFactory::GetForProfile(profile); |
| 85 if (!ts) |
| 86 return; |
| 87 |
| 88 // Compute the thumbnail score. |
| 89 ThumbnailScore score; |
| 90 score.at_top = jat_top; |
| 91 score.boring_score = color_utils::CalculateBoringScore(sk_bitmap); |
| 92 score.good_clipping = true; |
| 93 score.load_completed = !web_contents->IsLoading(); |
| 94 |
| 95 gfx::Image image = gfx::Image::CreateFrom1xBitmap(sk_bitmap); |
| 96 const GURL& url = web_contents->GetURL(); |
| 97 ts->SetPageThumbnail(url, image, score); |
| 98 } |
OLD | NEW |