Commit e33136ac authored by Evan Stade's avatar Evan Stade Committed by Chromium LUCI CQ

Remove some Chrome dependencies from AddToHomescreenInstaller.

Bug: 1164069
Change-Id: I0abf13d932f72f3ed2b3f387afe3edc5578c9c89
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2637930
Commit-Queue: Evan Stade <estade@chromium.org>
Reviewed-by: default avatarDominick Ng <dominickn@chromium.org>
Cr-Commit-Position: refs/heads/master@{#846139}
parent ca0b40a8
......@@ -13,8 +13,8 @@ import org.chromium.base.Log;
import org.chromium.base.PackageUtils;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.chrome.browser.banners.AppData;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TabUtils;
import org.chromium.content_public.browser.WebContents;
import org.chromium.ui.base.WindowAndroid;
/**
* Provides functionality related to native Android apps for its C++ counterpart,
......@@ -24,7 +24,7 @@ class AddToHomescreenInstaller {
private static final String TAG = "AddToHomescreen";
@CalledByNative
private static boolean installOrOpenNativeApp(Tab tab, AppData appData) {
private static boolean installOrOpenNativeApp(WebContents webContents, AppData appData) {
Context context = ContextUtils.getApplicationContext();
Intent launchIntent;
if (PackageUtils.isPackageInstalled(context, appData.packageName())) {
......@@ -33,14 +33,20 @@ class AddToHomescreenInstaller {
} else {
launchIntent = appData.installIntent();
}
if (launchIntent != null && TabUtils.getActivity(tab) != null) {
try {
TabUtils.getActivity(tab).startActivity(launchIntent);
} catch (ActivityNotFoundException e) {
Log.e(TAG, "Failed to install or open app : %s!", appData.packageName(), e);
return false;
if (launchIntent != null) {
WindowAndroid window = webContents.getTopLevelNativeWindow();
Context intentLauncher = window == null ? null : window.getActivity().get();
if (intentLauncher != null) {
try {
intentLauncher.startActivity(launchIntent);
} catch (ActivityNotFoundException e) {
Log.e(TAG, "Failed to install or open app : %s!", appData.packageName(), e);
return false;
}
}
}
return true;
}
}
......@@ -8,7 +8,6 @@
#include "base/callback.h"
#include "chrome/android/chrome_jni_headers/AddToHomescreenInstaller_jni.h"
#include "chrome/browser/android/tab_android.h"
#include "components/webapps/browser/webapps_client.h"
#include "content/public/browser/web_contents.h"
......@@ -47,15 +46,9 @@ void AddToHomescreenInstaller::InstallOrOpenNativeApp(
const base::RepeatingCallback<void(Event, const AddToHomescreenParams&)>&
event_callback) {
JNIEnv* env = base::android::AttachCurrentThread();
TabAndroid* tab = TabAndroid::FromWebContents(web_contents);
if (tab == nullptr) {
event_callback.Run(Event::INSTALL_FAILED, params);
return;
}
bool was_successful = Java_AddToHomescreenInstaller_installOrOpenNativeApp(
env, tab->GetJavaObject(), params.native_app_data);
env, web_contents->GetJavaWebContents(), params.native_app_data);
event_callback.Run(was_successful ? Event::NATIVE_INSTALL_OR_OPEN_SUCCEEDED
: Event::NATIVE_INSTALL_OR_OPEN_FAILED,
params);
......
......@@ -45,6 +45,8 @@ class AddToHomescreenInstaller {
const AddToHomescreenParams& params,
const base::RepeatingCallback<void(Event, const AddToHomescreenParams&)>&
event_callback);
static void InstallShortcut(content::WebContents* web_contents,
const AddToHomescreenParams& params);
static void InstallWebApk(content::WebContents* web_contents,
const AddToHomescreenParams& params);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment