Chromium Code Reviews| 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_android.h" | 5 #include "chrome/browser/android/banners/app_banner_infobar_delegate_android.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/guid.h" | 9 #include "base/guid.h" |
| 10 #include "base/location.h" | 10 #include "base/location.h" |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 30 #include "ui/gfx/android/java_bitmap.h" | 30 #include "ui/gfx/android/java_bitmap.h" |
| 31 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 32 | 32 |
| 33 using base::android::ConvertJavaStringToUTF8; | 33 using base::android::ConvertJavaStringToUTF8; |
| 34 using base::android::ConvertJavaStringToUTF16; | 34 using base::android::ConvertJavaStringToUTF16; |
| 35 using base::android::ConvertUTF8ToJavaString; | 35 using base::android::ConvertUTF8ToJavaString; |
| 36 using base::android::ConvertUTF16ToJavaString; | 36 using base::android::ConvertUTF16ToJavaString; |
| 37 using base::android::JavaParamRef; | 37 using base::android::JavaParamRef; |
| 38 using base::android::ScopedJavaLocalRef; | 38 using base::android::ScopedJavaLocalRef; |
| 39 | 39 |
| 40 namespace { | |
| 41 | |
| 42 bool isShortcutInfoEmpty(ShortcutInfo* info) { | |
| 43 if (!info) | |
| 44 return true; | |
| 45 return info->url.is_empty(); | |
|
gone
2016/08/31 18:12:45
return !info || info->url.is_empty();
Xi Han
2016/08/31 20:11:48
Done.
| |
| 46 } | |
| 47 | |
| 48 } // anonymous namespace | |
| 49 | |
| 40 namespace banners { | 50 namespace banners { |
| 41 | 51 |
| 42 AppBannerInfoBarDelegateAndroid::AppBannerInfoBarDelegateAndroid( | 52 AppBannerInfoBarDelegateAndroid::AppBannerInfoBarDelegateAndroid( |
| 43 base::WeakPtr<AppBannerManager> weak_manager, | 53 base::WeakPtr<AppBannerManager> weak_manager, |
| 44 const base::string16& app_title, | 54 const base::string16& app_title, |
| 45 const GURL& manifest_url, | 55 const GURL& manifest_url, |
| 46 const content::Manifest& manifest, | 56 std::unique_ptr<ShortcutInfo> info, |
| 47 const GURL& icon_url, | 57 const GURL& icon_url, |
| 48 std::unique_ptr<SkBitmap> icon, | 58 std::unique_ptr<SkBitmap> icon, |
| 49 int event_request_id, | 59 int event_request_id, |
| 50 bool is_webapk) | 60 bool is_webapk) |
| 51 : weak_manager_(weak_manager), | 61 : weak_manager_(weak_manager), |
| 52 app_title_(app_title), | 62 app_title_(app_title), |
| 53 manifest_url_(manifest_url), | 63 manifest_url_(manifest_url), |
| 54 manifest_(manifest), | 64 info_(std::move(info)), |
| 55 icon_url_(icon_url), | 65 icon_url_(icon_url), |
| 56 icon_(std::move(icon)), | 66 icon_(std::move(icon)), |
| 57 event_request_id_(event_request_id), | 67 event_request_id_(event_request_id), |
| 58 has_user_interaction_(false), | 68 has_user_interaction_(false), |
| 59 is_webapk_(is_webapk), | 69 is_webapk_(is_webapk), |
| 60 weak_ptr_factory_(this) { | 70 weak_ptr_factory_(this) { |
| 61 DCHECK(!manifest.IsEmpty()); | 71 DCHECK(!isShortcutInfoEmpty(info_.get())); |
| 62 CreateJavaDelegate(); | 72 CreateJavaDelegate(); |
| 63 } | 73 } |
| 64 | 74 |
| 65 AppBannerInfoBarDelegateAndroid::AppBannerInfoBarDelegateAndroid( | 75 AppBannerInfoBarDelegateAndroid::AppBannerInfoBarDelegateAndroid( |
| 66 const base::string16& app_title, | 76 const base::string16& app_title, |
| 67 const base::android::ScopedJavaGlobalRef<jobject>& native_app_data, | 77 const base::android::ScopedJavaGlobalRef<jobject>& native_app_data, |
| 68 std::unique_ptr<SkBitmap> icon, | 78 std::unique_ptr<SkBitmap> icon, |
| 69 const std::string& native_app_package, | 79 const std::string& native_app_package, |
| 70 const std::string& referrer, | 80 const std::string& referrer, |
| 71 int event_request_id) | 81 int event_request_id) |
| 72 : app_title_(app_title), | 82 : app_title_(app_title), |
| 73 native_app_data_(native_app_data), | 83 native_app_data_(native_app_data), |
| 74 icon_(std::move(icon)), | 84 icon_(std::move(icon)), |
| 75 native_app_package_(native_app_package), | 85 native_app_package_(native_app_package), |
| 76 referrer_(referrer), | 86 referrer_(referrer), |
| 77 event_request_id_(event_request_id), | 87 event_request_id_(event_request_id), |
| 78 has_user_interaction_(false), | 88 has_user_interaction_(false), |
| 79 weak_ptr_factory_(this) { | 89 weak_ptr_factory_(this) { |
| 80 DCHECK(!native_app_data_.is_null()); | 90 DCHECK(!native_app_data_.is_null()); |
| 81 CreateJavaDelegate(); | 91 CreateJavaDelegate(); |
| 82 } | 92 } |
| 83 | 93 |
| 84 AppBannerInfoBarDelegateAndroid::~AppBannerInfoBarDelegateAndroid() { | 94 AppBannerInfoBarDelegateAndroid::~AppBannerInfoBarDelegateAndroid() { |
| 85 weak_ptr_factory_.InvalidateWeakPtrs(); | 95 weak_ptr_factory_.InvalidateWeakPtrs(); |
| 86 | 96 |
| 87 if (!has_user_interaction_) { | 97 if (!has_user_interaction_) { |
| 88 if (!native_app_data_.is_null()) | 98 if (!native_app_data_.is_null()) |
| 89 TrackUserResponse(USER_RESPONSE_NATIVE_APP_IGNORED); | 99 TrackUserResponse(USER_RESPONSE_NATIVE_APP_IGNORED); |
| 90 else if (!manifest_.IsEmpty()) | 100 else if (!isShortcutInfoEmpty(info_.get())) |
| 91 TrackUserResponse(USER_RESPONSE_WEB_APP_IGNORED); | 101 TrackUserResponse(USER_RESPONSE_WEB_APP_IGNORED); |
| 92 } | 102 } |
| 93 | 103 |
| 94 TrackDismissEvent(DISMISS_EVENT_DISMISSED); | 104 TrackDismissEvent(DISMISS_EVENT_DISMISSED); |
| 95 JNIEnv* env = base::android::AttachCurrentThread(); | 105 JNIEnv* env = base::android::AttachCurrentThread(); |
| 96 Java_AppBannerInfoBarDelegateAndroid_destroy(env, java_delegate_); | 106 Java_AppBannerInfoBarDelegateAndroid_destroy(env, java_delegate_); |
| 97 java_delegate_.Reset(); | 107 java_delegate_.Reset(); |
| 98 } | 108 } |
| 99 | 109 |
| 100 void AppBannerInfoBarDelegateAndroid::UpdateInstallState( | 110 void AppBannerInfoBarDelegateAndroid::UpdateInstallState( |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 147 | 157 |
| 148 if (success) { | 158 if (success) { |
| 149 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_COMPLETED); | 159 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_COMPLETED); |
| 150 UpdateInstallState(env, obj); | 160 UpdateInstallState(env, obj); |
| 151 } else if (infobar()->owner()) { | 161 } else if (infobar()->owner()) { |
| 152 TrackDismissEvent(DISMISS_EVENT_INSTALL_TIMEOUT); | 162 TrackDismissEvent(DISMISS_EVENT_INSTALL_TIMEOUT); |
| 153 infobar()->owner()->RemoveInfoBar(infobar()); | 163 infobar()->owner()->RemoveInfoBar(infobar()); |
| 154 } | 164 } |
| 155 } | 165 } |
| 156 | 166 |
| 167 void AppBannerInfoBarDelegateAndroid::InstallWebApk( | |
| 168 content::WebContents* web_contents) { | |
| 169 if (!web_contents) { | |
| 170 LOG(ERROR) << "Failed to create infobar to install the WebAPK: " | |
| 171 << "the associated WebContents is null."; | |
|
gone
2016/08/31 18:12:45
Indentation is wrong.
Xi Han
2016/08/31 20:11:48
Done.
| |
| 172 return; | |
| 173 } | |
| 174 AcceptWebApk(web_contents); | |
| 175 } | |
| 176 | |
| 157 void AppBannerInfoBarDelegateAndroid::CreateJavaDelegate() { | 177 void AppBannerInfoBarDelegateAndroid::CreateJavaDelegate() { |
| 158 JNIEnv* env = base::android::AttachCurrentThread(); | 178 JNIEnv* env = base::android::AttachCurrentThread(); |
| 159 java_delegate_.Reset(Java_AppBannerInfoBarDelegateAndroid_create( | 179 java_delegate_.Reset(Java_AppBannerInfoBarDelegateAndroid_create( |
| 160 env, | 180 env, |
| 161 reinterpret_cast<intptr_t>(this))); | 181 reinterpret_cast<intptr_t>(this))); |
| 162 } | 182 } |
| 163 | 183 |
| 164 void AppBannerInfoBarDelegateAndroid::SendBannerAccepted( | 184 void AppBannerInfoBarDelegateAndroid::SendBannerAccepted( |
| 165 content::WebContents* web_contents, | 185 content::WebContents* web_contents, |
| 166 const std::string& platform) { | 186 const std::string& platform) { |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 190 | 210 |
| 191 web_contents->GetMainFrame()->Send( | 211 web_contents->GetMainFrame()->Send( |
| 192 new ChromeViewMsg_AppBannerDismissed( | 212 new ChromeViewMsg_AppBannerDismissed( |
| 193 web_contents->GetMainFrame()->GetRoutingID(), | 213 web_contents->GetMainFrame()->GetRoutingID(), |
| 194 event_request_id_)); | 214 event_request_id_)); |
| 195 | 215 |
| 196 if (!native_app_data_.is_null()) { | 216 if (!native_app_data_.is_null()) { |
| 197 TrackUserResponse(USER_RESPONSE_NATIVE_APP_DISMISSED); | 217 TrackUserResponse(USER_RESPONSE_NATIVE_APP_DISMISSED); |
| 198 AppBannerSettingsHelper::RecordBannerDismissEvent( | 218 AppBannerSettingsHelper::RecordBannerDismissEvent( |
| 199 web_contents, native_app_package_, AppBannerSettingsHelper::NATIVE); | 219 web_contents, native_app_package_, AppBannerSettingsHelper::NATIVE); |
| 200 } else if (!manifest_.IsEmpty()) { | 220 } else if (!isShortcutInfoEmpty(info_.get())) { |
| 201 TrackUserResponse(USER_RESPONSE_WEB_APP_DISMISSED); | 221 TrackUserResponse(USER_RESPONSE_WEB_APP_DISMISSED); |
| 202 AppBannerSettingsHelper::RecordBannerDismissEvent( | 222 AppBannerSettingsHelper::RecordBannerDismissEvent( |
| 203 web_contents, manifest_.start_url.spec(), | 223 web_contents, info_->url.spec(), |
| 204 AppBannerSettingsHelper::WEB); | 224 AppBannerSettingsHelper::WEB); |
| 205 } | 225 } |
| 206 } | 226 } |
| 207 | 227 |
| 208 base::string16 AppBannerInfoBarDelegateAndroid::GetMessageText() const { | 228 base::string16 AppBannerInfoBarDelegateAndroid::GetMessageText() const { |
| 209 return app_title_; | 229 return app_title_; |
| 210 } | 230 } |
| 211 | 231 |
| 212 int AppBannerInfoBarDelegateAndroid::GetButtons() const { | 232 int AppBannerInfoBarDelegateAndroid::GetButtons() const { |
| 213 return BUTTON_OK; | 233 return BUTTON_OK; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 253 TrackDismissEvent(DISMISS_EVENT_APP_OPEN); | 273 TrackDismissEvent(DISMISS_EVENT_APP_OPEN); |
| 254 } else { | 274 } else { |
| 255 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_TRIGGERED); | 275 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_TRIGGERED); |
| 256 } | 276 } |
| 257 SendBannerAccepted(web_contents, "play"); | 277 SendBannerAccepted(web_contents, "play"); |
| 258 return was_opened; | 278 return was_opened; |
| 259 } | 279 } |
| 260 | 280 |
| 261 bool AppBannerInfoBarDelegateAndroid::AcceptWebApp( | 281 bool AppBannerInfoBarDelegateAndroid::AcceptWebApp( |
| 262 content::WebContents* web_contents) { | 282 content::WebContents* web_contents) { |
| 263 if (manifest_.IsEmpty()) | 283 if (isShortcutInfoEmpty(info_.get())) |
| 264 return true; | 284 return true; |
| 265 TrackUserResponse(USER_RESPONSE_WEB_APP_ACCEPTED); | 285 TrackUserResponse(USER_RESPONSE_WEB_APP_ACCEPTED); |
| 266 | 286 |
| 267 AppBannerSettingsHelper::RecordBannerInstallEvent( | 287 AppBannerSettingsHelper::RecordBannerInstallEvent( |
| 268 web_contents, manifest_.start_url.spec(), | 288 web_contents, info_->url.spec(), |
| 269 AppBannerSettingsHelper::WEB); | 289 AppBannerSettingsHelper::WEB); |
| 270 | 290 |
| 271 if (weak_manager_) { | 291 if (weak_manager_) { |
| 272 ShortcutInfo info(GURL::EmptyGURL()); | |
| 273 info.UpdateFromManifest(manifest_); | |
| 274 info.manifest_url = manifest_url_; | |
| 275 info.icon_url = icon_url_; | |
| 276 info.UpdateSource(ShortcutInfo::SOURCE_APP_BANNER); | |
| 277 | |
| 278 const std::string& uid = base::GenerateGUID(); | 292 const std::string& uid = base::GenerateGUID(); |
| 279 ShortcutHelper::AddToLauncherWithSkBitmap( | 293 ShortcutHelper::AddToLauncherWithSkBitmap( |
| 280 web_contents->GetBrowserContext(), info, uid, *icon_.get(), | 294 web_contents->GetBrowserContext(), *info_.get(), uid, *icon_.get(), |
| 281 weak_manager_->FetchWebappSplashScreenImageCallback(uid)); | 295 weak_manager_->FetchWebappSplashScreenImageCallback(uid)); |
| 282 } | 296 } |
| 283 | 297 |
| 284 SendBannerAccepted(web_contents, "web"); | 298 SendBannerAccepted(web_contents, "web"); |
| 285 return true; | 299 return true; |
| 286 } | 300 } |
| 287 | 301 |
| 288 bool AppBannerInfoBarDelegateAndroid::AcceptWebApk( | 302 bool AppBannerInfoBarDelegateAndroid::AcceptWebApk( |
| 289 content::WebContents* web_contents) { | 303 content::WebContents* web_contents) { |
| 290 if (manifest_.IsEmpty()) | 304 if (isShortcutInfoEmpty(info_.get())) |
| 291 return true; | 305 return true; |
| 292 | 306 |
| 293 JNIEnv* env = base::android::AttachCurrentThread(); | 307 JNIEnv* env = base::android::AttachCurrentThread(); |
| 294 // |webapk_package_name_| is set when the WebAPK has finished installing. | 308 // |webapk_package_name_| is set when the WebAPK has finished installing. |
| 295 // If the |webapk_package_name_| is empty, it means the "Add to Homescreen" | 309 // If the |webapk_package_name_| is empty, it means the "Add to Homescreen" |
| 296 // button is pressed, so request WebAPK installation. Otherwise, it means | 310 // button is pressed, so request WebAPK installation. Otherwise, it means |
| 297 // the "Open" button is pressed, then open the installed WebAPK. | 311 // the "Open" button is pressed, then open the installed WebAPK. |
| 298 if (webapk_package_name_.empty()) { | 312 if (webapk_package_name_.empty()) { |
| 299 // Request install the WebAPK. | 313 // Request install the WebAPK. |
| 300 TrackUserResponse(USER_RESPONSE_WEB_APP_ACCEPTED); | 314 TrackUserResponse(USER_RESPONSE_WEB_APP_ACCEPTED); |
| 301 | 315 |
| 302 AppBannerSettingsHelper::RecordBannerInstallEvent( | 316 AppBannerSettingsHelper::RecordBannerInstallEvent( |
| 303 web_contents, manifest_.start_url.spec(), | 317 web_contents, info_->url.spec(), |
| 304 AppBannerSettingsHelper::WEB); | 318 AppBannerSettingsHelper::WEB); |
| 305 | 319 |
| 306 ShortcutInfo info(GURL::EmptyGURL()); | |
| 307 info.UpdateFromManifest(manifest_); | |
| 308 info.manifest_url = manifest_url_; | |
| 309 info.icon_url = icon_url_; | |
| 310 info.UpdateSource(ShortcutInfo::SOURCE_APP_BANNER); | |
| 311 | |
| 312 Java_AppBannerInfoBarDelegateAndroid_setWebApkInstallingState( | 320 Java_AppBannerInfoBarDelegateAndroid_setWebApkInstallingState( |
| 313 env, java_delegate_, true); | 321 env, java_delegate_, true); |
| 314 UpdateInstallState(env, nullptr); | 322 UpdateInstallState(env, nullptr); |
| 315 | 323 |
| 316 WebApkInstaller::FinishCallback callback = base::Bind( | 324 WebApkInstaller::FinishCallback callback = base::Bind( |
| 317 &AppBannerInfoBarDelegateAndroid::OnWebApkInstallFinished, | 325 &AppBannerInfoBarDelegateAndroid::OnWebApkInstallFinished, |
| 318 weak_ptr_factory_.GetWeakPtr()); | 326 weak_ptr_factory_.GetWeakPtr()); |
| 319 DVLOG(1) << "Trigger the installation of the WebAPK."; | 327 DVLOG(1) << "Trigger the installation of the WebAPK."; |
| 320 ShortcutHelper::InstallWebApkWithSkBitmap( | 328 ShortcutHelper::InstallWebApkWithSkBitmap( |
| 321 web_contents->GetBrowserContext(), info, *icon_.get(), callback); | 329 web_contents->GetBrowserContext(), *info_.get(), |
| 330 *icon_.get(), callback); | |
| 322 | 331 |
| 323 SendBannerAccepted(web_contents, "web"); | 332 SendBannerAccepted(web_contents, "web"); |
| 324 // Returns false to prevent the infobar from disappearing. | 333 // Returns false to prevent the infobar from disappearing. |
| 325 return false; | 334 return false; |
| 326 } | 335 } |
| 327 | 336 |
| 328 // Open the WebAPK. | 337 // Open the WebAPK. |
| 329 ScopedJavaLocalRef<jstring> java_webapk_package_name = | 338 ScopedJavaLocalRef<jstring> java_webapk_package_name = |
| 330 base::android::ConvertUTF8ToJavaString(env, webapk_package_name_); | 339 base::android::ConvertUTF8ToJavaString(env, webapk_package_name_); |
| 331 Java_AppBannerInfoBarDelegateAndroid_openWebApk( | 340 Java_AppBannerInfoBarDelegateAndroid_openWebApk( |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 380 | 389 |
| 381 TrackDismissEvent(DISMISS_EVENT_BANNER_CLICK); | 390 TrackDismissEvent(DISMISS_EVENT_BANNER_CLICK); |
| 382 return true; | 391 return true; |
| 383 } | 392 } |
| 384 | 393 |
| 385 bool RegisterAppBannerInfoBarDelegateAndroid(JNIEnv* env) { | 394 bool RegisterAppBannerInfoBarDelegateAndroid(JNIEnv* env) { |
| 386 return RegisterNativesImpl(env); | 395 return RegisterNativesImpl(env); |
| 387 } | 396 } |
| 388 | 397 |
| 389 } // namespace banners | 398 } // namespace banners |
| OLD | NEW |