Index: chrome/browser/android/webapps/add_to_homescreen_process.cc |
diff --git a/chrome/browser/android/webapps/add_to_homescreen_dialog_helper.cc b/chrome/browser/android/webapps/add_to_homescreen_process.cc |
similarity index 47% |
rename from chrome/browser/android/webapps/add_to_homescreen_dialog_helper.cc |
rename to chrome/browser/android/webapps/add_to_homescreen_process.cc |
index 465ec4c7f6cb5f07ab1a9758f20b8c083657c170..3f714a8700e0f86cf614a5e702c07d2a811c9620 100644 |
--- a/chrome/browser/android/webapps/add_to_homescreen_dialog_helper.cc |
+++ b/chrome/browser/android/webapps/add_to_homescreen_process.cc |
@@ -2,94 +2,54 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-#include "chrome/browser/android/webapps/add_to_homescreen_dialog_helper.h" |
+#include "chrome/browser/android/webapps/add_to_homescreen_process.h" |
#include "base/android/jni_android.h" |
#include "base/android/jni_string.h" |
+#include "base/command_line.h" |
#include "base/guid.h" |
#include "base/location.h" |
#include "base/strings/string16.h" |
#include "base/strings/utf_string_conversions.h" |
#include "chrome/browser/android/shortcut_helper.h" |
#include "chrome/browser/banners/app_banner_settings_helper.h" |
+#include "chrome/browser/installable/installable_manager.h" |
+#include "chrome/common/chrome_switches.h" |
#include "content/public/browser/browser_thread.h" |
#include "content/public/browser/web_contents.h" |
-#include "jni/AddToHomescreenDialogHelper_jni.h" |
+#include "jni/AddToHomescreenProcess_jni.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
#include "ui/gfx/android/java_bitmap.h" |
using base::android::JavaParamRef; |
using base::android::ScopedJavaLocalRef; |
-jlong Initialize(JNIEnv* env, |
- const JavaParamRef<jobject>& obj, |
- const JavaParamRef<jobject>& java_web_contents) { |
+jlong InitializeAndStart(JNIEnv* env, |
+ const JavaParamRef<jobject>& obj, |
+ const JavaParamRef<jobject>& java_web_contents) { |
content::WebContents* web_contents = |
content::WebContents::FromJavaWebContents(java_web_contents); |
- AddToHomescreenDialogHelper* add_to_homescreen_helper = |
- new AddToHomescreenDialogHelper(env, obj, web_contents); |
- return reinterpret_cast<intptr_t>(add_to_homescreen_helper); |
+ AddToHomescreenProcess* process = new AddToHomescreenProcess(env, obj); |
+ process->Start(web_contents); |
+ return reinterpret_cast<intptr_t>(process); |
} |
-AddToHomescreenDialogHelper::AddToHomescreenDialogHelper( |
- JNIEnv* env, |
- jobject obj, |
- content::WebContents* web_contents) |
- : add_shortcut_pending_(false), |
- data_fetcher_(new AddToHomescreenDataFetcher(web_contents, |
- ShortcutHelper::GetIdealHomescreenIconSizeInDp(), |
- ShortcutHelper::GetMinimumHomescreenIconSizeInDp(), |
- ShortcutHelper::GetIdealSplashImageSizeInDp(), |
- ShortcutHelper::GetMinimumSplashImageSizeInDp(), |
- this)) { |
+AddToHomescreenProcess::AddToHomescreenProcess(JNIEnv* env, jobject obj) |
+ : add_shortcut_pending_(false), weak_ptr_factory_(this) { |
java_ref_.Reset(env, obj); |
} |
-AddToHomescreenDialogHelper::~AddToHomescreenDialogHelper() { |
- data_fetcher_->set_weak_observer(nullptr); |
- data_fetcher_ = nullptr; |
-} |
- |
-void AddToHomescreenDialogHelper::OnUserTitleAvailable( |
- const base::string16& user_title) { |
- JNIEnv* env = base::android::AttachCurrentThread(); |
- ScopedJavaLocalRef<jstring> j_user_title = |
- base::android::ConvertUTF16ToJavaString(env, user_title); |
- Java_AddToHomescreenDialogHelper_onUserTitleAvailable(env, |
- java_ref_.obj(), |
- j_user_title.obj()); |
-} |
- |
-void AddToHomescreenDialogHelper::OnDataAvailable(const ShortcutInfo& info, |
- const SkBitmap& icon) { |
- JNIEnv* env = base::android::AttachCurrentThread(); |
- ScopedJavaLocalRef<jobject> java_bitmap; |
- if (icon.getSize()) |
- java_bitmap = gfx::ConvertToJavaBitmap(&icon); |
- |
- Java_AddToHomescreenDialogHelper_onIconAvailable(env, |
- java_ref_.obj(), |
- java_bitmap.obj()); |
- |
- if (add_shortcut_pending_) |
- AddShortcut(info, icon); |
+// static |
+bool AddToHomescreenProcess::Register(JNIEnv* env) { |
+ return RegisterNativesImpl(env); |
} |
-void AddToHomescreenDialogHelper::Destroy(JNIEnv* env, |
- const JavaParamRef<jobject>& obj) { |
+void AddToHomescreenProcess::Destroy(JNIEnv* env, |
+ const JavaParamRef<jobject>& obj) { |
delete this; |
} |
-SkBitmap AddToHomescreenDialogHelper::FinalizeLauncherIconInBackground( |
- const SkBitmap& bitmap, |
- const GURL& url, |
- bool* is_generated) { |
- DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
- |
- return ShortcutHelper::FinalizeLauncherIconInBackground(bitmap, url, |
- is_generated); |
-} |
- |
-void AddToHomescreenDialogHelper::AddShortcut( |
+void AddToHomescreenProcess::AddShortcut( |
JNIEnv* env, |
const JavaParamRef<jobject>& obj, |
const JavaParamRef<jstring>& j_user_title) { |
@@ -107,8 +67,61 @@ void AddToHomescreenDialogHelper::AddShortcut( |
} |
} |
-void AddToHomescreenDialogHelper::AddShortcut(const ShortcutInfo& info, |
- const SkBitmap& icon) { |
+void AddToHomescreenProcess::Start(content::WebContents* web_contents) { |
+ if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
+ switches::kEnableWebApk)) { |
+ CheckWebApkCompatible(web_contents); |
+ return; |
+ } |
+ ShowDialog(); |
+ StartFetchingInfoForShortcut(web_contents); |
+} |
+ |
+AddToHomescreenProcess::~AddToHomescreenProcess() { |
+ if (data_fetcher_) { |
+ data_fetcher_->set_weak_observer(nullptr); |
+ data_fetcher_ = nullptr; |
+ } |
+} |
+ |
+void AddToHomescreenProcess::CheckWebApkCompatible( |
+ content::WebContents* web_contents) { |
+ InstallableManager::CreateForWebContents(web_contents); |
+ InstallableManager* installable_manager = |
+ InstallableManager::FromWebContents(web_contents); |
+ InstallableParams params; |
+ params.check_installable = true; |
+ params.fetch_valid_icon = false; |
+ installable_manager->GetData( |
+ params, base::Bind(&AddToHomescreenProcess::OnGotWebApkCompatibilityData, |
+ weak_ptr_factory_.GetWeakPtr(), web_contents)); |
+} |
+ |
+void AddToHomescreenProcess::OnGotWebApkCompatibilityData( |
+ content::WebContents* web_contents, |
+ const InstallableData& installable_data) { |
+ // TODO(pkotwicz): Select whether to use the dialog or not based on |
+ // |installable_data.is_installable|. |
+ ShowDialog(); |
+ StartFetchingInfoForShortcut(web_contents); |
+} |
+ |
+void AddToHomescreenProcess::ShowDialog() { |
+ JNIEnv* env = base::android::AttachCurrentThread(); |
+ Java_AddToHomescreenProcess_showDialog(env, java_ref_.obj()); |
+} |
+ |
+void AddToHomescreenProcess::StartFetchingInfoForShortcut( |
+ content::WebContents* web_contents) { |
+ data_fetcher_ = new AddToHomescreenDataFetcher( |
+ web_contents, ShortcutHelper::GetIdealHomescreenIconSizeInDp(), |
+ ShortcutHelper::GetMinimumHomescreenIconSizeInDp(), |
+ ShortcutHelper::GetIdealSplashImageSizeInDp(), |
+ ShortcutHelper::GetMinimumSplashImageSizeInDp(), this); |
+} |
+ |
+void AddToHomescreenProcess::AddShortcut(const ShortcutInfo& info, |
+ const SkBitmap& icon) { |
DCHECK(add_shortcut_pending_); |
if (!add_shortcut_pending_) |
return; |
@@ -126,12 +139,7 @@ void AddToHomescreenDialogHelper::AddShortcut(const ShortcutInfo& info, |
data_fetcher_->FetchSplashScreenImageCallback(uid)); |
} |
-bool AddToHomescreenDialogHelper::RegisterAddToHomescreenDialogHelper( |
- JNIEnv* env) { |
- return RegisterNativesImpl(env); |
-} |
- |
-void AddToHomescreenDialogHelper::RecordAddToHomescreen() { |
+void AddToHomescreenProcess::RecordAddToHomescreen() { |
// Record that the shortcut has been added, so no banners will be shown |
// for this app. |
content::WebContents* web_contents = data_fetcher_->web_contents(); |
@@ -144,3 +152,37 @@ void AddToHomescreenDialogHelper::RecordAddToHomescreen() { |
AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, |
base::Time::Now()); |
} |
+ |
+void AddToHomescreenProcess::OnUserTitleAvailable( |
+ const base::string16& user_title) { |
+ JNIEnv* env = base::android::AttachCurrentThread(); |
+ ScopedJavaLocalRef<jstring> j_user_title = |
+ base::android::ConvertUTF16ToJavaString(env, user_title); |
+ Java_AddToHomescreenProcess_onUserTitleAvailable(env, |
+ java_ref_.obj(), |
Xi Han
2016/08/19 14:11:06
It seems ".obj()" has been removed in my recently
|
+ j_user_title.obj()); |
+} |
+ |
+void AddToHomescreenProcess::OnDataAvailable(const ShortcutInfo& info, |
+ const SkBitmap& icon) { |
Xi Han
2016/08/19 14:11:06
The indent of "const SkBitmap& icon" is wrong, ple
|
+ JNIEnv* env = base::android::AttachCurrentThread(); |
+ ScopedJavaLocalRef<jobject> java_bitmap; |
+ if (icon.getSize()) |
+ java_bitmap = gfx::ConvertToJavaBitmap(&icon); |
+ |
+ Java_AddToHomescreenProcess_onReadyToAdd(env, java_ref_.obj(), |
+ java_bitmap.obj()); |
+ |
+ if (add_shortcut_pending_) |
+ AddShortcut(info, icon); |
+} |
+ |
+SkBitmap AddToHomescreenProcess::FinalizeLauncherIconInBackground( |
+ const SkBitmap& bitmap, |
+ const GURL& url, |
+ bool* is_generated) { |
+ DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); |
+ |
+ return ShortcutHelper::FinalizeLauncherIconInBackground(bitmap, url, |
+ is_generated); |
+} |