OLD | NEW |
---|---|
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/banners/app_banner_infobar_delegate_android.h" | 5 #include "chrome/browser/android/banners/app_banner_infobar_delegate_android.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" |
(...skipping 19 matching lines...) Expand all Loading... | |
30 #include "ui/gfx/android/java_bitmap.h" | 30 #include "ui/gfx/android/java_bitmap.h" |
31 #include "url/gurl.h" | 31 #include "url/gurl.h" |
32 | 32 |
33 using base::android::ConvertJavaStringToUTF8; | 33 using base::android::ConvertJavaStringToUTF8; |
34 using base::android::ConvertJavaStringToUTF16; | 34 using base::android::ConvertJavaStringToUTF16; |
35 using base::android::ConvertUTF8ToJavaString; | 35 using base::android::ConvertUTF8ToJavaString; |
36 using base::android::ConvertUTF16ToJavaString; | 36 using base::android::ConvertUTF16ToJavaString; |
37 using base::android::JavaParamRef; | 37 using base::android::JavaParamRef; |
38 using base::android::ScopedJavaLocalRef; | 38 using base::android::ScopedJavaLocalRef; |
39 | 39 |
40 namespace { | |
41 | |
42 bool IsInfoEmpty(const ShortcutInfo* info) { | |
43 return !info || info->url.is_empty(); | |
44 } | |
45 | |
46 } | |
47 | |
40 namespace banners { | 48 namespace banners { |
41 | 49 |
42 AppBannerInfoBarDelegateAndroid::AppBannerInfoBarDelegateAndroid( | 50 // static |
51 bool AppBannerInfoBarDelegateAndroid::Create( | |
52 content::WebContents* web_contents, | |
43 base::WeakPtr<AppBannerManager> weak_manager, | 53 base::WeakPtr<AppBannerManager> weak_manager, |
44 const base::string16& app_title, | 54 const base::string16& app_title, |
45 const GURL& manifest_url, | 55 std::unique_ptr<ShortcutInfo> shortcut_info, |
46 const content::Manifest& manifest, | |
47 const GURL& icon_url, | |
48 std::unique_ptr<SkBitmap> icon, | 56 std::unique_ptr<SkBitmap> icon, |
49 int event_request_id, | 57 int event_request_id, |
50 bool is_webapk) | 58 bool is_webapk, |
51 : weak_manager_(weak_manager), | 59 bool start_install_webapk) { |
52 app_title_(app_title), | 60 const GURL& url = shortcut_info->url; |
53 manifest_url_(manifest_url), | 61 auto infobar_delegate = |
54 manifest_(manifest), | 62 base::WrapUnique(new banners::AppBannerInfoBarDelegateAndroid( |
55 icon_url_(icon_url), | 63 weak_manager, app_title, std::move(shortcut_info), std::move(icon), |
56 icon_(std::move(icon)), | 64 event_request_id, is_webapk)); |
57 event_request_id_(event_request_id), | 65 auto raw_delegate = infobar_delegate.get(); |
58 has_user_interaction_(false), | 66 auto infobar = base::MakeUnique<AppBannerInfoBarAndroid>( |
59 is_webapk_(is_webapk), | 67 std::move(infobar_delegate), url, is_webapk); |
60 weak_ptr_factory_(this) { | 68 if (!InfoBarService::FromWebContents(web_contents) |
61 DCHECK(!manifest.IsEmpty()); | 69 ->AddInfoBar(std::move(infobar))) |
62 CreateJavaDelegate(); | 70 return false; |
71 | |
72 if (is_webapk && start_install_webapk) | |
73 raw_delegate->AcceptWebApk(web_contents); | |
74 | |
75 return true; | |
63 } | 76 } |
64 | 77 |
65 AppBannerInfoBarDelegateAndroid::AppBannerInfoBarDelegateAndroid( | 78 // static |
79 bool AppBannerInfoBarDelegateAndroid::Create( | |
80 content::WebContents* web_contents, | |
66 const base::string16& app_title, | 81 const base::string16& app_title, |
67 const base::android::ScopedJavaGlobalRef<jobject>& native_app_data, | 82 const base::android::ScopedJavaGlobalRef<jobject>& native_app_data, |
68 std::unique_ptr<SkBitmap> icon, | 83 std::unique_ptr<SkBitmap> icon, |
69 const std::string& native_app_package, | 84 const std::string& native_app_package, |
70 const std::string& referrer, | 85 const std::string& referrer, |
71 int event_request_id) | 86 int event_request_id) { |
72 : app_title_(app_title), | 87 auto infobar_delegate = base::WrapUnique(new AppBannerInfoBarDelegateAndroid( |
73 native_app_data_(native_app_data), | 88 app_title, native_app_data, std::move(icon), native_app_package, referrer, |
74 icon_(std::move(icon)), | 89 event_request_id)); |
75 native_app_package_(native_app_package), | 90 return InfoBarService::FromWebContents(web_contents) |
76 referrer_(referrer), | 91 ->AddInfoBar(base::MakeUnique<AppBannerInfoBarAndroid>( |
77 event_request_id_(event_request_id), | 92 std::move(infobar_delegate), native_app_data)); |
78 has_user_interaction_(false), | |
79 weak_ptr_factory_(this) { | |
80 DCHECK(!native_app_data_.is_null()); | |
81 CreateJavaDelegate(); | |
82 } | 93 } |
83 | 94 |
84 AppBannerInfoBarDelegateAndroid::~AppBannerInfoBarDelegateAndroid() { | 95 AppBannerInfoBarDelegateAndroid::~AppBannerInfoBarDelegateAndroid() { |
85 weak_ptr_factory_.InvalidateWeakPtrs(); | 96 weak_ptr_factory_.InvalidateWeakPtrs(); |
86 | 97 |
87 if (!has_user_interaction_) { | 98 if (!has_user_interaction_) { |
88 if (!native_app_data_.is_null()) | 99 if (!native_app_data_.is_null()) |
89 TrackUserResponse(USER_RESPONSE_NATIVE_APP_IGNORED); | 100 TrackUserResponse(USER_RESPONSE_NATIVE_APP_IGNORED); |
90 else if (!manifest_.IsEmpty()) | 101 else if (!IsInfoEmpty(shortcut_info_.get())) |
91 TrackUserResponse(USER_RESPONSE_WEB_APP_IGNORED); | 102 TrackUserResponse(USER_RESPONSE_WEB_APP_IGNORED); |
92 } | 103 } |
93 | 104 |
94 TrackDismissEvent(DISMISS_EVENT_DISMISSED); | 105 TrackDismissEvent(DISMISS_EVENT_DISMISSED); |
95 JNIEnv* env = base::android::AttachCurrentThread(); | 106 JNIEnv* env = base::android::AttachCurrentThread(); |
96 Java_AppBannerInfoBarDelegateAndroid_destroy(env, java_delegate_); | 107 Java_AppBannerInfoBarDelegateAndroid_destroy(env, java_delegate_); |
97 java_delegate_.Reset(); | 108 java_delegate_.Reset(); |
98 } | 109 } |
99 | 110 |
100 void AppBannerInfoBarDelegateAndroid::UpdateInstallState( | 111 void AppBannerInfoBarDelegateAndroid::UpdateInstallState( |
101 JNIEnv* env, | 112 JNIEnv* env, |
102 const JavaParamRef<jobject>& obj) { | 113 const JavaParamRef<jobject>& obj) { |
103 if (native_app_data_.is_null() && !is_webapk_) | 114 if (native_app_data_.is_null() && !is_webapk_) |
104 return; | 115 return; |
105 | 116 |
106 int newState = Java_AppBannerInfoBarDelegateAndroid_determineInstallState( | 117 int newState = Java_AppBannerInfoBarDelegateAndroid_determineInstallState( |
107 env, java_delegate_, native_app_data_); | 118 env, java_delegate_, native_app_data_); |
108 static_cast<AppBannerInfoBarAndroid*>(infobar()) | 119 static_cast<AppBannerInfoBarAndroid*>(infobar()) |
109 ->OnInstallStateChanged(newState); | 120 ->OnInstallStateChanged(newState); |
110 } | 121 } |
111 | 122 |
112 void AppBannerInfoBarDelegateAndroid::OnInstallIntentReturned( | 123 void AppBannerInfoBarDelegateAndroid::OnInstallIntentReturned( |
113 JNIEnv* env, | 124 JNIEnv* env, |
114 const JavaParamRef<jobject>& obj, | 125 const JavaParamRef<jobject>& obj, |
115 jboolean jis_installing) { | 126 jboolean jis_installing) { |
116 if (!infobar()) | 127 DCHECK(infobar()); |
117 return; | |
118 | 128 |
119 content::WebContents* web_contents = | 129 content::WebContents* web_contents = |
120 InfoBarService::WebContentsFromInfoBar(infobar()); | 130 InfoBarService::WebContentsFromInfoBar(infobar()); |
121 if (!web_contents) | |
122 return; | |
123 | |
124 if (jis_installing) { | 131 if (jis_installing) { |
125 AppBannerSettingsHelper::RecordBannerEvent( | 132 AppBannerSettingsHelper::RecordBannerEvent( |
126 web_contents, | 133 web_contents, |
127 web_contents->GetURL(), | 134 web_contents->GetURL(), |
128 native_app_package_, | 135 native_app_package_, |
129 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, | 136 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, |
130 AppBannerManager::GetCurrentTime()); | 137 AppBannerManager::GetCurrentTime()); |
131 | 138 |
132 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_STARTED); | 139 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_STARTED); |
133 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(), | 140 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(), |
134 "AppBanner.NativeApp.Installed", | 141 "AppBanner.NativeApp.Installed", |
135 web_contents->GetURL()); | 142 web_contents->GetURL()); |
136 } | 143 } |
137 | 144 |
138 UpdateInstallState(env, obj); | 145 UpdateInstallState(env, obj); |
139 } | 146 } |
140 | 147 |
141 void AppBannerInfoBarDelegateAndroid::OnInstallFinished( | 148 void AppBannerInfoBarDelegateAndroid::OnInstallFinished( |
142 JNIEnv* env, | 149 JNIEnv* env, |
143 const JavaParamRef<jobject>& obj, | 150 const JavaParamRef<jobject>& obj, |
144 jboolean success) { | 151 jboolean success) { |
145 if (!infobar()) | 152 DCHECK(infobar()); |
146 return; | |
147 | 153 |
148 if (success) { | 154 if (success) { |
149 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_COMPLETED); | 155 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_COMPLETED); |
150 UpdateInstallState(env, obj); | 156 UpdateInstallState(env, obj); |
151 } else if (infobar()->owner()) { | 157 } else if (infobar()->owner()) { |
152 TrackDismissEvent(DISMISS_EVENT_INSTALL_TIMEOUT); | 158 TrackDismissEvent(DISMISS_EVENT_INSTALL_TIMEOUT); |
153 infobar()->owner()->RemoveInfoBar(infobar()); | 159 infobar()->owner()->RemoveInfoBar(infobar()); |
154 } | 160 } |
155 } | 161 } |
156 | 162 |
163 bool AppBannerInfoBarDelegateAndroid::AcceptWebApk( | |
164 content::WebContents* web_contents) { | |
165 if (IsInfoEmpty(shortcut_info_.get())) | |
166 return true; | |
167 | |
168 JNIEnv* env = base::android::AttachCurrentThread(); | |
169 // |webapk_package_name_| is set when the WebAPK has finished installing. | |
170 // If the |webapk_package_name_| is empty, it means the "Add to Homescreen" | |
171 // button is pressed, so request WebAPK installation. Otherwise, it means | |
172 // the "Open" button is pressed, so open the installed WebAPK. | |
173 if (!webapk_package_name_.empty()) { | |
174 // Open the WebAPK. | |
175 ScopedJavaLocalRef<jstring> java_webapk_package_name = | |
176 base::android::ConvertUTF8ToJavaString(env, webapk_package_name_); | |
177 Java_AppBannerInfoBarDelegateAndroid_openWebApk(env, java_delegate_, | |
178 java_webapk_package_name); | |
179 | |
180 SendBannerAccepted(web_contents, "web"); | |
181 return true; | |
182 } | |
183 | |
184 // Request install the WebAPK. | |
185 TrackUserResponse(USER_RESPONSE_WEB_APP_ACCEPTED); | |
186 AppBannerSettingsHelper::RecordBannerInstallEvent( | |
187 web_contents, shortcut_info_->url.spec(), AppBannerSettingsHelper::WEB); | |
188 | |
189 Java_AppBannerInfoBarDelegateAndroid_setWebApkInstallingState( | |
190 env, java_delegate_, true); | |
191 UpdateInstallState(env, nullptr); | |
192 WebApkInstaller::FinishCallback callback = | |
193 base::Bind(&AppBannerInfoBarDelegateAndroid::OnWebApkInstallFinished, | |
194 weak_ptr_factory_.GetWeakPtr()); | |
195 ShortcutHelper::InstallWebApkWithSkBitmap(web_contents->GetBrowserContext(), | |
196 *shortcut_info_, | |
197 *icon_.get(), callback); | |
198 SendBannerAccepted(web_contents, "web"); | |
199 | |
200 // Prevent the infobar from disappearing, because the infobar will show | |
201 // "Adding" during the installation process. | |
202 return false; | |
203 } | |
204 | |
205 AppBannerInfoBarDelegateAndroid::AppBannerInfoBarDelegateAndroid( | |
206 base::WeakPtr<AppBannerManager> weak_manager, | |
207 const base::string16& app_title, | |
208 std::unique_ptr<ShortcutInfo> shortcut_info, | |
209 std::unique_ptr<SkBitmap> icon, | |
210 int event_request_id, | |
211 bool is_webapk) | |
212 : weak_manager_(weak_manager), | |
213 app_title_(app_title), | |
214 shortcut_info_(std::move(shortcut_info)), | |
215 icon_(std::move(icon)), | |
216 event_request_id_(event_request_id), | |
217 has_user_interaction_(false), | |
218 is_webapk_(is_webapk), | |
219 weak_ptr_factory_(this) { | |
220 DCHECK(!IsInfoEmpty(shortcut_info_.get())); | |
221 CreateJavaDelegate(); | |
222 } | |
223 | |
224 AppBannerInfoBarDelegateAndroid::AppBannerInfoBarDelegateAndroid( | |
225 const base::string16& app_title, | |
226 const base::android::ScopedJavaGlobalRef<jobject>& native_app_data, | |
227 std::unique_ptr<SkBitmap> icon, | |
228 const std::string& native_app_package, | |
229 const std::string& referrer, | |
230 int event_request_id) | |
231 : app_title_(app_title), | |
232 native_app_data_(native_app_data), | |
233 icon_(std::move(icon)), | |
234 native_app_package_(native_app_package), | |
235 referrer_(referrer), | |
236 event_request_id_(event_request_id), | |
237 has_user_interaction_(false), | |
238 weak_ptr_factory_(this) { | |
239 DCHECK(!native_app_data_.is_null()); | |
240 CreateJavaDelegate(); | |
241 } | |
242 | |
157 void AppBannerInfoBarDelegateAndroid::CreateJavaDelegate() { | 243 void AppBannerInfoBarDelegateAndroid::CreateJavaDelegate() { |
158 JNIEnv* env = base::android::AttachCurrentThread(); | |
159 java_delegate_.Reset(Java_AppBannerInfoBarDelegateAndroid_create( | 244 java_delegate_.Reset(Java_AppBannerInfoBarDelegateAndroid_create( |
160 env, | 245 base::android::AttachCurrentThread(), |
161 reinterpret_cast<intptr_t>(this))); | 246 reinterpret_cast<intptr_t>(this))); |
162 } | 247 } |
163 | 248 |
164 void AppBannerInfoBarDelegateAndroid::SendBannerAccepted( | 249 void AppBannerInfoBarDelegateAndroid::SendBannerAccepted( |
165 content::WebContents* web_contents, | 250 content::WebContents* web_contents, |
166 const std::string& platform) { | 251 const std::string& platform) { |
167 web_contents->GetMainFrame()->Send( | 252 web_contents->GetMainFrame()->Send( |
168 new ChromeViewMsg_AppBannerAccepted( | 253 new ChromeViewMsg_AppBannerAccepted( |
169 web_contents->GetMainFrame()->GetRoutingID(), | 254 web_contents->GetMainFrame()->GetRoutingID(), |
170 event_request_id_, | 255 event_request_id_, |
171 platform)); | 256 platform)); |
172 } | 257 } |
173 | 258 |
174 infobars::InfoBarDelegate::InfoBarIdentifier | 259 infobars::InfoBarDelegate::InfoBarIdentifier |
175 AppBannerInfoBarDelegateAndroid::GetIdentifier() const { | 260 AppBannerInfoBarDelegateAndroid::GetIdentifier() const { |
176 return APP_BANNER_INFOBAR_DELEGATE_ANDROID; | 261 return APP_BANNER_INFOBAR_DELEGATE_ANDROID; |
177 } | 262 } |
178 | 263 |
179 gfx::Image AppBannerInfoBarDelegateAndroid::GetIcon() const { | 264 gfx::Image AppBannerInfoBarDelegateAndroid::GetIcon() const { |
180 return gfx::Image::CreateFrom1xBitmap(*icon_.get()); | 265 return gfx::Image::CreateFrom1xBitmap(*icon_.get()); |
181 } | 266 } |
182 | 267 |
183 void AppBannerInfoBarDelegateAndroid::InfoBarDismissed() { | 268 void AppBannerInfoBarDelegateAndroid::InfoBarDismissed() { |
184 has_user_interaction_ = true; | 269 has_user_interaction_ = true; |
185 | 270 |
186 content::WebContents* web_contents = | 271 content::WebContents* web_contents = |
187 InfoBarService::WebContentsFromInfoBar(infobar()); | 272 InfoBarService::WebContentsFromInfoBar(infobar()); |
188 if (!web_contents) | |
189 return; | |
190 | 273 |
191 web_contents->GetMainFrame()->Send( | 274 web_contents->GetMainFrame()->Send( |
192 new ChromeViewMsg_AppBannerDismissed( | 275 new ChromeViewMsg_AppBannerDismissed( |
193 web_contents->GetMainFrame()->GetRoutingID(), | 276 web_contents->GetMainFrame()->GetRoutingID(), |
194 event_request_id_)); | 277 event_request_id_)); |
195 | 278 |
196 if (!native_app_data_.is_null()) { | 279 if (!native_app_data_.is_null()) { |
Peter Kasting
2016/09/08 20:57:05
Nit: Reverse this condition and the arms, so the "
| |
197 TrackUserResponse(USER_RESPONSE_NATIVE_APP_DISMISSED); | 280 TrackUserResponse(USER_RESPONSE_NATIVE_APP_DISMISSED); |
198 AppBannerSettingsHelper::RecordBannerDismissEvent( | 281 AppBannerSettingsHelper::RecordBannerDismissEvent( |
199 web_contents, native_app_package_, AppBannerSettingsHelper::NATIVE); | 282 web_contents, native_app_package_, AppBannerSettingsHelper::NATIVE); |
200 } else if (!manifest_.IsEmpty()) { | 283 } else { |
201 TrackUserResponse(USER_RESPONSE_WEB_APP_DISMISSED); | 284 TrackUserResponse(USER_RESPONSE_WEB_APP_DISMISSED); |
202 AppBannerSettingsHelper::RecordBannerDismissEvent( | 285 AppBannerSettingsHelper::RecordBannerDismissEvent( |
203 web_contents, manifest_.start_url.spec(), | 286 web_contents, shortcut_info_->url.spec(), AppBannerSettingsHelper::WEB); |
204 AppBannerSettingsHelper::WEB); | |
205 } | 287 } |
206 } | 288 } |
207 | 289 |
208 base::string16 AppBannerInfoBarDelegateAndroid::GetMessageText() const { | 290 base::string16 AppBannerInfoBarDelegateAndroid::GetMessageText() const { |
209 return app_title_; | 291 return app_title_; |
210 } | 292 } |
211 | 293 |
212 int AppBannerInfoBarDelegateAndroid::GetButtons() const { | 294 int AppBannerInfoBarDelegateAndroid::GetButtons() const { |
213 return BUTTON_OK; | 295 return BUTTON_OK; |
214 } | 296 } |
215 | 297 |
216 bool AppBannerInfoBarDelegateAndroid::Accept() { | 298 bool AppBannerInfoBarDelegateAndroid::Accept() { |
217 has_user_interaction_ = true; | 299 has_user_interaction_ = true; |
218 | 300 |
219 content::WebContents* web_contents = | 301 content::WebContents* web_contents = |
220 InfoBarService::WebContentsFromInfoBar(infobar()); | 302 InfoBarService::WebContentsFromInfoBar(infobar()); |
221 if (!web_contents) { | 303 if (!web_contents) { |
222 TrackDismissEvent(DISMISS_EVENT_ERROR); | 304 TrackDismissEvent(DISMISS_EVENT_ERROR); |
223 return true; | 305 return true; |
224 } | 306 } |
225 | 307 |
226 if (!native_app_data_.is_null()) { | 308 if (!native_app_data_.is_null()) |
227 return AcceptNativeApp(web_contents); | 309 return AcceptNativeApp(web_contents); |
228 } else if (is_webapk_) { | 310 |
311 if (is_webapk_) | |
229 return AcceptWebApk(web_contents); | 312 return AcceptWebApk(web_contents); |
230 } | 313 |
231 return AcceptWebApp(web_contents); | 314 return AcceptWebApp(web_contents); |
232 } | 315 } |
233 | 316 |
234 bool AppBannerInfoBarDelegateAndroid::AcceptNativeApp( | 317 bool AppBannerInfoBarDelegateAndroid::AcceptNativeApp( |
235 content::WebContents* web_contents) { | 318 content::WebContents* web_contents) { |
236 TrackUserResponse(USER_RESPONSE_NATIVE_APP_ACCEPTED); | 319 TrackUserResponse(USER_RESPONSE_NATIVE_APP_ACCEPTED); |
237 JNIEnv* env = base::android::AttachCurrentThread(); | 320 JNIEnv* env = base::android::AttachCurrentThread(); |
238 | 321 |
239 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); | 322 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
240 if (tab == nullptr) { | 323 DCHECK(tab); |
241 TrackDismissEvent(DISMISS_EVENT_ERROR); | |
242 return true; | |
243 } | |
244 ScopedJavaLocalRef<jstring> jreferrer( | 324 ScopedJavaLocalRef<jstring> jreferrer( |
245 ConvertUTF8ToJavaString(env, referrer_)); | 325 ConvertUTF8ToJavaString(env, referrer_)); |
246 | 326 |
247 bool was_opened = | 327 bool was_opened = |
248 Java_AppBannerInfoBarDelegateAndroid_installOrOpenNativeApp( | 328 Java_AppBannerInfoBarDelegateAndroid_installOrOpenNativeApp( |
249 env, java_delegate_, tab->GetJavaObject(), | 329 env, java_delegate_, tab->GetJavaObject(), |
250 native_app_data_, jreferrer); | 330 native_app_data_, jreferrer); |
251 | 331 |
252 if (was_opened) { | 332 if (was_opened) |
253 TrackDismissEvent(DISMISS_EVENT_APP_OPEN); | 333 TrackDismissEvent(DISMISS_EVENT_APP_OPEN); |
254 } else { | 334 else |
255 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_TRIGGERED); | 335 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_TRIGGERED); |
256 } | 336 |
257 SendBannerAccepted(web_contents, "play"); | 337 SendBannerAccepted(web_contents, "play"); |
258 return was_opened; | 338 return was_opened; |
259 } | 339 } |
260 | 340 |
261 bool AppBannerInfoBarDelegateAndroid::AcceptWebApp( | 341 bool AppBannerInfoBarDelegateAndroid::AcceptWebApp( |
262 content::WebContents* web_contents) { | 342 content::WebContents* web_contents) { |
263 if (manifest_.IsEmpty()) | 343 if (IsInfoEmpty(shortcut_info_.get())) |
264 return true; | 344 return true; |
265 TrackUserResponse(USER_RESPONSE_WEB_APP_ACCEPTED); | 345 TrackUserResponse(USER_RESPONSE_WEB_APP_ACCEPTED); |
266 | 346 |
267 AppBannerSettingsHelper::RecordBannerInstallEvent( | 347 AppBannerSettingsHelper::RecordBannerInstallEvent( |
268 web_contents, manifest_.start_url.spec(), | 348 web_contents, shortcut_info_->url.spec(), AppBannerSettingsHelper::WEB); |
269 AppBannerSettingsHelper::WEB); | |
270 | 349 |
271 if (weak_manager_) { | 350 if (weak_manager_) { |
272 ShortcutInfo info(GURL::EmptyGURL()); | |
273 info.UpdateFromManifest(manifest_); | |
274 info.manifest_url = manifest_url_; | |
275 info.icon_url = icon_url_; | |
276 info.UpdateSource(ShortcutInfo::SOURCE_APP_BANNER); | |
277 | |
278 const std::string& uid = base::GenerateGUID(); | 351 const std::string& uid = base::GenerateGUID(); |
279 ShortcutHelper::AddToLauncherWithSkBitmap( | 352 ShortcutHelper::AddToLauncherWithSkBitmap( |
280 web_contents->GetBrowserContext(), info, uid, *icon_.get(), | 353 web_contents->GetBrowserContext(), *shortcut_info_, uid, |
281 weak_manager_->FetchWebappSplashScreenImageCallback(uid)); | 354 *icon_.get(), weak_manager_->FetchWebappSplashScreenImageCallback(uid)); |
282 } | 355 } |
283 | 356 |
284 SendBannerAccepted(web_contents, "web"); | 357 SendBannerAccepted(web_contents, "web"); |
285 return true; | 358 return true; |
286 } | 359 } |
287 | 360 |
288 bool AppBannerInfoBarDelegateAndroid::AcceptWebApk( | |
289 content::WebContents* web_contents) { | |
290 if (manifest_.IsEmpty()) | |
291 return true; | |
292 | |
293 JNIEnv* env = base::android::AttachCurrentThread(); | |
294 // |webapk_package_name_| is set when the WebAPK has finished installing. | |
295 // If the |webapk_package_name_| is empty, it means the "Add to Homescreen" | |
296 // button is pressed, so request WebAPK installation. Otherwise, it means | |
297 // the "Open" button is pressed, then open the installed WebAPK. | |
298 if (webapk_package_name_.empty()) { | |
299 // Request install the WebAPK. | |
300 TrackUserResponse(USER_RESPONSE_WEB_APP_ACCEPTED); | |
301 | |
302 AppBannerSettingsHelper::RecordBannerInstallEvent( | |
303 web_contents, manifest_.start_url.spec(), | |
304 AppBannerSettingsHelper::WEB); | |
305 | |
306 ShortcutInfo info(GURL::EmptyGURL()); | |
307 info.UpdateFromManifest(manifest_); | |
308 info.manifest_url = manifest_url_; | |
309 info.icon_url = icon_url_; | |
310 info.UpdateSource(ShortcutInfo::SOURCE_APP_BANNER); | |
311 | |
312 Java_AppBannerInfoBarDelegateAndroid_setWebApkInstallingState( | |
313 env, java_delegate_, true); | |
314 UpdateInstallState(env, nullptr); | |
315 | |
316 WebApkInstaller::FinishCallback callback = base::Bind( | |
317 &AppBannerInfoBarDelegateAndroid::OnWebApkInstallFinished, | |
318 weak_ptr_factory_.GetWeakPtr()); | |
319 DVLOG(1) << "Trigger the installation of the WebAPK."; | |
320 ShortcutHelper::InstallWebApkWithSkBitmap( | |
321 web_contents->GetBrowserContext(), info, *icon_.get(), callback); | |
322 | |
323 SendBannerAccepted(web_contents, "web"); | |
324 // Returns false to prevent the infobar from disappearing. | |
325 return false; | |
326 } | |
327 | |
328 // Open the WebAPK. | |
329 ScopedJavaLocalRef<jstring> java_webapk_package_name = | |
330 base::android::ConvertUTF8ToJavaString(env, webapk_package_name_); | |
331 Java_AppBannerInfoBarDelegateAndroid_openWebApk( | |
332 env, java_delegate_, java_webapk_package_name); | |
333 | |
334 SendBannerAccepted(web_contents, "web"); | |
335 return true; | |
336 } | |
337 | |
338 void AppBannerInfoBarDelegateAndroid::OnWebApkInstallFinished( | 361 void AppBannerInfoBarDelegateAndroid::OnWebApkInstallFinished( |
339 bool success, | 362 bool success, |
340 const std::string& webapk_package_name) { | 363 const std::string& webapk_package_name) { |
341 JNIEnv* env = base::android::AttachCurrentThread(); | 364 JNIEnv* env = base::android::AttachCurrentThread(); |
342 if (!success) { | 365 if (!success) { |
343 // The installation failed. | 366 // The installation failed. |
344 if (infobar()) | 367 if (infobar()) |
345 infobar()->RemoveSelf(); | 368 infobar()->RemoveSelf(); |
346 Java_AppBannerInfoBarDelegateAndroid_showWebApkInstallFailureToast(env); | 369 Java_AppBannerInfoBarDelegateAndroid_showWebApkInstallFailureToast(env); |
347 DVLOG(1) << "The WebAPK installation failed."; | 370 DVLOG(1) << "The WebAPK installation failed."; |
(...skipping 13 matching lines...) Expand all Loading... | |
361 bool AppBannerInfoBarDelegateAndroid::LinkClicked( | 384 bool AppBannerInfoBarDelegateAndroid::LinkClicked( |
362 WindowOpenDisposition disposition) { | 385 WindowOpenDisposition disposition) { |
363 if (native_app_data_.is_null()) | 386 if (native_app_data_.is_null()) |
364 return false; | 387 return false; |
365 | 388 |
366 // Try to show the details for the native app. | 389 // Try to show the details for the native app. |
367 JNIEnv* env = base::android::AttachCurrentThread(); | 390 JNIEnv* env = base::android::AttachCurrentThread(); |
368 | 391 |
369 content::WebContents* web_contents = | 392 content::WebContents* web_contents = |
370 InfoBarService::WebContentsFromInfoBar(infobar()); | 393 InfoBarService::WebContentsFromInfoBar(infobar()); |
371 TabAndroid* tab = web_contents ? TabAndroid::FromWebContents(web_contents) | 394 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
372 : nullptr; | 395 DCHECK(tab); |
373 if (tab == nullptr) { | |
374 TrackDismissEvent(DISMISS_EVENT_ERROR); | |
375 return true; | |
376 } | |
377 | 396 |
378 Java_AppBannerInfoBarDelegateAndroid_showAppDetails( | 397 Java_AppBannerInfoBarDelegateAndroid_showAppDetails( |
379 env, java_delegate_, tab->GetJavaObject(), native_app_data_); | 398 env, java_delegate_, tab->GetJavaObject(), native_app_data_); |
380 | 399 |
381 TrackDismissEvent(DISMISS_EVENT_BANNER_CLICK); | 400 TrackDismissEvent(DISMISS_EVENT_BANNER_CLICK); |
382 return true; | 401 return true; |
383 } | 402 } |
384 | 403 |
385 bool RegisterAppBannerInfoBarDelegateAndroid(JNIEnv* env) { | 404 bool RegisterAppBannerInfoBarDelegateAndroid(JNIEnv* env) { |
386 return RegisterNativesImpl(env); | 405 return RegisterNativesImpl(env); |
387 } | 406 } |
388 | 407 |
389 } // namespace banners | 408 } // namespace banners |
OLD | NEW |