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 "chrome/browser/android/banners/app_banner_data_fetcher_android.h"
10 #include "chrome/browser/android/shortcut_helper.h"
11 #include "chrome/browser/banners/app_banner_metrics.h"
12 #include "chrome/common/chrome_constants.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/common/frame_navigate_params.h"
15 #include "jni/AppBannerManager_jni.h"
17 using base::android::ConvertJavaStringToUTF8
;
18 using base::android::ConvertJavaStringToUTF16
;
19 using base::android::ConvertUTF8ToJavaString
;
20 using base::android::ConvertUTF16ToJavaString
;
24 const char kPlayPlatform
[] = "play";
25 const char kReferrerName
[] = "referrer";
26 const char kIdName
[] = "id";
27 const char kPlayInlineReferrer
[] = "playinline=chrome_inline";
29 } // anonymous namespace
33 AppBannerManagerAndroid::AppBannerManagerAndroid(
37 weak_java_banner_view_manager_(env
, obj
) {
40 AppBannerManagerAndroid::~AppBannerManagerAndroid() {
43 void AppBannerManagerAndroid::Destroy(JNIEnv
* env
, jobject obj
) {
47 void AppBannerManagerAndroid::ReplaceWebContents(JNIEnv
* env
,
49 jobject jweb_contents
) {
50 content::WebContents
* web_contents
=
51 content::WebContents::FromJavaWebContents(jweb_contents
);
52 AppBannerManager::ReplaceWebContents(web_contents
);
55 bool AppBannerManagerAndroid::HandleNonWebApp(const std::string
& platform
,
57 const std::string
& id
) {
58 if (!CheckPlatformAndId(platform
, id
))
61 banners::TrackDisplayEvent(DISPLAY_EVENT_NATIVE_APP_BANNER_REQUESTED
);
63 // Send the info to the Java side to get info about the app.
64 JNIEnv
* env
= base::android::AttachCurrentThread();
65 ScopedJavaLocalRef
<jobject
> jobj
= weak_java_banner_view_manager_
.get(env
);
69 std::string id_from_app_url
= ExtractQueryValueForName(url
, kIdName
);
70 if (id_from_app_url
.size() && id
!= id_from_app_url
) {
71 banners::OutputDeveloperDebugMessage(web_contents(),
72 banners::kIgnoredIdsDoNotMatch
);
76 std::string referrer
=
77 ExtractQueryValueForName(url
, kReferrerName
);
79 // Attach the chrome_inline referrer value, prefixed with "&" if the referrer
82 referrer
= kPlayInlineReferrer
;
84 referrer
.append("&").append(kPlayInlineReferrer
);
86 ScopedJavaLocalRef
<jstring
> jurl(
87 ConvertUTF8ToJavaString(env
, data_fetcher()->validated_url().spec()));
88 ScopedJavaLocalRef
<jstring
> jpackage(
89 ConvertUTF8ToJavaString(env
, id
));
90 ScopedJavaLocalRef
<jstring
> jreferrer(
91 ConvertUTF8ToJavaString(env
, referrer
));
92 Java_AppBannerManager_fetchAppDetails(
93 env
, jobj
.obj(), jurl
.obj(),
94 jpackage
.obj(), jreferrer
.obj(),
95 ShortcutHelper::GetIdealHomescreenIconSizeInDp());
99 bool AppBannerManagerAndroid::CheckPlatformAndId(const std::string
& platform
,
100 const std::string
& id
) {
101 if (platform
!= kPlayPlatform
) {
102 banners::OutputDeveloperDebugMessage(
103 web_contents(), platform
+ banners::kIgnoredNotSupportedOnAndroid
);
107 banners::OutputDeveloperDebugMessage(web_contents(), banners::kIgnoredNoId
);
113 bool AppBannerManagerAndroid::CheckFetcherMatchesContents() {
117 if (!data_fetcher() ||
118 data_fetcher()->validated_url() != web_contents()->GetURL()) {
119 banners::OutputDeveloperNotShownMessage(
120 web_contents(), banners::kUserNavigatedBeforeBannerShown
);
126 std::string
AppBannerManagerAndroid::ExtractQueryValueForName(
128 const std::string
& name
) {
129 url::Component query
= url
.parsed_for_possibly_invalid_spec().query
;
130 url::Component key
, value
;
131 const char* url_spec
= url
.spec().c_str();
133 while (url::ExtractQueryKeyValue(url_spec
, &query
, &key
, &value
)) {
134 std::string
key_str(url_spec
, key
.begin
, key
.len
);
135 std::string
value_str(url_spec
, value
.begin
, value
.len
);
142 AppBannerDataFetcher
* AppBannerManagerAndroid::CreateAppBannerDataFetcher(
143 base::WeakPtr
<Delegate
> weak_delegate
) {
144 return new AppBannerDataFetcherAndroid(
147 ShortcutHelper::GetIdealHomescreenIconSizeInDp(),
148 ShortcutHelper::GetMinimumHomescreenIconSizeInDp(),
149 ShortcutHelper::GetIdealSplashImageSizeInDp(),
150 ShortcutHelper::GetMinimumSplashImageSizeInDp());
153 bool AppBannerManagerAndroid::OnAppDetailsRetrieved(JNIEnv
* env
,
157 jstring japp_package
,
159 if (!CheckFetcherMatchesContents())
162 base::android::ScopedJavaLocalRef
<jobject
> native_app_data
;
163 native_app_data
.Reset(env
, japp_data
);
164 GURL image_url
= GURL(ConvertJavaStringToUTF8(env
, jicon_url
));
166 AppBannerDataFetcherAndroid
* android_fetcher
=
167 static_cast<AppBannerDataFetcherAndroid
*>(data_fetcher().get());
168 return android_fetcher
->ContinueFetching(
169 ConvertJavaStringToUTF16(env
, japp_title
),
170 ConvertJavaStringToUTF8(env
, japp_package
),
171 native_app_data
, image_url
);
174 bool AppBannerManagerAndroid::IsFetcherActive(JNIEnv
* env
, jobject obj
) {
175 return AppBannerManager::IsFetcherActive();
179 bool AppBannerManagerAndroid::Register(JNIEnv
* env
) {
180 return RegisterNativesImpl(env
);
183 jlong
Init(JNIEnv
* env
,
184 const JavaParamRef
<jobject
>& obj
) {
185 AppBannerManagerAndroid
* manager
= new AppBannerManagerAndroid(env
, obj
);
186 return reinterpret_cast<intptr_t>(manager
);
189 void SetTimeDeltaForTesting(JNIEnv
* env
,
190 const JavaParamRef
<jclass
>& clazz
,
192 AppBannerDataFetcher::SetTimeDeltaForTesting(days
);
195 void DisableSecureSchemeCheckForTesting(JNIEnv
* env
,
196 const JavaParamRef
<jclass
>& clazz
) {
197 AppBannerManager::DisableSecureSchemeCheckForTesting();
200 void SetEngagementWeights(JNIEnv
* env
,
201 const JavaParamRef
<jclass
>& clazz
,
202 jdouble direct_engagement
,
203 jdouble indirect_engagement
) {
204 AppBannerManager::SetEngagementWeights(direct_engagement
,
205 indirect_engagement
);
208 } // namespace banners