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/android/content_view_core.h"
     15 #include "content/public/browser/cert_store.h"
     16 #include "content/public/browser/navigation_controller.h"
     17 #include "content/public/browser/navigation_entry.h"
     18 #include "content/public/browser/web_contents.h"
     19 #include "content/public/common/ssl_status.h"
     20 #include "grit/generated_resources.h"
     21 #include "jni/WebsiteSettingsPopup_jni.h"
     22 #include "net/cert/x509_certificate.h"
     23 #include "ui/base/l10n/l10n_util.h"
     24 
     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;
     32 
     33 static jobjectArray GetCertificateChain(JNIEnv* env,
     34                                         jobject obj,
     35                                         jobject view) {
     36   content::WebContents* contents =
     37       content::ContentViewCore::GetNativeContentViewCore(env, view)->
     38           GetWebContents();
     39   int cert_id = contents->GetController().GetVisibleEntry()->GetSSL().cert_id;
     40   scoped_refptr<net::X509Certificate> cert;
     41   bool ok = CertStore::GetInstance()->RetrieveCert(cert_id, &cert);
     42   CHECK(ok);
     43 
     44   std::vector<std::string> cert_chain;
     45   net::X509Certificate::OSCertHandles cert_handles =
     46       cert->GetIntermediateCertificates();
     47   // Make sure the peer's own cert is the first in the chain, if it's not
     48   // already there.
     49   if (cert_handles.empty() || cert_handles[0] != cert->os_cert_handle())
     50     cert_handles.insert(cert_handles.begin(), cert->os_cert_handle());
     51 
     52   cert_chain.reserve(cert_handles.size());
     53   for (net::X509Certificate::OSCertHandles::const_iterator it =
     54            cert_handles.begin();
     55        it != cert_handles.end();
     56        ++it) {
     57     std::string cert_bytes;
     58     net::X509Certificate::GetDEREncoded(*it, &cert_bytes);
     59     cert_chain.push_back(cert_bytes);
     60   }
     61 
     62   // OK to release, JNI binding.
     63   return base::android::ToJavaArrayOfByteArray(env, cert_chain).Release();
     64 }
     65 
     66 // static
     67 void WebsiteSettingsPopupAndroid::Show(JNIEnv* env,
     68                                        jobject context,
     69                                        jobject java_content_view,
     70                                        WebContents* web_contents) {
     71   new WebsiteSettingsPopupAndroid(env, context, java_content_view,
     72                                   web_contents);
     73 }
     74 
     75 WebsiteSettingsPopupAndroid::WebsiteSettingsPopupAndroid(
     76     JNIEnv* env,
     77     jobject context,
     78     jobject java_content_view,
     79     WebContents* web_contents) {
     80   // Important to use GetVisibleEntry to match what's showing in the omnibox.
     81   content::NavigationEntry* nav_entry =
     82       web_contents->GetController().GetVisibleEntry();
     83   if (nav_entry == NULL)
     84     return;
     85 
     86   popup_jobject_.Reset(
     87       Java_WebsiteSettingsPopup_create(env, context, java_content_view,
     88                                        reinterpret_cast<jint>(this)));
     89 
     90   presenter_.reset(new WebsiteSettings(
     91       this,
     92       Profile::FromBrowserContext(web_contents->GetBrowserContext()),
     93       TabSpecificContentSettings::FromWebContents(web_contents),
     94       InfoBarService::FromWebContents(web_contents),
     95       nav_entry->GetURL(),
     96       nav_entry->GetSSL(),
     97       content::CertStore::GetInstance()));
     98 }
     99 
    100 WebsiteSettingsPopupAndroid::~WebsiteSettingsPopupAndroid() {}
    101 
    102 void WebsiteSettingsPopupAndroid::Destroy(JNIEnv* env, jobject obj) {
    103   delete this;
    104 }
    105 
    106 void WebsiteSettingsPopupAndroid::SetIdentityInfo(
    107     const IdentityInfo& identity_info) {
    108   JNIEnv* env = base::android::AttachCurrentThread();
    109 
    110   {
    111     int icon_id = ResourceMapper::MapFromChromiumId(
    112         WebsiteSettingsUI::GetIdentityIconID(identity_info.identity_status));
    113 
    114     // The headline and the certificate dialog link of the site's identity
    115     // section is only displayed if the site's identity was verified. If the
    116     // site's identity was verified, then the headline contains the organization
    117     // name from the provided certificate. If the organization name is not
    118     // available than the hostname of the site is used instead.
    119     std::string headline;
    120     if (identity_info.cert_id) {
    121       headline = identity_info.site_identity;
    122     }
    123 
    124     ScopedJavaLocalRef<jstring> description = ConvertUTF8ToJavaString(
    125         env, identity_info.identity_status_description);
    126     Java_WebsiteSettingsPopup_addSection(env, popup_jobject_.obj(), icon_id,
    127         ConvertUTF8ToJavaString(env, headline).obj(), description.obj());
    128 
    129     string16 certificate_label =
    130         l10n_util::GetStringUTF16(IDS_PAGEINFO_CERT_INFO_BUTTON);
    131     if (!certificate_label.empty()) {
    132       Java_WebsiteSettingsPopup_setCertificateViewer(env, popup_jobject_.obj(),
    133           ConvertUTF16ToJavaString(env, certificate_label).obj());
    134     }
    135 
    136     Java_WebsiteSettingsPopup_addDivider(env, popup_jobject_.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_addSection(env, popup_jobject_.obj(), icon_id,
    147         NULL, description.obj());
    148 
    149     Java_WebsiteSettingsPopup_addDivider(env, popup_jobject_.obj());
    150   }
    151 
    152   Java_WebsiteSettingsPopup_addMoreInfoLink(env, popup_jobject_.obj(),
    153       ConvertUTF8ToJavaString(
    154           env, l10n_util::GetStringUTF8(IDS_PAGE_INFO_HELP_CENTER_LINK)).obj());
    155   Java_WebsiteSettingsPopup_show(env, popup_jobject_.obj());
    156 }
    157 
    158 void WebsiteSettingsPopupAndroid::SetCookieInfo(
    159     const CookieInfoList& cookie_info_list) {
    160   NOTIMPLEMENTED();
    161 }
    162 
    163 void WebsiteSettingsPopupAndroid::SetPermissionInfo(
    164     const PermissionInfoList& permission_info_list) {
    165   NOTIMPLEMENTED();
    166 }
    167 
    168 void WebsiteSettingsPopupAndroid::SetSelectedTab(
    169     WebsiteSettingsUI::TabId tab_id) {
    170   // There's no tab UI on Android - only connection info is shown.
    171   NOTIMPLEMENTED();
    172 }
    173 
    174 void WebsiteSettingsPopupAndroid::SetFirstVisit(
    175     const string16& first_visit) {
    176   NOTIMPLEMENTED();
    177 }
    178 
    179 // static
    180 bool WebsiteSettingsPopupAndroid::RegisterWebsiteSettingsPopupAndroid(
    181     JNIEnv* env) {
    182   return RegisterNativesImpl(env);
    183 }
    184