OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_manager.h" | 5 #include "chrome/browser/android/banners/app_banner_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/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 } | 92 } |
93 | 93 |
94 void AppBannerManager::OnInfoBarDestroyed() { | 94 void AppBannerManager::OnInfoBarDestroyed() { |
95 weak_infobar_ptr_ = nullptr; | 95 weak_infobar_ptr_ = nullptr; |
96 } | 96 } |
97 | 97 |
98 bool AppBannerManager::OnButtonClicked() const { | 98 bool AppBannerManager::OnButtonClicked() const { |
99 if (!web_contents()) | 99 if (!web_contents()) |
100 return true; | 100 return true; |
101 | 101 |
102 if (!web_app_data_.IsEmpty()) { | 102 if (!native_app_data_.is_null()) { |
| 103 JNIEnv* env = base::android::AttachCurrentThread(); |
| 104 ScopedJavaLocalRef<jobject> jobj = weak_java_banner_view_manager_.get(env); |
| 105 if (jobj.is_null()) |
| 106 return true; |
| 107 |
| 108 return Java_AppBannerManager_installOrOpenNativeApp(env, |
| 109 jobj.obj(), |
| 110 native_app_data_.obj()); |
| 111 } else if (!web_app_data_.IsEmpty()) { |
103 AppBannerSettingsHelper::RecordBannerEvent( | 112 AppBannerSettingsHelper::RecordBannerEvent( |
104 web_contents(), web_contents()->GetURL(), | 113 web_contents(), web_contents()->GetURL(), |
105 web_app_data_.start_url.spec(), | 114 web_app_data_.start_url.spec(), |
106 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, | 115 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, |
107 base::Time::Now()); | 116 base::Time::Now()); |
108 | 117 |
109 InstallManifestApp(web_app_data_, *app_icon_.get()); | 118 InstallManifestApp(web_app_data_, *app_icon_.get()); |
110 return true; | 119 return true; |
111 } | 120 } |
112 | 121 |
113 return true; | 122 return true; |
114 } | 123 } |
115 | 124 |
| 125 bool AppBannerManager::OnLinkClicked() const { |
| 126 if (!web_contents()) |
| 127 return true; |
| 128 |
| 129 if (!native_app_data_.is_null()) { |
| 130 // Try to show the details for the native app. |
| 131 JNIEnv* env = base::android::AttachCurrentThread(); |
| 132 ScopedJavaLocalRef<jobject> jobj = weak_java_banner_view_manager_.get(env); |
| 133 if (jobj.is_null()) |
| 134 return true; |
| 135 |
| 136 Java_AppBannerManager_showAppDetails(env, |
| 137 jobj.obj(), |
| 138 native_app_data_.obj()); |
| 139 return true; |
| 140 } else { |
| 141 // Nothing should happen if the user is installing a web app. |
| 142 return false; |
| 143 } |
| 144 } |
| 145 |
116 base::string16 AppBannerManager::GetTitle() const { | 146 base::string16 AppBannerManager::GetTitle() const { |
117 return app_title_; | 147 return app_title_; |
118 } | 148 } |
119 | 149 |
120 gfx::Image AppBannerManager::GetIcon() const { | 150 gfx::Image AppBannerManager::GetIcon() const { |
121 return gfx::Image::CreateFrom1xBitmap(*app_icon_.get()); | 151 return gfx::Image::CreateFrom1xBitmap(*app_icon_.get()); |
122 } | 152 } |
123 | 153 |
124 void AppBannerManager::ReplaceWebContents(JNIEnv* env, | 154 void AppBannerManager::ReplaceWebContents(JNIEnv* env, |
125 jobject obj, | 155 jobject obj, |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
312 if (validated_url_ != web_contents()->GetURL()) | 342 if (validated_url_ != web_contents()->GetURL()) |
313 return false; | 343 return false; |
314 | 344 |
315 std::string image_url = ConvertJavaStringToUTF8(env, jicon_url); | 345 std::string image_url = ConvertJavaStringToUTF8(env, jicon_url); |
316 app_title_ = ConvertJavaStringToUTF16(env, japp_title); | 346 app_title_ = ConvertJavaStringToUTF16(env, japp_title); |
317 native_app_package_ = ConvertJavaStringToUTF8(env, japp_package); | 347 native_app_package_ = ConvertJavaStringToUTF8(env, japp_package); |
318 native_app_data_.Reset(env, japp_data); | 348 native_app_data_.Reset(env, japp_data); |
319 return FetchIcon(GURL(image_url)); | 349 return FetchIcon(GURL(image_url)); |
320 } | 350 } |
321 | 351 |
| 352 void AppBannerManager::OnInstallIntentReturned(JNIEnv* env, |
| 353 jobject obj, |
| 354 jboolean jis_installing) { |
| 355 if (!weak_infobar_ptr_) |
| 356 return; |
| 357 |
| 358 if (jis_installing) { |
| 359 AppBannerSettingsHelper::RecordBannerEvent( |
| 360 web_contents(), |
| 361 web_contents()->GetURL(), |
| 362 native_app_package_, |
| 363 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, |
| 364 base::Time::Now()); |
| 365 } |
| 366 |
| 367 UpdateInstallState(env, obj); |
| 368 } |
| 369 |
| 370 void AppBannerManager::OnInstallFinished(JNIEnv* env, |
| 371 jobject obj, |
| 372 jboolean success) { |
| 373 if (!weak_infobar_ptr_) |
| 374 return; |
| 375 |
| 376 if (success) { |
| 377 UpdateInstallState(env, obj); |
| 378 } else { |
| 379 InfoBarService* service = InfoBarService::FromWebContents(web_contents()); |
| 380 service->RemoveInfoBar(weak_infobar_ptr_); |
| 381 } |
| 382 } |
| 383 |
| 384 void AppBannerManager::UpdateInstallState(JNIEnv* env, jobject obj) { |
| 385 if (!weak_infobar_ptr_ || native_app_data_.is_null()) |
| 386 return; |
| 387 |
| 388 ScopedJavaLocalRef<jobject> jobj = weak_java_banner_view_manager_.get(env); |
| 389 if (jobj.is_null()) |
| 390 return; |
| 391 |
| 392 int newState = Java_AppBannerManager_determineInstallState( |
| 393 env, |
| 394 jobj.obj(), |
| 395 native_app_data_.obj()); |
| 396 weak_infobar_ptr_->OnInstallStateChanged(newState); |
| 397 } |
| 398 |
322 bool AppBannerManager::FetchIcon(const GURL& image_url) { | 399 bool AppBannerManager::FetchIcon(const GURL& image_url) { |
323 if (!web_contents()) | 400 if (!web_contents()) |
324 return false; | 401 return false; |
325 | 402 |
326 // Begin asynchronously fetching the app icon. | 403 // Begin asynchronously fetching the app icon. |
327 Profile* profile = | 404 Profile* profile = |
328 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); | 405 Profile::FromBrowserContext(web_contents()->GetBrowserContext()); |
329 fetcher_.reset(new chrome::BitmapFetcher(image_url, this)); | 406 fetcher_.reset(new chrome::BitmapFetcher(image_url, this)); |
330 fetcher_.get()->Start( | 407 fetcher_.get()->Start( |
331 profile->GetRequestContext(), | 408 profile->GetRequestContext(), |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 return base::CommandLine::ForCurrentProcess()->HasSwitch( | 453 return base::CommandLine::ForCurrentProcess()->HasSwitch( |
377 switches::kEnableAppInstallAlerts); | 454 switches::kEnableAppInstallAlerts); |
378 } | 455 } |
379 | 456 |
380 // Register native methods | 457 // Register native methods |
381 bool RegisterAppBannerManager(JNIEnv* env) { | 458 bool RegisterAppBannerManager(JNIEnv* env) { |
382 return RegisterNativesImpl(env); | 459 return RegisterNativesImpl(env); |
383 } | 460 } |
384 | 461 |
385 } // namespace banners | 462 } // namespace banners |
OLD | NEW |