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