| 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/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| 11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/command_line.h" |
| 14 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 16 #include "chrome/browser/manifest/manifest_icon_downloader.h" | 17 #include "chrome/browser/manifest/manifest_icon_downloader.h" |
| 18 #include "chrome/common/chrome_switches.h" |
| 17 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/web_contents.h" | 20 #include "content/public/browser/web_contents.h" |
| 19 #include "jni/ShortcutHelper_jni.h" | 21 #include "jni/ShortcutHelper_jni.h" |
| 20 #include "ui/gfx/android/java_bitmap.h" | 22 #include "ui/gfx/android/java_bitmap.h" |
| 21 #include "ui/gfx/color_analysis.h" | 23 #include "ui/gfx/color_analysis.h" |
| 22 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 23 | 25 |
| 24 using content::Manifest; | 26 using content::Manifest; |
| 25 | 27 |
| 26 namespace { | 28 namespace { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 52 kMinimumSplashImageSize = sizes[3]; | 54 kMinimumSplashImageSize = sizes[3]; |
| 53 | 55 |
| 54 // Try to ensure that the data returned is sane. | 56 // Try to ensure that the data returned is sane. |
| 55 DCHECK(kMinimumHomescreenIconSize <= kIdealHomescreenIconSize); | 57 DCHECK(kMinimumHomescreenIconSize <= kIdealHomescreenIconSize); |
| 56 DCHECK(kMinimumSplashImageSize <= kIdealSplashImageSize); | 58 DCHECK(kMinimumSplashImageSize <= kIdealSplashImageSize); |
| 57 } | 59 } |
| 58 | 60 |
| 59 } // anonymous namespace | 61 } // anonymous namespace |
| 60 | 62 |
| 61 // static | 63 // static |
| 62 void ShortcutHelper::AddShortcutInBackgroundWithSkBitmap( | 64 void ShortcutHelper::AddToLauncherInBackgroundWithSkBitmap( |
| 63 const ShortcutInfo& info, | 65 const ShortcutInfo& info, |
| 64 const std::string& webapp_id, | 66 const std::string& webapp_id, |
| 65 const SkBitmap& icon_bitmap, | 67 const SkBitmap& icon_bitmap, |
| 68 const base::Closure& splash_image_callback) { |
| 69 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 70 |
| 71 if (info.display == blink::WebDisplayModeStandalone || |
| 72 info.display == blink::WebDisplayModeFullscreen) { |
| 73 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 74 switches::kEnableWebApk)) { |
| 75 InstallWebApkInBackgroundWithSkBitmap(info, webapp_id, icon_bitmap); |
| 76 return; |
| 77 } |
| 78 AddWebappInBackgroundWithSkBitmap(info, webapp_id, icon_bitmap, |
| 79 splash_image_callback); |
| 80 return; |
| 81 } |
| 82 AddShortcutInBackgroundWithSkBitmap(info, icon_bitmap); |
| 83 } |
| 84 |
| 85 // static |
| 86 void ShortcutHelper::InstallWebApkInBackgroundWithSkBitmap( |
| 87 const ShortcutInfo& info, |
| 88 const std::string& webapp_id, |
| 89 const SkBitmap& icon_bitmap) { |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 91 |
| 92 // TODO(pkotwicz): Send request to WebAPK server to generate WebAPK. |
| 93 |
| 94 JNIEnv* env = base::android::AttachCurrentThread(); |
| 95 ScopedJavaLocalRef<jstring> java_url = |
| 96 base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
| 97 ScopedJavaLocalRef<jstring> java_scope_url = |
| 98 base::android::ConvertUTF8ToJavaString(env, info.scope.spec()); |
| 99 ScopedJavaLocalRef<jstring> java_name = |
| 100 base::android::ConvertUTF16ToJavaString(env, info.name); |
| 101 ScopedJavaLocalRef<jstring> java_short_name = |
| 102 base::android::ConvertUTF16ToJavaString(env, info.short_name); |
| 103 ScopedJavaLocalRef<jstring> java_icon_url = |
| 104 base::android::ConvertUTF8ToJavaString(env, info.icon_url.spec()); |
| 105 ScopedJavaLocalRef<jobject> java_bitmap; |
| 106 if (icon_bitmap.getSize()) |
| 107 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
| 108 ScopedJavaLocalRef<jstring> java_manifest_url = |
| 109 base::android::ConvertUTF8ToJavaString(env, info.manifest_url.spec()); |
| 110 |
| 111 Java_ShortcutHelper_installWebApk( |
| 112 env, |
| 113 java_url.obj(), |
| 114 java_scope_url.obj(), |
| 115 java_name.obj(), |
| 116 java_short_name.obj(), |
| 117 java_icon_url.obj(), |
| 118 java_bitmap.obj(), |
| 119 info.display, |
| 120 info.orientation, |
| 121 info.theme_color, |
| 122 info.background_color, |
| 123 java_manifest_url.obj()); |
| 124 } |
| 125 |
| 126 // static |
| 127 void ShortcutHelper::AddWebappInBackgroundWithSkBitmap( |
| 128 const ShortcutInfo& info, |
| 129 const std::string& webapp_id, |
| 130 const SkBitmap& icon_bitmap, |
| 66 const base::Closure& splash_image_callback) { | 131 const base::Closure& splash_image_callback) { |
| 67 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 132 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| 68 | 133 |
| 69 // Send the data to the Java side to create the shortcut. | 134 // Send the data to the Java side to create the shortcut. |
| 70 JNIEnv* env = base::android::AttachCurrentThread(); | 135 JNIEnv* env = base::android::AttachCurrentThread(); |
| 71 ScopedJavaLocalRef<jstring> java_webapp_id = | 136 ScopedJavaLocalRef<jstring> java_webapp_id = |
| 72 base::android::ConvertUTF8ToJavaString(env, webapp_id); | 137 base::android::ConvertUTF8ToJavaString(env, webapp_id); |
| 73 ScopedJavaLocalRef<jstring> java_url = | 138 ScopedJavaLocalRef<jstring> java_url = |
| 74 base::android::ConvertUTF8ToJavaString(env, info.url.spec()); | 139 base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
| 75 ScopedJavaLocalRef<jstring> java_scope_url = | 140 ScopedJavaLocalRef<jstring> java_scope_url = |
| 76 base::android::ConvertUTF8ToJavaString(env, info.scope.spec()); | 141 base::android::ConvertUTF8ToJavaString(env, info.scope.spec()); |
| 77 ScopedJavaLocalRef<jstring> java_user_title = | 142 ScopedJavaLocalRef<jstring> java_user_title = |
| 78 base::android::ConvertUTF16ToJavaString(env, info.user_title); | 143 base::android::ConvertUTF16ToJavaString(env, info.user_title); |
| 79 ScopedJavaLocalRef<jstring> java_name = | 144 ScopedJavaLocalRef<jstring> java_name = |
| 80 base::android::ConvertUTF16ToJavaString(env, info.name); | 145 base::android::ConvertUTF16ToJavaString(env, info.name); |
| 81 ScopedJavaLocalRef<jstring> java_short_name = | 146 ScopedJavaLocalRef<jstring> java_short_name = |
| 82 base::android::ConvertUTF16ToJavaString(env, info.short_name); | 147 base::android::ConvertUTF16ToJavaString(env, info.short_name); |
| 83 ScopedJavaLocalRef<jstring> java_icon_url = | 148 ScopedJavaLocalRef<jstring> java_icon_url = |
| 84 base::android::ConvertUTF8ToJavaString(env, info.icon_url.spec()); | 149 base::android::ConvertUTF8ToJavaString(env, info.icon_url.spec()); |
| 85 ScopedJavaLocalRef<jobject> java_bitmap; | 150 ScopedJavaLocalRef<jobject> java_bitmap; |
| 86 if (icon_bitmap.getSize()) | 151 if (icon_bitmap.getSize()) |
| 87 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); | 152 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
| 88 ScopedJavaLocalRef<jstring> java_manifest_url = | |
| 89 base::android::ConvertUTF8ToJavaString(env, info.manifest_url.spec()); | |
| 90 | 153 |
| 91 uintptr_t callback_pointer = 0; | 154 // The callback will need to be run after shortcut creation completes in order |
| 92 if (info.display == blink::WebDisplayModeStandalone || | 155 // to download the splash image and save it to the WebappDataStorage. Create a |
| 93 info.display == blink::WebDisplayModeFullscreen) { | 156 // copy of the callback here and send the pointer to Java, which will send it |
| 94 // The callback will need to be run after shortcut creation completes in | 157 // back once the asynchronous shortcut creation process finishes. |
| 95 // order to download the splash image and save it to the WebappDataStorage. | 158 uintptr_t callback_pointer = |
| 96 // Create a copy of the callback here and send the pointer to Java, which | 159 reinterpret_cast<uintptr_t>(new base::Closure(splash_image_callback)); |
| 97 // will send it back once the asynchronous shortcut creation process | |
| 98 // finishes. | |
| 99 callback_pointer = | |
| 100 reinterpret_cast<uintptr_t>(new base::Closure(splash_image_callback)); | |
| 101 } | |
| 102 | 160 |
| 103 Java_ShortcutHelper_addShortcut( | 161 Java_ShortcutHelper_addWebapp( |
| 104 env, | 162 env, |
| 105 java_webapp_id.obj(), | 163 java_webapp_id.obj(), |
| 106 java_url.obj(), | 164 java_url.obj(), |
| 107 java_scope_url.obj(), | 165 java_scope_url.obj(), |
| 108 java_user_title.obj(), | 166 java_user_title.obj(), |
| 109 java_name.obj(), | 167 java_name.obj(), |
| 110 java_short_name.obj(), | 168 java_short_name.obj(), |
| 111 java_icon_url.obj(), | 169 java_icon_url.obj(), |
| 112 java_bitmap.obj(), | 170 java_bitmap.obj(), |
| 113 info.display, | 171 info.display, |
| 114 info.orientation, | 172 info.orientation, |
| 115 info.source, | 173 info.source, |
| 116 info.theme_color, | 174 info.theme_color, |
| 117 info.background_color, | 175 info.background_color, |
| 118 java_manifest_url.obj(), | |
| 119 callback_pointer); | 176 callback_pointer); |
| 120 } | 177 } |
| 121 | 178 |
| 179 void ShortcutHelper::AddShortcutInBackgroundWithSkBitmap( |
| 180 const ShortcutInfo& info, |
| 181 const SkBitmap& icon_bitmap) { |
| 182 JNIEnv* env = base::android::AttachCurrentThread(); |
| 183 ScopedJavaLocalRef<jstring> java_url = |
| 184 base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
| 185 ScopedJavaLocalRef<jstring> java_user_title = |
| 186 base::android::ConvertUTF16ToJavaString(env, info.user_title); |
| 187 ScopedJavaLocalRef<jobject> java_bitmap; |
| 188 if (icon_bitmap.getSize()) |
| 189 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
| 190 |
| 191 Java_ShortcutHelper_addShortcut(env, java_url.obj(), java_user_title.obj(), |
| 192 java_bitmap.obj(), info.source); |
| 193 } |
| 194 |
| 122 int ShortcutHelper::GetIdealHomescreenIconSizeInDp() { | 195 int ShortcutHelper::GetIdealHomescreenIconSizeInDp() { |
| 123 if (kIdealHomescreenIconSize == -1) | 196 if (kIdealHomescreenIconSize == -1) |
| 124 GetHomescreenIconAndSplashImageSizes(); | 197 GetHomescreenIconAndSplashImageSizes(); |
| 125 return kIdealHomescreenIconSize; | 198 return kIdealHomescreenIconSize; |
| 126 } | 199 } |
| 127 | 200 |
| 128 int ShortcutHelper::GetMinimumHomescreenIconSizeInDp() { | 201 int ShortcutHelper::GetMinimumHomescreenIconSizeInDp() { |
| 129 if (kMinimumHomescreenIconSize == -1) | 202 if (kMinimumHomescreenIconSize == -1) |
| 130 GetHomescreenIconAndSplashImageSizes(); | 203 GetHomescreenIconAndSplashImageSizes(); |
| 131 return kMinimumHomescreenIconSize; | 204 return kMinimumHomescreenIconSize; |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 DCHECK(jsplash_image_callback); | 309 DCHECK(jsplash_image_callback); |
| 237 base::Closure* splash_image_callback = | 310 base::Closure* splash_image_callback = |
| 238 reinterpret_cast<base::Closure*>(jsplash_image_callback); | 311 reinterpret_cast<base::Closure*>(jsplash_image_callback); |
| 239 splash_image_callback->Run(); | 312 splash_image_callback->Run(); |
| 240 delete splash_image_callback; | 313 delete splash_image_callback; |
| 241 } | 314 } |
| 242 | 315 |
| 243 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { | 316 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { |
| 244 return RegisterNativesImpl(env); | 317 return RegisterNativesImpl(env); |
| 245 } | 318 } |
| OLD | NEW |