Add button to page info to revoke user certificate decisions.
[chromium-blink-merge.git] / chrome / browser / ui / android / website_settings_popup_android.cc
blob8ce315363ef20644037cf71caaf6b23f67bb8cf8
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/ssl/chrome_ssl_host_state_delegate.h"
14 #include "chrome/browser/ssl/chrome_ssl_host_state_delegate_factory.h"
15 #include "chrome/browser/ui/website_settings/website_settings.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/cert_store.h"
18 #include "content/public/browser/navigation_controller.h"
19 #include "content/public/browser/navigation_entry.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/common/ssl_status.h"
22 #include "grit/generated_resources.h"
23 #include "jni/WebsiteSettingsPopup_jni.h"
24 #include "net/cert/x509_certificate.h"
25 #include "ui/base/l10n/l10n_util.h"
27 using base::android::CheckException;
28 using base::android::ConvertUTF8ToJavaString;
29 using base::android::ConvertUTF16ToJavaString;
30 using base::android::GetClass;
31 using base::android::ScopedJavaLocalRef;
32 using content::CertStore;
33 using content::WebContents;
35 static jobjectArray GetCertificateChain(JNIEnv* env,
36 jobject obj,
37 jobject java_web_contents) {
38 content::WebContents* web_contents =
39 content::WebContents::FromJavaWebContents(java_web_contents);
40 if (!web_contents)
41 return NULL;
43 int cert_id =
44 web_contents->GetController().GetVisibleEntry()->GetSSL().cert_id;
45 scoped_refptr<net::X509Certificate> cert;
46 bool ok = CertStore::GetInstance()->RetrieveCert(cert_id, &cert);
47 CHECK(ok);
49 std::vector<std::string> cert_chain;
50 net::X509Certificate::OSCertHandles cert_handles =
51 cert->GetIntermediateCertificates();
52 // Make sure the peer's own cert is the first in the chain, if it's not
53 // already there.
54 if (cert_handles.empty() || cert_handles[0] != cert->os_cert_handle())
55 cert_handles.insert(cert_handles.begin(), cert->os_cert_handle());
57 cert_chain.reserve(cert_handles.size());
58 for (net::X509Certificate::OSCertHandles::const_iterator it =
59 cert_handles.begin();
60 it != cert_handles.end();
61 ++it) {
62 std::string cert_bytes;
63 net::X509Certificate::GetDEREncoded(*it, &cert_bytes);
64 cert_chain.push_back(cert_bytes);
67 // OK to release, JNI binding.
68 return base::android::ToJavaArrayOfByteArray(env, cert_chain).Release();
71 // static
72 static jlong Init(JNIEnv* env,
73 jclass clazz,
74 jobject obj,
75 jobject java_web_contents) {
76 content::WebContents* web_contents =
77 content::WebContents::FromJavaWebContents(java_web_contents);
79 return reinterpret_cast<intptr_t>(
80 new WebsiteSettingsPopupAndroid(env, obj, web_contents));
83 WebsiteSettingsPopupAndroid::WebsiteSettingsPopupAndroid(
84 JNIEnv* env,
85 jobject java_website_settings_pop,
86 WebContents* web_contents) {
87 // Important to use GetVisibleEntry to match what's showing in the omnibox.
88 content::NavigationEntry* nav_entry =
89 web_contents->GetController().GetVisibleEntry();
90 if (nav_entry == NULL)
91 return;
93 popup_jobject_.Reset(env, java_website_settings_pop);
95 presenter_.reset(new WebsiteSettings(
96 this,
97 Profile::FromBrowserContext(web_contents->GetBrowserContext()),
98 TabSpecificContentSettings::FromWebContents(web_contents),
99 InfoBarService::FromWebContents(web_contents),
100 nav_entry->GetURL(),
101 nav_entry->GetSSL(),
102 content::CertStore::GetInstance()));
105 WebsiteSettingsPopupAndroid::~WebsiteSettingsPopupAndroid() {}
107 void WebsiteSettingsPopupAndroid::Destroy(JNIEnv* env, jobject obj) {
108 delete this;
111 void WebsiteSettingsPopupAndroid::ResetCertDecisions(
112 JNIEnv* env,
113 jobject obj,
114 jobject java_web_contents) {
115 content::WebContents* web_contents =
116 content::WebContents::FromJavaWebContents(java_web_contents);
117 if (!web_contents)
118 return;
119 ChromeSSLHostStateDelegate* delegate =
120 presenter_->chrome_ssl_host_state_delegate();
121 DCHECK(delegate);
122 delegate->RevokeUserDecisionsHard(presenter_->site_url().host());
125 void WebsiteSettingsPopupAndroid::SetIdentityInfo(
126 const IdentityInfo& identity_info) {
127 JNIEnv* env = base::android::AttachCurrentThread();
130 int icon_id = ResourceMapper::MapFromChromiumId(
131 WebsiteSettingsUI::GetIdentityIconID(identity_info.identity_status));
133 // The headline and the certificate dialog link of the site's identity
134 // section is only displayed if the site's identity was verified. If the
135 // site's identity was verified, then the headline contains the organization
136 // name from the provided certificate. If the organization name is not
137 // available than the hostname of the site is used instead.
138 std::string headline;
139 if (identity_info.cert_id) {
140 headline = identity_info.site_identity;
143 ScopedJavaLocalRef<jstring> description = ConvertUTF8ToJavaString(
144 env, identity_info.identity_status_description);
145 Java_WebsiteSettingsPopup_addDescriptionSection(
146 env,
147 popup_jobject_.obj(),
148 icon_id,
149 ConvertUTF8ToJavaString(env, headline).obj(),
150 description.obj());
152 base::string16 certificate_label =
153 l10n_util::GetStringUTF16(IDS_PAGEINFO_CERT_INFO_BUTTON);
154 Java_WebsiteSettingsPopup_addCertificateSection(
155 env,
156 popup_jobject_.obj(),
157 icon_id,
158 ConvertUTF8ToJavaString(env, headline).obj(),
159 description.obj(),
160 ConvertUTF16ToJavaString(env, certificate_label).obj());
162 if (identity_info.show_ssl_decision_revoke_button) {
163 base::string16 reset_button_label = l10n_util::GetStringUTF16(
164 IDS_PAGEINFO_RESET_INVALID_CERTIFICATE_DECISIONS_BUTTON);
165 Java_WebsiteSettingsPopup_addResetCertDecisionsButton(
166 env,
167 popup_jobject_.obj(),
168 ConvertUTF16ToJavaString(env, reset_button_label).obj());
173 int icon_id = ResourceMapper::MapFromChromiumId(
174 WebsiteSettingsUI::GetConnectionIconID(
175 identity_info.connection_status));
177 ScopedJavaLocalRef<jstring> description = ConvertUTF8ToJavaString(
178 env, identity_info.connection_status_description);
179 Java_WebsiteSettingsPopup_addDescriptionSection(
180 env, popup_jobject_.obj(), icon_id, NULL, description.obj());
183 Java_WebsiteSettingsPopup_addMoreInfoLink(env, popup_jobject_.obj(),
184 ConvertUTF8ToJavaString(
185 env, l10n_util::GetStringUTF8(IDS_PAGE_INFO_HELP_CENTER_LINK)).obj());
186 Java_WebsiteSettingsPopup_showDialog(env, popup_jobject_.obj());
189 void WebsiteSettingsPopupAndroid::SetCookieInfo(
190 const CookieInfoList& cookie_info_list) {
191 NOTIMPLEMENTED();
194 void WebsiteSettingsPopupAndroid::SetPermissionInfo(
195 const PermissionInfoList& permission_info_list) {
196 NOTIMPLEMENTED();
199 void WebsiteSettingsPopupAndroid::SetSelectedTab(
200 WebsiteSettingsUI::TabId tab_id) {
201 // There's no tab UI on Android - only connection info is shown.
202 NOTIMPLEMENTED();
205 void WebsiteSettingsPopupAndroid::SetFirstVisit(
206 const base::string16& first_visit) {
207 NOTIMPLEMENTED();
210 // static
211 bool WebsiteSettingsPopupAndroid::RegisterWebsiteSettingsPopupAndroid(
212 JNIEnv* env) {
213 return RegisterNativesImpl(env);