| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/android/webapps/add_to_homescreen_dialog_helper.h" | |
| 6 | |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/android/jni_string.h" | |
| 9 #include "base/guid.h" | |
| 10 #include "base/location.h" | |
| 11 #include "base/strings/string16.h" | |
| 12 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "chrome/browser/android/shortcut_helper.h" | |
| 14 #include "chrome/browser/banners/app_banner_settings_helper.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/browser/web_contents.h" | |
| 17 #include "jni/AddToHomescreenDialogHelper_jni.h" | |
| 18 #include "ui/gfx/android/java_bitmap.h" | |
| 19 | |
| 20 using base::android::JavaParamRef; | |
| 21 using base::android::ScopedJavaLocalRef; | |
| 22 | |
| 23 jlong Initialize(JNIEnv* env, | |
| 24 const JavaParamRef<jobject>& obj, | |
| 25 const JavaParamRef<jobject>& java_web_contents) { | |
| 26 content::WebContents* web_contents = | |
| 27 content::WebContents::FromJavaWebContents(java_web_contents); | |
| 28 AddToHomescreenDialogHelper* add_to_homescreen_helper = | |
| 29 new AddToHomescreenDialogHelper(env, obj, web_contents); | |
| 30 return reinterpret_cast<intptr_t>(add_to_homescreen_helper); | |
| 31 } | |
| 32 | |
| 33 // static | |
| 34 bool AddToHomescreenDialogHelper::RegisterAddToHomescreenDialogHelper( | |
| 35 JNIEnv* env) { | |
| 36 return RegisterNativesImpl(env); | |
| 37 } | |
| 38 | |
| 39 AddToHomescreenDialogHelper::AddToHomescreenDialogHelper( | |
| 40 JNIEnv* env, | |
| 41 jobject obj, | |
| 42 content::WebContents* web_contents) | |
| 43 : add_shortcut_pending_(false), | |
| 44 data_fetcher_(new AddToHomescreenDataFetcher(web_contents, | |
| 45 ShortcutHelper::GetIdealHomescreenIconSizeInDp(), | |
| 46 ShortcutHelper::GetMinimumHomescreenIconSizeInDp(), | |
| 47 ShortcutHelper::GetIdealSplashImageSizeInDp(), | |
| 48 ShortcutHelper::GetMinimumSplashImageSizeInDp(), | |
| 49 this)) { | |
| 50 java_ref_.Reset(env, obj); | |
| 51 } | |
| 52 | |
| 53 void AddToHomescreenDialogHelper::Destroy(JNIEnv* env, | |
| 54 const JavaParamRef<jobject>& obj) { | |
| 55 delete this; | |
| 56 } | |
| 57 | |
| 58 void AddToHomescreenDialogHelper::AddShortcut( | |
| 59 JNIEnv* env, | |
| 60 const JavaParamRef<jobject>& obj, | |
| 61 const JavaParamRef<jstring>& j_user_title) { | |
| 62 add_shortcut_pending_ = true; | |
| 63 | |
| 64 base::string16 user_title = | |
| 65 base::android::ConvertJavaStringToUTF16(env, j_user_title); | |
| 66 if (!user_title.empty()) | |
| 67 data_fetcher_->shortcut_info().user_title = user_title; | |
| 68 | |
| 69 if (data_fetcher_->is_ready()) { | |
| 70 // If the fetcher isn't ready yet, the shortcut will be added when it is | |
| 71 // via OnDataAvailable(); | |
| 72 AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->shortcut_icon()); | |
| 73 } | |
| 74 } | |
| 75 | |
| 76 AddToHomescreenDialogHelper::~AddToHomescreenDialogHelper() { | |
| 77 data_fetcher_->set_weak_observer(nullptr); | |
| 78 data_fetcher_ = nullptr; | |
| 79 } | |
| 80 | |
| 81 void AddToHomescreenDialogHelper::AddShortcut(const ShortcutInfo& info, | |
| 82 const SkBitmap& icon) { | |
| 83 DCHECK(add_shortcut_pending_); | |
| 84 if (!add_shortcut_pending_) | |
| 85 return; | |
| 86 add_shortcut_pending_ = false; | |
| 87 | |
| 88 content::WebContents* web_contents = data_fetcher_->web_contents(); | |
| 89 if (!web_contents) | |
| 90 return; | |
| 91 | |
| 92 RecordAddToHomescreen(); | |
| 93 | |
| 94 const std::string& uid = base::GenerateGUID(); | |
| 95 ShortcutHelper::AddToLauncherWithSkBitmap( | |
| 96 web_contents->GetBrowserContext(), info, uid, icon, | |
| 97 data_fetcher_->FetchSplashScreenImageCallback(uid)); | |
| 98 } | |
| 99 | |
| 100 void AddToHomescreenDialogHelper::RecordAddToHomescreen() { | |
| 101 // Record that the shortcut has been added, so no banners will be shown | |
| 102 // for this app. | |
| 103 content::WebContents* web_contents = data_fetcher_->web_contents(); | |
| 104 if (!web_contents) | |
| 105 return; | |
| 106 | |
| 107 AppBannerSettingsHelper::RecordBannerEvent( | |
| 108 web_contents, web_contents->GetURL(), | |
| 109 data_fetcher_->shortcut_info().url.spec(), | |
| 110 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, | |
| 111 base::Time::Now()); | |
| 112 } | |
| 113 | |
| 114 void AddToHomescreenDialogHelper::OnUserTitleAvailable( | |
| 115 const base::string16& user_title) { | |
| 116 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 117 ScopedJavaLocalRef<jstring> j_user_title = | |
| 118 base::android::ConvertUTF16ToJavaString(env, user_title); | |
| 119 Java_AddToHomescreenDialogHelper_onUserTitleAvailable(env, java_ref_, | |
| 120 j_user_title); | |
| 121 } | |
| 122 | |
| 123 void AddToHomescreenDialogHelper::OnDataAvailable(const ShortcutInfo& info, | |
| 124 const SkBitmap& icon) { | |
| 125 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 126 ScopedJavaLocalRef<jobject> java_bitmap; | |
| 127 if (icon.getSize()) | |
| 128 java_bitmap = gfx::ConvertToJavaBitmap(&icon); | |
| 129 | |
| 130 Java_AddToHomescreenDialogHelper_onIconAvailable(env, java_ref_, java_bitmap); | |
| 131 | |
| 132 if (add_shortcut_pending_) | |
| 133 AddShortcut(info, icon); | |
| 134 } | |
| 135 | |
| 136 SkBitmap AddToHomescreenDialogHelper::FinalizeLauncherIconInBackground( | |
| 137 const SkBitmap& bitmap, | |
| 138 const GURL& url, | |
| 139 bool* is_generated) { | |
| 140 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 141 | |
| 142 return ShortcutHelper::FinalizeLauncherIconInBackground(bitmap, url, | |
| 143 is_generated); | |
| 144 } | |
| OLD | NEW |