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" | 9 #include "base/android/context_utils.h" |
10 #include "base/android/jni_android.h" | 10 #include "base/android/jni_android.h" |
11 #include "base/android/jni_array.h" | 11 #include "base/android/jni_array.h" |
12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 13 #include "base/bind.h" |
| 14 #include "base/callback.h" |
13 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
14 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
15 #include "chrome/browser/manifest/manifest_icon_downloader.h" | 17 #include "chrome/browser/manifest/manifest_icon_downloader.h" |
16 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
18 #include "jni/ShortcutHelper_jni.h" | 20 #include "jni/ShortcutHelper_jni.h" |
19 #include "ui/gfx/android/java_bitmap.h" | 21 #include "ui/gfx/android/java_bitmap.h" |
20 #include "ui/gfx/color_analysis.h" | 22 #include "ui/gfx/color_analysis.h" |
21 #include "url/gurl.h" | 23 #include "url/gurl.h" |
22 | 24 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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::AddShortcutInBackgroundWithSkBitmap( |
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) { |
66 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 69 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
67 | 70 |
68 // Send the data to the Java side to create the shortcut. | 71 // Send the data to the Java side to create the shortcut. |
69 JNIEnv* env = base::android::AttachCurrentThread(); | 72 JNIEnv* env = base::android::AttachCurrentThread(); |
70 ScopedJavaLocalRef<jstring> java_webapp_id = | 73 ScopedJavaLocalRef<jstring> java_webapp_id = |
71 base::android::ConvertUTF8ToJavaString(env, webapp_id); | 74 base::android::ConvertUTF8ToJavaString(env, webapp_id); |
72 ScopedJavaLocalRef<jstring> java_url = | 75 ScopedJavaLocalRef<jstring> java_url = |
73 base::android::ConvertUTF8ToJavaString(env, info.url.spec()); | 76 base::android::ConvertUTF8ToJavaString(env, info.url.spec()); |
74 ScopedJavaLocalRef<jstring> java_user_title = | 77 ScopedJavaLocalRef<jstring> java_user_title = |
75 base::android::ConvertUTF16ToJavaString(env, info.user_title); | 78 base::android::ConvertUTF16ToJavaString(env, info.user_title); |
76 ScopedJavaLocalRef<jstring> java_name = | 79 ScopedJavaLocalRef<jstring> java_name = |
77 base::android::ConvertUTF16ToJavaString(env, info.name); | 80 base::android::ConvertUTF16ToJavaString(env, info.name); |
78 ScopedJavaLocalRef<jstring> java_short_name = | 81 ScopedJavaLocalRef<jstring> java_short_name = |
79 base::android::ConvertUTF16ToJavaString(env, info.short_name); | 82 base::android::ConvertUTF16ToJavaString(env, info.short_name); |
80 ScopedJavaLocalRef<jobject> java_bitmap; | 83 ScopedJavaLocalRef<jobject> java_bitmap; |
81 if (icon_bitmap.getSize()) | 84 if (icon_bitmap.getSize()) |
82 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); | 85 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
83 | 86 |
| 87 uintptr_t callback_pointer = 0; |
| 88 bool is_webapp_capable = info.display == blink::WebDisplayModeStandalone; |
| 89 |
| 90 if (is_webapp_capable) { |
| 91 // The callback will need to be run after shortcut creation completes in |
| 92 // order to download the splash image and save it to the WebappDataStorage. |
| 93 // Create a copy of the callback here and send the pointer to Java, which |
| 94 // will send it back once the asynchronous shortcut creation process |
| 95 // finishes. |
| 96 callback_pointer = |
| 97 reinterpret_cast<uintptr_t>(new base::Closure(splash_image_callback)); |
| 98 } |
| 99 |
84 Java_ShortcutHelper_addShortcut( | 100 Java_ShortcutHelper_addShortcut( |
85 env, | 101 env, |
86 base::android::GetApplicationContext(), | 102 base::android::GetApplicationContext(), |
87 java_webapp_id.obj(), | 103 java_webapp_id.obj(), |
88 java_url.obj(), | 104 java_url.obj(), |
89 java_user_title.obj(), | 105 java_user_title.obj(), |
90 java_name.obj(), | 106 java_name.obj(), |
91 java_short_name.obj(), | 107 java_short_name.obj(), |
92 java_bitmap.obj(), | 108 java_bitmap.obj(), |
93 info.display == blink::WebDisplayModeStandalone, | 109 is_webapp_capable, |
94 info.orientation, | 110 info.orientation, |
95 info.source, | 111 info.source, |
96 info.theme_color, | 112 info.theme_color, |
97 info.background_color, | 113 info.background_color, |
98 info.is_icon_generated); | 114 info.is_icon_generated, |
| 115 callback_pointer); |
99 } | 116 } |
100 | 117 |
101 int ShortcutHelper::GetIdealHomescreenIconSizeInDp() { | 118 int ShortcutHelper::GetIdealHomescreenIconSizeInDp() { |
102 if (kIdealHomescreenIconSize == -1) | 119 if (kIdealHomescreenIconSize == -1) |
103 GetHomescreenIconAndSplashImageSizes(); | 120 GetHomescreenIconAndSplashImageSizes(); |
104 return kIdealHomescreenIconSize; | 121 return kIdealHomescreenIconSize; |
105 } | 122 } |
106 | 123 |
107 int ShortcutHelper::GetMinimumHomescreenIconSizeInDp() { | 124 int ShortcutHelper::GetMinimumHomescreenIconSizeInDp() { |
108 if (kMinimumHomescreenIconSize == -1) | 125 if (kMinimumHomescreenIconSize == -1) |
(...skipping 12 matching lines...) Expand all Loading... |
121 GetHomescreenIconAndSplashImageSizes(); | 138 GetHomescreenIconAndSplashImageSizes(); |
122 return kMinimumSplashImageSize; | 139 return kMinimumSplashImageSize; |
123 } | 140 } |
124 | 141 |
125 // static | 142 // static |
126 void ShortcutHelper::FetchSplashScreenImage( | 143 void ShortcutHelper::FetchSplashScreenImage( |
127 content::WebContents* web_contents, | 144 content::WebContents* web_contents, |
128 const GURL& image_url, | 145 const GURL& image_url, |
129 const int ideal_splash_image_size_in_dp, | 146 const int ideal_splash_image_size_in_dp, |
130 const int minimum_splash_image_size_in_dp, | 147 const int minimum_splash_image_size_in_dp, |
131 const std::string& webapp_id, | 148 const std::string& webapp_id) { |
132 const std::string& webapp_scope) { | |
133 // This is a fire and forget task. It is not vital for the splash screen image | 149 // This is a fire and forget task. It is not vital for the splash screen image |
134 // to be downloaded so if the downloader returns false there is no fallback. | 150 // to be downloaded so if the downloader returns false there is no fallback. |
135 ManifestIconDownloader::Download( | 151 ManifestIconDownloader::Download( |
136 web_contents, image_url, ideal_splash_image_size_in_dp, | 152 web_contents, image_url, ideal_splash_image_size_in_dp, |
137 minimum_splash_image_size_in_dp, | 153 minimum_splash_image_size_in_dp, |
138 base::Bind(&ShortcutHelper::StoreWebappData, webapp_id, webapp_scope)); | 154 base::Bind(&ShortcutHelper::StoreWebappSplashImage, webapp_id)); |
139 } | 155 } |
140 | 156 |
141 // static | 157 // static |
142 void ShortcutHelper::StoreWebappData( | 158 void ShortcutHelper::StoreWebappSplashImage( |
143 const std::string& webapp_id, | 159 const std::string& webapp_id, |
144 const std::string& webapp_url, | |
145 const SkBitmap& splash_image) { | 160 const SkBitmap& splash_image) { |
146 if (splash_image.drawsNothing()) | 161 if (splash_image.drawsNothing()) |
147 return; | 162 return; |
148 | 163 |
149 JNIEnv* env = base::android::AttachCurrentThread(); | 164 JNIEnv* env = base::android::AttachCurrentThread(); |
150 ScopedJavaLocalRef<jstring> java_webapp_id = | 165 ScopedJavaLocalRef<jstring> java_webapp_id = |
151 base::android::ConvertUTF8ToJavaString(env, webapp_id); | 166 base::android::ConvertUTF8ToJavaString(env, webapp_id); |
152 ScopedJavaLocalRef<jstring> java_webapp_url = | |
153 base::android::ConvertUTF8ToJavaString(env, webapp_url); | |
154 ScopedJavaLocalRef<jobject> java_splash_image = | 167 ScopedJavaLocalRef<jobject> java_splash_image = |
155 gfx::ConvertToJavaBitmap(&splash_image); | 168 gfx::ConvertToJavaBitmap(&splash_image); |
156 | 169 |
157 Java_ShortcutHelper_storeWebappData( | 170 Java_ShortcutHelper_storeWebappSplashImage( |
158 env, | 171 env, |
159 base::android::GetApplicationContext(), | 172 base::android::GetApplicationContext(), |
160 java_webapp_id.obj(), | 173 java_webapp_id.obj(), |
161 java_webapp_url.obj(), | |
162 java_splash_image.obj()); | 174 java_splash_image.obj()); |
163 } | 175 } |
164 | 176 |
165 // static | 177 // static |
166 SkBitmap ShortcutHelper::FinalizeLauncherIcon(const SkBitmap& bitmap, | 178 SkBitmap ShortcutHelper::FinalizeLauncherIcon(const SkBitmap& bitmap, |
167 const GURL& url, | 179 const GURL& url, |
168 bool* is_generated) { | 180 bool* is_generated) { |
169 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 181 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
170 | 182 |
171 JNIEnv* env = base::android::AttachCurrentThread(); | 183 JNIEnv* env = base::android::AttachCurrentThread(); |
(...skipping 23 matching lines...) Expand all Loading... |
195 *is_generated = true; | 207 *is_generated = true; |
196 result = Java_ShortcutHelper_generateHomeScreenIcon( | 208 result = Java_ShortcutHelper_generateHomeScreenIcon( |
197 env, base::android::GetApplicationContext(), java_url.obj(), | 209 env, base::android::GetApplicationContext(), java_url.obj(), |
198 SkColorGetR(mean_color), SkColorGetG(mean_color), | 210 SkColorGetR(mean_color), SkColorGetG(mean_color), |
199 SkColorGetB(mean_color)); | 211 SkColorGetB(mean_color)); |
200 } | 212 } |
201 | 213 |
202 return gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result.obj())); | 214 return gfx::CreateSkBitmapFromJavaBitmap(gfx::JavaBitmap(result.obj())); |
203 } | 215 } |
204 | 216 |
| 217 // Callback used by Java when the shortcut has been created. |
| 218 // |splash_image_callback| is a pointer to a base::Closure allocated in |
| 219 // AddShortcutInBackgroundWithSkBitmap, so reinterpret_cast it back and run it. |
| 220 // |
| 221 // This callback should only ever be called when the shortcut was for a |
| 222 // webapp-capable site; otherwise, |splash_image_callback| will have never been |
| 223 // allocated and doesn't need to be run or deleted. |
| 224 void OnWebappDataStored(JNIEnv* env, |
| 225 const JavaParamRef<jclass>& clazz, |
| 226 jlong jsplash_image_callback) { |
| 227 DCHECK(jsplash_image_callback); |
| 228 base::Closure* splash_image_callback = |
| 229 reinterpret_cast<base::Closure*>(jsplash_image_callback); |
| 230 splash_image_callback->Run(); |
| 231 delete splash_image_callback; |
| 232 } |
| 233 |
205 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { | 234 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { |
206 return RegisterNativesImpl(env); | 235 return RegisterNativesImpl(env); |
207 } | 236 } |
OLD | NEW |