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/banners/app_banner_manager_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/command_line.h"
10 #include "base/metrics/field_trial.h"
11 #include "chrome/browser/android/banners/app_banner_data_fetcher_android.h"
12 #include "chrome/browser/banners/app_banner_metrics.h"
13 #include "chrome/common/chrome_constants.h"
14 #include "chrome/common/render_messages.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/common/frame_navigate_params.h"
17 #include "jni/AppBannerManager_jni.h"
19 using base::android::ConvertJavaStringToUTF8
;
20 using base::android::ConvertJavaStringToUTF16
;
21 using base::android::ConvertUTF8ToJavaString
;
22 using base::android::ConvertUTF16ToJavaString
;
25 const char kPlayPlatform
[] = "play";
26 } // anonymous namespace
30 AppBannerManagerAndroid::AppBannerManagerAndroid(JNIEnv
* env
,
33 : AppBannerManager(icon_size
),
34 weak_java_banner_view_manager_(env
, obj
) {
37 AppBannerManagerAndroid::~AppBannerManagerAndroid() {
40 void AppBannerManagerAndroid::Destroy(JNIEnv
* env
, jobject obj
) {
44 void AppBannerManagerAndroid::ReplaceWebContents(JNIEnv
* env
,
46 jobject jweb_contents
) {
47 content::WebContents
* web_contents
=
48 content::WebContents::FromJavaWebContents(jweb_contents
);
49 AppBannerManager::ReplaceWebContents(web_contents
);
52 bool AppBannerManagerAndroid::HandleNonWebApp(const std::string
& platform
,
54 const std::string
& id
) {
55 if (!CheckPlatformAndId(platform
, id
))
58 banners::TrackDisplayEvent(DISPLAY_EVENT_NATIVE_APP_BANNER_REQUESTED
);
60 // Send the info to the Java side to get info about the app.
61 JNIEnv
* env
= base::android::AttachCurrentThread();
62 ScopedJavaLocalRef
<jobject
> jobj
= weak_java_banner_view_manager_
.get(env
);
66 ScopedJavaLocalRef
<jstring
> jurl(
67 ConvertUTF8ToJavaString(env
, data_fetcher()->validated_url().spec()));
68 ScopedJavaLocalRef
<jstring
> jpackage(
69 ConvertUTF8ToJavaString(env
, id
));
70 Java_AppBannerManager_fetchAppDetails(
71 env
, jobj
.obj(), jurl
.obj(), jpackage
.obj(), ideal_icon_size());
75 bool AppBannerManagerAndroid::CheckPlatformAndId(const std::string
& platform
,
76 const std::string
& id
) {
77 if (platform
!= kPlayPlatform
) {
78 banners::OutputDeveloperDebugMessage(
79 web_contents(), platform
+ banners::kIgnoredNotSupportedOnAndroid
);
83 banners::OutputDeveloperDebugMessage(web_contents(), banners::kIgnoredNoId
);
89 bool AppBannerManagerAndroid::CheckFetcherMatchesContents() {
90 if (!web_contents()) {
93 if (!data_fetcher() ||
94 data_fetcher()->validated_url() != web_contents()->GetURL()) {
95 banners::OutputDeveloperNotShownMessage(
96 web_contents(), banners::kUserNavigatedBeforeBannerShown
);
102 AppBannerDataFetcher
* AppBannerManagerAndroid::CreateAppBannerDataFetcher(
103 base::WeakPtr
<Delegate
> weak_delegate
,
104 const int ideal_icon_size
) {
105 return new AppBannerDataFetcherAndroid(web_contents(), weak_delegate
,
109 bool AppBannerManagerAndroid::OnAppDetailsRetrieved(JNIEnv
* env
,
113 jstring japp_package
,
115 if (!CheckFetcherMatchesContents())
118 base::android::ScopedJavaLocalRef
<jobject
> native_app_data
;
119 native_app_data
.Reset(env
, japp_data
);
120 GURL image_url
= GURL(ConvertJavaStringToUTF8(env
, jicon_url
));
122 AppBannerDataFetcherAndroid
* android_fetcher
=
123 static_cast<AppBannerDataFetcherAndroid
*>(data_fetcher().get());
124 return android_fetcher
->ContinueFetching(
125 ConvertJavaStringToUTF16(env
, japp_title
),
126 ConvertJavaStringToUTF8(env
, japp_package
),
127 native_app_data
, image_url
);
130 bool AppBannerManagerAndroid::IsFetcherActive(JNIEnv
* env
, jobject obj
) {
131 return AppBannerManager::IsFetcherActive();
135 bool AppBannerManagerAndroid::Register(JNIEnv
* env
) {
136 return RegisterNativesImpl(env
);
139 jlong
Init(JNIEnv
* env
, jobject obj
, jint icon_size
) {
140 AppBannerManagerAndroid
* manager
=
141 new AppBannerManagerAndroid(env
, obj
, icon_size
);
142 return reinterpret_cast<intptr_t>(manager
);
145 void SetTimeDeltaForTesting(JNIEnv
* env
, jclass clazz
, jint days
) {
146 AppBannerDataFetcher::SetTimeDeltaForTesting(days
);
149 void DisableSecureSchemeCheckForTesting(JNIEnv
* env
, jclass clazz
) {
150 AppBannerManager::DisableSecureSchemeCheckForTesting();
153 void ForceEngagementWeightsForTesting(JNIEnv
* env
,
155 jdouble direct_engagement
,
156 jdouble indirect_engagement
) {
157 AppBannerManager::ForceEngagementWeightsForTesting(direct_engagement
,
158 indirect_engagement
);
161 jboolean
IsEnabled(JNIEnv
* env
, jclass clazz
) {
162 return base::FieldTrialList::FindFullName("AppBanners") == "Enabled";
165 } // namespace banners