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) return true; | |
Ted C
2015/02/14 00:06:48
goes on the next line
gone
2015/02/14 00:16:01
Done.
| |
137 | |
138 return Java_AppBannerInfoBarDelegate_installOrOpenNativeApp( | |
139 env, | |
140 java_delegate_.obj(), | |
141 tab->GetJavaObject().obj(), | |
142 native_app_data_.obj()); | |
143 } else if (!web_app_data_.IsEmpty()) { | |
144 AppBannerSettingsHelper::RecordBannerEvent( | |
145 web_contents, web_contents->GetURL(), | |
146 web_app_data_.start_url.spec(), | |
147 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN, | |
148 base::Time::Now()); | |
149 | |
150 ShortcutInfo info; | |
151 info.UpdateFromManifest(web_app_data_); | |
152 base::WorkerPool::PostTask( | |
153 FROM_HERE, | |
154 base::Bind(&ShortcutHelper::AddShortcutInBackgroundWithSkBitmap, | |
155 info, | |
156 *app_icon_.get()), | |
157 true); | |
158 return true; | |
159 } | |
160 | |
161 return true; | |
72 } | 162 } |
73 | 163 |
74 bool AppBannerInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) { | 164 bool AppBannerInfoBarDelegate::LinkClicked(WindowOpenDisposition disposition) { |
75 DCHECK(delegate_); | 165 if (!native_app_data_.is_null()) { |
76 return delegate_->OnLinkClicked(); | 166 // Try to show the details for the native app. |
167 JNIEnv* env = base::android::AttachCurrentThread(); | |
168 | |
169 content::WebContents* web_contents = | |
170 InfoBarService::WebContentsFromInfoBar(infobar()); | |
171 TabAndroid* tab = web_contents ? TabAndroid::FromWebContents(web_contents) | |
172 : nullptr; | |
173 if (tab == nullptr) | |
174 return true; | |
175 | |
176 Java_AppBannerInfoBarDelegate_showAppDetails(env, | |
177 java_delegate_.obj(), | |
178 tab->GetJavaObject().obj(), | |
179 native_app_data_.obj()); | |
180 return true; | |
181 } else { | |
182 // Nothing should happen if the user is installing a web app. | |
183 return false; | |
Ted C
2015/02/14 00:06:48
I would early exit on this conditional at the top
gone
2015/02/14 00:16:01
Done.
| |
184 } | |
185 } | |
186 | |
187 void AppBannerInfoBarDelegate::OnInstallIntentReturned(JNIEnv* env, | |
188 jobject obj, | |
Ted C
2015/02/14 00:06:48
not aligned correctly
gone
2015/02/14 00:16:01
Done.
| |
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, | |
Ted C
2015/02/14 00:06:48
same here
gone
2015/02/14 00:16:01
Done.
| |
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 |