1c0aabb861159c1090f0125fae56ff2fd5730b1c
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / android / website_settings_popup_android.cc
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.
4
5 #include "chrome/browser/ui/android/website_settings_popup_android.h"
6
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"
23
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;
31
32 static jobjectArray GetCertificateChain(JNIEnv* env,
33                                         jobject obj,
34                                         jobject java_web_contents) {
35   content::WebContents* web_contents =
36       content::WebContents::FromJavaWebContents(java_web_contents);
37   if (!web_contents)
38     return NULL;
39
40   int cert_id =
41       web_contents->GetController().GetVisibleEntry()->GetSSL().cert_id;
42   scoped_refptr<net::X509Certificate> cert;
43   bool ok = CertStore::GetInstance()->RetrieveCert(cert_id, &cert);
44   CHECK(ok);
45
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
50   // already there.
51   if (cert_handles.empty() || cert_handles[0] != cert->os_cert_handle())
52     cert_handles.insert(cert_handles.begin(), cert->os_cert_handle());
53
54   cert_chain.reserve(cert_handles.size());
55   for (net::X509Certificate::OSCertHandles::const_iterator it =
56            cert_handles.begin();
57        it != cert_handles.end();
58        ++it) {
59     std::string cert_bytes;
60     net::X509Certificate::GetDEREncoded(*it, &cert_bytes);
61     cert_chain.push_back(cert_bytes);
62   }
63
64   // OK to release, JNI binding.
65   return base::android::ToJavaArrayOfByteArray(env, cert_chain).Release();
66 }
67
68 // static
69 static jlong Init(JNIEnv* env,
70                   jclass clazz,
71                   jobject obj,
72                   jobject java_web_contents) {
73   content::WebContents* web_contents =
74       content::WebContents::FromJavaWebContents(java_web_contents);
75
76   return reinterpret_cast<intptr_t>(
77       new WebsiteSettingsPopupAndroid(env, obj, web_contents));
78 }
79
80 WebsiteSettingsPopupAndroid::WebsiteSettingsPopupAndroid(
81     JNIEnv* env,
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)
88     return;
89
90   popup_jobject_.Reset(env, java_website_settings_pop);
91
92   presenter_.reset(new WebsiteSettings(
93       this,
94       Profile::FromBrowserContext(web_contents->GetBrowserContext()),
95       TabSpecificContentSettings::FromWebContents(web_contents),
96       InfoBarService::FromWebContents(web_contents),
97       nav_entry->GetURL(),
98       nav_entry->GetSSL(),
99       content::CertStore::GetInstance()));
100 }
101
102 WebsiteSettingsPopupAndroid::~WebsiteSettingsPopupAndroid() {}
103
104 void WebsiteSettingsPopupAndroid::Destroy(JNIEnv* env, jobject obj) {
105   delete this;
106 }
107
108 void WebsiteSettingsPopupAndroid::SetIdentityInfo(
109     const IdentityInfo& identity_info) {
110   JNIEnv* env = base::android::AttachCurrentThread();
111
112   {
113     int icon_id = ResourceMapper::MapFromChromiumId(
114         WebsiteSettingsUI::GetIdentityIconID(identity_info.identity_status));
115
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;
124     }
125
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());
130
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());
136     }
137
138     Java_WebsiteSettingsPopup_addDivider(env, popup_jobject_.obj());
139   }
140
141   {
142     int icon_id = ResourceMapper::MapFromChromiumId(
143         WebsiteSettingsUI::GetConnectionIconID(
144             identity_info.connection_status));
145
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());
150
151     Java_WebsiteSettingsPopup_addDivider(env, popup_jobject_.obj());
152   }
153
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());
158 }
159
160 void WebsiteSettingsPopupAndroid::SetCookieInfo(
161     const CookieInfoList& cookie_info_list) {
162   NOTIMPLEMENTED();
163 }
164
165 void WebsiteSettingsPopupAndroid::SetPermissionInfo(
166     const PermissionInfoList& permission_info_list) {
167   NOTIMPLEMENTED();
168 }
169
170 void WebsiteSettingsPopupAndroid::SetSelectedTab(
171     WebsiteSettingsUI::TabId tab_id) {
172   // There's no tab UI on Android - only connection info is shown.
173   NOTIMPLEMENTED();
174 }
175
176 void WebsiteSettingsPopupAndroid::SetFirstVisit(
177     const base::string16& first_visit) {
178   NOTIMPLEMENTED();
179 }
180
181 // static
182 bool WebsiteSettingsPopupAndroid::RegisterWebsiteSettingsPopupAndroid(
183     JNIEnv* env) {
184   return RegisterNativesImpl(env);
185 }