1 // Copyright (c) 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/website_settings_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 "content/public/browser/android/content_view_core.h"
15 #include "content/public/browser/cert_store.h"
16 #include "content/public/browser/navigation_controller.h"
17 #include "content/public/browser/navigation_entry.h"
18 #include "content/public/browser/web_contents.h"
19 #include "content/public/common/ssl_status.h"
20 #include "grit/generated_resources.h"
21 #include "jni/WebsiteSettingsPopup_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 jobjectArray
GetCertificateChain(JNIEnv
* env
,
36 content::WebContents
* contents
=
37 content::ContentViewCore::GetNativeContentViewCore(env
, view
)->
39 int cert_id
= contents
->GetController().GetVisibleEntry()->GetSSL().cert_id
;
40 scoped_refptr
<net::X509Certificate
> cert
;
41 bool ok
= CertStore::GetInstance()->RetrieveCert(cert_id
, &cert
);
44 std::vector
<std::string
> cert_chain
;
45 net::X509Certificate::OSCertHandles cert_handles
=
46 cert
->GetIntermediateCertificates();
47 // Make sure the peer's own cert is the first in the chain, if it's not
49 if (cert_handles
.empty() || cert_handles
[0] != cert
->os_cert_handle())
50 cert_handles
.insert(cert_handles
.begin(), cert
->os_cert_handle());
52 cert_chain
.reserve(cert_handles
.size());
53 for (net::X509Certificate::OSCertHandles::const_iterator it
=
55 it
!= cert_handles
.end();
57 std::string cert_bytes
;
58 net::X509Certificate::GetDEREncoded(*it
, &cert_bytes
);
59 cert_chain
.push_back(cert_bytes
);
62 // OK to release, JNI binding.
63 return base::android::ToJavaArrayOfByteArray(env
, cert_chain
).Release();
67 void WebsiteSettingsPopupAndroid::Show(JNIEnv
* env
,
69 jobject java_content_view
,
70 WebContents
* web_contents
) {
71 new WebsiteSettingsPopupAndroid(env
, context
, java_content_view
,
75 WebsiteSettingsPopupAndroid::WebsiteSettingsPopupAndroid(
78 jobject java_content_view
,
79 WebContents
* web_contents
) {
80 // Important to use GetVisibleEntry to match what's showing in the omnibox.
81 content::NavigationEntry
* nav_entry
=
82 web_contents
->GetController().GetVisibleEntry();
83 if (nav_entry
== NULL
)
87 Java_WebsiteSettingsPopup_create(env
, context
, java_content_view
,
88 reinterpret_cast<intptr_t>(this)));
90 presenter_
.reset(new WebsiteSettings(
92 Profile::FromBrowserContext(web_contents
->GetBrowserContext()),
93 TabSpecificContentSettings::FromWebContents(web_contents
),
94 InfoBarService::FromWebContents(web_contents
),
97 content::CertStore::GetInstance()));
100 WebsiteSettingsPopupAndroid::~WebsiteSettingsPopupAndroid() {}
102 void WebsiteSettingsPopupAndroid::Destroy(JNIEnv
* env
, jobject obj
) {
106 void WebsiteSettingsPopupAndroid::SetIdentityInfo(
107 const IdentityInfo
& identity_info
) {
108 JNIEnv
* env
= base::android::AttachCurrentThread();
111 int icon_id
= ResourceMapper::MapFromChromiumId(
112 WebsiteSettingsUI::GetIdentityIconID(identity_info
.identity_status
));
114 // The headline and the certificate dialog link of the site's identity
115 // section is only displayed if the site's identity was verified. If the
116 // site's identity was verified, then the headline contains the organization
117 // name from the provided certificate. If the organization name is not
118 // available than the hostname of the site is used instead.
119 std::string headline
;
120 if (identity_info
.cert_id
) {
121 headline
= identity_info
.site_identity
;
124 ScopedJavaLocalRef
<jstring
> description
= ConvertUTF8ToJavaString(
125 env
, identity_info
.identity_status_description
);
126 Java_WebsiteSettingsPopup_addSection(env
, popup_jobject_
.obj(), icon_id
,
127 ConvertUTF8ToJavaString(env
, headline
).obj(), description
.obj());
129 base::string16 certificate_label
=
130 l10n_util::GetStringUTF16(IDS_PAGEINFO_CERT_INFO_BUTTON
);
131 if (!certificate_label
.empty()) {
132 Java_WebsiteSettingsPopup_setCertificateViewer(env
, popup_jobject_
.obj(),
133 ConvertUTF16ToJavaString(env
, certificate_label
).obj());
136 Java_WebsiteSettingsPopup_addDivider(env
, popup_jobject_
.obj());
140 int icon_id
= ResourceMapper::MapFromChromiumId(
141 WebsiteSettingsUI::GetConnectionIconID(
142 identity_info
.connection_status
));
144 ScopedJavaLocalRef
<jstring
> description
= ConvertUTF8ToJavaString(
145 env
, identity_info
.connection_status_description
);
146 Java_WebsiteSettingsPopup_addSection(env
, popup_jobject_
.obj(), icon_id
,
147 NULL
, description
.obj());
149 Java_WebsiteSettingsPopup_addDivider(env
, popup_jobject_
.obj());
152 Java_WebsiteSettingsPopup_addMoreInfoLink(env
, popup_jobject_
.obj(),
153 ConvertUTF8ToJavaString(
154 env
, l10n_util::GetStringUTF8(IDS_PAGE_INFO_HELP_CENTER_LINK
)).obj());
155 Java_WebsiteSettingsPopup_show(env
, popup_jobject_
.obj());
158 void WebsiteSettingsPopupAndroid::SetCookieInfo(
159 const CookieInfoList
& cookie_info_list
) {
163 void WebsiteSettingsPopupAndroid::SetPermissionInfo(
164 const PermissionInfoList
& permission_info_list
) {
168 void WebsiteSettingsPopupAndroid::SetSelectedTab(
169 WebsiteSettingsUI::TabId tab_id
) {
170 // There's no tab UI on Android - only connection info is shown.
174 void WebsiteSettingsPopupAndroid::SetFirstVisit(
175 const base::string16
& first_visit
) {
180 bool WebsiteSettingsPopupAndroid::RegisterWebsiteSettingsPopupAndroid(
182 return RegisterNativesImpl(env
);