Home | History | Annotate | Download | only in android
      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     base::string16 certificate_label =
    129             l10n_util::GetStringUTF16(IDS_PAGEINFO_CERT_INFO_BUTTON);
    130     Java_WebsiteSettingsPopup_addCertificateSection(
    131         env,
    132         popup_jobject_.obj(),
    133         icon_id,
    134         ConvertUTF8ToJavaString(env, headline).obj(),
    135         description.obj(),
    136         ConvertUTF16ToJavaString(env, certificate_label).obj());
    137   }
    138 
    139   {
    140     int icon_id = ResourceMapper::MapFromChromiumId(
    141         WebsiteSettingsUI::GetConnectionIconID(
    142             identity_info.connection_status));
    143 
    144     ScopedJavaLocalRef<jstring> description = ConvertUTF8ToJavaString(
    145         env, identity_info.connection_status_description);
    146     Java_WebsiteSettingsPopup_addDescriptionSection(
    147         env, popup_jobject_.obj(), icon_id, NULL, description.obj());
    148   }
    149 
    150   Java_WebsiteSettingsPopup_addMoreInfoLink(env, popup_jobject_.obj(),
    151       ConvertUTF8ToJavaString(
    152           env, l10n_util::GetStringUTF8(IDS_PAGE_INFO_HELP_CENTER_LINK)).obj());
    153   Java_WebsiteSettingsPopup_showDialog(env, popup_jobject_.obj());
    154 }
    155 
    156 void WebsiteSettingsPopupAndroid::SetCookieInfo(
    157     const CookieInfoList& cookie_info_list) {
    158   NOTIMPLEMENTED();
    159 }
    160 
    161 void WebsiteSettingsPopupAndroid::SetPermissionInfo(
    162     const PermissionInfoList& permission_info_list) {
    163   NOTIMPLEMENTED();
    164 }
    165 
    166 void WebsiteSettingsPopupAndroid::SetSelectedTab(
    167     WebsiteSettingsUI::TabId tab_id) {
    168   // There's no tab UI on Android - only connection info is shown.
    169   NOTIMPLEMENTED();
    170 }
    171 
    172 void WebsiteSettingsPopupAndroid::SetFirstVisit(
    173     const base::string16& first_visit) {
    174   NOTIMPLEMENTED();
    175 }
    176 
    177 // static
    178 bool WebsiteSettingsPopupAndroid::RegisterWebsiteSettingsPopupAndroid(
    179     JNIEnv* env) {
    180   return RegisterNativesImpl(env);
    181 }
    182