Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / android / infobars / app_banner_infobar_android.cc
blobcf9847cd8e44ada26217f956090945a98abb5df7
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/ui/android/infobars/app_banner_infobar_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "chrome/browser/android/banners/app_banner_infobar_delegate_android.h"
11 #include "jni/AppBannerInfoBarAndroid_jni.h"
12 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
13 #include "ui/gfx/android/java_bitmap.h"
14 #include "ui/gfx/image/image.h"
17 AppBannerInfoBarAndroid::AppBannerInfoBarAndroid(
18 scoped_ptr<banners::AppBannerInfoBarDelegateAndroid> delegate,
19 const base::android::ScopedJavaGlobalRef<jobject>& japp_data)
20 : ConfirmInfoBar(delegate.Pass()),
21 japp_data_(japp_data) {
24 AppBannerInfoBarAndroid::AppBannerInfoBarAndroid(
25 scoped_ptr<banners::AppBannerInfoBarDelegateAndroid> delegate,
26 const GURL& app_url)
27 : ConfirmInfoBar(delegate.Pass()),
28 app_url_(app_url) {
31 AppBannerInfoBarAndroid::~AppBannerInfoBarAndroid() {
34 base::android::ScopedJavaLocalRef<jobject>
35 AppBannerInfoBarAndroid::CreateRenderInfoBar(JNIEnv* env) {
36 ConfirmInfoBarDelegate* app_banner_infobar_delegate = GetDelegate();
38 base::android::ScopedJavaLocalRef<jstring> app_title =
39 base::android::ConvertUTF16ToJavaString(
40 env, app_banner_infobar_delegate->GetMessageText());
42 base::android::ScopedJavaLocalRef<jobject> java_bitmap;
43 if (!app_banner_infobar_delegate->GetIcon().IsEmpty()) {
44 java_bitmap = gfx::ConvertToJavaBitmap(
45 app_banner_infobar_delegate->GetIcon().ToSkBitmap());
48 base::android::ScopedJavaLocalRef<jobject> infobar;
49 if (!japp_data_.is_null()) {
50 infobar.Reset(Java_AppBannerInfoBarAndroid_createNativeAppInfoBar(
51 env,
52 app_title.obj(),
53 java_bitmap.obj(),
54 japp_data_.obj()));
55 } else {
56 // Trim down the app URL to the domain and registry.
57 std::string trimmed_url =
58 net::registry_controlled_domains::GetDomainAndRegistry(
59 app_url_,
60 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
62 base::android::ScopedJavaLocalRef<jstring> app_url =
63 base::android::ConvertUTF8ToJavaString(env, trimmed_url);
65 infobar.Reset(Java_AppBannerInfoBarAndroid_createWebAppInfoBar(
66 env,
67 app_title.obj(),
68 java_bitmap.obj(),
69 app_url.obj()));
72 java_infobar_.Reset(env, infobar.obj());
73 return infobar;
76 void AppBannerInfoBarAndroid::OnInstallStateChanged(int new_state) {
77 JNIEnv* env = base::android::AttachCurrentThread();
78 Java_AppBannerInfoBarAndroid_onInstallStateChanged(env, java_infobar_.obj(),
79 new_state);
82 // Native JNI methods ---------------------------------------------------------
84 bool RegisterAppBannerInfoBarAndroid(JNIEnv* env) {
85 return RegisterNativesImpl(env);