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.h" | 5 #include "chrome/browser/android/banners/app_banner_infobar_delegate.h" |
6 | 6 |
7 #include "base/android/jni_android.h" | 7 #include "base/android/jni_android.h" |
| 8 #include "base/android/jni_string.h" |
| 9 #include "base/location.h" |
8 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
9 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/threading/worker_pool.h" |
| 13 #include "chrome/browser/android/shortcut_helper.h" |
| 14 #include "chrome/browser/android/shortcut_info.h" |
| 15 #include "chrome/browser/android/tab_android.h" |
| 16 #include "chrome/browser/banners/app_banner_settings_helper.h" |
| 17 #include "chrome/browser/infobars/infobar_service.h" |
10 #include "chrome/browser/ui/android/infobars/app_banner_infobar.h" | 18 #include "chrome/browser/ui/android/infobars/app_banner_infobar.h" |
11 #include "chrome/grit/generated_resources.h" | 19 #include "chrome/grit/generated_resources.h" |
12 #include "components/infobars/core/infobar.h" | 20 #include "components/infobars/core/infobar.h" |
13 #include "components/infobars/core/infobar_manager.h" | 21 #include "components/infobars/core/infobar_manager.h" |
| 22 #include "content/public/common/manifest.h" |
| 23 #include "jni/AppBannerInfoBarDelegate_jni.h" |
| 24 #include "ui/gfx/android/java_bitmap.h" |
| 25 |
| 26 using base::android::ConvertJavaStringToUTF8; |
| 27 using base::android::ConvertJavaStringToUTF16; |
| 28 using base::android::ConvertUTF8ToJavaString; |
| 29 using base::android::ConvertUTF16ToJavaString; |
14 | 30 |
15 namespace banners { | 31 namespace banners { |
16 | 32 |
17 // static | 33 // static |
18 AppBannerInfoBar* AppBannerInfoBarDelegate::CreateForNativeApp( | 34 AppBannerInfoBar* AppBannerInfoBarDelegate::CreateForNativeApp( |
19 infobars::InfoBarManager* infobar_manager, | 35 infobars::InfoBarManager* infobar_manager, |
20 AppDelegate* app_delegate, | 36 const base::string16& app_title, |
21 const base::android::ScopedJavaGlobalRef<jobject>& japp_data) { | 37 SkBitmap* app_icon, |
| 38 const base::android::ScopedJavaGlobalRef<jobject>& app_data, |
| 39 const std::string& app_package) { |
22 scoped_ptr<AppBannerInfoBarDelegate> delegate(new AppBannerInfoBarDelegate( | 40 scoped_ptr<AppBannerInfoBarDelegate> delegate(new AppBannerInfoBarDelegate( |
23 app_delegate)); | 41 app_title, |
24 AppBannerInfoBar* infobar = new AppBannerInfoBar(delegate.Pass(), japp_data); | 42 app_icon, |
| 43 content::Manifest(), |
| 44 app_data, |
| 45 app_package)); |
| 46 AppBannerInfoBar* infobar = new AppBannerInfoBar(delegate.Pass(), |
| 47 app_data); |
25 return infobar_manager->AddInfoBar(make_scoped_ptr(infobar)) | 48 return infobar_manager->AddInfoBar(make_scoped_ptr(infobar)) |
26 ? infobar : nullptr; | 49 ? infobar : nullptr; |
27 } | 50 } |
28 | 51 |
29 // static | 52 // static |
30 AppBannerInfoBar* AppBannerInfoBarDelegate::CreateForWebApp( | 53 AppBannerInfoBar* AppBannerInfoBarDelegate::CreateForWebApp( |
31 infobars::InfoBarManager* infobar_manager, | 54 infobars::InfoBarManager* infobar_manager, |
32 AppDelegate* app_delegate, | 55 const base::string16& app_title, |
33 const GURL& url) { | 56 SkBitmap* app_icon, |
| 57 const content::Manifest& web_app_data) { |
34 scoped_ptr<AppBannerInfoBarDelegate> delegate(new AppBannerInfoBarDelegate( | 58 scoped_ptr<AppBannerInfoBarDelegate> delegate(new AppBannerInfoBarDelegate( |
35 app_delegate)); | 59 app_title, |
36 AppBannerInfoBar* infobar = new AppBannerInfoBar(delegate.Pass(), url); | 60 app_icon, |
| 61 web_app_data, |
| 62 base::android::ScopedJavaGlobalRef<jobject>(), |
| 63 std::string())); |
| 64 AppBannerInfoBar* infobar = new AppBannerInfoBar(delegate.Pass(), |
| 65 web_app_data.start_url); |
37 return infobar_manager->AddInfoBar(make_scoped_ptr(infobar)) | 66 return infobar_manager->AddInfoBar(make_scoped_ptr(infobar)) |
38 ? infobar : nullptr; | 67 ? infobar : nullptr; |
39 } | 68 } |
40 | 69 |
41 AppBannerInfoBarDelegate::AppBannerInfoBarDelegate(AppDelegate* app_delegate) | 70 AppBannerInfoBarDelegate::AppBannerInfoBarDelegate( |
42 : delegate_(app_delegate) { | 71 const base::string16& app_title, |
| 72 SkBitmap* app_icon, |
| 73 const content::Manifest& web_app_data, |
| 74 const base::android::ScopedJavaGlobalRef<jobject>& native_app_data, |
| 75 const std::string& native_app_package) |
| 76 : app_title_(app_title), |
| 77 app_icon_(app_icon), |
| 78 web_app_data_(web_app_data), |
| 79 native_app_data_(native_app_data), |
| 80 native_app_package_(native_app_package) { |
| 81 DCHECK(native_app_data_.is_null() ^ web_app_data_.IsEmpty()); |
| 82 JNIEnv* env = base::android::AttachCurrentThread(); |
| 83 java_delegate_.Reset(Java_AppBannerInfoBarDelegate_create( |
| 84 env, |
| 85 reinterpret_cast<intptr_t>(this))); |
43 } | 86 } |
44 | 87 |
45 AppBannerInfoBarDelegate::~AppBannerInfoBarDelegate() { | 88 AppBannerInfoBarDelegate::~AppBannerInfoBarDelegate() { |
46 DCHECK(delegate_); | 89 JNIEnv* env = base::android::AttachCurrentThread(); |
47 delegate_->OnInfoBarDestroyed(); | 90 Java_AppBannerInfoBarDelegate_destroy(env, |
| 91 java_delegate_.obj()); |
| 92 java_delegate_.Reset(); |
48 } | 93 } |
49 | 94 |
50 base::string16 AppBannerInfoBarDelegate::GetMessageText() const { | 95 base::string16 AppBannerInfoBarDelegate::GetMessageText() const { |
51 DCHECK(delegate_); | 96 return app_title_; |
52 return delegate_->GetTitle(); | |
53 } | 97 } |
54 | 98 |
55 int AppBannerInfoBarDelegate::GetButtons() const { | 99 int AppBannerInfoBarDelegate::GetButtons() const { |
56 return BUTTON_OK; | 100 return BUTTON_OK; |
57 } | 101 } |
58 | 102 |
59 gfx::Image AppBannerInfoBarDelegate::GetIcon() const { | 103 gfx::Image AppBannerInfoBarDelegate::GetIcon() const { |
60 DCHECK(delegate_); | 104 return gfx::Image::CreateFrom1xBitmap(*app_icon_.get()); |
61 return delegate_->GetIcon(); | |
62 } | 105 } |
63 | 106 |
64 void AppBannerInfoBarDelegate::InfoBarDismissed() { | 107 void AppBannerInfoBarDelegate::InfoBarDismissed() { |
65 DCHECK(delegate_); | 108 content::WebContents* web_contents = |
66 delegate_->Block(); | 109 InfoBarService::WebContentsFromInfoBar(infobar()); |
| 110 if (!web_contents) |
| 111 return; |
| 112 |
| 113 if (!native_app_data_.is_null()) { |
| 114 AppBannerSettingsHelper::RecordBannerEvent( |
| 115 web_contents, web_contents->GetURL(), |
| 116 native_app_package_, |
| 117 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_BLOCK, base::Time::Now()); |
| 118 } else if (!web_app_data_.IsEmpty()) { |
| 119 AppBannerSettingsHelper::RecordBannerEvent( |
| 120 web_contents, web_contents->GetURL(), |
| 121 web_app_data_.start_url.spec(), |
| 122 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_BLOCK, base::Time::Now()); |
| 123 } |
67 } | 124 } |
68 | 125 |
69 bool AppBannerInfoBarDelegate::Accept() { | 126 bool AppBannerInfoBarDelegate::Accept() { |
70 DCHECK(delegate_); | 127 content::WebContents* web_contents = |
71 return delegate_->OnButtonClicked(); | 128 InfoBarService::WebContentsFromInfoBar(infobar()); |
| 129 if (!web_contents) |
| 130 return true; |
| 131 |
| 132 if (!native_app_data_.is_null()) { |
| 133 JNIEnv* env = base::android::AttachCurrentThread(); |
| 134 |
| 135 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); |
| 136 if (tab == nullptr) |
| 137 return true; |
| 138 |
| 139 return Java_AppBannerInfoBarDelegate_installOrOpenNativeApp( |
| 140 env, |
| 141 java_delegate_.obj(), |
| 142 tab->GetJavaObject().obj(), |
| 143 native_app_data_.obj()); |
| 144 } else if (!web_app_data_.IsEmpty()) { |
| 145 AppBannerSettingsHelper::RecordBannerEvent( |
| 146 web_contents, web_contents->GetURL(), |
| 147 web_app_data_.start_url.spec(), |
| 148 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, |
| 149 base::Time::Now()); |
| 150 |
| 151 ShortcutInfo info; |
| 152 info.UpdateFromManifest(web_app_data_); |
| 153 base::WorkerPool::PostTask( |
| 154 FROM_HERE, |
| 155 base::Bind(&ShortcutHelper::AddShortcutInBackgroundWithSkBitmap, |
| 156 info, |
| 157 *app_icon_.get()), |
| 158 true); |
| 159 return true; |
| 160 } |
| 161 |
| 162 return true; |
72 } | 163 } |
73 | 164 |
74 bool AppBannerInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) { | 165 bool AppBannerInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) { |
75 DCHECK(delegate_); | 166 if (native_app_data_.is_null()) |
76 return delegate_->OnLinkClicked(); | 167 return false; |
| 168 |
| 169 // Try to show the details for the native app. |
| 170 JNIEnv* env = base::android::AttachCurrentThread(); |
| 171 |
| 172 content::WebContents* web_contents = |
| 173 InfoBarService::WebContentsFromInfoBar(infobar()); |
| 174 TabAndroid* tab = web_contents ? TabAndroid::FromWebContents(web_contents) |
| 175 : nullptr; |
| 176 if (tab == nullptr) |
| 177 return true; |
| 178 |
| 179 Java_AppBannerInfoBarDelegate_showAppDetails(env, |
| 180 java_delegate_.obj(), |
| 181 tab->GetJavaObject().obj(), |
| 182 native_app_data_.obj()); |
| 183 return true; |
| 184 } |
| 185 |
| 186 void AppBannerInfoBarDelegate::OnInstallIntentReturned( |
| 187 JNIEnv* env, |
| 188 jobject obj, |
| 189 jboolean jis_installing) { |
| 190 if (!infobar()) |
| 191 return; |
| 192 |
| 193 content::WebContents* web_contents = |
| 194 InfoBarService::WebContentsFromInfoBar(infobar()); |
| 195 if (!web_contents) |
| 196 return; |
| 197 |
| 198 if (jis_installing) { |
| 199 AppBannerSettingsHelper::RecordBannerEvent( |
| 200 web_contents, |
| 201 web_contents->GetURL(), |
| 202 native_app_package_, |
| 203 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, |
| 204 base::Time::Now()); |
| 205 } |
| 206 |
| 207 UpdateInstallState(env, obj); |
| 208 } |
| 209 |
| 210 void AppBannerInfoBarDelegate::OnInstallFinished(JNIEnv* env, |
| 211 jobject obj, |
| 212 jboolean success) { |
| 213 if (!infobar()) |
| 214 return; |
| 215 |
| 216 if (success) { |
| 217 UpdateInstallState(env, obj); |
| 218 } else if (infobar()->owner()) { |
| 219 infobar()->owner()->RemoveInfoBar(infobar()); |
| 220 } |
| 221 } |
| 222 |
| 223 void AppBannerInfoBarDelegate::UpdateInstallState(JNIEnv* env, jobject obj) { |
| 224 if (native_app_data_.is_null()) |
| 225 return; |
| 226 |
| 227 int newState = Java_AppBannerInfoBarDelegate_determineInstallState( |
| 228 env, |
| 229 java_delegate_.obj(), |
| 230 native_app_data_.obj()); |
| 231 static_cast<AppBannerInfoBar*>(infobar())->OnInstallStateChanged(newState); |
| 232 } |
| 233 |
| 234 |
| 235 bool RegisterAppBannerInfoBarDelegate(JNIEnv* env) { |
| 236 return RegisterNativesImpl(env); |
77 } | 237 } |
78 | 238 |
79 } // namespace banners | 239 } // namespace banners |
OLD | NEW |