Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / android / connection_info_popup_android.cc
blob225b6204233bd299f676e8123c06f8f3692e49b3
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(
34 JNIEnv* env,
35 const JavaParamRef<jobject>& obj,
36 const JavaParamRef<jobject>& java_web_contents) {
37 content::WebContents* web_contents =
38 content::WebContents::FromJavaWebContents(java_web_contents);
39 if (!web_contents)
40 return ScopedJavaLocalRef<jobjectArray>();
42 int cert_id =
43 web_contents->GetController().GetVisibleEntry()->GetSSL().cert_id;
44 scoped_refptr<net::X509Certificate> cert;
45 bool ok = CertStore::GetInstance()->RetrieveCert(cert_id, &cert);
46 CHECK(ok);
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
52 // already there.
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 =
58 cert_handles.begin();
59 it != cert_handles.end();
60 ++it) {
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);
69 // static
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(
82 JNIEnv* env,
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)
89 return;
91 popup_jobject_.Reset(env, java_website_settings_pop);
93 SecurityStateModel* security_model =
94 SecurityStateModel::FromWebContents(web_contents);
95 DCHECK(security_model);
97 presenter_.reset(new WebsiteSettings(
98 this, Profile::FromBrowserContext(web_contents->GetBrowserContext()),
99 TabSpecificContentSettings::FromWebContents(web_contents), web_contents,
100 nav_entry->GetURL(), security_model->GetSecurityInfo(),
101 content::CertStore::GetInstance()));
104 ConnectionInfoPopupAndroid::~ConnectionInfoPopupAndroid() {
107 void ConnectionInfoPopupAndroid::Destroy(JNIEnv* env, jobject obj) {
108 delete this;
111 void ConnectionInfoPopupAndroid::ResetCertDecisions(
112 JNIEnv* env,
113 jobject obj,
114 jobject java_web_contents) {
115 presenter_->OnRevokeSSLErrorBypassButtonPressed();
118 void ConnectionInfoPopupAndroid::SetIdentityInfo(
119 const IdentityInfo& identity_info) {
120 JNIEnv* env = base::android::AttachCurrentThread();
123 int icon_id = ResourceMapper::MapFromChromiumId(
124 WebsiteSettingsUI::GetIdentityIconID(identity_info.identity_status));
126 // The headline and the certificate dialog link of the site's identity
127 // section is only displayed if the site's identity was verified. If the
128 // site's identity was verified, then the headline contains the organization
129 // name from the provided certificate. If the organization name is not
130 // available than the hostname of the site is used instead.
131 std::string headline;
132 if (identity_info.cert_id) {
133 headline = identity_info.site_identity;
136 ScopedJavaLocalRef<jstring> description =
137 ConvertUTF8ToJavaString(env, identity_info.identity_status_description);
138 base::string16 certificate_label =
139 l10n_util::GetStringUTF16(IDS_PAGEINFO_CERT_INFO_BUTTON);
140 Java_ConnectionInfoPopup_addCertificateSection(
141 env,
142 popup_jobject_.obj(),
143 icon_id,
144 ConvertUTF8ToJavaString(env, headline).obj(),
145 description.obj(),
146 ConvertUTF16ToJavaString(env, certificate_label).obj());
148 if (identity_info.show_ssl_decision_revoke_button) {
149 base::string16 reset_button_label = l10n_util::GetStringUTF16(
150 IDS_PAGEINFO_RESET_INVALID_CERTIFICATE_DECISIONS_BUTTON);
151 Java_ConnectionInfoPopup_addResetCertDecisionsButton(
152 env,
153 popup_jobject_.obj(),
154 ConvertUTF16ToJavaString(env, reset_button_label).obj());
159 int icon_id = ResourceMapper::MapFromChromiumId(
160 WebsiteSettingsUI::GetConnectionIconID(
161 identity_info.connection_status));
163 ScopedJavaLocalRef<jstring> description = ConvertUTF8ToJavaString(
164 env, identity_info.connection_status_description);
165 Java_ConnectionInfoPopup_addDescriptionSection(
166 env, popup_jobject_.obj(), icon_id, NULL, description.obj());
169 Java_ConnectionInfoPopup_addMoreInfoLink(
170 env,
171 popup_jobject_.obj(),
172 ConvertUTF8ToJavaString(
173 env, l10n_util::GetStringUTF8(IDS_PAGE_INFO_HELP_CENTER_LINK)).obj());
174 Java_ConnectionInfoPopup_showDialog(env, popup_jobject_.obj());
177 void ConnectionInfoPopupAndroid::SetCookieInfo(
178 const CookieInfoList& cookie_info_list) {
179 NOTIMPLEMENTED();
182 void ConnectionInfoPopupAndroid::SetPermissionInfo(
183 const PermissionInfoList& permission_info_list) {
184 NOTIMPLEMENTED();
187 void ConnectionInfoPopupAndroid::SetSelectedTab(
188 WebsiteSettingsUI::TabId tab_id) {
189 // There's no tab UI on Android - only connection info is shown.
190 NOTIMPLEMENTED();
193 // static
194 bool
195 ConnectionInfoPopupAndroid::RegisterConnectionInfoPopupAndroid(
196 JNIEnv* env) {
197 return RegisterNativesImpl(env);