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/cert_store.h"
15 #include "content/public/browser/navigation_controller.h"
16 #include "content/public/browser/navigation_entry.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/common/ssl_status.h"
19 #include "grit/generated_resources.h"
20 #include "jni/WebsiteSettingsPopup_jni.h"
21 #include "net/cert/x509_certificate.h"
22 #include "ui/base/l10n/l10n_util.h"
24 using base::android::CheckException;
25 using base::android::ConvertUTF8ToJavaString;
26 using base::android::ConvertUTF16ToJavaString;
27 using base::android::GetClass;
28 using base::android::ScopedJavaLocalRef;
29 using content::CertStore;
30 using content::WebContents;
32 static jobjectArray GetCertificateChain(JNIEnv* env,
34 jobject java_web_contents) {
35 content::WebContents* web_contents =
36 content::WebContents::FromJavaWebContents(java_web_contents);
41 web_contents->GetController().GetVisibleEntry()->GetSSL().cert_id;
42 scoped_refptr<net::X509Certificate> cert;
43 bool ok = CertStore::GetInstance()->RetrieveCert(cert_id, &cert);
46 std::vector<std::string> cert_chain;
47 net::X509Certificate::OSCertHandles cert_handles =
48 cert->GetIntermediateCertificates();
49 // Make sure the peer's own cert is the first in the chain, if it's not
51 if (cert_handles.empty() || cert_handles[0] != cert->os_cert_handle())
52 cert_handles.insert(cert_handles.begin(), cert->os_cert_handle());
54 cert_chain.reserve(cert_handles.size());
55 for (net::X509Certificate::OSCertHandles::const_iterator it =
57 it != cert_handles.end();
59 std::string cert_bytes;
60 net::X509Certificate::GetDEREncoded(*it, &cert_bytes);
61 cert_chain.push_back(cert_bytes);
64 // OK to release, JNI binding.
65 return base::android::ToJavaArrayOfByteArray(env, cert_chain).Release();
69 static jlong Init(JNIEnv* env,
72 jobject java_web_contents) {
73 content::WebContents* web_contents =
74 content::WebContents::FromJavaWebContents(java_web_contents);
76 return reinterpret_cast<intptr_t>(
77 new WebsiteSettingsPopupAndroid(env, obj, web_contents));
80 WebsiteSettingsPopupAndroid::WebsiteSettingsPopupAndroid(
82 jobject java_website_settings_pop,
83 WebContents* web_contents) {
84 // Important to use GetVisibleEntry to match what's showing in the omnibox.
85 content::NavigationEntry* nav_entry =
86 web_contents->GetController().GetVisibleEntry();
87 if (nav_entry == NULL)
90 popup_jobject_.Reset(env, java_website_settings_pop);
92 presenter_.reset(new WebsiteSettings(
94 Profile::FromBrowserContext(web_contents->GetBrowserContext()),
95 TabSpecificContentSettings::FromWebContents(web_contents),
96 InfoBarService::FromWebContents(web_contents),
99 content::CertStore::GetInstance()));
102 WebsiteSettingsPopupAndroid::~WebsiteSettingsPopupAndroid() {}
104 void WebsiteSettingsPopupAndroid::Destroy(JNIEnv* env, jobject obj) {
108 void WebsiteSettingsPopupAndroid::SetIdentityInfo(
109 const IdentityInfo& identity_info) {
110 JNIEnv* env = base::android::AttachCurrentThread();
113 int icon_id = ResourceMapper::MapFromChromiumId(
114 WebsiteSettingsUI::GetIdentityIconID(identity_info.identity_status));
116 // The headline and the certificate dialog link of the site's identity
117 // section is only displayed if the site's identity was verified. If the
118 // site's identity was verified, then the headline contains the organization
119 // name from the provided certificate. If the organization name is not
120 // available than the hostname of the site is used instead.
121 std::string headline;
122 if (identity_info.cert_id) {
123 headline = identity_info.site_identity;
126 ScopedJavaLocalRef<jstring> description = ConvertUTF8ToJavaString(
127 env, identity_info.identity_status_description);
128 Java_WebsiteSettingsPopup_addSection(env, popup_jobject_.obj(), icon_id,
129 ConvertUTF8ToJavaString(env, headline).obj(), description.obj());
131 base::string16 certificate_label =
132 l10n_util::GetStringUTF16(IDS_PAGEINFO_CERT_INFO_BUTTON);
133 if (!certificate_label.empty()) {
134 Java_WebsiteSettingsPopup_setCertificateViewer(env, popup_jobject_.obj(),
135 ConvertUTF16ToJavaString(env, certificate_label).obj());
138 Java_WebsiteSettingsPopup_addDivider(env, popup_jobject_.obj());
142 int icon_id = ResourceMapper::MapFromChromiumId(
143 WebsiteSettingsUI::GetConnectionIconID(
144 identity_info.connection_status));
146 ScopedJavaLocalRef<jstring> description = ConvertUTF8ToJavaString(
147 env, identity_info.connection_status_description);
148 Java_WebsiteSettingsPopup_addSection(env, popup_jobject_.obj(), icon_id,
149 NULL, description.obj());
151 Java_WebsiteSettingsPopup_addDivider(env, popup_jobject_.obj());
154 Java_WebsiteSettingsPopup_addMoreInfoLink(env, popup_jobject_.obj(),
155 ConvertUTF8ToJavaString(
156 env, l10n_util::GetStringUTF8(IDS_PAGE_INFO_HELP_CENTER_LINK)).obj());
157 Java_WebsiteSettingsPopup_showDialog(env, popup_jobject_.obj());
160 void WebsiteSettingsPopupAndroid::SetCookieInfo(
161 const CookieInfoList& cookie_info_list) {
165 void WebsiteSettingsPopupAndroid::SetPermissionInfo(
166 const PermissionInfoList& permission_info_list) {
170 void WebsiteSettingsPopupAndroid::SetSelectedTab(
171 WebsiteSettingsUI::TabId tab_id) {
172 // There's no tab UI on Android - only connection info is shown.
176 void WebsiteSettingsPopupAndroid::SetFirstVisit(
177 const base::string16& first_visit) {
182 bool WebsiteSettingsPopupAndroid::RegisterWebsiteSettingsPopupAndroid(
184 return RegisterNativesImpl(env);