1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/banners/app_banner_manager_android.h"
#include "base/android/jni_android.h"
#include "base/android/jni_string.h"
#include "chrome/browser/android/banners/app_banner_data_fetcher_android.h"
#include "chrome/browser/android/shortcut_helper.h"
#include "chrome/browser/banners/app_banner_metrics.h"
#include "chrome/common/chrome_constants.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/frame_navigate_params.h"
#include "jni/AppBannerManager_jni.h"
using base::android::ConvertJavaStringToUTF8;
using base::android::ConvertJavaStringToUTF16;
using base::android::ConvertUTF8ToJavaString;
using base::android::ConvertUTF16ToJavaString;
namespace {
const char kPlayPlatform[] = "play";
const char kReferrerName[] = "referrer";
const char kIdName[] = "id";
const char kPlayInlineReferrer[] = "playinline=chrome_inline";
} // anonymous namespace
namespace banners {
AppBannerManagerAndroid::AppBannerManagerAndroid(
JNIEnv* env,
jobject obj)
: AppBannerManager(),
weak_java_banner_view_manager_(env, obj) {
}
AppBannerManagerAndroid::~AppBannerManagerAndroid() {
}
void AppBannerManagerAndroid::Destroy(JNIEnv* env,
const JavaParamRef<jobject>& obj) {
delete this;
}
void AppBannerManagerAndroid::ReplaceWebContents(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jobject>& jweb_contents) {
content::WebContents* web_contents =
content::WebContents::FromJavaWebContents(jweb_contents);
AppBannerManager::ReplaceWebContents(web_contents);
}
bool AppBannerManagerAndroid::HandleNonWebApp(const std::string& platform,
const GURL& url,
const std::string& id,
bool is_debug_mode) {
if (!CheckPlatformAndId(platform, id, is_debug_mode))
return false;
banners::TrackDisplayEvent(DISPLAY_EVENT_NATIVE_APP_BANNER_REQUESTED);
// Send the info to the Java side to get info about the app.
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jobject> jobj = weak_java_banner_view_manager_.get(env);
if (jobj.is_null())
return false;
std::string id_from_app_url = ExtractQueryValueForName(url, kIdName);
if (id_from_app_url.size() && id != id_from_app_url) {
banners::OutputDeveloperDebugMessage(
web_contents(), banners::kIgnoredIdsDoNotMatch, is_debug_mode);
return false;
}
std::string referrer =
ExtractQueryValueForName(url, kReferrerName);
// Attach the chrome_inline referrer value, prefixed with "&" if the referrer
// is non empty.
if (referrer.empty())
referrer = kPlayInlineReferrer;
else
referrer.append("&").append(kPlayInlineReferrer);
ScopedJavaLocalRef<jstring> jurl(
ConvertUTF8ToJavaString(env, data_fetcher()->validated_url().spec()));
ScopedJavaLocalRef<jstring> jpackage(
ConvertUTF8ToJavaString(env, id));
ScopedJavaLocalRef<jstring> jreferrer(
ConvertUTF8ToJavaString(env, referrer));
Java_AppBannerManager_fetchAppDetails(
env, jobj.obj(), jurl.obj(),
jpackage.obj(), jreferrer.obj(),
ShortcutHelper::GetIdealHomescreenIconSizeInDp());
return true;
}
bool AppBannerManagerAndroid::CheckPlatformAndId(const std::string& platform,
const std::string& id,
bool is_debug_mode) {
if (platform != kPlayPlatform) {
banners::OutputDeveloperDebugMessage(
web_contents(), platform + banners::kIgnoredNotSupportedOnAndroid,
is_debug_mode);
return false;
}
if (id.empty()) {
banners::OutputDeveloperDebugMessage(web_contents(), banners::kIgnoredNoId,
is_debug_mode);
return false;
}
return true;
}
bool AppBannerManagerAndroid::CheckFetcherMatchesContents(bool is_debug_mode) {
if (!web_contents())
return false;
if (!data_fetcher() ||
data_fetcher()->validated_url() != web_contents()->GetURL()) {
banners::OutputDeveloperNotShownMessage(
web_contents(), banners::kUserNavigatedBeforeBannerShown,
is_debug_mode);
return false;
}
return true;
}
std::string AppBannerManagerAndroid::ExtractQueryValueForName(
const GURL& url,
const std::string& name) {
url::Component query = url.parsed_for_possibly_invalid_spec().query;
url::Component key, value;
const char* url_spec = url.spec().c_str();
while (url::ExtractQueryKeyValue(url_spec, &query, &key, &value)) {
std::string key_str(url_spec, key.begin, key.len);
std::string value_str(url_spec, value.begin, value.len);
if (key_str == name)
return value_str;
}
return "";
}
AppBannerDataFetcher* AppBannerManagerAndroid::CreateAppBannerDataFetcher(
base::WeakPtr<Delegate> weak_delegate,
bool is_debug_mode) {
return new AppBannerDataFetcherAndroid(
web_contents(), weak_delegate,
ShortcutHelper::GetIdealHomescreenIconSizeInDp(),
ShortcutHelper::GetMinimumHomescreenIconSizeInDp(),
ShortcutHelper::GetIdealSplashImageSizeInDp(),
ShortcutHelper::GetMinimumSplashImageSizeInDp(), is_debug_mode);
}
bool AppBannerManagerAndroid::OnAppDetailsRetrieved(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jobject>& japp_data,
const JavaParamRef<jstring>& japp_title,
const JavaParamRef<jstring>& japp_package,
const JavaParamRef<jstring>& jicon_url) {
AppBannerDataFetcherAndroid* android_fetcher =
static_cast<AppBannerDataFetcherAndroid*>(data_fetcher().get());
if (!CheckFetcherMatchesContents(android_fetcher->is_debug_mode()))
return false;
GURL image_url = GURL(ConvertJavaStringToUTF8(env, jicon_url));
return android_fetcher->ContinueFetching(
ConvertJavaStringToUTF16(env, japp_title),
ConvertJavaStringToUTF8(env, japp_package), japp_data, image_url);
}
bool AppBannerManagerAndroid::IsFetcherActive(
JNIEnv* env,
const JavaParamRef<jobject>& obj) {
return AppBannerManager::IsFetcherActive();
}
void AppBannerManagerAndroid::RequestAppBanner(
JNIEnv* env,
const JavaParamRef<jobject>& obj) {
AppBannerManager::RequestAppBanner(web_contents()->GetMainFrame(),
web_contents()->GetLastCommittedURL(),
true);
}
// static
bool AppBannerManagerAndroid::Register(JNIEnv* env) {
return RegisterNativesImpl(env);
}
jlong Init(JNIEnv* env,
const JavaParamRef<jobject>& obj) {
AppBannerManagerAndroid* manager = new AppBannerManagerAndroid(env, obj);
return reinterpret_cast<intptr_t>(manager);
}
void SetTimeDeltaForTesting(JNIEnv* env,
const JavaParamRef<jclass>& clazz,
jint days) {
AppBannerDataFetcher::SetTimeDeltaForTesting(days);
}
void DisableSecureSchemeCheckForTesting(JNIEnv* env,
const JavaParamRef<jclass>& clazz) {
AppBannerManager::DisableSecureSchemeCheckForTesting();
}
void SetEngagementWeights(JNIEnv* env,
const JavaParamRef<jclass>& clazz,
jdouble direct_engagement,
jdouble indirect_engagement) {
AppBannerManager::SetEngagementWeights(direct_engagement,
indirect_engagement);
}
} // namespace banners
|