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" |
| 11 #include "base/memory/ptr_util.h" | |
| 11 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/browser/android/banners/app_banner_infobar_delegate_android.h" | |
| 13 #include "chrome/browser/android/shortcut_helper.h" | 15 #include "chrome/browser/android/shortcut_helper.h" |
| 14 #include "chrome/browser/android/webapk/chrome_webapk_host.h" | 16 #include "chrome/browser/android/webapk/chrome_webapk_host.h" |
| 15 #include "chrome/browser/banners/app_banner_settings_helper.h" | 17 #include "chrome/browser/banners/app_banner_settings_helper.h" |
| 18 #include "chrome/browser/infobars/infobar_service.h" | |
| 16 #include "chrome/browser/installable/installable_manager.h" | 19 #include "chrome/browser/installable/installable_manager.h" |
| 20 #include "chrome/browser/ui/android/infobars/app_banner_infobar_android.h" | |
| 17 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/web_contents.h" | 22 #include "content/public/browser/web_contents.h" |
| 19 #include "content/public/common/origin_util.h" | 23 #include "content/public/common/origin_util.h" |
| 20 #include "jni/AddToHomescreenManager_jni.h" | 24 #include "jni/AddToHomescreenManager_jni.h" |
| 21 #include "third_party/skia/include/core/SkBitmap.h" | 25 #include "third_party/skia/include/core/SkBitmap.h" |
| 22 #include "ui/gfx/android/java_bitmap.h" | 26 #include "ui/gfx/android/java_bitmap.h" |
| 23 | 27 |
| 24 using base::android::JavaParamRef; | 28 using base::android::JavaParamRef; |
| 25 using base::android::ScopedJavaLocalRef; | 29 using base::android::ScopedJavaLocalRef; |
| 26 | 30 |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 105 void AddToHomescreenManager::OnGotWebApkCompatibilityData( | 109 void AddToHomescreenManager::OnGotWebApkCompatibilityData( |
| 106 content::WebContents* web_contents, | 110 content::WebContents* web_contents, |
| 107 const InstallableData& installable_data) { | 111 const InstallableData& installable_data) { |
| 108 OnDeterminedWebApkCompatibility(web_contents, | 112 OnDeterminedWebApkCompatibility(web_contents, |
| 109 installable_data.is_installable); | 113 installable_data.is_installable); |
| 110 } | 114 } |
| 111 | 115 |
| 112 void AddToHomescreenManager::OnDeterminedWebApkCompatibility( | 116 void AddToHomescreenManager::OnDeterminedWebApkCompatibility( |
| 113 content::WebContents* web_contents, | 117 content::WebContents* web_contents, |
| 114 bool is_compatible) { | 118 bool is_compatible) { |
| 115 // TODO(pkotwicz): Select whether to use dialog or not based on | 119 is_compatible_ = is_compatible; |
| 116 // |is_compatible|. | 120 if (!is_compatible) |
| 117 ShowDialog(); | 121 ShowDialog(); |
| 118 StartFetchingInfoForShortcut(web_contents); | 122 StartFetchingInfoForShortcut(web_contents); |
| 119 } | 123 } |
| 120 | 124 |
| 121 void AddToHomescreenManager::ShowDialog() { | 125 void AddToHomescreenManager::ShowDialog() { |
| 122 JNIEnv* env = base::android::AttachCurrentThread(); | 126 JNIEnv* env = base::android::AttachCurrentThread(); |
| 123 Java_AddToHomescreenManager_showDialog(env, java_ref_); | 127 Java_AddToHomescreenManager_showDialog(env, java_ref_); |
| 124 } | 128 } |
| 125 | 129 |
| 126 void AddToHomescreenManager::StartFetchingInfoForShortcut( | 130 void AddToHomescreenManager::StartFetchingInfoForShortcut( |
| 127 content::WebContents* web_contents) { | 131 content::WebContents* web_contents) { |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 | 164 |
| 161 AppBannerSettingsHelper::RecordBannerEvent( | 165 AppBannerSettingsHelper::RecordBannerEvent( |
| 162 web_contents, web_contents->GetURL(), | 166 web_contents, web_contents->GetURL(), |
| 163 data_fetcher_->shortcut_info().url.spec(), | 167 data_fetcher_->shortcut_info().url.spec(), |
| 164 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, | 168 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, |
| 165 base::Time::Now()); | 169 base::Time::Now()); |
| 166 } | 170 } |
| 167 | 171 |
| 168 void AddToHomescreenManager::OnUserTitleAvailable( | 172 void AddToHomescreenManager::OnUserTitleAvailable( |
| 169 const base::string16& user_title) { | 173 const base::string16& user_title) { |
| 174 if (is_compatible_) | |
| 175 return; | |
| 170 JNIEnv* env = base::android::AttachCurrentThread(); | 176 JNIEnv* env = base::android::AttachCurrentThread(); |
| 171 ScopedJavaLocalRef<jstring> j_user_title = | 177 ScopedJavaLocalRef<jstring> j_user_title = |
| 172 base::android::ConvertUTF16ToJavaString(env, user_title); | 178 base::android::ConvertUTF16ToJavaString(env, user_title); |
| 173 Java_AddToHomescreenManager_onUserTitleAvailable(env, | 179 Java_AddToHomescreenManager_onUserTitleAvailable(env, |
| 174 java_ref_, | 180 java_ref_, |
| 175 j_user_title); | 181 j_user_title); |
| 176 } | 182 } |
| 177 | 183 |
| 178 void AddToHomescreenManager::OnDataAvailable(const ShortcutInfo& info, | 184 void AddToHomescreenManager::OnDataAvailable(const ShortcutInfo& info, |
| 179 const SkBitmap& icon) { | 185 const SkBitmap& icon) { |
| 186 if (is_compatible_) { | |
| 187 CreateInfobar(info, icon); | |
| 188 return; | |
| 189 } | |
| 190 | |
| 180 JNIEnv* env = base::android::AttachCurrentThread(); | 191 JNIEnv* env = base::android::AttachCurrentThread(); |
| 181 ScopedJavaLocalRef<jobject> java_bitmap; | 192 ScopedJavaLocalRef<jobject> java_bitmap; |
| 182 if (icon.getSize()) | 193 if (icon.getSize()) |
| 183 java_bitmap = gfx::ConvertToJavaBitmap(&icon); | 194 java_bitmap = gfx::ConvertToJavaBitmap(&icon); |
| 184 | 195 |
| 185 Java_AddToHomescreenManager_onReadyToAdd(env, java_ref_, java_bitmap); | 196 Java_AddToHomescreenManager_onReadyToAdd(env, java_ref_, java_bitmap); |
| 186 | 197 |
| 187 if (add_shortcut_pending_) | 198 if (add_shortcut_pending_) |
| 188 AddShortcut(info, icon); | 199 AddShortcut(info, icon); |
| 189 } | 200 } |
| 190 | 201 |
| 202 void AddToHomescreenManager::CreateInfobar(const ShortcutInfo& info, | |
| 203 const SkBitmap& icon) { | |
| 204 std::unique_ptr<ShortcutInfo> info_ptr(new ShortcutInfo(info)); | |
| 205 std::unique_ptr<SkBitmap> icon_ptr(new SkBitmap(icon)); | |
| 206 std::unique_ptr<banners::AppBannerInfoBarDelegateAndroid> delegate( | |
| 207 new banners::AppBannerInfoBarDelegateAndroid( | |
| 208 nullptr, info.user_title, info.manifest_url, std::move(info_ptr), | |
| 209 info.icon_url, std::move(icon_ptr), -1 /* event_request_id */, | |
| 210 true)); | |
| 211 | |
| 212 infobars::InfoBar* infobar = new AppBannerInfoBarAndroid( | |
| 213 std::move(delegate), data_fetcher_->shortcut_info().url, true); | |
| 214 if (infobar) { | |
|
gone
2016/08/31 18:12:45
if (!infobar)
return;
saves you a level of inde
Xi Han
2016/08/31 20:11:48
I will be more careful and use early exist as much
| |
| 215 content::WebContents* web_contents = data_fetcher_->web_contents(); | |
| 216 if (!web_contents) { | |
| 217 LOG(ERROR) << "Failed to create infobar to install the WebAPK: " | |
| 218 << "the associated WebContents is null."; | |
| 219 return; | |
| 220 } | |
| 221 InfoBarService::FromWebContents(web_contents)->AddInfoBar( | |
| 222 base::WrapUnique(infobar)); | |
| 223 static_cast<AppBannerInfoBarAndroid*>(infobar)->InstallWebApk(web_contents); | |
| 224 } | |
| 225 } | |
| 226 | |
| 191 SkBitmap AddToHomescreenManager::FinalizeLauncherIconInBackground( | 227 SkBitmap AddToHomescreenManager::FinalizeLauncherIconInBackground( |
| 192 const SkBitmap& bitmap, | 228 const SkBitmap& bitmap, |
| 193 const GURL& url, | 229 const GURL& url, |
| 194 bool* is_generated) { | 230 bool* is_generated) { |
| 195 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | 231 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
| 196 | 232 |
| 197 return ShortcutHelper::FinalizeLauncherIconInBackground(bitmap, url, | 233 return ShortcutHelper::FinalizeLauncherIconInBackground(bitmap, url, |
| 198 is_generated); | 234 is_generated); |
| 199 } | 235 } |
| OLD | NEW |