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
,
32 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(
48 JNIEnv
* env
, jclass clazz
, jobject jprofile
, jstring jurl
) {
49 Profile
* profile
= ProfileAndroid::FromProfileAndroid(jprofile
);
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
));
58 static void UpdateThumbnail(JNIEnv
* env
,
60 jobject jweb_contents
,
63 content::WebContents
* web_contents
=
64 content::WebContents::FromJavaWebContents(jweb_contents
);
67 Profile
* profile
= Profile::FromBrowserContext(
68 web_contents
->GetBrowserContext());
70 gfx::JavaBitmap
bitmap_lock(bitmap
);
72 gfx::Size size
= bitmap_lock
.size();
73 SkColorType color_type
= kN32_SkColorType
;
75 SkImageInfo::Make(size
.width(), size
.height(),
76 color_type
, kPremul_SkAlphaType
), 0);
77 sk_bitmap
.setPixels(bitmap_lock
.pixels());
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
);
88 // Compute the thumbnail 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();
95 gfx::Image image
= gfx::Image::CreateFrom1xBitmap(sk_bitmap
);
96 const GURL
& url
= web_contents
->GetURL();
97 ts
->SetPageThumbnail(url
, image
, score
);