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_builder.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 content::Manifest; | 27 using content::Manifest; |
(...skipping 28 matching lines...) Expand all Loading... |
55 | 56 |
56 // Try to ensure that the data returned is sane. | 57 // Try to ensure that the data returned is sane. |
57 DCHECK(kMinimumHomescreenIconSize <= kIdealHomescreenIconSize); | 58 DCHECK(kMinimumHomescreenIconSize <= kIdealHomescreenIconSize); |
58 DCHECK(kMinimumSplashImageSize <= kIdealSplashImageSize); | 59 DCHECK(kMinimumSplashImageSize <= kIdealSplashImageSize); |
59 } | 60 } |
60 | 61 |
61 } // anonymous namespace | 62 } // anonymous namespace |
62 | 63 |
63 // static | 64 // static |
64 void ShortcutHelper::AddShortcutInBackgroundWithSkBitmap( | 65 void ShortcutHelper::AddShortcutInBackgroundWithSkBitmap( |
| 66 content::BrowserContext* browser_context, |
65 const ShortcutInfo& info, | 67 const ShortcutInfo& info, |
66 const std::string& webapp_id, | 68 const std::string& webapp_id, |
67 const SkBitmap& icon_bitmap, | 69 const SkBitmap& icon_bitmap, |
68 const base::Closure& splash_image_callback) { | 70 const base::Closure& splash_image_callback) { |
69 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 71 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
70 | 72 |
71 if (info.display == blink::WebDisplayModeStandalone || | 73 if (info.display == blink::WebDisplayModeStandalone || |
72 info.display == blink::WebDisplayModeFullscreen) { | 74 info.display == blink::WebDisplayModeFullscreen) { |
73 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 75 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
74 switches::kEnableWebApk)) { | 76 switches::kEnableWebApk)) { |
75 AddWebApkInBackgroundWithSkBitmap(info, webapp_id, icon_bitmap); | 77 AddWebApkInBackgroundWithSkBitmap(browser_context, info, icon_bitmap); |
76 return; | 78 return; |
77 } | 79 } |
78 AddWebappInBackgroundWithSkBitmap(info, webapp_id, icon_bitmap, | 80 AddWebappInBackgroundWithSkBitmap(info, webapp_id, icon_bitmap, |
79 splash_image_callback); | 81 splash_image_callback); |
80 return; | 82 return; |
81 } | 83 } |
82 AddBookmarkShortcutInBackgroundWithSkBitmap(info, icon_bitmap); | 84 AddBookmarkShortcutInBackgroundWithSkBitmap(info, icon_bitmap); |
83 } | 85 } |
84 | 86 |
85 // static | 87 // static |
86 void ShortcutHelper::AddWebApkInBackgroundWithSkBitmap( | 88 void ShortcutHelper::AddWebApkInBackgroundWithSkBitmap( |
| 89 content::BrowserContext* browser_context, |
87 const ShortcutInfo& info, | 90 const ShortcutInfo& info, |
88 const std::string& webapp_id, | |
89 const SkBitmap& icon_bitmap) { | 91 const SkBitmap& icon_bitmap) { |
90 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 92 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
91 | 93 // WebApkBuilder destroys itself when it is done. |
92 // TODO(pkotwicz): Send request to WebAPK server to generate WebAPK. | 94 WebApkBuilder* builder = |
93 | 95 new WebApkBuilder(browser_context, info, icon_bitmap); |
94 JNIEnv* env = base::android::AttachCurrentThread(); | 96 builder->BuildAsync(base::Bind(&ShortcutHelper::OnBuiltWebApk)); |
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 } | 97 } |
125 | 98 |
126 // static | 99 // static |
127 void ShortcutHelper::AddWebappInBackgroundWithSkBitmap( | 100 void ShortcutHelper::AddWebappInBackgroundWithSkBitmap( |
128 const ShortcutInfo& info, | 101 const ShortcutInfo& info, |
129 const std::string& webapp_id, | 102 const std::string& webapp_id, |
130 const SkBitmap& icon_bitmap, | 103 const SkBitmap& icon_bitmap, |
131 const base::Closure& splash_image_callback) { | 104 const base::Closure& splash_image_callback) { |
132 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | 105 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
133 | 106 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
186 base::android::ConvertUTF16ToJavaString(env, info.user_title); | 159 base::android::ConvertUTF16ToJavaString(env, info.user_title); |
187 ScopedJavaLocalRef<jobject> java_bitmap; | 160 ScopedJavaLocalRef<jobject> java_bitmap; |
188 if (icon_bitmap.getSize()) | 161 if (icon_bitmap.getSize()) |
189 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); | 162 java_bitmap = gfx::ConvertToJavaBitmap(&icon_bitmap); |
190 | 163 |
191 Java_ShortcutHelper_addBookmarkShortcut(env, java_url.obj(), | 164 Java_ShortcutHelper_addBookmarkShortcut(env, java_url.obj(), |
192 java_user_title.obj(), | 165 java_user_title.obj(), |
193 java_bitmap.obj(), info.source); | 166 java_bitmap.obj(), info.source); |
194 } | 167 } |
195 | 168 |
| 169 void ShortcutHelper::OnBuiltWebApk(bool success) { |
| 170 if (success) { |
| 171 LOG(ERROR) |
| 172 << "Sent request to create WebAPK to server. Seems to have worked."; |
| 173 } else { |
| 174 LOG(ERROR) << "Server request to create WebAPK failed."; |
| 175 } |
| 176 // TODO(pkotwicz): Figure out what to do when building WebAPK fails. |
| 177 // (crbug.com/626950) |
| 178 } |
| 179 |
196 int ShortcutHelper::GetIdealHomescreenIconSizeInDp() { | 180 int ShortcutHelper::GetIdealHomescreenIconSizeInDp() { |
197 if (kIdealHomescreenIconSize == -1) | 181 if (kIdealHomescreenIconSize == -1) |
198 GetHomescreenIconAndSplashImageSizes(); | 182 GetHomescreenIconAndSplashImageSizes(); |
199 return kIdealHomescreenIconSize; | 183 return kIdealHomescreenIconSize; |
200 } | 184 } |
201 | 185 |
202 int ShortcutHelper::GetMinimumHomescreenIconSizeInDp() { | 186 int ShortcutHelper::GetMinimumHomescreenIconSizeInDp() { |
203 if (kMinimumHomescreenIconSize == -1) | 187 if (kMinimumHomescreenIconSize == -1) |
204 GetHomescreenIconAndSplashImageSizes(); | 188 GetHomescreenIconAndSplashImageSizes(); |
205 return kMinimumHomescreenIconSize; | 189 return kMinimumHomescreenIconSize; |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 DCHECK(jsplash_image_callback); | 294 DCHECK(jsplash_image_callback); |
311 base::Closure* splash_image_callback = | 295 base::Closure* splash_image_callback = |
312 reinterpret_cast<base::Closure*>(jsplash_image_callback); | 296 reinterpret_cast<base::Closure*>(jsplash_image_callback); |
313 splash_image_callback->Run(); | 297 splash_image_callback->Run(); |
314 delete splash_image_callback; | 298 delete splash_image_callback; |
315 } | 299 } |
316 | 300 |
317 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { | 301 bool ShortcutHelper::RegisterShortcutHelper(JNIEnv* env) { |
318 return RegisterNativesImpl(env); | 302 return RegisterNativesImpl(env); |
319 } | 303 } |
OLD | NEW |