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_infobar_delegate_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/location.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/android/shortcut_helper.h"
13 #include "chrome/browser/android/shortcut_info.h"
14 #include "chrome/browser/android/tab_android.h"
15 #include "chrome/browser/banners/app_banner_data_fetcher.h"
16 #include "chrome/browser/banners/app_banner_metrics.h"
17 #include "chrome/browser/banners/app_banner_settings_helper.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/infobars/infobar_service.h"
20 #include "chrome/browser/ui/android/infobars/app_banner_infobar_android.h"
21 #include "chrome/common/render_messages.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "components/rappor/rappor_utils.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/render_frame_host.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/common/manifest.h"
28 #include "jni/AppBannerInfoBarDelegateAndroid_jni.h"
29 #include "ui/gfx/android/java_bitmap.h"
32 using base::android::ConvertJavaStringToUTF8
;
33 using base::android::ConvertJavaStringToUTF16
;
34 using base::android::ConvertUTF8ToJavaString
;
35 using base::android::ConvertUTF16ToJavaString
;
39 AppBannerInfoBarDelegateAndroid::AppBannerInfoBarDelegateAndroid(
41 const base::string16
& app_title
,
43 const content::Manifest
& web_app_data
)
44 : app_title_(app_title
),
46 event_request_id_(event_request_id
),
47 web_app_data_(web_app_data
),
48 has_user_interaction_(false) {
49 DCHECK(!web_app_data
.IsEmpty());
53 AppBannerInfoBarDelegateAndroid::AppBannerInfoBarDelegateAndroid(
55 const base::string16
& app_title
,
57 const base::android::ScopedJavaGlobalRef
<jobject
>& native_app_data
,
58 const std::string
& native_app_package
,
59 const std::string
& referrer
)
60 : app_title_(app_title
),
62 event_request_id_(event_request_id
),
63 native_app_data_(native_app_data
),
64 native_app_package_(native_app_package
),
66 has_user_interaction_(false) {
67 DCHECK(!native_app_data_
.is_null());
71 AppBannerInfoBarDelegateAndroid::~AppBannerInfoBarDelegateAndroid() {
72 if (!has_user_interaction_
) {
73 if (!native_app_data_
.is_null())
74 TrackUserResponse(USER_RESPONSE_NATIVE_APP_IGNORED
);
75 else if (!web_app_data_
.IsEmpty())
76 TrackUserResponse(USER_RESPONSE_WEB_APP_IGNORED
);
79 TrackDismissEvent(DISMISS_EVENT_DISMISSED
);
80 JNIEnv
* env
= base::android::AttachCurrentThread();
81 Java_AppBannerInfoBarDelegateAndroid_destroy(env
,
82 java_delegate_
.obj());
83 java_delegate_
.Reset();
86 void AppBannerInfoBarDelegateAndroid::UpdateInstallState(JNIEnv
* env
,
88 if (native_app_data_
.is_null())
91 int newState
= Java_AppBannerInfoBarDelegateAndroid_determineInstallState(
94 native_app_data_
.obj());
95 static_cast<AppBannerInfoBarAndroid
*>(infobar())
96 ->OnInstallStateChanged(newState
);
99 void AppBannerInfoBarDelegateAndroid::OnInstallIntentReturned(
102 jboolean jis_installing
) {
106 content::WebContents
* web_contents
=
107 InfoBarService::WebContentsFromInfoBar(infobar());
111 if (jis_installing
) {
112 AppBannerSettingsHelper::RecordBannerEvent(
114 web_contents
->GetURL(),
116 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN
,
117 AppBannerDataFetcher::GetCurrentTime());
119 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_STARTED
);
120 rappor::SampleDomainAndRegistryFromGURL(g_browser_process
->rappor_service(),
121 "AppBanner.NativeApp.Installed",
122 web_contents
->GetURL());
125 UpdateInstallState(env
, obj
);
128 void AppBannerInfoBarDelegateAndroid::OnInstallFinished(JNIEnv
* env
,
135 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_COMPLETED
);
136 UpdateInstallState(env
, obj
);
137 } else if (infobar()->owner()) {
138 TrackDismissEvent(DISMISS_EVENT_INSTALL_TIMEOUT
);
139 infobar()->owner()->RemoveInfoBar(infobar());
143 void AppBannerInfoBarDelegateAndroid::CreateJavaDelegate() {
144 JNIEnv
* env
= base::android::AttachCurrentThread();
145 java_delegate_
.Reset(Java_AppBannerInfoBarDelegateAndroid_create(
147 reinterpret_cast<intptr_t>(this)));
150 void AppBannerInfoBarDelegateAndroid::SendBannerAccepted(
151 content::WebContents
* web_contents
,
152 const std::string
& platform
) {
153 web_contents
->GetMainFrame()->Send(
154 new ChromeViewMsg_AppBannerAccepted(
155 web_contents
->GetMainFrame()->GetRoutingID(),
160 gfx::Image
AppBannerInfoBarDelegateAndroid::GetIcon() const {
161 return gfx::Image::CreateFrom1xBitmap(*app_icon_
.get());
164 void AppBannerInfoBarDelegateAndroid::InfoBarDismissed() {
165 has_user_interaction_
= true;
167 content::WebContents
* web_contents
=
168 InfoBarService::WebContentsFromInfoBar(infobar());
172 web_contents
->GetMainFrame()->Send(
173 new ChromeViewMsg_AppBannerDismissed(
174 web_contents
->GetMainFrame()->GetRoutingID(),
177 if (!native_app_data_
.is_null()) {
178 TrackUserResponse(USER_RESPONSE_NATIVE_APP_DISMISSED
);
179 AppBannerSettingsHelper::RecordBannerDismissEvent(
180 web_contents
, native_app_package_
, AppBannerSettingsHelper::NATIVE
);
181 } else if (!web_app_data_
.IsEmpty()) {
182 TrackUserResponse(USER_RESPONSE_WEB_APP_DISMISSED
);
183 AppBannerSettingsHelper::RecordBannerDismissEvent(
184 web_contents
, web_app_data_
.start_url
.spec(),
185 AppBannerSettingsHelper::WEB
);
189 base::string16
AppBannerInfoBarDelegateAndroid::GetMessageText() const {
193 int AppBannerInfoBarDelegateAndroid::GetButtons() const {
197 bool AppBannerInfoBarDelegateAndroid::Accept() {
198 has_user_interaction_
= true;
200 content::WebContents
* web_contents
=
201 InfoBarService::WebContentsFromInfoBar(infobar());
203 TrackDismissEvent(DISMISS_EVENT_ERROR
);
207 if (!native_app_data_
.is_null()) {
208 TrackUserResponse(USER_RESPONSE_NATIVE_APP_ACCEPTED
);
209 JNIEnv
* env
= base::android::AttachCurrentThread();
211 TabAndroid
* tab
= TabAndroid::FromWebContents(web_contents
);
212 if (tab
== nullptr) {
213 TrackDismissEvent(DISMISS_EVENT_ERROR
);
216 ScopedJavaLocalRef
<jstring
> jreferrer(
217 ConvertUTF8ToJavaString(env
, referrer_
));
219 bool was_opened
= Java_AppBannerInfoBarDelegateAndroid_installOrOpenNativeApp(
221 java_delegate_
.obj(),
222 tab
->GetJavaObject().obj(),
223 native_app_data_
.obj(),
227 TrackDismissEvent(DISMISS_EVENT_APP_OPEN
);
229 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_TRIGGERED
);
231 SendBannerAccepted(web_contents
, "play");
233 } else if (!web_app_data_
.IsEmpty()) {
234 TrackUserResponse(USER_RESPONSE_WEB_APP_ACCEPTED
);
236 AppBannerSettingsHelper::RecordBannerInstallEvent(
237 web_contents
, web_app_data_
.start_url
.spec(),
238 AppBannerSettingsHelper::WEB
);
240 ShortcutInfo
info(GURL::EmptyGURL());
241 info
.UpdateFromManifest(web_app_data_
);
242 info
.UpdateSource(ShortcutInfo::SOURCE_APP_BANNER
);
243 content::BrowserThread::PostTask(
244 content::BrowserThread::IO
,
246 base::Bind(&ShortcutHelper::AddShortcutInBackgroundWithSkBitmap
,
250 SendBannerAccepted(web_contents
, "web");
257 bool AppBannerInfoBarDelegateAndroid::LinkClicked(
258 WindowOpenDisposition disposition
) {
259 if (native_app_data_
.is_null())
262 // Try to show the details for the native app.
263 JNIEnv
* env
= base::android::AttachCurrentThread();
265 content::WebContents
* web_contents
=
266 InfoBarService::WebContentsFromInfoBar(infobar());
267 TabAndroid
* tab
= web_contents
? TabAndroid::FromWebContents(web_contents
)
269 if (tab
== nullptr) {
270 TrackDismissEvent(DISMISS_EVENT_ERROR
);
274 Java_AppBannerInfoBarDelegateAndroid_showAppDetails(env
,
275 java_delegate_
.obj(),
276 tab
->GetJavaObject().obj(),
277 native_app_data_
.obj());
279 TrackDismissEvent(DISMISS_EVENT_BANNER_CLICK
);
283 bool RegisterAppBannerInfoBarDelegateAndroid(JNIEnv
* env
) {
284 return RegisterNativesImpl(env
);
287 } // namespace banners