Chromium Code Reviews| Index: chrome/browser/android/webapk/webapk_update_manager.cc |
| diff --git a/chrome/browser/android/webapk/webapk_update_manager.cc b/chrome/browser/android/webapk/webapk_update_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ff46893238b34d087f81267c65e98ffba92a7655 |
| --- /dev/null |
| +++ b/chrome/browser/android/webapk/webapk_update_manager.cc |
| @@ -0,0 +1,116 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/android/webapk/webapk_update_manager.h" |
| + |
| +#include <jni.h> |
| + |
| +#include "base/android/jni_string.h" |
| +#include "base/bind.h" |
| +#include "chrome/browser/android/webapk/webapk_installer.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "jni/WebApkUpdateManager_jni.h" |
| +#include "third_party/skia/include/core/SkBitmap.h" |
| +#include "ui/gfx/android/java_bitmap.h" |
| +#include "url/gurl.h" |
| + |
| +namespace { |
| +void UpdateAsyncHelper(content::BrowserContext* browser_context, |
| + const ShortcutInfo& info, |
| + const SkBitmap& icon_bitmap, |
| + const std::string& webapk_package, |
| + int version) { |
| + WebApkInstaller* installer = new WebApkInstaller(info, icon_bitmap); |
| + installer->UpdateAsync(browser_context, |
| + base::Bind(&WebApkUpdateManager::OnBuiltWebApk, |
| + webapk_package), |
| + webapk_package, version); |
| +} |
| + |
| +void GetProfileOnUIThread(const ShortcutInfo& info, |
| + const SkBitmap& icon_bitmap, |
| + const std::string& webapk_package, |
| + int version) { |
| + Profile* profile = ProfileManager::GetLastUsedProfile(); |
| + if (profile == nullptr) { |
| + NOTREACHED() << "Profile not found."; |
| + return; |
| + } |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::IO, FROM_HERE, |
| + base::Bind(&UpdateAsyncHelper, |
| + static_cast<content::BrowserContext*>(profile), |
| + info, icon_bitmap, webapk_package, version)); |
| +} |
| +} // anonymous namespace |
| + |
| +// static |
| +bool WebApkUpdateManager::Register(JNIEnv* env) { |
| + return RegisterNativesImpl(env); |
| +} |
| + |
| +// static |
| +void WebApkUpdateManager::OnBuiltWebApk(const std::string& webapk_package, |
| + bool success) { |
| + JNIEnv* env = base::android::AttachCurrentThread(); |
| + |
| + if (success) { |
| + LOG(WARNING) |
| + << "Sent request to update WebAPK to server. Seems to have worked."; |
|
pkotwicz
2016/08/08 18:59:16
Nit: Use DVLOG(1) for the success case.
DVLOG(1)
Xi Han
2016/08/08 21:25:05
That makes sense, we shouldn't log warning for the
|
| + } else { |
| + LOG(WARNING) << "Server request to update WebAPK failed."; |
| + } |
| + |
| + ScopedJavaLocalRef<jstring> java_webapk_package = |
| + base::android::ConvertUTF8ToJavaString(env, webapk_package); |
| + Java_WebApkUpdateManager_onBuiltWebApk( |
| + env, success, java_webapk_package.obj()); |
| +} |
| + |
| +// static JNI method. |
| +static void UpdateAsync( |
|
pkotwicz
2016/08/08 18:59:16
Nit: I have been trying to follow the ordering of
Xi Han
2016/08/08 21:25:05
Done.
|
| + JNIEnv* env, |
| + const JavaParamRef<jclass>& clazz, |
| + const JavaParamRef<jstring>& java_webapk_package, |
|
pkotwicz
2016/08/08 18:59:16
Nit: Keep |java_webapk_package| and |java_webapk_v
Xi Han
2016/08/08 21:25:06
Done.
|
| + const JavaParamRef<jstring>& java_url, |
|
pkotwicz
2016/08/08 18:59:16
Nit: java_url -> java_start_url
Xi Han
2016/08/08 21:25:05
Done.
|
| + const JavaParamRef<jstring>& java_scope, |
| + const JavaParamRef<jstring>& java_name, |
| + const JavaParamRef<jstring>& java_short_name, |
| + jint java_display_mode, |
| + jint java_orientation, |
| + jlong java_theme_color, |
| + jlong java_background_color, |
| + const JavaParamRef<jstring>& java_web_manifest_url, |
| + const JavaParamRef<jstring>& java_icon_url, |
| + const JavaParamRef<jobject>& java_icon_bitmap, |
| + jint java_version) { |
| + GURL url(ConvertJavaStringToUTF8(env, java_url)); |
| + GURL scope(ConvertJavaStringToUTF8(env, java_scope)); |
| + GURL web_manifest_url(ConvertJavaStringToUTF8(env, java_web_manifest_url)); |
|
pkotwicz
2016/08/08 18:59:16
|web_manifest_url| is unused?
Xi Han
2016/08/08 21:25:05
Good catch, add it to |info|.
|
| + GURL icon_url(ConvertJavaStringToUTF8(env, java_icon_url)); |
| + ShortcutInfo info(url); |
| + info.scope = scope; |
| + info.name = ConvertJavaStringToUTF16(env, java_name); |
| + info.short_name = ConvertJavaStringToUTF16(env, java_short_name); |
| + info.display = (blink::WebDisplayMode) java_display_mode; |
|
pkotwicz
2016/08/08 18:59:16
Nit: Use static_cast<blink::WebDisplayMode> (Or st
Xi Han
2016/08/08 21:25:06
static_cast<blink::WebDisplayMode> works, updated.
|
| + info.orientation = (blink::WebScreenOrientationLockType) java_orientation; |
| + info.theme_color = (long) java_theme_color; |
| + info.background_color = (long) java_background_color; |
| + info.icon_url = icon_url; |
| + |
| + gfx::JavaBitmap java_bitmap_lock(java_icon_bitmap); |
| + SkBitmap icon_bitmap = gfx::CreateSkBitmapFromJavaBitmap(java_bitmap_lock); |
| + icon_bitmap.setImmutable(); |
| + |
| + std::string webapk_package; |
| + ConvertJavaStringToUTF8(env, java_webapk_package, &webapk_package); |
| + |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&GetProfileOnUIThread, |
| + info, icon_bitmap, webapk_package, java_version)); |
| +} |