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/command_line.h" |
15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
| 17 #include "chrome/browser/android/webapk/webapk_installer.h" |
17 #include "chrome/browser/manifest/manifest_icon_downloader.h" | 18 #include "chrome/browser/manifest/manifest_icon_downloader.h" |
18 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
20 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
21 #include "jni/ShortcutHelper_jni.h" | 22 #include "jni/ShortcutHelper_jni.h" |
22 #include "ui/gfx/android/java_bitmap.h" | 23 #include "ui/gfx/android/java_bitmap.h" |
23 #include "ui/gfx/color_analysis.h" | 24 #include "ui/gfx/color_analysis.h" |
24 #include "url/gurl.h" | 25 #include "url/gurl.h" |
25 | 26 |
26 using base::android::JavaParamRef; | 27 using base::android::JavaParamRef; |
(...skipping 30 matching lines...) Expand all Loading... |
57 | 58 |
58 // Try to ensure that the data returned is sane. | 59 // Try to ensure that the data returned is sane. |
59 DCHECK(kMinimumHomescreenIconSize <= kIdealHomescreenIconSize); | 60 DCHECK(kMinimumHomescreenIconSize <= kIdealHomescreenIconSize); |
60 DCHECK(kMinimumSplashImageSize <= kIdealSplashImageSize); | 61 DCHECK(kMinimumSplashImageSize <= kIdealSplashImageSize); |
61 } | 62 } |
62 | 63 |
63 } // anonymous namespace | 64 } // anonymous namespace |
64 | 65 |
65 // static | 66 // static |
66 void ShortcutHelper::AddToLauncherInBackgroundWithSkBitmap( | 67 void ShortcutHelper::AddToLauncherInBackgroundWithSkBitmap( |
| 68 content::BrowserContext* browser_context, |
67 const ShortcutInfo& info, | 69 const ShortcutInfo& info, |
68 const std::string& webapp_id, | 70 const std::string& webapp_id, |
69 const SkBitmap& icon_bitmap, | 71 const SkBitmap& icon_bitmap, |
70 const base::Closure& splash_image_callback) { | 72 const base::Closure& splash_image_callback) { |
71 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 73 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
72 | 74 |
73 if (info.display == blink::WebDisplayModeStandalone || | 75 if (info.display == blink::WebDisplayModeStandalone || |
74 info.display == blink::WebDisplayModeFullscreen) { | 76 info.display == blink::WebDisplayModeFullscreen) { |
75 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 77 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
76 switches::kEnableWebApk)) { | 78 switches::kEnableWebApk)) { |
77 InstallWebApkInBackgroundWithSkBitmap(info, webapp_id, icon_bitmap); | 79 InstallWebApkInBackgroundWithSkBitmap(browser_context, info, icon_bitmap); |
78 return; | 80 return; |
79 } | 81 } |
80 AddWebappInBackgroundWithSkBitmap(info, webapp_id, icon_bitmap, | 82 AddWebappInBackgroundWithSkBitmap(info, webapp_id, icon_bitmap, |
81 splash_image_callback); | 83 splash_image_callback); |
82 return; | 84 return; |
83 } | 85 } |
84 AddShortcutInBackgroundWithSkBitmap(info, icon_bitmap); | 86 AddShortcutInBackgroundWithSkBitmap(info, icon_bitmap); |
85 } | 87 } |
86 | 88 |
87 // static | 89 // static |
88 void ShortcutHelper::InstallWebApkInBackgroundWithSkBitmap( | 90 void ShortcutHelper::InstallWebApkInBackgroundWithSkBitmap( |
| 91 content::BrowserContext* browser_context, |
89 const ShortcutInfo& info, | 92 const ShortcutInfo& info, |
90 const std::string& webapp_id, | |
91 const SkBitmap& icon_bitmap) { | 93 const SkBitmap& icon_bitmap) { |
92 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 94 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
93 | 95 // WebApkInstaller destroys itself when it is done. |
94 // TODO(pkotwicz): Send request to WebAPK server to generate WebAPK. | 96 WebApkInstaller* installer = new WebApkInstaller(info, icon_bitmap); |
95 | 97 installer->InstallAsync(browser_context, |
96 JNIEnv* env = base::android::AttachCurrentThread(); | 98 base::Bind(&ShortcutHelper::OnBuiltWebApk)); |
97 ScopedJavaLocalRef<jstring> java_url = | |
98 base::android::ConvertUTF8ToJavaString(env, info.url.spec()); | |
99 ScopedJavaLocalRef<jstring> java_scope_url = | |
100 base::android::ConvertUTF8ToJavaString(env, info.scope.spec()); | |
101 ScopedJavaLocalRef<jstring> java_name = | |
102 base::android::ConvertUTF16ToJavaString(env, info.name); | |
103 ScopedJavaLocalRef<jstring> java_short_name = | |
104 base::android::ConvertUTF16ToJavaString(env, info.short_name); | |
105 ScopedJavaLocalRef<jstring> java_icon_url = | |
106 base::android::ConvertUTF8ToJavaString(env, info.icon_url.spec()); | |
107 ScopedJavaLocalRef<jobject> java_bitmap; | |
108 if (icon_bitmap.getSize()) | |
109 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); | |
110 ScopedJavaLocalRef<jstring> java_manifest_url = | |
111 base::android::ConvertUTF8ToJavaString(env, info.manifest_url.spec()); | |
112 | |
113 Java_ShortcutHelper_installWebApk( | |
114 env, | |
115 java_url.obj(), | |
116 java_scope_url.obj(), | |
117 java_name.obj(), | |
118 java_short_name.obj(), | |
119 java_icon_url.obj(), | |
120 java_bitmap.obj(), | |
121 info.display, | |
122 info.orientation, | |
123 info.theme_color, | |
124 info.background_color, | |
125 java_manifest_url.obj()); | |
126 } | 99 } |
127 | 100 |
128 // static | 101 // static |
129 void ShortcutHelper::AddWebappInBackgroundWithSkBitmap( | 102 void ShortcutHelper::AddWebappInBackgroundWithSkBitmap( |
130 const ShortcutInfo& info, | 103 const ShortcutInfo& info, |
131 const std::string& webapp_id, | 104 const std::string& webapp_id, |
132 const SkBitmap& icon_bitmap, | 105 const SkBitmap& icon_bitmap, |
133 const base::Closure& splash_image_callback) { | 106 const base::Closure& splash_image_callback) { |
134 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 107 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
135 | 108 |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 ScopedJavaLocalRef<jstring> java_user_title = | 160 ScopedJavaLocalRef<jstring> java_user_title = |
188 base::android::ConvertUTF16ToJavaString(env, info.user_title); | 161 base::android::ConvertUTF16ToJavaString(env, info.user_title); |
189 ScopedJavaLocalRef<jobject> java_bitmap; | 162 ScopedJavaLocalRef<jobject> java_bitmap; |
190 if (icon_bitmap.getSize()) | 163 if (icon_bitmap.getSize()) |
191 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); | 164 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
192 | 165 |
193 Java_ShortcutHelper_addShortcut(env, java_url.obj(), java_user_title.obj(), | 166 Java_ShortcutHelper_addShortcut(env, java_url.obj(), java_user_title.obj(), |
194 java_bitmap.obj(), info.source); | 167 java_bitmap.obj(), info.source); |
195 } | 168 } |
196 | 169 |
| 170 void ShortcutHelper::OnBuiltWebApk(bool success) { |
| 171 if (success) { |
| 172 DVLOG(1) << "Sent request to install WebAPK. Seems to have worked."; |
| 173 } else { |
| 174 LOG(ERROR) << "WebAPK install failed."; |
| 175 } |
| 176 // TODO(pkotwicz): Figure out what to do when installing WebAPK fails. |
| 177 // (crbug.com/626950) |
| 178 } |
| 179 |
197 int ShortcutHelper::GetIdealHomescreenIconSizeInDp() { | 180 int ShortcutHelper::GetIdealHomescreenIconSizeInDp() { |
198 if (kIdealHomescreenIconSize == -1) | 181 if (kIdealHomescreenIconSize == -1) |
199 GetHomescreenIconAndSplashImageSizes(); | 182 GetHomescreenIconAndSplashImageSizes(); |
200 return kIdealHomescreenIconSize; | 183 return kIdealHomescreenIconSize; |
201 } | 184 } |
202 | 185 |
203 int ShortcutHelper::GetMinimumHomescreenIconSizeInDp() { | 186 int ShortcutHelper::GetMinimumHomescreenIconSizeInDp() { |
204 if (kMinimumHomescreenIconSize == -1) | 187 if (kMinimumHomescreenIconSize == -1) |
205 GetHomescreenIconAndSplashImageSizes(); | 188 GetHomescreenIconAndSplashImageSizes(); |
206 return kMinimumHomescreenIconSize; | 189 return kMinimumHomescreenIconSize; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 DCHECK(jsplash_image_callback); | 296 DCHECK(jsplash_image_callback); |
314 base::Closure* splash_image_callback = | 297 base::Closure* splash_image_callback = |
315 reinterpret_cast<base::Closure*>(jsplash_image_callback); | 298 reinterpret_cast<base::Closure*>(jsplash_image_callback); |
316 splash_image_callback->Run(); | 299 splash_image_callback->Run(); |
317 delete splash_image_callback; | 300 delete splash_image_callback; |
318 } | 301 } |
319 | 302 |
320 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { | 303 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { |
321 return RegisterNativesImpl(env); | 304 return RegisterNativesImpl(env); |
322 } | 305 } |
OLD | NEW |