OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/android/webapk/webapk_update_manager.h" |
| 6 |
| 7 #include <jni.h> |
| 8 |
| 9 #include "base/android/jni_string.h" |
| 10 #include "base/bind.h" |
| 11 #include "chrome/browser/android/webapk/webapk_installer.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/profiles/profile_manager.h" |
| 14 #include "content/public/browser/browser_context.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "jni/WebApkUpdateManager_jni.h" |
| 17 #include "third_party/skia/include/core/SkBitmap.h" |
| 18 #include "ui/gfx/android/java_bitmap.h" |
| 19 #include "url/gurl.h" |
| 20 |
| 21 namespace { |
| 22 void UpdateAsyncHelper(content::BrowserContext* browser_context, |
| 23 const ShortcutInfo& info, |
| 24 const SkBitmap& icon_bitmap, |
| 25 const std::string& webapk_package, |
| 26 int version) { |
| 27 WebApkInstaller* installer = new WebApkInstaller(info, icon_bitmap); |
| 28 installer->UpdateAsync(browser_context, |
| 29 base::Bind(&WebApkUpdateManager::OnBuiltWebApk, |
| 30 webapk_package), |
| 31 webapk_package, version); |
| 32 } |
| 33 } // anonymous namespace |
| 34 |
| 35 // static |
| 36 bool WebApkUpdateManager::Register(JNIEnv* env) { |
| 37 return RegisterNativesImpl(env); |
| 38 } |
| 39 |
| 40 // static |
| 41 void WebApkUpdateManager::OnBuiltWebApk(const std::string& webapk_package, |
| 42 bool success) { |
| 43 JNIEnv* env = base::android::AttachCurrentThread(); |
| 44 |
| 45 if (success) { |
| 46 LOG(WARNING) |
| 47 << "Sent request to update WebAPK to server. Seems to have worked."; |
| 48 } else { |
| 49 LOG(WARNING) << "Server request to update WebAPK failed."; |
| 50 } |
| 51 |
| 52 ScopedJavaLocalRef<jstring> java_webapk_package = |
| 53 base::android::ConvertUTF8ToJavaString(env, webapk_package); |
| 54 Java_WebApkUpdateManager_onBuiltWebApk( |
| 55 env, success, java_webapk_package.obj()); |
| 56 } |
| 57 |
| 58 // static JNI method. |
| 59 static void UpdateAsync( |
| 60 JNIEnv* env, |
| 61 const JavaParamRef<jclass>& clazz, |
| 62 const JavaParamRef<jstring>& java_webapk_package, |
| 63 const JavaParamRef<jstring>& java_url, |
| 64 const JavaParamRef<jstring>& java_scope, |
| 65 const JavaParamRef<jstring>& java_name, |
| 66 const JavaParamRef<jstring>& java_short_name, |
| 67 jint java_display_mode, |
| 68 jint java_orientation, |
| 69 jlong java_theme_color, |
| 70 jlong java_background_color, |
| 71 const JavaParamRef<jstring>& java_web_manifest_url, |
| 72 const JavaParamRef<jstring>& java_icon_url, |
| 73 const JavaParamRef<jobject>& java_icon_bitmap, |
| 74 jint java_version) { |
| 75 GURL url(ConvertJavaStringToUTF8(env, java_url)); |
| 76 GURL scope(ConvertJavaStringToUTF8(env, java_scope)); |
| 77 GURL web_manifest_url(ConvertJavaStringToUTF8(env, java_web_manifest_url)); |
| 78 GURL icon_url(ConvertJavaStringToUTF8(env, java_icon_url)); |
| 79 ShortcutInfo info(url); |
| 80 info.scope = scope; |
| 81 info.name = ConvertJavaStringToUTF16(env, java_name); |
| 82 info.short_name = ConvertJavaStringToUTF16(env, java_short_name); |
| 83 info.display = (blink::WebDisplayMode) java_display_mode; |
| 84 info.orientation = (blink::WebScreenOrientationLockType) java_orientation; |
| 85 info.theme_color = (long) java_theme_color; |
| 86 info.background_color = (long) java_background_color; |
| 87 info.icon_url = icon_url; |
| 88 |
| 89 gfx::JavaBitmap java_bitmap_lock(java_icon_bitmap); |
| 90 SkBitmap icon_bitmap = gfx::CreateSkBitmapFromJavaBitmap(java_bitmap_lock); |
| 91 icon_bitmap.setImmutable(); |
| 92 |
| 93 std::string webapk_package; |
| 94 ConvertJavaStringToUTF8(env, java_webapk_package, &webapk_package); |
| 95 |
| 96 Profile* profile = ProfileManager::GetLastUsedProfile(); |
| 97 if (profile == nullptr) { |
| 98 NOTREACHED() << "Profile not found."; |
| 99 return; |
| 100 } |
| 101 content::BrowserThread::PostTask( |
| 102 content::BrowserThread::IO, FROM_HERE, |
| 103 base::Bind(&UpdateAsyncHelper, |
| 104 static_cast<content::BrowserContext*>(profile), |
| 105 info, icon_bitmap, webapk_package, java_version)); |
| 106 } |
OLD | NEW |