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/webapps/add_to_homescreen_manager.h" | 5 #include "chrome/browser/android/webapps/add_to_homescreen_manager.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 24 matching lines...) Expand all Loading... | |
| 35 const JavaParamRef<jobject>& java_web_contents) { | 35 const JavaParamRef<jobject>& java_web_contents) { |
| 36 content::WebContents* web_contents = | 36 content::WebContents* web_contents = |
| 37 content::WebContents::FromJavaWebContents(java_web_contents); | 37 content::WebContents::FromJavaWebContents(java_web_contents); |
| 38 AddToHomescreenManager* manager = new AddToHomescreenManager(env, obj); | 38 AddToHomescreenManager* manager = new AddToHomescreenManager(env, obj); |
| 39 manager->Start(web_contents); | 39 manager->Start(web_contents); |
| 40 return reinterpret_cast<intptr_t>(manager); | 40 return reinterpret_cast<intptr_t>(manager); |
| 41 } | 41 } |
| 42 | 42 |
| 43 AddToHomescreenManager::AddToHomescreenManager(JNIEnv* env, jobject obj) | 43 AddToHomescreenManager::AddToHomescreenManager(JNIEnv* env, jobject obj) |
| 44 : add_shortcut_pending_(false), | 44 : add_shortcut_pending_(false), |
| 45 is_webapk_compatible_(false) { | 45 is_webapk_compatible_(false), |
| 46 weak_ptr_factory_(this) { | |
| 46 java_ref_.Reset(env, obj); | 47 java_ref_.Reset(env, obj); |
| 47 } | 48 } |
| 48 | 49 |
| 49 // static | 50 // static |
| 50 bool AddToHomescreenManager::Register(JNIEnv* env) { | 51 bool AddToHomescreenManager::Register(JNIEnv* env) { |
| 51 return RegisterNativesImpl(env); | 52 return RegisterNativesImpl(env); |
| 52 } | 53 } |
| 53 | 54 |
| 54 void AddToHomescreenManager::Destroy(JNIEnv* env, | 55 void AddToHomescreenManager::Destroy(JNIEnv* env, |
| 55 const JavaParamRef<jobject>& obj) { | 56 const JavaParamRef<jobject>& obj) { |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 72 // via OnDataAvailable(); | 73 // via OnDataAvailable(); |
| 73 AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->primary_icon()); | 74 AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->primary_icon()); |
| 74 } | 75 } |
| 75 } | 76 } |
| 76 | 77 |
| 77 void AddToHomescreenManager::Start(content::WebContents* web_contents) { | 78 void AddToHomescreenManager::Start(content::WebContents* web_contents) { |
| 78 bool check_webapk_compatible = false; | 79 bool check_webapk_compatible = false; |
| 79 if (ChromeWebApkHost::AreWebApkEnabled() && | 80 if (ChromeWebApkHost::AreWebApkEnabled() && |
| 80 InstallableManager::IsContentSecure(web_contents)) { | 81 InstallableManager::IsContentSecure(web_contents)) { |
| 81 check_webapk_compatible = true; | 82 check_webapk_compatible = true; |
| 82 } else { | 83 if (ChromeWebApkHost::CanUseGooglePlayToInstallWebApk()) { |
| 84 // It is possible that the Google Play Install API isn't available when | |
| 85 // Google Play install is enabled. In that case, we fallback to classic | |
| 86 // add-to-homescreen shortcut flow. | |
| 87 // ChromeWebApkHost will delete itself after the the check whether Google | |
| 88 // Play Install API is done. | |
| 89 ChromeWebApkHost* host = new ChromeWebApkHost(); | |
| 90 ChromeWebApkHost::CanUseGooglePlayInstallApiCallback callback | |
| 91 = base::Bind(&AddToHomescreenManager::OnCanUseGooglePlayInstallApi, | |
| 92 weak_ptr_factory_.GetWeakPtr(), | |
|
pkotwicz
2017/02/03 18:56:32
Is a weak ptr needed? We currently leak AddToHomes
Xi Han
2017/02/03 22:16:21
Updated to Unretained(this).
| |
| 93 web_contents, check_webapk_compatible); | |
| 94 host->CanUseGooglePlayInstallApi(callback); | |
| 95 return; | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 StartImpl(web_contents, check_webapk_compatible, true); | |
| 100 } | |
| 101 | |
| 102 void AddToHomescreenManager::StartImpl(content::WebContents* web_contents, | |
| 103 bool check_webapk_compatible, | |
| 104 bool can_use_webapk_install_flow) { | |
| 105 if (!check_webapk_compatible) | |
| 83 ShowDialog(); | 106 ShowDialog(); |
| 84 } | |
| 85 | 107 |
| 86 data_fetcher_ = new AddToHomescreenDataFetcher( | 108 data_fetcher_ = new AddToHomescreenDataFetcher( |
| 87 web_contents, ShortcutHelper::GetIdealHomescreenIconSizeInPx(), | 109 web_contents, ShortcutHelper::GetIdealHomescreenIconSizeInPx(), |
| 88 ShortcutHelper::GetMinimumHomescreenIconSizeInPx(), | 110 ShortcutHelper::GetMinimumHomescreenIconSizeInPx(), |
| 89 ShortcutHelper::GetIdealSplashImageSizeInPx(), | 111 ShortcutHelper::GetIdealSplashImageSizeInPx(), |
| 90 ShortcutHelper::GetMinimumSplashImageSizeInPx(), | 112 ShortcutHelper::GetMinimumSplashImageSizeInPx(), |
| 91 ShortcutHelper::GetIdealBadgeIconSizeInPx(), | 113 ShortcutHelper::GetIdealBadgeIconSizeInPx(), |
| 92 check_webapk_compatible, this); | 114 check_webapk_compatible, can_use_webapk_install_flow, this); |
| 93 } | 115 } |
| 94 | 116 |
| 95 AddToHomescreenManager::~AddToHomescreenManager() { | 117 AddToHomescreenManager::~AddToHomescreenManager() { |
| 96 if (data_fetcher_) { | 118 if (data_fetcher_) { |
| 97 data_fetcher_->set_weak_observer(nullptr); | 119 data_fetcher_->set_weak_observer(nullptr); |
| 98 data_fetcher_ = nullptr; | 120 data_fetcher_ = nullptr; |
| 99 } | 121 } |
| 100 } | 122 } |
| 101 | 123 |
| 124 void AddToHomescreenManager::OnCanUseGooglePlayInstallApi( | |
| 125 content::WebContents* web_contents, | |
| 126 bool check_webapk_compatible, | |
| 127 bool isAvailable) { | |
| 128 StartImpl(web_contents, check_webapk_compatible, isAvailable); | |
| 129 } | |
| 130 | |
| 102 void AddToHomescreenManager::ShowDialog() { | 131 void AddToHomescreenManager::ShowDialog() { |
| 103 JNIEnv* env = base::android::AttachCurrentThread(); | 132 JNIEnv* env = base::android::AttachCurrentThread(); |
| 104 Java_AddToHomescreenManager_showDialog(env, java_ref_); | 133 Java_AddToHomescreenManager_showDialog(env, java_ref_); |
| 105 } | 134 } |
| 106 | 135 |
| 107 void AddToHomescreenManager::AddShortcut(const ShortcutInfo& info, | 136 void AddToHomescreenManager::AddShortcut(const ShortcutInfo& info, |
| 108 const SkBitmap& icon) { | 137 const SkBitmap& icon) { |
| 109 DCHECK(add_shortcut_pending_); | 138 DCHECK(add_shortcut_pending_); |
| 110 if (!add_shortcut_pending_) | 139 if (!add_shortcut_pending_) |
| 111 return; | 140 return; |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 ScopedJavaLocalRef<jstring> j_user_title = | 188 ScopedJavaLocalRef<jstring> j_user_title = |
| 160 base::android::ConvertUTF16ToJavaString(env, user_title); | 189 base::android::ConvertUTF16ToJavaString(env, user_title); |
| 161 Java_AddToHomescreenManager_onUserTitleAvailable(env, | 190 Java_AddToHomescreenManager_onUserTitleAvailable(env, |
| 162 java_ref_, | 191 java_ref_, |
| 163 j_user_title); | 192 j_user_title); |
| 164 } | 193 } |
| 165 | 194 |
| 166 void AddToHomescreenManager::OnDataAvailable(const ShortcutInfo& info, | 195 void AddToHomescreenManager::OnDataAvailable(const ShortcutInfo& info, |
| 167 const SkBitmap& primary_icon, | 196 const SkBitmap& primary_icon, |
| 168 const SkBitmap& badge_icon) { | 197 const SkBitmap& badge_icon) { |
| 169 if (is_webapk_compatible_) { | 198 if (is_webapk_compatible_) { |
|
pkotwicz
2017/02/03 18:56:32
My preference would be to call ChromeWebApkHost::C
Xi Han
2017/02/03 22:16:21
As discussed offline, we keep the call in the AddT
| |
| 170 // TODO(zpeng): Add badge to WebAPK installation flow. | 199 // TODO(zpeng): Add badge to WebAPK installation flow. |
| 171 WebApkInstallService* install_service = | 200 WebApkInstallService* install_service = |
| 172 WebApkInstallService::Get( | 201 WebApkInstallService::Get( |
| 173 data_fetcher_->web_contents()->GetBrowserContext()); | 202 data_fetcher_->web_contents()->GetBrowserContext()); |
| 174 if (install_service->IsInstallInProgress(info.manifest_url)) | 203 if (install_service->IsInstallInProgress(info.manifest_url)) |
| 175 ShortcutHelper::ShowWebApkInstallInProgressToast(); | 204 ShortcutHelper::ShowWebApkInstallInProgressToast(); |
| 176 else | 205 else |
| 177 CreateInfoBarForWebApk(info, primary_icon); | 206 CreateInfoBarForWebApk(info, primary_icon); |
| 178 return; | 207 return; |
| 179 } | 208 } |
| 180 | 209 |
| 181 JNIEnv* env = base::android::AttachCurrentThread(); | 210 JNIEnv* env = base::android::AttachCurrentThread(); |
| 182 ScopedJavaLocalRef<jobject> java_bitmap; | 211 ScopedJavaLocalRef<jobject> java_bitmap; |
| 183 if (!primary_icon.drawsNothing()) | 212 if (!primary_icon.drawsNothing()) |
| 184 java_bitmap = gfx::ConvertToJavaBitmap(&primary_icon); | 213 java_bitmap = gfx::ConvertToJavaBitmap(&primary_icon); |
| 185 | 214 |
| 186 Java_AddToHomescreenManager_onReadyToAdd(env, java_ref_, java_bitmap); | 215 Java_AddToHomescreenManager_onReadyToAdd(env, java_ref_, java_bitmap); |
| 187 | 216 |
| 188 if (add_shortcut_pending_) | 217 if (add_shortcut_pending_) |
| 189 AddShortcut(info, primary_icon); | 218 AddShortcut(info, primary_icon); |
| 190 } | 219 } |
| 191 | 220 |
| 192 void AddToHomescreenManager::CreateInfoBarForWebApk(const ShortcutInfo& info, | 221 void AddToHomescreenManager::CreateInfoBarForWebApk(const ShortcutInfo& info, |
| 193 const SkBitmap& icon) { | 222 const SkBitmap& icon) { |
| 194 banners::AppBannerInfoBarDelegateAndroid::Create( | 223 banners::AppBannerInfoBarDelegateAndroid::Create( |
| 195 data_fetcher_->web_contents(), nullptr, info.user_title, | 224 data_fetcher_->web_contents(), nullptr, info.user_title, |
| 196 base::MakeUnique<ShortcutInfo>(info), base::MakeUnique<SkBitmap>(icon), | 225 base::MakeUnique<ShortcutInfo>(info), base::MakeUnique<SkBitmap>(icon), |
| 197 -1 /* event_request_id */, webapk::INSTALL_SOURCE_MENU); | 226 -1 /* event_request_id */, true /* can_use_webapk_install_flow */, |
| 227 webapk::INSTALL_SOURCE_MENU); | |
| 198 } | 228 } |
| 199 | 229 |
| 200 SkBitmap AddToHomescreenManager::FinalizeLauncherIconInBackground( | 230 SkBitmap AddToHomescreenManager::FinalizeLauncherIconInBackground( |
| 201 const SkBitmap& bitmap, | 231 const SkBitmap& bitmap, |
| 202 const GURL& url, | 232 const GURL& url, |
| 203 bool* is_generated) { | 233 bool* is_generated) { |
| 204 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 234 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 205 | 235 |
| 206 return ShortcutHelper::FinalizeLauncherIconInBackground(bitmap, url, | 236 return ShortcutHelper::FinalizeLauncherIconInBackground(bitmap, url, |
| 207 is_generated); | 237 is_generated); |
| 208 } | 238 } |
| OLD | NEW |