| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/shortcut_helper.h" | 5 #include "chrome/browser/android/shortcut_helper.h" |
| 6 | 6 |
| 7 #include <jni.h> | 7 #include <jni.h> |
| 8 | 8 |
| 9 #include "base/android/context_utils.h" | |
| 10 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 11 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| 12 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 13 #include "base/bind.h" | 12 #include "base/bind.h" |
| 14 #include "base/callback.h" | 13 #include "base/callback.h" |
| 15 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
| 16 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 17 #include "chrome/browser/manifest/manifest_icon_downloader.h" | 16 #include "chrome/browser/manifest/manifest_icon_downloader.h" |
| 18 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 31 static int kIdealSplashImageSize = -1; | 30 static int kIdealSplashImageSize = -1; |
| 32 static int kMinimumSplashImageSize = -1; | 31 static int kMinimumSplashImageSize = -1; |
| 33 | 32 |
| 34 static int kDefaultRGBIconValue = 145; | 33 static int kDefaultRGBIconValue = 145; |
| 35 | 34 |
| 36 // Retrieves and caches the ideal and minimum sizes of the Home screen icon | 35 // Retrieves and caches the ideal and minimum sizes of the Home screen icon |
| 37 // and the splash screen image. | 36 // and the splash screen image. |
| 38 void GetHomescreenIconAndSplashImageSizes() { | 37 void GetHomescreenIconAndSplashImageSizes() { |
| 39 JNIEnv* env = base::android::AttachCurrentThread(); | 38 JNIEnv* env = base::android::AttachCurrentThread(); |
| 40 ScopedJavaLocalRef<jintArray> java_size_array = | 39 ScopedJavaLocalRef<jintArray> java_size_array = |
| 41 Java_ShortcutHelper_getHomeScreenIconAndSplashImageSizes(env, | 40 Java_ShortcutHelper_getHomeScreenIconAndSplashImageSizes(env); |
| 42 base::android::GetApplicationContext()); | |
| 43 std::vector<int> sizes; | 41 std::vector<int> sizes; |
| 44 base::android::JavaIntArrayToIntVector( | 42 base::android::JavaIntArrayToIntVector( |
| 45 env, java_size_array.obj(), &sizes); | 43 env, java_size_array.obj(), &sizes); |
| 46 | 44 |
| 47 // Check that the size returned is what is expected. | 45 // Check that the size returned is what is expected. |
| 48 DCHECK(sizes.size() == 4); | 46 DCHECK(sizes.size() == 4); |
| 49 | 47 |
| 50 // This ordering must be kept up to date with the Java ShortcutHelper. | 48 // This ordering must be kept up to date with the Java ShortcutHelper. |
| 51 kIdealHomescreenIconSize = sizes[0]; | 49 kIdealHomescreenIconSize = sizes[0]; |
| 52 kMinimumHomescreenIconSize = sizes[1]; | 50 kMinimumHomescreenIconSize = sizes[1]; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 // order to download the splash image and save it to the WebappDataStorage. | 89 // order to download the splash image and save it to the WebappDataStorage. |
| 92 // Create a copy of the callback here and send the pointer to Java, which | 90 // Create a copy of the callback here and send the pointer to Java, which |
| 93 // will send it back once the asynchronous shortcut creation process | 91 // will send it back once the asynchronous shortcut creation process |
| 94 // finishes. | 92 // finishes. |
| 95 callback_pointer = | 93 callback_pointer = |
| 96 reinterpret_cast<uintptr_t>(new base::Closure(splash_image_callback)); | 94 reinterpret_cast<uintptr_t>(new base::Closure(splash_image_callback)); |
| 97 } | 95 } |
| 98 | 96 |
| 99 Java_ShortcutHelper_addShortcut( | 97 Java_ShortcutHelper_addShortcut( |
| 100 env, | 98 env, |
| 101 base::android::GetApplicationContext(), | |
| 102 java_webapp_id.obj(), | 99 java_webapp_id.obj(), |
| 103 java_url.obj(), | 100 java_url.obj(), |
| 104 java_user_title.obj(), | 101 java_user_title.obj(), |
| 105 java_name.obj(), | 102 java_name.obj(), |
| 106 java_short_name.obj(), | 103 java_short_name.obj(), |
| 107 java_bitmap.obj(), | 104 java_bitmap.obj(), |
| 108 info.display, | 105 info.display, |
| 109 info.orientation, | 106 info.orientation, |
| 110 info.source, | 107 info.source, |
| 111 info.theme_color, | 108 info.theme_color, |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 return; | 158 return; |
| 162 | 159 |
| 163 JNIEnv* env = base::android::AttachCurrentThread(); | 160 JNIEnv* env = base::android::AttachCurrentThread(); |
| 164 ScopedJavaLocalRef<jstring> java_webapp_id = | 161 ScopedJavaLocalRef<jstring> java_webapp_id = |
| 165 base::android::ConvertUTF8ToJavaString(env, webapp_id); | 162 base::android::ConvertUTF8ToJavaString(env, webapp_id); |
| 166 ScopedJavaLocalRef<jobject> java_splash_image = | 163 ScopedJavaLocalRef<jobject> java_splash_image = |
| 167 gfx::ConvertToJavaBitmap(&splash_image); | 164 gfx::ConvertToJavaBitmap(&splash_image); |
| 168 | 165 |
| 169 Java_ShortcutHelper_storeWebappSplashImage( | 166 Java_ShortcutHelper_storeWebappSplashImage( |
| 170 env, | 167 env, |
| 171 base::android::GetApplicationContext(), | |
| 172 java_webapp_id.obj(), | 168 java_webapp_id.obj(), |
| 173 java_splash_image.obj()); | 169 java_splash_image.obj()); |
| 174 } | 170 } |
| 175 | 171 |
| 176 // static | 172 // static |
| 177 SkBitmap ShortcutHelper::FinalizeLauncherIcon(const SkBitmap& bitmap, | 173 SkBitmap ShortcutHelper::FinalizeLauncherIcon(const SkBitmap& bitmap, |
| 178 const GURL& url, | 174 const GURL& url, |
| 179 bool* is_generated) { | 175 bool* is_generated) { |
| 180 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 176 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 181 | 177 |
| 182 JNIEnv* env = base::android::AttachCurrentThread(); | 178 JNIEnv* env = base::android::AttachCurrentThread(); |
| 183 ScopedJavaLocalRef<jobject> result; | 179 ScopedJavaLocalRef<jobject> result; |
| 184 *is_generated = false; | 180 *is_generated = false; |
| 185 | 181 |
| 186 if (!bitmap.isNull()) { | 182 if (!bitmap.isNull()) { |
| 187 if (Java_ShortcutHelper_isIconLargeEnoughForLauncher( | 183 if (Java_ShortcutHelper_isIconLargeEnoughForLauncher(env, bitmap.width(), |
| 188 env, base::android::GetApplicationContext(), bitmap.width(), | 184 bitmap.height())) { |
| 189 bitmap.height())) { | |
| 190 ScopedJavaLocalRef<jobject> java_bitmap = | 185 ScopedJavaLocalRef<jobject> java_bitmap = |
| 191 gfx::ConvertToJavaBitmap(&bitmap); | 186 gfx::ConvertToJavaBitmap(&bitmap); |
| 192 result = Java_ShortcutHelper_createHomeScreenIconFromWebIcon( | 187 result = Java_ShortcutHelper_createHomeScreenIconFromWebIcon( |
| 193 env, base::android::GetApplicationContext(), java_bitmap.obj()); | 188 env, java_bitmap.obj()); |
| 194 } | 189 } |
| 195 } | 190 } |
| 196 | 191 |
| 197 if (result.is_null()) { | 192 if (result.is_null()) { |
| 198 ScopedJavaLocalRef<jstring> java_url = | 193 ScopedJavaLocalRef<jstring> java_url = |
| 199 base::android::ConvertUTF8ToJavaString(env, url.spec()); | 194 base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 200 SkColor mean_color = SkColorSetRGB( | 195 SkColor mean_color = SkColorSetRGB( |
| 201 kDefaultRGBIconValue, kDefaultRGBIconValue, kDefaultRGBIconValue); | 196 kDefaultRGBIconValue, kDefaultRGBIconValue, kDefaultRGBIconValue); |
| 202 | 197 |
| 203 if (!bitmap.isNull()) | 198 if (!bitmap.isNull()) |
| 204 mean_color = color_utils::CalculateKMeanColorOfBitmap(bitmap); | 199 mean_color = color_utils::CalculateKMeanColorOfBitmap(bitmap); |
| 205 | 200 |
| 206 *is_generated = true; | 201 *is_generated = true; |
| 207 result = Java_ShortcutHelper_generateHomeScreenIcon( | 202 result = Java_ShortcutHelper_generateHomeScreenIcon( |
| 208 env, base::android::GetApplicationContext(), java_url.obj(), | 203 env, java_url.obj(), SkColorGetR(mean_color), SkColorGetG(mean_color), |
| 209 SkColorGetR(mean_color), SkColorGetG(mean_color), | |
| 210 SkColorGetB(mean_color)); | 204 SkColorGetB(mean_color)); |
| 211 } | 205 } |
| 212 | 206 |
| 213 return gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result.obj())); | 207 return gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result.obj())); |
| 214 } | 208 } |
| 215 | 209 |
| 216 // Callback used by Java when the shortcut has been created. | 210 // Callback used by Java when the shortcut has been created. |
| 217 // |splash_image_callback| is a pointer to a base::Closure allocated in | 211 // |splash_image_callback| is a pointer to a base::Closure allocated in |
| 218 // AddShortcutInBackgroundWithSkBitmap, so reinterpret_cast it back and run it. | 212 // AddShortcutInBackgroundWithSkBitmap, so reinterpret_cast it back and run it. |
| 219 // | 213 // |
| 220 // This callback should only ever be called when the shortcut was for a | 214 // This callback should only ever be called when the shortcut was for a |
| 221 // webapp-capable site; otherwise, |splash_image_callback| will have never been | 215 // webapp-capable site; otherwise, |splash_image_callback| will have never been |
| 222 // allocated and doesn't need to be run or deleted. | 216 // allocated and doesn't need to be run or deleted. |
| 223 void OnWebappDataStored(JNIEnv* env, | 217 void OnWebappDataStored(JNIEnv* env, |
| 224 const JavaParamRef<jclass>& clazz, | 218 const JavaParamRef<jclass>& clazz, |
| 225 jlong jsplash_image_callback) { | 219 jlong jsplash_image_callback) { |
| 226 DCHECK(jsplash_image_callback); | 220 DCHECK(jsplash_image_callback); |
| 227 base::Closure* splash_image_callback = | 221 base::Closure* splash_image_callback = |
| 228 reinterpret_cast<base::Closure*>(jsplash_image_callback); | 222 reinterpret_cast<base::Closure*>(jsplash_image_callback); |
| 229 splash_image_callback->Run(); | 223 splash_image_callback->Run(); |
| 230 delete splash_image_callback; | 224 delete splash_image_callback; |
| 231 } | 225 } |
| 232 | 226 |
| 233 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { | 227 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { |
| 234 return RegisterNativesImpl(env); | 228 return RegisterNativesImpl(env); |
| 235 } | 229 } |
| OLD | NEW |