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.
5 #include "chrome/browser/android/tab/thumbnail_tab_helper_android.h"
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"
26 bool RegisterThumbnailTabHelperAndroid(JNIEnv
* env
) {
27 return RegisterNativesImpl(env
);
30 static void InitThumbnailHelper(JNIEnv
* env
,
31 const JavaParamRef
<jclass
>& clazz
,
32 const JavaParamRef
<jobject
>& jweb_contents
) {
33 content::WebContents
* web_contents
=
34 content::WebContents::FromJavaWebContents(jweb_contents
);
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);
47 static jboolean
ShouldUpdateThumbnail(JNIEnv
* env
,
48 const JavaParamRef
<jclass
>& clazz
,
49 const JavaParamRef
<jobject
>& jprofile
,
50 const JavaParamRef
<jstring
>& jurl
) {
51 Profile
* profile
= ProfileAndroid::FromProfileAndroid(jprofile
);
53 GURL
url(base::android::ConvertJavaStringToUTF8(env
, jurl
));
54 scoped_refptr
<thumbnails::ThumbnailService
> thumbnail_service
=
55 ThumbnailServiceFactory::GetForProfile(profile
);
56 return (thumbnail_service
.get() != NULL
&&
57 thumbnail_service
->ShouldAcquirePageThumbnail(url
));
60 static void UpdateThumbnail(JNIEnv
* env
,
61 const JavaParamRef
<jclass
>& clazz
,
62 const JavaParamRef
<jobject
>& jweb_contents
,
63 const JavaParamRef
<jobject
>& bitmap
,
65 content::WebContents
* web_contents
=
66 content::WebContents::FromJavaWebContents(jweb_contents
);
69 Profile
* profile
= Profile::FromBrowserContext(
70 web_contents
->GetBrowserContext());
72 gfx::JavaBitmap
bitmap_lock(bitmap
);
74 gfx::Size size
= bitmap_lock
.size();
75 SkColorType color_type
= kN32_SkColorType
;
77 SkImageInfo::Make(size
.width(), size
.height(),
78 color_type
, kPremul_SkAlphaType
), 0);
79 sk_bitmap
.setPixels(bitmap_lock
.pixels());
81 // TODO(nileshagrawal): Refactor this.
82 // We were using some non-public methods from ThumbnailTabHelper. We need to
83 // either add android specific logic to ThumbnailTabHelper or create our own
84 // helper which is driven by the java app (will need to pull out some logic
85 // from ThumbnailTabHelper to a common class).
86 scoped_refptr
<history::TopSites
> ts
= TopSitesFactory::GetForProfile(profile
);
90 // Compute the thumbnail score.
92 score
.at_top
= jat_top
;
93 score
.boring_score
= color_utils::CalculateBoringScore(sk_bitmap
);
94 score
.good_clipping
= true;
95 score
.load_completed
= !web_contents
->IsLoading();
97 gfx::Image image
= gfx::Image::CreateFrom1xBitmap(sk_bitmap
);
98 const GURL
& url
= web_contents
->GetURL();
99 ts
->SetPageThumbnail(url
, image
, score
);