| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/android/banners/app_banner_infobar_delegate.h" | 5 #include "chrome/browser/android/banners/app_banner_infobar_delegate.h" |
| 6 | 6 |
| 7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" | 8 #include "base/android/jni_string.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/threading/worker_pool.h" | 12 #include "base/threading/worker_pool.h" |
| 13 #include "chrome/browser/android/banners/app_banner_manager.h" | 13 #include "chrome/browser/android/banners/app_banner_manager.h" |
| 14 #include "chrome/browser/android/shortcut_helper.h" | 14 #include "chrome/browser/android/shortcut_helper.h" |
| 15 #include "chrome/browser/android/shortcut_info.h" | 15 #include "chrome/browser/android/shortcut_info.h" |
| 16 #include "chrome/browser/android/tab_android.h" | 16 #include "chrome/browser/android/tab_android.h" |
| 17 #include "chrome/browser/banners/app_banner_metrics.h" |
| 17 #include "chrome/browser/banners/app_banner_settings_helper.h" | 18 #include "chrome/browser/banners/app_banner_settings_helper.h" |
| 18 #include "chrome/browser/infobars/infobar_service.h" | 19 #include "chrome/browser/infobars/infobar_service.h" |
| 19 #include "chrome/browser/ui/android/infobars/app_banner_infobar.h" | 20 #include "chrome/browser/ui/android/infobars/app_banner_infobar.h" |
| 20 #include "chrome/grit/generated_resources.h" | 21 #include "chrome/grit/generated_resources.h" |
| 21 #include "components/infobars/core/infobar.h" | 22 #include "components/infobars/core/infobar.h" |
| 22 #include "components/infobars/core/infobar_manager.h" | 23 #include "components/infobars/core/infobar_manager.h" |
| 23 #include "content/public/common/manifest.h" | 24 #include "content/public/common/manifest.h" |
| 24 #include "jni/AppBannerInfoBarDelegate_jni.h" | 25 #include "jni/AppBannerInfoBarDelegate_jni.h" |
| 25 #include "ui/gfx/android/java_bitmap.h" | 26 #include "ui/gfx/android/java_bitmap.h" |
| 26 | 27 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 80 native_app_data_(native_app_data), | 81 native_app_data_(native_app_data), |
| 81 native_app_package_(native_app_package) { | 82 native_app_package_(native_app_package) { |
| 82 DCHECK(native_app_data_.is_null() ^ web_app_data_.IsEmpty()); | 83 DCHECK(native_app_data_.is_null() ^ web_app_data_.IsEmpty()); |
| 83 JNIEnv* env = base::android::AttachCurrentThread(); | 84 JNIEnv* env = base::android::AttachCurrentThread(); |
| 84 java_delegate_.Reset(Java_AppBannerInfoBarDelegate_create( | 85 java_delegate_.Reset(Java_AppBannerInfoBarDelegate_create( |
| 85 env, | 86 env, |
| 86 reinterpret_cast<intptr_t>(this))); | 87 reinterpret_cast<intptr_t>(this))); |
| 87 } | 88 } |
| 88 | 89 |
| 89 AppBannerInfoBarDelegate::~AppBannerInfoBarDelegate() { | 90 AppBannerInfoBarDelegate::~AppBannerInfoBarDelegate() { |
| 91 TrackDismissEvent(DISMISS_EVENT_DISMISSED); |
| 90 JNIEnv* env = base::android::AttachCurrentThread(); | 92 JNIEnv* env = base::android::AttachCurrentThread(); |
| 91 Java_AppBannerInfoBarDelegate_destroy(env, | 93 Java_AppBannerInfoBarDelegate_destroy(env, |
| 92 java_delegate_.obj()); | 94 java_delegate_.obj()); |
| 93 java_delegate_.Reset(); | 95 java_delegate_.Reset(); |
| 94 } | 96 } |
| 95 | 97 |
| 96 base::string16 AppBannerInfoBarDelegate::GetMessageText() const { | 98 base::string16 AppBannerInfoBarDelegate::GetMessageText() const { |
| 97 return app_title_; | 99 return app_title_; |
| 98 } | 100 } |
| 99 | 101 |
| 100 int AppBannerInfoBarDelegate::GetButtons() const { | 102 int AppBannerInfoBarDelegate::GetButtons() const { |
| 101 return BUTTON_OK; | 103 return BUTTON_OK; |
| 102 } | 104 } |
| 103 | 105 |
| 104 gfx::Image AppBannerInfoBarDelegate::GetIcon() const { | 106 gfx::Image AppBannerInfoBarDelegate::GetIcon() const { |
| 105 return gfx::Image::CreateFrom1xBitmap(*app_icon_.get()); | 107 return gfx::Image::CreateFrom1xBitmap(*app_icon_.get()); |
| 106 } | 108 } |
| 107 | 109 |
| 108 void AppBannerInfoBarDelegate::InfoBarDismissed() { | 110 void AppBannerInfoBarDelegate::InfoBarDismissed() { |
| 109 content::WebContents* web_contents = | 111 content::WebContents* web_contents = |
| 110 InfoBarService::WebContentsFromInfoBar(infobar()); | 112 InfoBarService::WebContentsFromInfoBar(infobar()); |
| 111 if (!web_contents) | 113 if (!web_contents) |
| 112 return; | 114 return; |
| 113 | 115 |
| 116 TrackDismissEvent(DISMISS_EVENT_CLOSE_BUTTON); |
| 117 |
| 114 if (!native_app_data_.is_null()) { | 118 if (!native_app_data_.is_null()) { |
| 115 AppBannerSettingsHelper::RecordBannerEvent( | 119 AppBannerSettingsHelper::RecordBannerEvent( |
| 116 web_contents, web_contents->GetURL(), | 120 web_contents, web_contents->GetURL(), |
| 117 native_app_package_, | 121 native_app_package_, |
| 118 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_BLOCK, | 122 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_BLOCK, |
| 119 AppBannerManager::GetCurrentTime()); | 123 AppBannerManager::GetCurrentTime()); |
| 120 } else if (!web_app_data_.IsEmpty()) { | 124 } else if (!web_app_data_.IsEmpty()) { |
| 121 AppBannerSettingsHelper::RecordBannerEvent( | 125 AppBannerSettingsHelper::RecordBannerEvent( |
| 122 web_contents, web_contents->GetURL(), | 126 web_contents, web_contents->GetURL(), |
| 123 web_app_data_.start_url.spec(), | 127 web_app_data_.start_url.spec(), |
| 124 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_BLOCK, | 128 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_BLOCK, |
| 125 AppBannerManager::GetCurrentTime()); | 129 AppBannerManager::GetCurrentTime()); |
| 126 } | 130 } |
| 127 } | 131 } |
| 128 | 132 |
| 129 bool AppBannerInfoBarDelegate::Accept() { | 133 bool AppBannerInfoBarDelegate::Accept() { |
| 130 content::WebContents* web_contents = | 134 content::WebContents* web_contents = |
| 131 InfoBarService::WebContentsFromInfoBar(infobar()); | 135 InfoBarService::WebContentsFromInfoBar(infobar()); |
| 132 if (!web_contents) | 136 if (!web_contents) { |
| 137 TrackDismissEvent(DISMISS_EVENT_ERROR); |
| 133 return true; | 138 return true; |
| 139 } |
| 134 | 140 |
| 135 if (!native_app_data_.is_null()) { | 141 if (!native_app_data_.is_null()) { |
| 136 JNIEnv* env = base::android::AttachCurrentThread(); | 142 JNIEnv* env = base::android::AttachCurrentThread(); |
| 137 | 143 |
| 138 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); | 144 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
| 139 if (tab == nullptr) | 145 if (tab == nullptr) { |
| 146 TrackDismissEvent(DISMISS_EVENT_ERROR); |
| 140 return true; | 147 return true; |
| 148 } |
| 141 | 149 |
| 142 return Java_AppBannerInfoBarDelegate_installOrOpenNativeApp( | 150 bool was_opened = Java_AppBannerInfoBarDelegate_installOrOpenNativeApp( |
| 143 env, | 151 env, |
| 144 java_delegate_.obj(), | 152 java_delegate_.obj(), |
| 145 tab->GetJavaObject().obj(), | 153 tab->GetJavaObject().obj(), |
| 146 native_app_data_.obj()); | 154 native_app_data_.obj()); |
| 155 |
| 156 if (was_opened) { |
| 157 TrackDismissEvent(DISMISS_EVENT_APP_OPEN); |
| 158 } else { |
| 159 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_TRIGGERED); |
| 160 } |
| 161 return was_opened; |
| 147 } else if (!web_app_data_.IsEmpty()) { | 162 } else if (!web_app_data_.IsEmpty()) { |
| 148 AppBannerSettingsHelper::RecordBannerEvent( | 163 AppBannerSettingsHelper::RecordBannerEvent( |
| 149 web_contents, web_contents->GetURL(), | 164 web_contents, web_contents->GetURL(), |
| 150 web_app_data_.start_url.spec(), | 165 web_app_data_.start_url.spec(), |
| 151 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, | 166 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, |
| 152 AppBannerManager::GetCurrentTime()); | 167 AppBannerManager::GetCurrentTime()); |
| 153 | 168 |
| 154 ShortcutInfo info; | 169 ShortcutInfo info; |
| 155 info.UpdateFromManifest(web_app_data_); | 170 info.UpdateFromManifest(web_app_data_); |
| 156 base::WorkerPool::PostTask( | 171 base::WorkerPool::PostTask( |
| 157 FROM_HERE, | 172 FROM_HERE, |
| 158 base::Bind(&ShortcutHelper::AddShortcutInBackgroundWithSkBitmap, | 173 base::Bind(&ShortcutHelper::AddShortcutInBackgroundWithSkBitmap, |
| 159 info, | 174 info, |
| 160 *app_icon_.get()), | 175 *app_icon_.get()), |
| 161 true); | 176 true); |
| 177 |
| 178 TrackInstallEvent(INSTALL_EVENT_WEB_APP_INSTALLED); |
| 162 return true; | 179 return true; |
| 163 } | 180 } |
| 164 | 181 |
| 165 return true; | 182 return true; |
| 166 } | 183 } |
| 167 | 184 |
| 168 bool AppBannerInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) { | 185 bool AppBannerInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) { |
| 169 if (native_app_data_.is_null()) | 186 if (native_app_data_.is_null()) |
| 170 return false; | 187 return false; |
| 171 | 188 |
| 172 // Try to show the details for the native app. | 189 // Try to show the details for the native app. |
| 173 JNIEnv* env = base::android::AttachCurrentThread(); | 190 JNIEnv* env = base::android::AttachCurrentThread(); |
| 174 | 191 |
| 175 content::WebContents* web_contents = | 192 content::WebContents* web_contents = |
| 176 InfoBarService::WebContentsFromInfoBar(infobar()); | 193 InfoBarService::WebContentsFromInfoBar(infobar()); |
| 177 TabAndroid* tab = web_contents ? TabAndroid::FromWebContents(web_contents) | 194 TabAndroid* tab = web_contents ? TabAndroid::FromWebContents(web_contents) |
| 178 : nullptr; | 195 : nullptr; |
| 179 if (tab == nullptr) | 196 if (tab == nullptr) { |
| 197 TrackDismissEvent(DISMISS_EVENT_ERROR); |
| 180 return true; | 198 return true; |
| 199 } |
| 181 | 200 |
| 182 Java_AppBannerInfoBarDelegate_showAppDetails(env, | 201 Java_AppBannerInfoBarDelegate_showAppDetails(env, |
| 183 java_delegate_.obj(), | 202 java_delegate_.obj(), |
| 184 tab->GetJavaObject().obj(), | 203 tab->GetJavaObject().obj(), |
| 185 native_app_data_.obj()); | 204 native_app_data_.obj()); |
| 205 |
| 206 TrackDismissEvent(DISMISS_EVENT_BANNER_CLICK); |
| 186 return true; | 207 return true; |
| 187 } | 208 } |
| 188 | 209 |
| 189 void AppBannerInfoBarDelegate::OnInstallIntentReturned( | 210 void AppBannerInfoBarDelegate::OnInstallIntentReturned( |
| 190 JNIEnv* env, | 211 JNIEnv* env, |
| 191 jobject obj, | 212 jobject obj, |
| 192 jboolean jis_installing) { | 213 jboolean jis_installing) { |
| 193 if (!infobar()) | 214 if (!infobar()) |
| 194 return; | 215 return; |
| 195 | 216 |
| 196 content::WebContents* web_contents = | 217 content::WebContents* web_contents = |
| 197 InfoBarService::WebContentsFromInfoBar(infobar()); | 218 InfoBarService::WebContentsFromInfoBar(infobar()); |
| 198 if (!web_contents) | 219 if (!web_contents) |
| 199 return; | 220 return; |
| 200 | 221 |
| 201 if (jis_installing) { | 222 if (jis_installing) { |
| 202 AppBannerSettingsHelper::RecordBannerEvent( | 223 AppBannerSettingsHelper::RecordBannerEvent( |
| 203 web_contents, | 224 web_contents, |
| 204 web_contents->GetURL(), | 225 web_contents->GetURL(), |
| 205 native_app_package_, | 226 native_app_package_, |
| 206 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, | 227 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, |
| 207 AppBannerManager::GetCurrentTime()); | 228 AppBannerManager::GetCurrentTime()); |
| 229 |
| 230 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_STARTED); |
| 208 } | 231 } |
| 209 | 232 |
| 210 UpdateInstallState(env, obj); | 233 UpdateInstallState(env, obj); |
| 211 } | 234 } |
| 212 | 235 |
| 213 void AppBannerInfoBarDelegate::OnInstallFinished(JNIEnv* env, | 236 void AppBannerInfoBarDelegate::OnInstallFinished(JNIEnv* env, |
| 214 jobject obj, | 237 jobject obj, |
| 215 jboolean success) { | 238 jboolean success) { |
| 216 if (!infobar()) | 239 if (!infobar()) |
| 217 return; | 240 return; |
| 218 | 241 |
| 219 if (success) { | 242 if (success) { |
| 243 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_COMPLETED); |
| 220 UpdateInstallState(env, obj); | 244 UpdateInstallState(env, obj); |
| 221 } else if (infobar()->owner()) { | 245 } else if (infobar()->owner()) { |
| 246 TrackDismissEvent(DISMISS_EVENT_INSTALL_TIMEOUT); |
| 222 infobar()->owner()->RemoveInfoBar(infobar()); | 247 infobar()->owner()->RemoveInfoBar(infobar()); |
| 223 } | 248 } |
| 224 } | 249 } |
| 225 | 250 |
| 226 void AppBannerInfoBarDelegate::UpdateInstallState(JNIEnv* env, jobject obj) { | 251 void AppBannerInfoBarDelegate::UpdateInstallState(JNIEnv* env, jobject obj) { |
| 227 if (native_app_data_.is_null()) | 252 if (native_app_data_.is_null()) |
| 228 return; | 253 return; |
| 229 | 254 |
| 230 int newState = Java_AppBannerInfoBarDelegate_determineInstallState( | 255 int newState = Java_AppBannerInfoBarDelegate_determineInstallState( |
| 231 env, | 256 env, |
| 232 java_delegate_.obj(), | 257 java_delegate_.obj(), |
| 233 native_app_data_.obj()); | 258 native_app_data_.obj()); |
| 234 static_cast<AppBannerInfoBar*>(infobar())->OnInstallStateChanged(newState); | 259 static_cast<AppBannerInfoBar*>(infobar())->OnInstallStateChanged(newState); |
| 235 } | 260 } |
| 236 | 261 |
| 237 | 262 |
| 238 bool RegisterAppBannerInfoBarDelegate(JNIEnv* env) { | 263 bool RegisterAppBannerInfoBarDelegate(JNIEnv* env) { |
| 239 return RegisterNativesImpl(env); | 264 return RegisterNativesImpl(env); |
| 240 } | 265 } |
| 241 | 266 |
| 242 } // namespace banners | 267 } // namespace banners |
| OLD | NEW |