1 // Copyright 2013 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/confirm_infobar.h"
9 #include "base/android/jni_android.h"
10 #include "base/android/jni_array.h"
11 #include "base/android/jni_string.h"
12 #include "base/logging.h"
13 #include "chrome/browser/android/resource_mapper.h"
14 #include "chrome/browser/infobars/infobar_service.h"
15 #include "chrome/browser/media/media_stream_infobar_delegate.h"
16 #include "chrome/browser/permissions/permission_infobar_delegate.h"
17 #include "components/content_settings/core/common/content_settings_types.h"
18 #include "components/infobars/core/confirm_infobar_delegate.h"
19 #include "content/public/browser/android/content_view_core.h"
20 #include "content/public/browser/web_contents.h"
21 #include "jni/ConfirmInfoBarDelegate_jni.h"
22 #include "ui/android/window_android.h"
23 #include "ui/gfx/android/java_bitmap.h"
24 #include "ui/gfx/image/image.h"
26 // InfoBarService -------------------------------------------------------------
28 scoped_ptr
<infobars::InfoBar
> InfoBarService::CreateConfirmInfoBar(
29 scoped_ptr
<ConfirmInfoBarDelegate
> delegate
) {
30 return make_scoped_ptr(new ConfirmInfoBar(delegate
.Pass()));
34 // ConfirmInfoBar -------------------------------------------------------------
36 ConfirmInfoBar::ConfirmInfoBar(scoped_ptr
<ConfirmInfoBarDelegate
> delegate
)
37 : InfoBarAndroid(delegate
.Pass()), java_confirm_delegate_() {
40 ConfirmInfoBar::~ConfirmInfoBar() {
43 base::android::ScopedJavaLocalRef
<jobject
> ConfirmInfoBar::CreateRenderInfoBar(
45 java_confirm_delegate_
.Reset(Java_ConfirmInfoBarDelegate_create(env
));
46 base::android::ScopedJavaLocalRef
<jstring
> ok_button_text
=
47 base::android::ConvertUTF16ToJavaString(
48 env
, GetTextFor(ConfirmInfoBarDelegate::BUTTON_OK
));
49 base::android::ScopedJavaLocalRef
<jstring
> cancel_button_text
=
50 base::android::ConvertUTF16ToJavaString(
51 env
, GetTextFor(ConfirmInfoBarDelegate::BUTTON_CANCEL
));
52 ConfirmInfoBarDelegate
* delegate
= GetDelegate();
53 base::android::ScopedJavaLocalRef
<jstring
> message_text
=
54 base::android::ConvertUTF16ToJavaString(
55 env
, delegate
->GetMessageText());
56 base::android::ScopedJavaLocalRef
<jstring
> link_text
=
57 base::android::ConvertUTF16ToJavaString(
58 env
, delegate
->GetLinkText());
60 ScopedJavaLocalRef
<jobject
> java_bitmap
;
61 if (!delegate
->GetIcon().IsEmpty()) {
62 java_bitmap
= gfx::ConvertToJavaBitmap(delegate
->GetIcon().ToSkBitmap());
65 std::vector
<int> content_settings
;
66 if (delegate
->AsPermissionInfobarDelegate()) {
67 content_settings
.push_back(
68 delegate
->AsPermissionInfobarDelegate()->content_setting());
69 } else if (delegate
->AsMediaStreamInfoBarDelegate()) {
70 MediaStreamInfoBarDelegate
* media_delegate
=
71 delegate
->AsMediaStreamInfoBarDelegate();
72 if (media_delegate
->IsRequestingVideoAccess()) {
73 content_settings
.push_back(
74 ContentSettingsType::CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA
);
76 if (media_delegate
->IsRequestingMicrophoneAccess()) {
77 content_settings
.push_back(
78 ContentSettingsType::CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC
);
82 content::WebContents
* web_contents
=
83 InfoBarService::WebContentsFromInfoBar(this);
85 content::ContentViewCore
* cvc
=
86 content::ContentViewCore::FromWebContents(web_contents
);
88 base::android::ScopedJavaLocalRef
<jobject
> jwindow_android
=
89 cvc
->GetWindowAndroid()->GetJavaObject();
91 return Java_ConfirmInfoBarDelegate_showConfirmInfoBar(
92 env
, java_confirm_delegate_
.obj(),
93 jwindow_android
.obj(), GetEnumeratedIconId(), java_bitmap
.obj(),
94 message_text
.obj(), link_text
.obj(), ok_button_text
.obj(),
95 cancel_button_text
.obj(),
96 base::android::ToJavaIntArray(env
, content_settings
).obj());
99 void ConfirmInfoBar::OnLinkClicked(JNIEnv
* env
, jobject obj
) {
101 return; // We're closing; don't call anything, it might access the owner.
103 if (GetDelegate()->LinkClicked(NEW_FOREGROUND_TAB
))
107 void ConfirmInfoBar::ProcessButton(int action
,
108 const std::string
& action_value
) {
110 return; // We're closing; don't call anything, it might access the owner.
112 DCHECK((action
== InfoBarAndroid::ACTION_OK
) ||
113 (action
== InfoBarAndroid::ACTION_CANCEL
));
114 ConfirmInfoBarDelegate
* delegate
= GetDelegate();
115 if ((action
== InfoBarAndroid::ACTION_OK
) ?
116 delegate
->Accept() : delegate
->Cancel())
120 ConfirmInfoBarDelegate
* ConfirmInfoBar::GetDelegate() {
121 return delegate()->AsConfirmInfoBarDelegate();
124 base::string16
ConfirmInfoBar::GetTextFor(
125 ConfirmInfoBarDelegate::InfoBarButton button
) {
126 ConfirmInfoBarDelegate
* delegate
= GetDelegate();
127 return (delegate
->GetButtons() & button
) ?
128 delegate
->GetButtonLabel(button
) : base::string16();
132 // Native JNI methods ---------------------------------------------------------
134 bool RegisterConfirmInfoBarDelegate(JNIEnv
* env
) {
135 return RegisterNativesImpl(env
);