Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(182)

Side by Side Diff: chrome/browser/android/webapps/add_to_homescreen_manager.cc

Issue 2244223002: Determine whether to show "Add to Homescreen" dialog or WebAPK infobar (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Merge branch 'master' into webapk_dialog_detector Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/webapps/add_to_homescreen_dialog_helper.h" 5 #include "chrome/browser/android/webapps/add_to_homescreen_manager.h"
6 6
7 #include "base/android/jni_android.h" 7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h" 8 #include "base/android/jni_string.h"
9 #include "base/guid.h" 9 #include "base/guid.h"
10 #include "base/location.h" 10 #include "base/location.h"
11 #include "base/strings/string16.h" 11 #include "base/strings/string16.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/android/shortcut_helper.h" 13 #include "chrome/browser/android/shortcut_helper.h"
14 #include "chrome/browser/android/webapk/chrome_webapk_host.h"
14 #include "chrome/browser/banners/app_banner_settings_helper.h" 15 #include "chrome/browser/banners/app_banner_settings_helper.h"
16 #include "chrome/browser/installable/installable_manager.h"
15 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
16 #include "content/public/browser/web_contents.h" 18 #include "content/public/browser/web_contents.h"
17 #include "jni/AddToHomescreenDialogHelper_jni.h" 19 #include "content/public/common/origin_util.h"
20 #include "jni/AddToHomescreenManager_jni.h"
21 #include "third_party/skia/include/core/SkBitmap.h"
18 #include "ui/gfx/android/java_bitmap.h" 22 #include "ui/gfx/android/java_bitmap.h"
19 23
20 using base::android::JavaParamRef; 24 using base::android::JavaParamRef;
21 using base::android::ScopedJavaLocalRef; 25 using base::android::ScopedJavaLocalRef;
22 26
23 jlong Initialize(JNIEnv* env, 27 jlong InitializeAndStart(JNIEnv* env,
24 const JavaParamRef<jobject>& obj, 28 const JavaParamRef<jobject>& obj,
25 const JavaParamRef<jobject>& java_web_contents) { 29 const JavaParamRef<jobject>& java_web_contents) {
26 content::WebContents* web_contents = 30 content::WebContents* web_contents =
27 content::WebContents::FromJavaWebContents(java_web_contents); 31 content::WebContents::FromJavaWebContents(java_web_contents);
28 AddToHomescreenDialogHelper* add_to_homescreen_helper = 32 AddToHomescreenManager* manager = new AddToHomescreenManager(env, obj);
29 new AddToHomescreenDialogHelper(env, obj, web_contents); 33 manager->Start(web_contents);
30 return reinterpret_cast<intptr_t>(add_to_homescreen_helper); 34 return reinterpret_cast<intptr_t>(manager);
35 }
36
37 AddToHomescreenManager::AddToHomescreenManager(JNIEnv* env, jobject obj)
38 : add_shortcut_pending_(false), weak_ptr_factory_(this) {
39 java_ref_.Reset(env, obj);
31 } 40 }
32 41
33 // static 42 // static
34 bool AddToHomescreenDialogHelper::RegisterAddToHomescreenDialogHelper( 43 bool AddToHomescreenManager::Register(JNIEnv* env) {
35 JNIEnv* env) {
36 return RegisterNativesImpl(env); 44 return RegisterNativesImpl(env);
37 } 45 }
38 46
39 AddToHomescreenDialogHelper::AddToHomescreenDialogHelper( 47 void AddToHomescreenManager::Destroy(JNIEnv* env,
40 JNIEnv* env, 48 const JavaParamRef<jobject>& obj) {
41 jobject obj,
42 content::WebContents* web_contents)
43 : add_shortcut_pending_(false),
44 data_fetcher_(new AddToHomescreenDataFetcher(web_contents,
45 ShortcutHelper::GetIdealHomescreenIconSizeInDp(),
46 ShortcutHelper::GetMinimumHomescreenIconSizeInDp(),
47 ShortcutHelper::GetIdealSplashImageSizeInDp(),
48 ShortcutHelper::GetMinimumSplashImageSizeInDp(),
49 this)) {
50 java_ref_.Reset(env, obj);
51 }
52
53 void AddToHomescreenDialogHelper::Destroy(JNIEnv* env,
54 const JavaParamRef<jobject>& obj) {
55 delete this; 49 delete this;
56 } 50 }
57 51
58 void AddToHomescreenDialogHelper::AddShortcut( 52 void AddToHomescreenManager::AddShortcut(
59 JNIEnv* env, 53 JNIEnv* env,
60 const JavaParamRef<jobject>& obj, 54 const JavaParamRef<jobject>& obj,
61 const JavaParamRef<jstring>& j_user_title) { 55 const JavaParamRef<jstring>& j_user_title) {
62 add_shortcut_pending_ = true; 56 add_shortcut_pending_ = true;
63 57
64 base::string16 user_title = 58 base::string16 user_title =
65 base::android::ConvertJavaStringToUTF16(env, j_user_title); 59 base::android::ConvertJavaStringToUTF16(env, j_user_title);
66 if (!user_title.empty()) 60 if (!user_title.empty())
67 data_fetcher_->shortcut_info().user_title = user_title; 61 data_fetcher_->shortcut_info().user_title = user_title;
68 62
69 if (data_fetcher_->is_ready()) { 63 if (data_fetcher_->is_ready()) {
70 // If the fetcher isn't ready yet, the shortcut will be added when it is 64 // If the fetcher isn't ready yet, the shortcut will be added when it is
71 // via OnDataAvailable(); 65 // via OnDataAvailable();
72 AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->shortcut_icon()); 66 AddShortcut(data_fetcher_->shortcut_info(), data_fetcher_->shortcut_icon());
73 } 67 }
74 } 68 }
75 69
76 AddToHomescreenDialogHelper::~AddToHomescreenDialogHelper() { 70 void AddToHomescreenManager::Start(content::WebContents* web_contents) {
77 data_fetcher_->set_weak_observer(nullptr); 71 if (ChromeWebApkHost::AreWebApkEnabled()) {
78 data_fetcher_ = nullptr; 72 CheckWebApkCompatible(web_contents);
73 return;
74 }
75 OnDeterminedWebApkCompatibility(web_contents, false);
79 } 76 }
80 77
81 void AddToHomescreenDialogHelper::AddShortcut(const ShortcutInfo& info, 78 AddToHomescreenManager::~AddToHomescreenManager() {
82 const SkBitmap& icon) { 79 if (data_fetcher_) {
80 data_fetcher_->set_weak_observer(nullptr);
81 data_fetcher_ = nullptr;
82 }
83 }
84
85 void AddToHomescreenManager::CheckWebApkCompatible(
86 content::WebContents* web_contents) {
87 // A secure origin is required to create a WebAPK, so exit early if the origin
88 // is not secure.
89 if (!content::IsOriginSecure(web_contents->GetLastCommittedURL())) {
90 OnDeterminedWebApkCompatibility(web_contents, false);
91 return;
92 }
93
94 InstallableManager::CreateForWebContents(web_contents);
95 InstallableManager* installable_manager =
96 InstallableManager::FromWebContents(web_contents);
97 InstallableParams params;
98 params.check_installable = true;
99 params.fetch_valid_icon = false;
100 installable_manager->GetData(
dominickn 2016/08/29 00:13:37 This really should be in the data fetcher, not in
101 params, base::Bind(&AddToHomescreenManager::OnGotWebApkCompatibilityData,
102 weak_ptr_factory_.GetWeakPtr(), web_contents));
103 }
104
105 void AddToHomescreenManager::OnGotWebApkCompatibilityData(
dominickn 2016/08/29 00:13:37 Nit: Please name this OnDidGetInstallableData (and
106 content::WebContents* web_contents,
107 const InstallableData& installable_data) {
108 OnDeterminedWebApkCompatibility(web_contents,
109 installable_data.is_installable);
110 }
111
112 void AddToHomescreenManager::OnDeterminedWebApkCompatibility(
113 content::WebContents* web_contents,
114 bool is_compatible) {
115 // TODO(pkotwicz): Select whether to use dialog or not based on
116 // |is_compatible|.
117 ShowDialog();
118 StartFetchingInfoForShortcut(web_contents);
119 }
120
121 void AddToHomescreenManager::ShowDialog() {
122 JNIEnv* env = base::android::AttachCurrentThread();
123 Java_AddToHomescreenManager_showDialog(env, java_ref_);
124 }
125
126 void AddToHomescreenManager::StartFetchingInfoForShortcut(
127 content::WebContents* web_contents) {
128 data_fetcher_ = new AddToHomescreenDataFetcher(
129 web_contents, ShortcutHelper::GetIdealHomescreenIconSizeInDp(),
130 ShortcutHelper::GetMinimumHomescreenIconSizeInDp(),
131 ShortcutHelper::GetIdealSplashImageSizeInDp(),
132 ShortcutHelper::GetMinimumSplashImageSizeInDp(), this);
133 }
134
135 void AddToHomescreenManager::AddShortcut(const ShortcutInfo& info,
136 const SkBitmap& icon) {
83 DCHECK(add_shortcut_pending_); 137 DCHECK(add_shortcut_pending_);
84 if (!add_shortcut_pending_) 138 if (!add_shortcut_pending_)
85 return; 139 return;
86 add_shortcut_pending_ = false; 140 add_shortcut_pending_ = false;
87 141
88 content::WebContents* web_contents = data_fetcher_->web_contents(); 142 content::WebContents* web_contents = data_fetcher_->web_contents();
89 if (!web_contents) 143 if (!web_contents)
90 return; 144 return;
91 145
92 RecordAddToHomescreen(); 146 RecordAddToHomescreen();
93 147
94 const std::string& uid = base::GenerateGUID(); 148 const std::string& uid = base::GenerateGUID();
95 ShortcutHelper::AddToLauncherWithSkBitmap( 149 ShortcutHelper::AddToLauncherWithSkBitmap(
96 web_contents->GetBrowserContext(), info, uid, icon, 150 web_contents->GetBrowserContext(), info, uid, icon,
97 data_fetcher_->FetchSplashScreenImageCallback(uid)); 151 data_fetcher_->FetchSplashScreenImageCallback(uid));
98 } 152 }
99 153
100 void AddToHomescreenDialogHelper::RecordAddToHomescreen() { 154 void AddToHomescreenManager::RecordAddToHomescreen() {
101 // Record that the shortcut has been added, so no banners will be shown 155 // Record that the shortcut has been added, so no banners will be shown
102 // for this app. 156 // for this app.
103 content::WebContents* web_contents = data_fetcher_->web_contents(); 157 content::WebContents* web_contents = data_fetcher_->web_contents();
104 if (!web_contents) 158 if (!web_contents)
105 return; 159 return;
106 160
107 AppBannerSettingsHelper::RecordBannerEvent( 161 AppBannerSettingsHelper::RecordBannerEvent(
108 web_contents, web_contents->GetURL(), 162 web_contents, web_contents->GetURL(),
109 data_fetcher_->shortcut_info().url.spec(), 163 data_fetcher_->shortcut_info().url.spec(),
110 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, 164 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN,
111 base::Time::Now()); 165 base::Time::Now());
112 } 166 }
113 167
114 void AddToHomescreenDialogHelper::OnUserTitleAvailable( 168 void AddToHomescreenManager::OnUserTitleAvailable(
115 const base::string16& user_title) { 169 const base::string16& user_title) {
116 JNIEnv* env = base::android::AttachCurrentThread(); 170 JNIEnv* env = base::android::AttachCurrentThread();
117 ScopedJavaLocalRef<jstring> j_user_title = 171 ScopedJavaLocalRef<jstring> j_user_title =
118 base::android::ConvertUTF16ToJavaString(env, user_title); 172 base::android::ConvertUTF16ToJavaString(env, user_title);
119 Java_AddToHomescreenDialogHelper_onUserTitleAvailable(env, java_ref_, 173 Java_AddToHomescreenManager_onUserTitleAvailable(env,
120 j_user_title); 174 java_ref_,
175 j_user_title);
121 } 176 }
122 177
123 void AddToHomescreenDialogHelper::OnDataAvailable(const ShortcutInfo& info, 178 void AddToHomescreenManager::OnDataAvailable(const ShortcutInfo& info,
124 const SkBitmap& icon) { 179 const SkBitmap& icon) {
125 JNIEnv* env = base::android::AttachCurrentThread(); 180 JNIEnv* env = base::android::AttachCurrentThread();
126 ScopedJavaLocalRef<jobject> java_bitmap; 181 ScopedJavaLocalRef<jobject> java_bitmap;
127 if (icon.getSize()) 182 if (icon.getSize())
128 java_bitmap = gfx::ConvertToJavaBitmap(&icon); 183 java_bitmap = gfx::ConvertToJavaBitmap(&icon);
129 184
130 Java_AddToHomescreenDialogHelper_onIconAvailable(env, java_ref_, java_bitmap); 185 Java_AddToHomescreenManager_onReadyToAdd(env, java_ref_, java_bitmap);
131 186
132 if (add_shortcut_pending_) 187 if (add_shortcut_pending_)
133 AddShortcut(info, icon); 188 AddShortcut(info, icon);
134 } 189 }
135 190
136 SkBitmap AddToHomescreenDialogHelper::FinalizeLauncherIconInBackground( 191 SkBitmap AddToHomescreenManager::FinalizeLauncherIconInBackground(
137 const SkBitmap& bitmap, 192 const SkBitmap& bitmap,
138 const GURL& url, 193 const GURL& url,
139 bool* is_generated) { 194 bool* is_generated) {
140 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); 195 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
141 196
142 return ShortcutHelper::FinalizeLauncherIconInBackground(bitmap, url, 197 return ShortcutHelper::FinalizeLauncherIconInBackground(bitmap, url,
143 is_generated); 198 is_generated);
144 } 199 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698