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/connection_info_popup_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_array.h"
9 #include "base/android/jni_string.h"
10 #include "chrome/browser/android/resource_mapper.h"
11 #include "chrome/browser/infobars/infobar_service.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/website_settings/website_settings.h"
14 #include "chrome/grit/generated_resources.h"
15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/cert_store.h"
17 #include "content/public/browser/navigation_controller.h"
18 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/web_contents.h"
20 #include "content/public/common/ssl_status.h"
21 #include "jni/ConnectionInfoPopup_jni.h"
22 #include "net/cert/x509_certificate.h"
23 #include "ui/base/l10n/l10n_util.h"
25 using base::android::CheckException
;
26 using base::android::ConvertUTF8ToJavaString
;
27 using base::android::ConvertUTF16ToJavaString
;
28 using base::android::GetClass
;
29 using base::android::ScopedJavaLocalRef
;
30 using content::CertStore
;
31 using content::WebContents
;
33 static ScopedJavaLocalRef
<jobjectArray
> GetCertificateChain(
35 const JavaParamRef
<jobject
>& obj
,
36 const JavaParamRef
<jobject
>& java_web_contents
) {
37 content::WebContents
* web_contents
=
38 content::WebContents::FromJavaWebContents(java_web_contents
);
40 return ScopedJavaLocalRef
<jobjectArray
>();
43 web_contents
->GetController().GetVisibleEntry()->GetSSL().cert_id
;
44 scoped_refptr
<net::X509Certificate
> cert
;
45 bool ok
= CertStore::GetInstance()->RetrieveCert(cert_id
, &cert
);
48 std::vector
<std::string
> cert_chain
;
49 net::X509Certificate::OSCertHandles cert_handles
=
50 cert
->GetIntermediateCertificates();
51 // Make sure the peer's own cert is the first in the chain, if it's not
53 if (cert_handles
.empty() || cert_handles
[0] != cert
->os_cert_handle())
54 cert_handles
.insert(cert_handles
.begin(), cert
->os_cert_handle());
56 cert_chain
.reserve(cert_handles
.size());
57 for (net::X509Certificate::OSCertHandles::const_iterator it
=
59 it
!= cert_handles
.end();
61 std::string cert_bytes
;
62 net::X509Certificate::GetDEREncoded(*it
, &cert_bytes
);
63 cert_chain
.push_back(cert_bytes
);
66 return base::android::ToJavaArrayOfByteArray(env
, cert_chain
);
70 static jlong
Init(JNIEnv
* env
,
71 const JavaParamRef
<jclass
>& clazz
,
72 const JavaParamRef
<jobject
>& obj
,
73 const JavaParamRef
<jobject
>& java_web_contents
) {
74 content::WebContents
* web_contents
=
75 content::WebContents::FromJavaWebContents(java_web_contents
);
77 return reinterpret_cast<intptr_t>(
78 new ConnectionInfoPopupAndroid(env
, obj
, web_contents
));
81 ConnectionInfoPopupAndroid::ConnectionInfoPopupAndroid(
83 jobject java_website_settings_pop
,
84 WebContents
* web_contents
) {
85 // Important to use GetVisibleEntry to match what's showing in the omnibox.
86 content::NavigationEntry
* nav_entry
=
87 web_contents
->GetController().GetVisibleEntry();
88 if (nav_entry
== NULL
)
91 popup_jobject_
.Reset(env
, java_website_settings_pop
);
93 presenter_
.reset(new WebsiteSettings(
95 Profile::FromBrowserContext(web_contents
->GetBrowserContext()),
96 TabSpecificContentSettings::FromWebContents(web_contents
),
100 content::CertStore::GetInstance()));
103 ConnectionInfoPopupAndroid::~ConnectionInfoPopupAndroid() {
106 void ConnectionInfoPopupAndroid::Destroy(JNIEnv
* env
, jobject obj
) {
110 void ConnectionInfoPopupAndroid::ResetCertDecisions(
113 jobject java_web_contents
) {
114 presenter_
->OnRevokeSSLErrorBypassButtonPressed();
117 void ConnectionInfoPopupAndroid::SetIdentityInfo(
118 const IdentityInfo
& identity_info
) {
119 JNIEnv
* env
= base::android::AttachCurrentThread();
122 int icon_id
= ResourceMapper::MapFromChromiumId(
123 WebsiteSettingsUI::GetIdentityIconID(identity_info
.identity_status
));
125 // The headline and the certificate dialog link of the site's identity
126 // section is only displayed if the site's identity was verified. If the
127 // site's identity was verified, then the headline contains the organization
128 // name from the provided certificate. If the organization name is not
129 // available than the hostname of the site is used instead.
130 std::string headline
;
131 if (identity_info
.cert_id
) {
132 headline
= identity_info
.site_identity
;
135 ScopedJavaLocalRef
<jstring
> description
=
136 ConvertUTF8ToJavaString(env
, identity_info
.identity_status_description
);
137 base::string16 certificate_label
=
138 l10n_util::GetStringUTF16(IDS_PAGEINFO_CERT_INFO_BUTTON
);
139 Java_ConnectionInfoPopup_addCertificateSection(
141 popup_jobject_
.obj(),
143 ConvertUTF8ToJavaString(env
, headline
).obj(),
145 ConvertUTF16ToJavaString(env
, certificate_label
).obj());
147 if (identity_info
.show_ssl_decision_revoke_button
) {
148 base::string16 reset_button_label
= l10n_util::GetStringUTF16(
149 IDS_PAGEINFO_RESET_INVALID_CERTIFICATE_DECISIONS_BUTTON
);
150 Java_ConnectionInfoPopup_addResetCertDecisionsButton(
152 popup_jobject_
.obj(),
153 ConvertUTF16ToJavaString(env
, reset_button_label
).obj());
158 int icon_id
= ResourceMapper::MapFromChromiumId(
159 WebsiteSettingsUI::GetConnectionIconID(
160 identity_info
.connection_status
));
162 ScopedJavaLocalRef
<jstring
> description
= ConvertUTF8ToJavaString(
163 env
, identity_info
.connection_status_description
);
164 Java_ConnectionInfoPopup_addDescriptionSection(
165 env
, popup_jobject_
.obj(), icon_id
, NULL
, description
.obj());
168 Java_ConnectionInfoPopup_addMoreInfoLink(
170 popup_jobject_
.obj(),
171 ConvertUTF8ToJavaString(
172 env
, l10n_util::GetStringUTF8(IDS_PAGE_INFO_HELP_CENTER_LINK
)).obj());
173 Java_ConnectionInfoPopup_showDialog(env
, popup_jobject_
.obj());
176 void ConnectionInfoPopupAndroid::SetCookieInfo(
177 const CookieInfoList
& cookie_info_list
) {
181 void ConnectionInfoPopupAndroid::SetPermissionInfo(
182 const PermissionInfoList
& permission_info_list
) {
186 void ConnectionInfoPopupAndroid::SetSelectedTab(
187 WebsiteSettingsUI::TabId tab_id
) {
188 // There's no tab UI on Android - only connection info is shown.
194 ConnectionInfoPopupAndroid::RegisterConnectionInfoPopupAndroid(
196 return RegisterNativesImpl(env
);