Rename GetIconID to GetIconId
[chromium-blink-merge.git] / chrome / browser / android / banners / app_banner_infobar_delegate_android.cc
blob93bc0fa9dc03ececabda86f6e3f2c6a4f4d42123
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
3 // found in the LICENSE file.
5 #include "chrome/browser/android/banners/app_banner_infobar_delegate_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/location.h"
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/android/shortcut_helper.h"
13 #include "chrome/browser/android/shortcut_info.h"
14 #include "chrome/browser/android/tab_android.h"
15 #include "chrome/browser/banners/app_banner_data_fetcher.h"
16 #include "chrome/browser/banners/app_banner_metrics.h"
17 #include "chrome/browser/banners/app_banner_settings_helper.h"
18 #include "chrome/browser/browser_process.h"
19 #include "chrome/browser/infobars/infobar_service.h"
20 #include "chrome/browser/ui/android/infobars/app_banner_infobar_android.h"
21 #include "chrome/common/render_messages.h"
22 #include "chrome/grit/generated_resources.h"
23 #include "components/rappor/rappor_utils.h"
24 #include "content/public/browser/browser_thread.h"
25 #include "content/public/browser/render_frame_host.h"
26 #include "content/public/browser/web_contents.h"
27 #include "content/public/common/manifest.h"
28 #include "jni/AppBannerInfoBarDelegateAndroid_jni.h"
29 #include "ui/gfx/android/java_bitmap.h"
30 #include "url/gurl.h"
32 using base::android::ConvertJavaStringToUTF8;
33 using base::android::ConvertJavaStringToUTF16;
34 using base::android::ConvertUTF8ToJavaString;
35 using base::android::ConvertUTF16ToJavaString;
37 namespace banners {
39 AppBannerInfoBarDelegateAndroid::AppBannerInfoBarDelegateAndroid(
40 int event_request_id,
41 const base::string16& app_title,
42 SkBitmap* app_icon,
43 const content::Manifest& web_app_data)
44 : app_title_(app_title),
45 app_icon_(app_icon),
46 event_request_id_(event_request_id),
47 web_app_data_(web_app_data),
48 has_user_interaction_(false) {
49 DCHECK(!web_app_data.IsEmpty());
50 CreateJavaDelegate();
53 AppBannerInfoBarDelegateAndroid::AppBannerInfoBarDelegateAndroid(
54 int event_request_id,
55 const base::string16& app_title,
56 SkBitmap* app_icon,
57 const base::android::ScopedJavaGlobalRef<jobject>& native_app_data,
58 const std::string& native_app_package,
59 const std::string& referrer)
60 : app_title_(app_title),
61 app_icon_(app_icon),
62 event_request_id_(event_request_id),
63 native_app_data_(native_app_data),
64 native_app_package_(native_app_package),
65 referrer_(referrer),
66 has_user_interaction_(false) {
67 DCHECK(!native_app_data_.is_null());
68 CreateJavaDelegate();
71 AppBannerInfoBarDelegateAndroid::~AppBannerInfoBarDelegateAndroid() {
72 if (!has_user_interaction_) {
73 if (!native_app_data_.is_null())
74 TrackUserResponse(USER_RESPONSE_NATIVE_APP_IGNORED);
75 else if (!web_app_data_.IsEmpty())
76 TrackUserResponse(USER_RESPONSE_WEB_APP_IGNORED);
79 TrackDismissEvent(DISMISS_EVENT_DISMISSED);
80 JNIEnv* env = base::android::AttachCurrentThread();
81 Java_AppBannerInfoBarDelegateAndroid_destroy(env,
82 java_delegate_.obj());
83 java_delegate_.Reset();
86 void AppBannerInfoBarDelegateAndroid::UpdateInstallState(JNIEnv* env,
87 jobject obj) {
88 if (native_app_data_.is_null())
89 return;
91 int newState = Java_AppBannerInfoBarDelegateAndroid_determineInstallState(
92 env,
93 java_delegate_.obj(),
94 native_app_data_.obj());
95 static_cast<AppBannerInfoBarAndroid*>(infobar())
96 ->OnInstallStateChanged(newState);
99 void AppBannerInfoBarDelegateAndroid::OnInstallIntentReturned(
100 JNIEnv* env,
101 jobject obj,
102 jboolean jis_installing) {
103 if (!infobar())
104 return;
106 content::WebContents* web_contents =
107 InfoBarService::WebContentsFromInfoBar(infobar());
108 if (!web_contents)
109 return;
111 if (jis_installing) {
112 AppBannerSettingsHelper::RecordBannerEvent(
113 web_contents,
114 web_contents->GetURL(),
115 native_app_package_,
116 AppBannerSettingsHelper::APP_BANNER_EVENT_DID_ADD_TO_HOMESCREEN,
117 AppBannerDataFetcher::GetCurrentTime());
119 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_STARTED);
120 rappor::SampleDomainAndRegistryFromGURL(g_browser_process->rappor_service(),
121 "AppBanner.NativeApp.Installed",
122 web_contents->GetURL());
125 UpdateInstallState(env, obj);
128 void AppBannerInfoBarDelegateAndroid::OnInstallFinished(JNIEnv* env,
129 jobject obj,
130 jboolean success) {
131 if (!infobar())
132 return;
134 if (success) {
135 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_COMPLETED);
136 UpdateInstallState(env, obj);
137 } else if (infobar()->owner()) {
138 TrackDismissEvent(DISMISS_EVENT_INSTALL_TIMEOUT);
139 infobar()->owner()->RemoveInfoBar(infobar());
143 void AppBannerInfoBarDelegateAndroid::CreateJavaDelegate() {
144 JNIEnv* env = base::android::AttachCurrentThread();
145 java_delegate_.Reset(Java_AppBannerInfoBarDelegateAndroid_create(
146 env,
147 reinterpret_cast<intptr_t>(this)));
150 void AppBannerInfoBarDelegateAndroid::SendBannerAccepted(
151 content::WebContents* web_contents,
152 const std::string& platform) {
153 web_contents->GetMainFrame()->Send(
154 new ChromeViewMsg_AppBannerAccepted(
155 web_contents->GetMainFrame()->GetRoutingID(),
156 event_request_id_,
157 platform));
160 gfx::Image AppBannerInfoBarDelegateAndroid::GetIcon() const {
161 return gfx::Image::CreateFrom1xBitmap(*app_icon_.get());
164 void AppBannerInfoBarDelegateAndroid::InfoBarDismissed() {
165 has_user_interaction_ = true;
167 content::WebContents* web_contents =
168 InfoBarService::WebContentsFromInfoBar(infobar());
169 if (!web_contents)
170 return;
172 web_contents->GetMainFrame()->Send(
173 new ChromeViewMsg_AppBannerDismissed(
174 web_contents->GetMainFrame()->GetRoutingID(),
175 event_request_id_));
177 if (!native_app_data_.is_null()) {
178 TrackUserResponse(USER_RESPONSE_NATIVE_APP_DISMISSED);
179 AppBannerSettingsHelper::RecordBannerDismissEvent(
180 web_contents, native_app_package_, AppBannerSettingsHelper::NATIVE);
181 } else if (!web_app_data_.IsEmpty()) {
182 TrackUserResponse(USER_RESPONSE_WEB_APP_DISMISSED);
183 AppBannerSettingsHelper::RecordBannerDismissEvent(
184 web_contents, web_app_data_.start_url.spec(),
185 AppBannerSettingsHelper::WEB);
189 base::string16 AppBannerInfoBarDelegateAndroid::GetMessageText() const {
190 return app_title_;
193 int AppBannerInfoBarDelegateAndroid::GetButtons() const {
194 return BUTTON_OK;
197 bool AppBannerInfoBarDelegateAndroid::Accept() {
198 has_user_interaction_ = true;
200 content::WebContents* web_contents =
201 InfoBarService::WebContentsFromInfoBar(infobar());
202 if (!web_contents) {
203 TrackDismissEvent(DISMISS_EVENT_ERROR);
204 return true;
207 if (!native_app_data_.is_null()) {
208 TrackUserResponse(USER_RESPONSE_NATIVE_APP_ACCEPTED);
209 JNIEnv* env = base::android::AttachCurrentThread();
211 TabAndroid* tab = TabAndroid::FromWebContents(web_contents);
212 if (tab == nullptr) {
213 TrackDismissEvent(DISMISS_EVENT_ERROR);
214 return true;
216 ScopedJavaLocalRef<jstring> jreferrer(
217 ConvertUTF8ToJavaString(env, referrer_));
219 bool was_opened = Java_AppBannerInfoBarDelegateAndroid_installOrOpenNativeApp(
220 env,
221 java_delegate_.obj(),
222 tab->GetJavaObject().obj(),
223 native_app_data_.obj(),
224 jreferrer.obj());
226 if (was_opened) {
227 TrackDismissEvent(DISMISS_EVENT_APP_OPEN);
228 } else {
229 TrackInstallEvent(INSTALL_EVENT_NATIVE_APP_INSTALL_TRIGGERED);
231 SendBannerAccepted(web_contents, "play");
232 return was_opened;
233 } else if (!web_app_data_.IsEmpty()) {
234 TrackUserResponse(USER_RESPONSE_WEB_APP_ACCEPTED);
236 AppBannerSettingsHelper::RecordBannerInstallEvent(
237 web_contents, web_app_data_.start_url.spec(),
238 AppBannerSettingsHelper::WEB);
240 ShortcutInfo info(GURL::EmptyGURL());
241 info.UpdateFromManifest(web_app_data_);
242 info.UpdateSource(ShortcutInfo::SOURCE_APP_BANNER);
243 content::BrowserThread::PostTask(
244 content::BrowserThread::IO,
245 FROM_HERE,
246 base::Bind(&ShortcutHelper::AddShortcutInBackgroundWithSkBitmap,
247 info,
248 *app_icon_.get()));
250 SendBannerAccepted(web_contents, "web");
251 return true;
254 return true;
257 bool AppBannerInfoBarDelegateAndroid::LinkClicked(
258 WindowOpenDisposition disposition) {
259 if (native_app_data_.is_null())
260 return false;
262 // Try to show the details for the native app.
263 JNIEnv* env = base::android::AttachCurrentThread();
265 content::WebContents* web_contents =
266 InfoBarService::WebContentsFromInfoBar(infobar());
267 TabAndroid* tab = web_contents ? TabAndroid::FromWebContents(web_contents)
268 : nullptr;
269 if (tab == nullptr) {
270 TrackDismissEvent(DISMISS_EVENT_ERROR);
271 return true;
274 Java_AppBannerInfoBarDelegateAndroid_showAppDetails(env,
275 java_delegate_.obj(),
276 tab->GetJavaObject().obj(),
277 native_app_data_.obj());
279 TrackDismissEvent(DISMISS_EVENT_BANNER_CLICK);
280 return true;
283 bool RegisterAppBannerInfoBarDelegateAndroid(JNIEnv* env) {
284 return RegisterNativesImpl(env);
287 } // namespace banners